Search code examples
androidscreen-size

Android : Best way to apply code based on screen size


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()));

Solution

  • 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.