Sunday, 1 September 2013

Find Device Screen Size Demo

Demo:


1) MainActivity.java: 
package com.venky.finddevicescreensizedemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.widget.TextView;

public class MainActivity extends Activity {

       TextView tv = null;
       String TAG = "";
       int width, height;

       @SuppressLint("NewApi")
       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);

             tv = (TextView) findViewById(R.id.tv);

             Display display = getWindowManager().getDefaultDisplay();

             if (android.os.Build.VERSION.SDK_INT <= 13) {
                    // these methods are deprecated
                    width = display.getWidth();
                    height = display.getHeight();
             } else {
                    Point p = new Point();
                    display.getSize(p);
                    // new methods
                    width = p.x;
                    height = p.y;
             }

             tv.setText("Size of the Device is\n" + width + "<=>" + height);

       }
}
 
2) activity_main.xml: 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5F04B4"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        android:textColor="#ffffff"
        android:textSize="15dp"
        android:textStyle="bold" />

</RelativeLayout>

3) Download this Project Click Here

No comments:

Post a Comment