Support multiple screen sizes in android responsive example

Create & use both tablet layout, normal device layout in single app.

Android devices can also supports multiple screen sizes like web applications do. Process of making your app responsive all you have to do is create multiple layout so if app opens in tablet than it calls tablet layout & if open in normal device it calls another tablet layout. You can do this by using WindowManager() function. So all you have to do is create multiple designing layout files in your layout folder one file for normal device and another for tablet layout. Tablet layout will also applied through horizontal phone moment. So here is the complete step by step tutorial for Support multiple screen sizes in android responsive example .

android-project-download-code-button

How to Support multiple screen sizes in android responsive example .

Code for MainActivity.java file.

 package com.android_examples.com.layoutmultiplesupport;

import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 WindowManager winManager = getWindowManager();
 Display display = winManager.getDefaultDisplay();
 int width = display.getWidth();
 int height = display.getHeight();

 if(width > height)
 {
 // Tablet layout called automatically in this If condition.
 setContentView(R.layout.activity_tablet_device);
 }
 else
 {
 // Normal Phone layout called automatically in this else condition.
 setContentView(R.layout.activity_normal_main);
 }
 }
 
 }

Code for activity_normal_device.xml layout file.

 <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="com.android_examples.com.layoutmultiplesupport.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Normal device Layout"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Code for activity_tablet_device.xml layout file.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Tablet Device Layout"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot on Normal mobile device:

normal layout

Screenshot of tablet layout when opens application on tablet:

Support multiple screen sizes in android responsive example

Click Here To Download Support multiple screen sizes in android responsive example project.

Leave a Reply

Your email address will not be published. Required fields are marked *