How We Can Make Back Button In Our Android Application?
Back Button
Button
Android
Application
- By Code solution
- Jan 20th, 2021
- 0 comments
- 0
The back button is the very important concept.Nowadays, Back button use's every android apps.If we want to use the back button in our app.So this is the process.
- First, you have to open new empty project.
- Here we will need two activities.
- So we will create a new activity. for creating new activity go resource folder use right click choose New after that choose Activity after that we have to choose an Empty activity.
- Choose first XML page and create here Button code for jumping the second page.
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35dp"
android:text="Click"
android:id="@+id/button"/>
- Choose MainActivity.java page and here use the Intent code for jumping the second page.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
}
}
- Choose second activity MainActivity.java page.
- Create this code in the Oncreate method
if (getSupportActionBar()!=null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
- After creating this code use this code out of an Oncreate method
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId()==R.id.home);
finish();
return super.onOptionsItemSelected(item);
}
- Run the program and check.
Back Button All Codes :-)
- activity_main.XML Page Code.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35dp"
android:text="Click"
android:id="@+id/button"/>
</android.support.constraint.ConstraintLayout>
- MainActivity.java All Codes:-)
package waytofeed.wapptech.com.backbuttoncode;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
}
}
- activity_main .xml All Codes:-)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
</android.support.constraint.ConstraintLayout>
- MainActivity2.java All Codes:-)
package waytofeed.wapptech.com.backbuttoncode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
if (getSupportActionBar()!=null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId()==R.id.home);
finish();
return super.onOptionsItemSelected(item);
}
}