Set height and width in percentage format in layout android

How to use assign width and height in % format in android linearlayout,RelativeLayout.

Percentage format is used to mostly create web applications because websites are open in different types of screen sizes like CRT, LED, LCD, Tablets, Mobile phones etc. When application developer develop application using percentage then it will automatically adjust the whole app using percentage form. For example if developer define two views inside app and assign 50% – 50% to both views then no matter what is screen size it will automatically share 50-50 of complete screen. By adding android:weightSum=”100″ attribute inside LinearLayout gives us the facility to divide its child view elements using adding another attribute android:layout_weight=” “ into child view elements. So here is the complete step by step tutorial for Set height and width in percentage format in layout android.

Brief Example : If you have define LinearLayout with android:weightSum=”100″ and after that you have add three layouts inside parent layout with android:layout_weight=”50″android:layout_weight=”25″android:layout_weight=”25″ attribute then it will automatically divide the complete device screen into three parts and gives the first layout to 50% of screen and second layout to 25% and third layout to 25% of screen.

android-project-download-code-button

How to Set height and width in percentage format in layout android.

Code for MainActivity.java file.

package com.setheightwidthpercentage_android_examples.com;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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

Code for activity_main.xml layout file.

 <LinearLayout 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"
 tools:context="com.setheightwidthpercentage_android_examples.com.MainActivity"
 android:weightSum="100"
 android:orientation="vertical"
 >

 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="50"
 android:orientation="vertical"
 android:background="#fde200">

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="This is layout first with 50% width and height"
 android:textAppearance="?android:attr/textAppearanceLarge"
 
 />
 
 </LinearLayout>
 
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="25"
 android:orientation="vertical"
 android:background="#0042fd">
 
 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="This is layout Second with 25% width and height"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:textColor="#fbfdfc"
 />
 
 </LinearLayout>
 
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_weight="25"
 android:orientation="vertical"
 android:background="#00fd77">
 
 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="This is layout Third with 25% width and height"
 android:textAppearance="?android:attr/textAppearanceLarge"
 
 />
 
 </LinearLayout>

</LinearLayout>

Screenshot :

Set height and width in percentage format in layout android

Click here to download Set height and width in percentage format in layout android project.

Leave a Reply

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