0% found this document useful (0 votes)
7 views30 pages

5- Android Layout Examples

Uploaded by

redshadow1525
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views30 pages

5- Android Layout Examples

Uploaded by

redshadow1525
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 30

Layout Examples:

Android Linear Layout

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.

2 Modify the default content of res/layout/activity_main.xml file to include few buttons in


linear layout.

3 No need to change string Constants.Android studio takes care of default strings

4 Run the application to launch Android emulator and verify the result of the changes done
in the application.

Following is the content of the modified main activity


file src/com.example.demo/MainActivity.java. This file can include each of the fundamental
lifecycle methods.

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {


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

Following will be the content of res/layout/activity_main.xml file −

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<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>

Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">HelloWorld</string>
<string name="action_settings">Settings</string>
</resources>

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.

3 Define required constants in res/values/strings.xml file

4 Run the application to launch Android emulator and verify the result of the changes done in
the application.

Following is the content of the modified main activity


file src/com.example.demo/MainActivity.java. This file can include each of the fundamental
lifecycle methods.

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {


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

Following will be the content of res/layout/activity_main.xml file −

<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>

Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="action_settings">Settings</string>
<string name="reminder">Enter your name</string>
</resources>

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.

2 Modify the default content of res/layout/activity_main.xml file to include few widgets in


table layout.

3 No need to modify string.xml, Android studio takes care of default constants

4 Run the application to launch Android emulator and verify the result of the changes done
in the application.

Following is the content of the modified main activity


file src/com.example.demo/MainActivity.java. This file can include each of the fundamental
lifecycle methods.

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {


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

Following will be the content of res/layout/activity_main.xml file −

<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>

Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">HelloWorld</string>
<string name="action_settings">Settings</string>
</resources>

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.

3 No need to modify string.xml, Android studio takes care of default constants

4 Run the application to launch Android emulator and verify the result of the changes done
in the application.

Following is the content of the modified main activity


file src/com.example.demo/MainActivity.java. This file can include each of the fundamental
lifecycle methods.

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {


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

Following will be the content of res/layout/activity_main.xml file −

<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>

Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">demo</string>
<string name="action_settings">Settings</string>
</resources>

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.

2 Modify the default content of res/layout/activity_main.xml file to include few widgets in


frame layout.

3 No need to change string.xml, android takes care default constants

4 Run the application to launch Android emulator and verify the result of the changes done in
the application.

Following is the content of the modified main activity


file src/com.example.demo/MainActivity.java. This file can include each of the fundamental
lifecycle methods.

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {


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

Following will be the content of res/layout/activity_main.xml file −

<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>

Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">demo</string>
<string name="action_settings">Settings</string>
</resources>

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 List View


Example

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.

2 Modify the default content of res/layout/activity_main.xml file to include ListView content


with the self explanatory attributes.

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.

Following is the content of the modified main activity


file src/com.example.ListDisplay/ListDisplay.java. This file can include each of the
fundamental life cycle methods.

package com.example.ListDisplay;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListDisplay extends Activity {


// Array of strings...
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
"WebOS","Ubuntu","Windows7","Max OS X"};

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

ArrayAdapter adapter = new ArrayAdapter<String>(this,


R.layout.activity_listview, mobileArray);

ListView listView = (ListView) findViewById(R.id.mobile_list);


listView.setAdapter(adapter);
}
}
Following will be the content of res/layout/activity_main.xml 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"
android:orientation="vertical"
tools:context=".ListActivity" >

<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">ListDisplay</string>
<string name="action_settings">Settings</string>
</resources>

Following will be the content of res/layout/activity_listview.xml file −

<?xml version="1.0" encoding="utf-8"?>


<!-- Single List Item Design -->

<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 −

String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME,


ContactsContract.CommonDataKinds.Phone.NUMBER};
int[] toViews = {R.id.display_name, R.id.phone_number};

When you instantiate the SimpleCursorAdapter, pass the layout to use for each result, the Cursor
containing the results, and these two arrays −

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,


R.layout.person_name_and_number, cursor, fromColumns, toViews, 0);

ListView listView = getListView();


listView.setAdapter(adapter);

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.

Android Grid 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.

2 Modify the detault content of res/layout/activity_main.xml file to include GridView content


with the self explanatory attributes.

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.

Following is the content of the modified main activity


file src/com.example.helloworld/MainActivity.java. This file can include each of the
fundamental lifecycle methods.

package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.GridView;

public class MainActivity extends Activity {


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

GridView gridview = (GridView) findViewById(R.id.gridview);


gridview.setAdapter(new ImageAdapter(this));
}
}

Following will be the content of res/layout/activity_main.xml file −

<?xml version="1.0" encoding="utf-8"?>


<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>

Following will be the content of res/values/strings.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">HelloWorld</string>
<string name="action_settings">Settings</string>
</resources>

Following will be the content of src/com.example.helloworld/ImageAdapter.java file −

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;

public class ImageAdapter extends BaseAdapter {


private Context mContext;

// Constructor
public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {


return mThumbIds.length;
}

public Object getItem(int position) {


return null;
}

public long getItemId(int position) {


return 0;
}

// create a new ImageView for each item referenced by the Adapter


public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;

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.

2 Create a new Activity class as SingleViewActivity.java under a


package com.example.helloworld as shown below.

3 Create new layout file for the new activity under res/layout/ folder. Let's name this XML
file as single_view.xml.

4 Define your new activity in AndroidManifest.xml file using <activity.../> tag. An


application can have one or more activities without any restrictions.

5 Run the application to launch Android emulator and verify the result of the changes done
in the application.

Following is the content of the modified main activity


file src/com.example.helloworld/MainActivity.java. This file can include each of the
fundamental life cycle methods.

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;

public class MainActivity extends Activity {


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

GridView gridview = (GridView) findViewById(R.id.gridview);


gridview.setAdapter(new ImageAdapter(this));

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);
}
});
}
}

Following will be the content of new activity


file src/com.example.helloworld/SingleViewActivity.java file −
package com.example.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

public class SingleViewActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);

// Get intent data


Intent i = getIntent();

// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);

ImageView imageView = (ImageView) findViewById(R.id.SingleView);


imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}

Following will be the content of res/layout/single_view.xml file −

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:id="@+id/SingleView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

</LinearLayout>

Following will be the content of AndroidManifest.xml to define two new constants −

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld">

<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.

You might also like