Wednesday 26 March 2014

Check Android Phone Screen Resolutions Programatically

Hi Android Lover's,

Today i am here going to show you the code how to check android phone screen resolutions programatically, this will help you in many ways, if you are developing any app where you need to know the android mobile screen resolutions them this will help you .......... 

Example:



Code

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:gravity="center"
        android:text="Your Mobile Screen Resolutions"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/resolutions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.checkmobilescreenresolutions;

import android.os.Bundle;
import android.app.Activity;
import android.view.Display;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.resolutions);
  Display display = getWindowManager().getDefaultDisplay();

  tv.setText("Width: "+display.getWidth()+" x " +"Height: "+display.getHeight());
  
  //For Service (If you want to get screen resolution within the Service class
  
/* window = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
   Display display = window.getDefaultDisplay();
   int width = display.getWidth();
   int height = display.getHeight();
   
   tv.setText("Width: "+display.getWidth()+" x " +"Height: "+display.getHeight());
*/   
  
}

}


Download full project from here:



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

No comments:

Post a Comment