- Add a Back Button to Action Bar Android Studio (Kotlin)
- Step 1: Create a new Project or open new project
- Step 2: Create New Activity File>New>Activity>EmptyActivity
- Step 3: Code:
- ActionBar in Android with Example
- Designing a Custom ActionBar
- Add items or menu in Actionbar/Toolbar (Kotlin) – Android Studio
- Step 1: Create a new Project or open new project
- Step 2: Create new “Android Resource Directory” by clicking “res>New>Android Resource Directory”, choose menu from Resource type
- Step 5: Code
- Step 5: Code
- Android Tutorials
- VIDEO
- SOURCE CODE
- Step 1: Create a new Project or open new project
- Step 2: Create New Activity File>New>Activity>EmptyActivity
- Step 3: Code:
- Step 4: Run Project
- Comments
- Post a Comment
- Popular posts from this blog
- Manage External Storage Permission | Android Studio | Java
- Manage External Storage Permission | Android Studio | Kotlin
Add a Back Button to Action Bar Android Studio (Kotlin)
In this tutorial we will add a back button in action bar, when it is clicked it will go to previous activity(the app will close if this was launcher activity). We will go from main activity to new activity by clicking button in main activity. In new activity we will add a back button to actionbar when that button is clicked the main activity will appear..
Step 1: Create a new Project or open new project
Step 2: Create New Activity File>New>Activity>EmptyActivity
Step 3: Code:
package com.jigopost.myapplication import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Button class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //button val mStartActBtn = findViewById(R.id.startActBtn) //handle button click mStartActBtn.setOnClickListener < //start activity intent startActivity(Intent(this@MainActivity, NewActivity::class.java)) >> >
package com.jigopost.myapplication import android.support.v7.app.AppCompatActivity import android.os.Bundle class NewActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_new) //actionbar val actionbar = supportActionBar //set actionbar title actionbar. title = "New Activity" //set back button actionbar.setDisplayHomeAsUpEnabled(true) actionbar.setDisplayHomeAsUpEnabled(true) >override fun onSupportNavigateUp(): Boolean < onBackPressed() return true >>
ActionBar in Android with Example
In Android applications, ActionBar is the element present at the top of the activity screen. It is a salient feature of a mobile application that has a consistent presence over all its activities. It provides a visual structure to the app and contains some of the frequently used elements for the users. Android ActionBar was launched by Google in 2013 with the release of Android 3.0(API 11). Before that, the name of this top most visual element was AppBar. AppBar contains only the name of the application or current activity. It was not very much useful for the users and developers also have negligible option to customize it.
Google announced a support library along with the introduction of ActionBar. This library is a part of AppCompat and its purpose is to provide backward compatibility for older versions of Android and to support tabbed interfaces. All applications that use the default theme provided by the Android(Theme.AppCompat.Light.DarkActionBar), contains an ActionBar by default. However, developers can customize it in several ways depending upon their needs. Components included in the ActionBar are:
- App Icon: Display the branding logo/icon of the application.
- View Controls: Section that displays the name of the application or current activity. Developers can also include spinner or tabbed navigation for switching between views.
- Action Button: Contains some important actions/elements of the app that may be required to the users frequently.
- Action Overflow: Include other actions that will be displayed as a menu.
Designing a Custom ActionBar
The following example demonstrates the steps involved in creating a custom ActionBar for the MainActivity of an application. All important aspects of visual elements like icon, title, subtitle, action buttons, and overflow menu will be covered.
Note: Following steps are performed on Android Studio version 4.0
Step 1: Default ActionBar
As mentioned earlier, every android app contains an ActionBar by default. This pre-included ActionBar display title for the current activity that is managed by the AncdroidManifest.xml file. The string value of the application’s title is provided by @string/app_name resource present under the application nodes.
Step 2: Creating a new directory and design items of ActionBar
To code the elements of ActionBar, create a new directory in the resource folder of the application project files. Right-click on the res folder and selects New -> Directory. Give the name “menu” to the new directory.
Further, create a new Menu Resource File by right click on the menu directory. As the ActionBar is being created for the main Activity, type the name as “main” to the Menu Resource File. With this, a new file named “main.xml” must be created under the menu directory. In this file, one can declare the items which will be displayed as the action buttons of the ActionBar.
For every menu items, the following attributes are needed to be configured:
- android:title: Its value contains the title of the menu item that will be displayed when a user clicks and holds that item in the app.
- android:id: A unique ID for the menu item that will be used to access it anywhere in the whole application files.
- android:orderInCategory: The value of this attribute specify the item’s position in the ActionBar. There are two ways to define the position of different menu items. The first one is to provide the same value of this attribute for all items and the position will be defined in the same order as they are declared in the code. The second way is to provide a different numeric value for all items and then the items will position themselves according to ascending order of this attribute’s value.
- app:showAsAction: This attribute defines how the item is going to be present in the action bar. There are four possible flags to choose from:
- a. always: To display the item in the ActionBar all the time.
- b. ifRoom: To keep the item if space is available.
- c. never: With this flag, the item will be not be displayed as an icon in ActionBar, but will be present in the overflow menu.
- d. withText: To represent an item as both icon and the title, one can append this flag with the always or ifRoom flag(always|withText or ifRoom|withText).
Icon of an ActionBar Item
In order to provide an icon to an item, right-click on the res folder, select new, and then Image Asset. A dialog box will appear, choose the Icon Type as Action Bar and Tab Icons. Choose assets type as “Clip Art” and select an image from the clip art collection. Provide a desired name to the icon. Click on Next, then Finish. This icon will now get loaded in the drawable directory of the res folder. The name provided by the developers to these icons will now be used to reference the item’s icon resource.
Below is the code to place a search icon, refresh icon, and an overflow menu in the ActionBar.
Add items or menu in Actionbar/Toolbar (Kotlin) – Android Studio
In this tutorial we will add items in actionbar/toolbar, 3-dot menu in actionbar/toolbar and handle item clicks using Kotlin. There will be three items in menu you can add as many as you want. One item will be displayed on action bar with icon, other two items will be displayed by clicking 3-dot menu.
Step 1: Create a new Project or open new project
Step 2: Create new “Android Resource Directory” by clicking “res>New>Android Resource Directory”, choose menu from Resource type
Step 3: Create menu.xml by clicking “menu>New>Menu resource file”
Step 4: Enter an icon in drawable folder by clicking “drawable>New>Image Asset”
Step 5: Code
Step 5: Code
activity_main.xml
MainActivity.kt
package com.jigopost.myapplication import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.widget.Toast class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) >override fun onCreateOptionsMenu(menu: Menu): Boolean < // Inflate the menu; this adds items to the action bar if it is present. menuInflater.inflate(R.menu.menu, menu) return true >override fun onOptionsItemSelected(item: MenuItem): Boolean < // Handle action bar item clicks here. val if (id == R.id.action_one) < Toast.makeText(this, "Item One Clicked", Toast.LENGTH_LONG).show() return true >if (id == R.id.action_two) < Toast.makeText(this, "Item Two Clicked", Toast.LENGTH_LONG).show() return true >if (id == R.id.action_three) < Toast.makeText(this, "Item Three Clicked", Toast.LENGTH_LONG).show() return true >return super.onOptionsItemSelected(item) > >
Android Tutorials
In this tutorial we will add a back button in action bar, when it is clicked it will go to previous activity(the app will close if this was launcher activity). We will go from main activity to new activity by clicking button in main activity. In new activity we will add a back button to actionbar when that button is clicked the main activity will appear.
VIDEO
SOURCE CODE
Step 1: Create a new Project or open new project
Step 2: Create New Activity File>New>Activity>EmptyActivity
Step 3: Code:
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" android:gravity="center" tools:context=".MainActivity"> android:id="@+id/startActBtn" android:text="New Activity" android:layout_width="wrap_content" android:layout_height="wrap_content" />
package com.blogspot.devofandroid.myapplication import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Button class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //button val mStartActBtn = findViewById(R.id.startActBtn) //handle button click mStartActBtn.setOnClickListener < //start activity intent startActivity(Intent(this@MainActivity, NewActivity::class.java)) > > >
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" android:orientation="vertical" android:gravity="center" tools:context=".NewActivity"> android:text="Click back buttoon of actionbar" android:layout_width="wrap_content" android:layout_height="wrap_content" />
package com.blogspot.devofandroid.myapplication import android.support.v7.app.AppCompatActivity import android.os.Bundle class NewActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_new) //actionbar val actionbar = supportActionBar //set actionbar title actionbar. title = "New Activity" //set back button actionbar.setDisplayHomeAsUpEnabled(true) actionbar.setDisplayHomeAsUpEnabled(true) > override fun onSupportNavigateUp(): Boolean < onBackPressed() return true > >
Step 4: Run Project
- Get link
- Other Apps
Comments
Post a Comment
Popular posts from this blog
Manage External Storage Permission | Android Studio | Java
Manage External Storage Permission Previously (before Android 11) we only need to request the WRITE_EXTERNAL_STORAGE Permission to perform the storage-related tasks. But, now for Android 11 and above we have to request the MANAGE_EXTERNAL_STORAGE Permission to perform the storage-related tasks. Keep in mind that if you’re 100% sure that your app requires this permission to work properly then request this permission otherwise Google will not allow you to publish/update your app. For example, if your app is WhatsApp Status Saver then you won’t be allowed to use this permission as your app works with images and videos, not files. After permission is granted, I’ll input a name and create a folder with that name just for example. You can do whatever you want instead. >>Check For Kotlin Video Tutorial: Coding: AndroidManifest.xml
Manage External Storage Permission | Android Studio | Kotlin
Manage External Storage Permission Previously (before Android 11) we only need to request the WRITE_EXTERNAL_STORAGE Permission to perform the storage-related tasks. But, now for Android 11 and above we have to request the MANAGE_EXTERNAL_STORAGE Permission to perform the storage-related tasks. Keep in mind that if you’re 100% sure that your app requires this permission to work properly then request this permission otherwise Google will not allow you to publish/update your app. For example, if your app is WhatsApp Status Saver then you won’t be allowed to use this permission as your app works with images and videos, not files. After permission is granted, I’ll input a name and create a folder with that name just for example. You can do whatever you want instead. >>Check For Java Video Tutorial: Coding: AndroidManifest.xml