Set/Display back arrow button in Actionbar in Android Activity

How to create an action bar with custom previous icon click which navigate to back activity screen.

Simple back arrow button also known as UP button are on of the most useful part of every android application because this icon will provide direct back button navigation to application user and by clicking on it the user will redirect to back activity. So in this tutorial we are creating two activity first one is MainActivity.java file and second is NextActivity.java file now we are creating a button on MainActivity including click event and by clicking on that button user will goto NextActivity . Inside NextActivity we will writing the back arrow  button code which redirect us again on MainActivity . So here is the complete step by step tutorial for Set/Display back arrow button in Actionbar in Android Activity.

android-project-download-code-button
Note: If you are facing any type of NullPointerException error then ready my this post to solve this error.

How to Set/Display back arrow button in Actionbar in Android Activity.

Code for MainActivity.java file.

 package com.setbackbuttonactionbar_android_examples.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

 Button button;
 Intent intent;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 intent = new Intent(MainActivity.this, NextActivity.class);
 startActivity(intent);
 
 }
 });
 }

}

Code for activity_main.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.setbackbuttonactionbar_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click here to goto next activity" />

</RelativeLayout>

Code for NextActivity.java file.

package com.setbackbuttonactionbar_android_examples.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

public class NextActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_next);
 
 getActionBar().setHomeButtonEnabled(true);
 getActionBar().setDisplayHomeAsUpEnabled(true);
 
 
 }
 
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
 case android.R.id.home:
 
 // app icon in action bar clicked; goto parent activity.
 this.finish();
 return true;
 default:
 return super.onOptionsItemSelected(item);
 }
 }
 
 
}

Code for activity_main.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.setbackbuttonactionbar_android_examples.com.NextActivity" >

</RelativeLayout>

Screenshots:

next-activity

Set/Display back arrow button in Actionbar in Android Activity

Click here to download Set/Display back arrow button in Actionbar in Android Activity project with source code.

Leave a Reply

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