Showing posts with label Action bar. Show all posts
Showing posts with label Action bar. Show all posts

Saturday, 20 October 2018

Make back button working in action bar in Android.

Recently I was trying to use working back button on my one of the project, but whatever I was done previously was not working. It was a crash every time with error message -


I am newbie to Android, but I was done back activity few day back but after updating my Android Studios and APIs nothing was working. I was also uses  inflated menu in my project, and debugger always stuck in onOptionsItemSelected(MenuItem item) method which I was used for option menu. I also tried many unsuccessful lines of code. Finally I got this solution for my problem -

1 .  In  onCreate(Bundle savedInstanceState) method use -

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

2 .  Override methos in your activity class

@Overridepublic boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}


3 . In onOptionsItemSelected(MenuItem item) method use -

switch (item.getItemId()) {  
       case android.R.id.home:
        Intent i = new Intent(this, Your_Parent_Activity.class);
        intent.addFlags(i.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        return true;
}


Thursday, 4 October 2018

Add app bar / action bar / tool bar and menu in Android Studio.

App bar or action bar or tool bar is a dedicated bat on the top of the every app/activity. It is also called title bar of activity. It may contain toolbox/icon or overflow menu or can be both.


So how can we create this action bar in your project?

1. create menu.xml file in res/menu directory of your project. To create go to -

New ---> Android Resource File

Enter the file name (should start with small letters alphabets) then select resource type as menu then click Ok.


Now create menu by adding items in this menu.xml file.

<item android:title="Item 1"></item>
<item android:title="Item 2"></item>
<item android:title="Item 3"></item>
<item android:title="Item 4"></item>

The complete menu.xml is-

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:title="Item 1"></item>
    <item android:title="Item 2"></item>
    <item android:title="Item 3"></item>
    <item android:title="Item 4"></item>
</menu>


2. After creating menu, you require it to inflate on title bar/action bar. To inflate, write given below code in you activity

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater in=getMenuInflater(); in.inflate(R.menu.menu,menu); return super.onCreateOptionsMenu(menu); }

After running your program you can see the menu in action bar, when you click on three dots on left side of action bar like -


If you want to add icon or tool box on the action bar then in menu.xml file, use icon property of item tag and select any .png image from your res/mipmap directory, like (You can see home icon in screen shot given below)  

<item android:title="home"    android:icon="@mipmap/home"    app:showAsAction="always"></item>




Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...