5- Android Layout Examples
5- Android Layout Examples
Example
This example will take you through simple steps to show how to create your own Android
application using Linear Layout. Follow the following steps to modify the Android application we
created in Hello World Example chapter −
Step Description
1 You will use Android Studio to create an Android application and name it as Demo under a
package com.example.demo as explained in the Hello World Example chapter.
4 Run the application to launch Android emulator and verify the result of the changes done
in the application.
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>
<Button android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/>
<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>
</LinearLayout>
Let's try to run our modified Hello World! application we just modified. I assume you had created
your AVD while doing environment setup. To run the app from Android studio, open one of your
project's activity files and click Run icon from the toolbar. Android studio installs the app on
your AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
Now let's change the orientation of Layout as android:orientation="horizontal" and try to run
the same application, it will give following screen −
Android Relative Layout
Example
This example will take you through simple steps to show how to create your own Android
application using Relative Layout. Follow the following steps to modify the Android application
we created in Hello World Example chapter −
Step Description
1 You will use Android Studio IDE to create an Android application and name it
as demo under a package com.example.demo as explained in the Hello World
Example chapter.
2 Modify the default content of res/layout/activity_main.xml file to include few widgets in
Relative layout.
4 Run the application to launch Android emulator and verify the result of the changes done in
the application.
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/name">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2" />
</LinearLayout>
</RelativeLayout>
Let's try to run our modified Hello World! application we just modified. I assume you had created
your AVD while doing environment setup. To run the app from Android Studio, open one of your
project's activity files and click Run icon from the toolbar. Android Studio installs the app on
your AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
Android Table Layout
Example
This example will take you through simple steps to show how to create your own Android
application using Table Layout. Follow the following steps to modify the Android application we
created in Hello World Example chapter −
Step Description
1 You will use Android Studio IDE to create an Android application and name it
as demo under a package com.example.demo as explained in the Hello World
Example chapter.
4 Run the application to launch Android emulator and verify the result of the changes done
in the application.
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textClock"
android:layout_column="2" />
</TableRow>
<TableRow>
<TextView
android:text="First Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />
<EditText
android:width="200px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:text="Last Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />
<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:layout_column="2" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_column="2" />
</TableRow>
</TableLayout>
Let's try to run our modified Hello World! application we just modified. I assume you had created
your AVD while doing environment setup. To run the app from Android Studio, open one of your
project's activity files and click Run icon from the toolbar. Android studio installs the app on
your AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
Android Absolute Layout
Example
This example will take you through simple steps to show how to create your own Android
application using absolute layout. Follow the following steps to modify the Android application
we created in Hello World Example chapter −
Step Description
1 You will use Android studio IDE to create an Android application and name it
as demo under a package com.example.demo as explained in the Hello World
Example chapter.
2 Modify the default content of res/layout/activity_main.xml file to include few widgets in
absolute layout.
4 Run the application to launch Android emulator and verify the result of the changes done
in the application.
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="OK"
android:layout_x="50px"
android:layout_y="361px" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_x="225px"
android:layout_y="361px" />
</AbsoluteLayout>
Let's try to run our modified Hello World! application we just modified. I assume you had created
your AVD while doing environment setup. To run the app from Android Studio, open one of your
project's activity files and click Run icon from the toolbar. Android Studio installs the app on
your AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
Android Frame Layout
Example
This example will take you through simple steps to show how to create your own Android
application using frame layout. Follow the following steps to modify the Android application we
created in Hello World Example chapter −
Step Description
1 You will use Android studio IDE to create an Android application and name it as demo under
a package com.example.demo as explained in the Hello World Example chapter.
4 Run the application to launch Android emulator and verify the result of the changes done in
the application.
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="@drawable/ic_launcher"
android:scaleType="fitCenter"
android:layout_height="250px"
android:layout_width="250px"/>
<TextView
android:text="Frame Demo"
android:textSize="30px"
android:textStyle="bold"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>
Let's try to run our modified Hello World! application we just modified. I assume you had created
your AVD while doing environment setup. To run the app from Android Studio, open one of your
project's activity files and click Run icon from the toolbar. Android Studio installs the app on
your AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
Following is the example which will take you through simple steps to show how to create your
own Android application using ListView. Follow the following steps to modify the Android
application we created in Hello World Example chapter −
Step Description
1 You will use Android Studio IDE to create an Android application and name it
as ListDisplay under a package com.example.ListDisplay as explained in the Hello World
Example chapter.
3 No need to change string.xml, Android studio takes care of default string constants.
4 Create a Text View file res/layout/activity_listview.xml. This file will have setting to
display all the list items. So you can customize its fonts, padding, color etc. using this file.
6 Run the application to launch Android emulator and verify the result of the changes done
in the application.
package com.example.ListDisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<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"
android:orientation="vertical"
tools:context=".ListActivity" >
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
Let's try to run our modified Hello World! application we just modified. I assume you had created
your AVD while doing environment set-up. To run the app from Android studio, open one of your
project's activity files and click Run icon from the tool bar. Android studio installs the app on
your AVD and starts it and if everything is fine with your set-up and application, it will display
following Emulator window −
SimpleCursorAdapter
You can use this adapter when your data source is a database Cursor. When
using SimpleCursorAdapter, you must specify a layout to use for each row in the Cursor and
which columns in the Cursor should be inserted into which views of the layout.
For example, if you want to create a list of people's names and phone numbers, you can perform a
query that returns a Cursor containing a row for each person and columns for the names and
numbers. You then create a string array specifying which columns from the Cursor you want in
the layout for each result and an integer array specifying the corresponding views that each column
should be placed −
When you instantiate the SimpleCursorAdapter, pass the layout to use for each result, the Cursor
containing the results, and these two arrays −
The SimpleCursorAdapter then creates a view for each row in the Cursor using the provided layout
by inserting each from Columns item into the corresponding toViews view.
Example
This example will take you through simple steps to show how to create your own Android
application using GridView. Follow the following steps to modify the Android application we
created in Hello World Example chapter −
Step Description
1 You will use Android studio IDE to create an Android application and name it
as HelloWorld under a package com.example.helloworld as explained in the Hello World
Example chapter.
3 No need to change string.xml, Android studio takes care of defaults strings which are
placed at string.xml
4 Let's put few pictures in res/drawable-hdpi folder. I have put sample0.jpg, sample1.jpg,
sample2.jpg, sample3.jpg, sample4.jpg, sample5.jpg, sample6.jpg and sample7.jpg.
5 Create a new class called ImageAdapter under a package com.example.helloworld that
extends BaseAdapter. This class will implement functionality of an adapter to be used to
fill the view.
6 Run the application to launch Android emulator and verify the result of the changes done
in the application.
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.GridView;
package com.example.helloworld;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
// Constructor
public ImageAdapter(Context c) {
mContext = c;
}
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
Let's try to run our modified Hello World! application we just modified. I assume you had created
your AVD while doing environment setup. To run the app from Android Studio, open one of your
project's activity files and click Run icon from the toolbar. Android studio installs the app on
your AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
Sub-Activity Example
Let's extend the functionality of above example where we will show selected grid image in full
screen. To achieve this we need to introduce a new activity. Just keep in mind for any activity we
need perform all the steps like we have to implement an activity class, define that activity in
AndroidManifest.xml file, define related layout and finally link that sub-activity with the main
activity by it in the main activity class. So let's follow the steps to modify above example −
Step Description
1 You will use Android studio IDE to create an Android application and name it
as HelloWorld under a package com.example.helloworld as explained in the Hello World
Example chapter.
3 Create new layout file for the new activity under res/layout/ folder. Let's name this XML
file as single_view.xml.
5 Run the application to launch Android emulator and verify the result of the changes done
in the application.
package com.example.helloworld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View v, int position, long id){
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
</LinearLayout>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.helloworld.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SingleViewActivity"></activity>
</application>
</manifest>
Let's try to run our modified Hello World! application we just modified. I assume you had created
your AVD while doing environment setup. To run the app from Android studio, open one of your
project's activity files and click Run icon from the toolbar. Android studio installs the app on
your AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
Now if you click on either of the images it will be displayed as a single image, for example−
Kindly note above mentioned images have been taken from Android official website.