roxdragon
roxdragon

Reputation: 95

How to know if I should use ldpi in a layout

Hi guys, I'm working on implementing multiple screen support. Is a similar case possibile?

 @Override
 protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.contatti);

    //if (device in use , use ldpi ) { do something
    else{


     Button btnNavigator = (Button) findViewById(R.id.btnNavigator);

     GoogleMap map=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(STARTING_POINT, 5));

Thank you so much. Can you show me an example please?

Upvotes: 1

Views: 39

Answers (2)

Phantômaxx
Phantômaxx

Reputation: 38098

Try this code:

private final boolean isLdpi()
{
    final DisplayMetrics metrics =
        Resources.getSystem().getDisplayMetrics();

    final float scale = metrics.density;

    return (scale == 0.75); // ldpi = 0.75, mdpi = 1.0, hdpi = 1.5, xhdpi = 2.0, xxhdpi = 3.0, ...
}

Usage:

if (isLdpi)
{
    // It's ldpi: do something
}

Upvotes: 2

Illegal Argument
Illegal Argument

Reputation: 10338

According to google

small screens are at least 426dp x 320dp.

So find out the width of the device and then compare.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//this gives you the pixels and no the dp
metrics.heightPixels;
metrics.widthPixels;

//convert to dp
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}

Please see here for more details.

Upvotes: 0

Related Questions