Pergunta

What is the best way to apply a piece of code only for a specific screen size of android device.

Example :

TextView textView = (TextView) rowView.findViewById(R.id.article_title_text);
// I would like to apply this code for only device Galaxy S3 mini (4" size)
textView.setText(Tools.cutTitleHomeForGalaxyMini(article.getTitle()));
// Else, i want to apply this code
textView.setText(Tools.cutTitleHome(article.getTitle()));
Foi útil?

Solução 2

If you have specific information for the model and screen size (as you mention is Galaxy S3 mini (4" size)) then get the information of the device from the System to check and apply your code:

Build.MODEL, Build.PRODUCT, Build.MANUFACTURER

And the screen size:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

Hope these help.

Outras dicas

with this you can get the screen width after then compare this screen width with the galaxy mini then set it

Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();

You can compute the screen size form the pixel screen width and height and its pixel density like this :

DisplayMetrics dm =new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

float height = dm.heightPixels/dm.xdpi;
float width = dm.widthPixels/dm.ydpi;

// you need to get the diagonal size to compare it to the 4'' of the Galaxy S3 mini.
float diagonalInch = FloatMath.sqrt(height*height+width*width);

Then use a simple test like :

if (diagonalInch < 4){
    ...
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top