For Android Tv, Android recommends Designing your artwork assets for best viewing at HD resolution (1920 x 1080 pixels).
There are following two available resolutions for TVs:
1280x720
1920x1080
Main two screen sizes available for android Tablets are 7 inch & 10 inch. This incurs in following two key resolutions(Considered from here):
7” tablets: 1024x600
10” tablets: 1280x800
Also, for different display devices we can have different resolution for Android TV devices and for tables for also.
I needs to calculate minimum readable textSize for each device for given message of given length.
Lets give me an example, User sets maximum message length to n chars. Now, I want to restrict user for this character length for specific device. So that i can have maximum length of message with minimum fontSize that is readable to users for all TV & tablet devices. It would be fine if we have lower limit, but maximum character limit should not make TextView scroll-able.
Initially I tried to calculate maximum message length as per fontSize of textview like following:
TextView text = (TextView) findViewById(R.id.text);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
int dens = dm.densityDpi;
double wi = (double) width / (double) dens;
double hi = (double) height / (double) dens;
double x = Math.pow(wi, 2);
double y = Math.pow(hi, 2);
double screenInches = Math.sqrt(x + y);
//long charLimit = (long) (Math.pow(Math.floor((dens / 18)), 2) * 2);
long charLimit = (long) (((Math.floor((dens / 26)) * wi) * (Math.floor((dens / 26)) * hi)) / (wi + hi));
//long charLimit = (long) ((Math.floor((dens / 22)) * Math.floor((x + y)) * 2));
String msg = "";
for (int i = 0; i < charLimit; i++) {
msg += "A";
}
text.setText(msg);
Log.e("screenInches", "screenInches: " + screenInches + " width:" + width + "/" + wi + " height:" + height + "/" + hi + " densityDpi:" + dens + " charLimit:" + charLimit);
But couldn't get satisfied results. Now,All what i want is to calculate minimum readable fontSize (say S), for given length(say n char) of message to be displayed without scroll on both Tv and tablet devices.
I know font-family and font-style also affects this. But considering system default configurations which are proper approaches, I should follow?
This is not possible to do very well with variable-width fonts. Each character I'm writing has a different width and this means each character's width has to be computed separately. A monospace font would be easier to compute.
Related
Trying to convert centimeters to inches then round to the nearest half inch and print 1 decimal point.
3.1 = 3.0
3.2 = 3.0
3.3 = 3.5
3.6 = 3.5
3.8 = 4.0
float index;
float height;
index = (Math.round((height * .393701)*2))/2;
text.setText("Index: " + index);
When I print index it wont show the decimal. Once the number reaches .75 or higher it rounds to the next higher whole number.
Try this,
float index;
float height;
index = (float) (Math.round((height / 2.54) * 10) / 10.0);
text.setText("Index: " + index);
I put an f at the end of each number.
(Math.round((height / 2.54f)*2f)/2f)
Height
147 / 2.54 = 57.874016 = 58
148 / 2.54 = 58.26772 = 58.5
I have an image that is being displayed perfectly for MDPI.
Its resolution is 191 x 255.
Then I resize it to XXDPI, using 1:3 proportion, it gets to 573 x 765.
Still, when the emulator displays it, the quality is not as good as the MDPI one.
It gets clearly poor.
For both cases, I am using it Wrap Content
If I display both images on a image editor, they have perfect quality.
Why does that happen? Am I missing something here?
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
float widthDpi = metrics.xdpi;
float heightDpi = metrics.ydpi;
float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;
double diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches)); // this code returns the actual screen size (eg. 7,8,9,10 inches in double format ex: 7.932189832)
diagonalInches = (double) Math.round(diagonalInches * 10) / 10; // round up to first decimal places
ArrayList<Integer> imageId = new ArrayList<>();
imageId.clear();
for (Integer i : imageNum){ // uses loop since im using TransitionDrawable to change images in single view
String stringID;
if (isPortrait) {
stringID = "featured_img_" + i.toString() + "_port"; // portrait images
} else {
if (diagonalInches >= 7 && diagonalInches <=8){
stringID = "featured_img_" + i.toString() + "_land_b"; // landscape images this is the drawable filename
} else {
stringID = "featured_img_" + i.toString() + "_land_a"; //landscape images this is the drawable filename
}
}
int drawableId = getResId(stringID);
imageId.add(drawableId); // adding it to arraylist
}
you can add android:scaleType=" . . . " on what you want for your image to be displayed
I have tried the codes below for finding out the screen size but none worked for me. The interface also goes out of proportion for different devices of the same screen sizes and same dimensions. I don't have actual devices, I am using the devices with screen sizes/dimensions provided by the AVD. The codes work for dimensions but it doesn't give the correct screen size. The screen size it calculates for a 4 inch screen is 3.9 inches (round figure) and the screen size for a 3.7 inch screen is also 3.9 inches. The interface code for the Nexus S (4", 480x800: hdpi) doesn't work for the 4" WVGA (480x800: hdpi) where both have the same screen size and same dimensions.
Could you please help me solve this problem.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int widthInch=dm.widthPixels;
int heightInch=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)widthInch/(double)dens;
double hi=(double)heightInch/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);
screenInches = (double)Math.round(screenInches * 10) / 10;
int widthPix = (int) Math.ceil(dm.widthPixels * (dm.densityDpi / 160.0));
Toast.makeText(getApplicationContext(), width+"*"+height+" - "+widthPix+
" - " +screenInches,
Toast.LENGTH_LONG).show();
//================================================================
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//get density per inch for example: 120 , 160 , 240
float mXDpi = metrics.xdpi; // 160 The exact physical pixels per inch of the screen in the X dimension.
float mYDpi = metrics.ydpi;
//density
int nDensity = metrics.densityDpi; // 160 screen density expressed as dots-per-inch
float mMetersToPixelsX = mXDpi / 0.0254f; // 1 inch == 0.0254 metre
float mMetersToPixelsY = mYDpi / 0.0254f;
//Resolution
//The total number of physical pixels on a screen.
int wPix = metrics.widthPixels; // 320 The absolute width of the display in pixels.
int hPix = metrics.heightPixels; // 480 The absolute height of the display in pixels.
int nWidthDisplay = (wPix < hPix)? wPix : hPix;
float nWidthScreenInInch = wPix / mXDpi; //320 / 160 == 2.0 in inch.
float nHeightScreenInInch = hPix / mYDpi; //480 / 160 == 3.0 in inch.
double screenInches = Math.sqrt( nHeightScreenInInch*nHeightScreenInInch +
nWidthScreenInInch * nWidthScreenInInch);
screenInches = (double)Math.round(screenInches * 10) / 10;
Toast.makeText(getApplicationContext(),"Screen size: "+screenInches,
Toast.LENGTH_LONG).show();
//================================================================
DisplayMetrics metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float hgt=metrics.heightPixels/metrics.xdpi;
float wdth=metrics.widthPixels/metrics.ydpi;
double screenInches = FloatMath.sqrt(hgt*hgt+wdth*wdth);
screenInches = (double)Math.round(screenInches * 10) / 10;
Toast.makeText(getApplicationContext(),width+"*"+height+" - "+screenInches,
Toast.LENGTH_LONG).show();
//===============================================================
How to get android device screen size?
Calculating android screen size?
Detect 7 inch and 10 inch tablet programmatically
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
final int height = metrics.heightPixels;
int width = metrics.widthPixels;
I am working to get device display diagonal length. I used the formula, diagonal = width* width + height* height. To achieve this I use below code.
DisplayInfo aDisplayInfo = new DisplayInfo();
DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
aDisplayInfo.widthInch = (dm.widthPixels * dm.density) / (dm.xdpi * 2);
aDisplayInfo.heightInch = ((dm.heightPixels * dm.density) / (dm.ydpi * 2));
aDisplayInfo.widthPix = dm.widthPixels;
aDisplayInfo.heightPix = dm.heightPixels;
// approaach 1
aDisplayInfo.diagonalInch = twoDecimalForm.format(Math.sqrt(Math.pow(
aDisplayInfo.widthInch, 2)
+ Math.pow(aDisplayInfo.heightInch, 2)));
After running this code I found out different results in different devices. Like:
Samsung Galaxy S3 = 4.8 inches (correct)
HTC One X = 6.8 inches (wrong) [ will be 4.7 inches]
What's wrong with this code? Any kind of help will be appreciated.
Try this:
aDisplayInfo.widthInch = dm.widthPixels / dm.xdpi;
aDisplayInfo.heightInch = dm.heightPixels / dm.ydpi;
widthPixels/heightPixels should be the physical pixel count. xdpi/ydpi should be pixels per inch. You shouldn't need to do any scaling on these values.
When using the layout-swdp qualifiers I get the results as shown in the attachment. The sw qualifier is supposed to mean the smallest dimension must match or be bigger than the qualifier. This doesn't seem to work with the Nexus 7 (running 4.2.1). Am I confused about what smallest width qualifiers do or is the N7 reporting wrongly?
To reproduce my test case, I have many layout-swdp folders. Each has 2 textfield. The first just states which folders it's in. The next is the code below:
private CharSequence collectScreenStats() {
StringBuilder str = new StringBuilder();
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
int dpWidth = (int)(width / metrics.density);
int dpHeight = (int)(height / metrics.density);
str.append(Build.MANUFACTURER);
str.append(" ");
str.append(Build.MODEL);
str.append("\n");
str.append("Pixels: ");
str.append(width);
str.append(" x " );
str.append(height);
str.append("\nDp (px / density): ");
str.append(dpWidth);
str.append("dp x " );
str.append(dpHeight);
str.append("dp" );
str.append("\nsmallest w: " + Math.min(dpWidth, dpHeight));
str.append("\ndensity: ");
str.append(metrics.density);
str.append("\ndensityDpi: ");
str.append(metrics.densityDpi);
return str;
}
Okay, this seems to be a bug in ICS where it doesn't accurate report the number of pixels of the entire screen as it taking into account the chrome.
Android DisplayMetrics returns incorrect screen size in pixels on ICS
So, my above display numbers are off as nexus 7 is 1280 x 800 and not 1280 x 736. Using the correct numbers, everything works.