Friday, 30 October 2015

Measure Device Density Idependence

There are multiple devices having different densities, So this is way by which we can measure which devices fall in which density.
Screen density is a ratio of resolution and display size.


Different sizes and densities are roughly categorized into the different size and density groups.

MainActivity.java


package com.demo.density.independence;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import static android.widget.Toast.LENGTH_LONG;
import static android.widget.Toast.makeText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getDensityIndependenceDevice();
    }

    private void getDensityIndependenceDevice() {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int density = displayMetrics.densityDpi;
        switch (density){
            case DisplayMetrics.DENSITY_LOW:
                showDensity("DENSITY_LOW",density);
                break;
            case DisplayMetrics.DENSITY_MEDIUM:
                showDensity("DENSITY_MEDIUM", density);
                break;
            case DisplayMetrics.DENSITY_HIGH:
                showDensity("DENSITY_HIGH",density);
                break;
            case DisplayMetrics.DENSITY_XHIGH:
                showDensity("DENSITY_XHIGH", density);
                break;
            case DisplayMetrics.DENSITY_XXHIGH:
                showDensity("DENSITY_XXHIGH", density);
                break;
            case DisplayMetrics.DENSITY_XXXHIGH:
                showDensity("DENSITY_XXXHIGH", density);
                break;
            case DisplayMetrics.DENSITY_560:
                showDensity("DENSITY_560", density);
                break;
            case DisplayMetrics.DENSITY_420:
                showDensity("DENSITY_420", density);
                break;
        }

        float densityPxPerDp = displayMetrics.density;
        float densityPxPerSp = displayMetrics.scaledDensity;
        float densityBucketDpi = displayMetrics.densityDpi;
        float xdpi = displayMetrics.xdpi;
        float ydpi = displayMetrics.ydpi;

    }

    private void showDensity(String densityString, int density) {
        TextView densityTextView= (TextView) findViewById(R.id.tvDensity);
        densityTextView.setText(String.format("%s... Density is %s", densityString, String.valueOf(density)));
        makeText(this, densityString + "... Density is " + String.valueOf(density), LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"
        />

</android.support.design.widget.CoordinatorLayout>

content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:id="@+id/tvDensity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black" />

</RelativeLayout>



No comments:

Post a Comment