Appium : Swipe in Android by passing percentage as attributes

I need to modify my existing swipe command "driver.swipe(300, 1050, 300, 200, 1500); " as it is not working on the device with lower screen resolution (854 x 480 in my case) as the code works only on HD resolution (1280 x 720).

Is there any way that we could pass percentage attributes for X and Y coordinates for swipe?

This will make it universal for any hardware configuration.

Can we use window_size method to get the size of device, and after that we can calculate the X and Y coordinates based on the percentage you have.

def custome_swipe
  width = window_size[:width]
  height = window_size[:height]
  # your code here
end

UPDATE ::

I implemented the percentage swipe by adding two parameters in config file of my project, which are

totalHorizontalPixels= 720
totalVerticalPixels= 1280

where I pass my device’s resolution in config file.

And I made a swipe method which accepts % attributes passed, calculate the pixels wrt the device resolution passed in config file.

Here is the method :

public static void swipe(double xStart, double yStart, double xEnd, double yEnd, int sweepTime, int totalSwipes) throws IOException{
            
           FileReader reader = new FileReader("../ProjectName/config.properties"); //Reading configuration file
	Properties prop = new Properties();
	prop.load(reader); 
	
	String totalHorizontalPixels = prop.getProperty("totalHorizontalPixels");
	String totalVerticalPixels = prop.getProperty("totalVerticalPixels");
	
	double totalHorizontalPixelCount = Double.parseDouble(totalHorizontalPixels);
	double totalVerticalPixelCount = Double.parseDouble(totalVerticalPixels);
	
	double X_start = Math.round(xStart * totalHorizontalPixelCount);
	double X_end = Math.round(xEnd * totalHorizontalPixelCount);
	double Y_start = Math.round(yStart * totalVerticalPixelCount);
	double Y_end = Math.round(yEnd * totalVerticalPixelCount);
	
	int startX = (int)(X_start);
	int endX = (int)(X_end);
	int startY = (int)(Y_start);
	int endY = (int)(Y_end);
	for(int i = 0; i<totalSwipes; i++)
	{		
	driver.swipe(startX,startY,endX,endY, sweepTime);
	}
}	 

Here is an example of call to this method :

MobileCommonMethods.swipe(.42, .82, .42, .47, 2000, 1); // To swipe the screen vertically

(Please note that the percentile is passes in double format)

You can modify code to pass percentile as int data.

Comment if you have any query.