Friday 6 September 2013

Android Screen Dimension

How to get the Screen Dimension in Android Application


Hi, This is the way to easily get the screen dimensions of currently running android device.

1.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: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/dimen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="screen dimensions" />

    <TextView
        android:id="@+id/res"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:layout_below="@+id/dimen"/>

</RelativeLayout>

2.MainActivity.java

package com.sri.screendimension;

import android.os.Bundle;
import android.view.Display;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Point;

public class MainActivity extends Activity {
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display disp = getWindowManager().getDefaultDisplay();
Point size = new Point();
disp.getSize(size);
int width = size.x;
int height = size.y;
tv = (TextView) findViewById(R.id.res);
tv.setText(width + "<----->" + height);
}

}

3.Run the Application.


No comments:

Post a Comment