How To Set Back Button In Android
How to Add and Customize Dorsum Push button of Action Bar in Android?
The action bar (sometimes referred to as the app bar), if information technology exists for an activity, will be at the top of the activity'south content area, typically directly underneath the condition bar. Information technology is a carte bar that runs across the acme of the activity screen in android. Android ActionBar can contain menu items that become visible when the user clicks the "menu" button. In general, an ActionBar composed of the following four components:
- App Icon: App branding logo or icon will be shown here
- View Control: A dedicated infinite to brandish the Application championship. Also provides the option to switch between views by adding spinner or tabbed navigation
- Activity Buttons: Major deportment of the app could be added here
- Activeness Overflow: All unimportant action volition be displayed every bit a menu
Below is a sample image to testify where the Activeness Bar/Toolbar/App Bar is nowadays on an android device.
The action bar is a primary toolbar within an activity that tin can be used to display an activity title and other interactive items. Ane of the nearly used items is a Back Navigation Button. The back push is used to move backward from the previously visited screen by the user. Well-nigh Android devices have a dedicated back button all the same a back button on the action bar enhances the user feel.
Add Back Button in Activeness Bar
To create a new project in Android Studio please refer to How to Create/Start a New Projection in Android Studio. At that place is no demand to alter anything in the activity_main.xml file. The simply file we have to piece of work with is Working with the MainActivity file.
- Create action bar variable and call function getSupportActionBar() in the java/kotlin file.
- Evidence back button using actionBar.setDisplayHomeAsUpEnabled(true) this will enable the back button.
- Custom the back effect at onOptionsItemSelected. This will enable the back office to the button on the press. Encounter the below lawmaking for reference. We have provided both the java and kotlin code for MainActivity.
Java
import android.os.Package;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
public grade MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Parcel savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled( true );
}
@Override
public boolean onOptionsItemSelected( @NonNull MenuItem item) {
switch (item.getItemId()) {
instance android.R.id.dwelling house:
this .finish();
return true ;
}
return super .onOptionsItemSelected(item);
}
}
Kotlin
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Packet?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var actionBar = getSupportActionBar()
if (actionBar != null ) {
actionBar.setDisplayHomeAsUpEnabled( true )
}
}
override fun onContextItemSelected(particular: MenuItem): Boolean {
when (particular.itemId) {
android.R.id.dwelling -> {
stop()
return truthful
}
}
render super .onContextItemSelected(item)
}
}
Output:
Customize Back Push button in Action Bar
Nosotros can hands Customize the Back Button by using the getSupportActionBar() library and setting the drawable file using setHomeAsUpIndicator in the java/kotlin file.
// Customize the dorsum push button
actionBar.setHomeAsUpIndicator(R.drawable.mybutton);
The complete code is given below.
Coffee
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Packet savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.mybutton);
actionBar.setDisplayHomeAsUpEnabled( true );
}
@Override
public boolean onOptionsItemSelected( @NonNull MenuItem particular) {
switch (item.getItemId()) {
case android.R.id.home:
this .finish();
return true ;
}
return super .onOptionsItemSelected(item);
}
}
Kotlin
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var actionBar = getSupportActionBar()
if (actionBar != null ) {
actionBar.setHomeAsUpIndicator(R.drawable.mybutton);
actionBar.setDisplayHomeAsUpEnabled( true );
}
}
override fun onContextItemSelected(item: MenuItem): Boolean {
when (particular.itemId) {
android.R.id.dwelling -> {
cease()
render true
}
}
render super .onContextItemSelected(particular)
}
}
Output:
How To Set Back Button In Android,
Source: https://www.geeksforgeeks.org/how-to-add-and-customize-back-button-of-action-bar-in-android/
Posted by: hibbittswiltow1939.blogspot.com

0 Response to "How To Set Back Button In Android"
Post a Comment