I have an activity that I would like the "up affordance" on.
public class ItemListActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// ...
}
}
This adds the "back button" like I want. The problem is that the Title is not considered part of the back button in this instance. Is there any way to make it so that it behaves more like the Messaging app? In the Messaging app you can tap either the back button or the title and it will take you back to the previous activity.
Add an onClickListener to the title of your ActionBar. See this post for example: Set OnClick Listener on Action Bar Title in Android
Related
I would like that in each Fragment the title of the ActionBar will be adapted. For example, if I select the option "Lists" (that is a Fragment), the title of the ActionBar have to be "Lists".
I can do it using the following code:
getActivity().setTitle("Lists");
but when I go back to MainActivity (using the menu option or the back button on the mobile phone) it maintains the title "Lists" instead of "My application" that is the default title.
If I try to set the title on MainActivity:
setTitle("My application");
it is not changed.
How can I maintain the title of the MainActivity and modify the title in each of the Fragments?
Thanks in advance!
Finally I am able to modify the title of your MainActivity after enter in a Fragment and modify the title of the ActionBar in it. I needed to change it with two different events: when I click on the menu of the application and when I press back button of the mobile phone. I will separate my answer in two parts, one for each event:
- To change title on MainActivity when you press back button on your mobile phone
You need to use onBackPressed() method on your MainActivity.class to detect the event and then use setTitle method to change the title of your ActionBar.
#Override
public void onBackPressed() {
setTitle("Lists");
}
- To change title on MainActivity when you press option "Home" in your menu.
On your onNavigationItemSelected method you can detect when you press the option "Home" and then start a new Fragment (let's call it MainFragment) which returns an empty layout so if you are in another Fragment, for example AnotherFragment (with another ActionBar title) and you press your "Home" option in your menu you can set again the title of your MainActivity as default one in your MainFragment with the following code:
getActivity().setTitle("Lists");
In this way you will be able to change the title of your MainActivity maintaining its layout.
Try using getSupportActionBar().setTitle("title");
That's what I use and it seems to work good. Here's an example using my code where I add fragments to a viewpager.
private void addPages(ViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
Intent intent = getIntent();
String workout = intent.getStringExtra("work");
Bundle args = new Bundle();
args.putString("work", work);
String title = args.getString("work");
switch(title) {
case "w29w1" : {
getSupportActionBar().setTitle("Work 29 Week 1");
break;
}
case "w29w2" : {
getSupportActionBar().setTitle("Work 29 Week 2");
break;
}
MondayFragment mondayFragment = new MondayFragment();
mondayFragment.setArguments(args);
In that example, I'm simply setting the title of the toolbar to something, depending on the intent it got from the previous class, as the fragment is used for multiple sets of data.
Put this code on onResume() of both activity and Fragment
Fragment:
#Override
public void onResume() {
super.onResume();
if(getActivity().getSupportActionBar()!=null)
getActivity().getSupportActionBar().setTitle("Lists");
}
Activity:
#Override
protected void onResume() {
super.onResume();
if(getSupportActionBar()!=null)
getSupportActionBar().setTitle("Title");
}
I am facing weird issue.I am using PageActivity inside three
fragments swipes one after another and so on.
My problem is,when I am in FragmentA page,on click the home
button ,again come back to the application, my action bar title name is
Chapter,which I was added the action bar title name in PageActivity and
after 2 seconds the fragment title was loading with the help of
query.
My only problem is,on home button click in fragment page instead of
activity title name,Fragment page title name have to be shown
directly after enter the application.
Below I am posted the code related to this:
PageActivity.java:
public class PageActivity extends FragmentActivity {
static TextView mTitleTextView;
.......
.......
#Override
protected void onResume() {
super.onResume();
ActionBar(Constants.Titlebarcolor);
........
........
}
public void ActionBar(String color) {
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mTitleTextView = (TextView) mCustomView.findViewById(R.id.textviewHeading);
mTitleTextView.setText("Chapter");
}
FragmentPageA.java:
#Override
public void onResume() {
super.onResume();
....
....
}
#Override
public void onPause() {
super.onPause();
....
....
}
#Override
public void setMenuVisibility(final boolean visible) {
//get action bar title for every fragment page
PageActivity.mTitleTextView.setText(DatabaseQueryHelper.getInstance().getPagename(getArguments().getInt(Constants.PAGEID)));
......
......
}
Anyone can give me any suggestion regarding to this.Thank you.
I just moved this ActionBar(Constants.Titlebarcolor); line inside onResume() method to onCrate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar(Constants.Titlebarcolor);
}
So that When I entering into the activity,the action bar activity title will not resume.
In my Android application I'm using the ActionBar. I want to show users some status using logo placed in the left side of the ActionBar. I can do this:
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.drawable.new_icon);
and it works.
The problem is that this state in to persistent. If I change activity ActionBar reset itself, so default logo is shown. Is there any way to save state of the ActionBar during application runtime?
Use the standar lifecycle callbacks from Activities
In the OnCreate method of your activity try to read the state
public void onCreate(Bundle icicle) {
if (data!=null)
{
ActionBar actionBar = getSupportActionBar();
int state = data.getInt("status");
if (state==1)
actionBar.setIcon(R.drawable.icon_a);
else
actionBar.setIcon(R.drawable.icon_b);
}
}
And save the state to recovery it later
protected void onSaveInstanceState(Bundle data) {
super.onSaveInstanceState(data);
data.putInt(icon_state);
}
I'm using the AppCompat/ActionBarCompat library and I need to create a custom ActionBar. I need to open the activity without an ActionBar and enable it only when I add the custom view. How can I do this?
PS: I need to define the activity to not use an ActionBar through the AndroidManifest.xml and my application minimum API level is 10.
.hide() the action bar and then .show() the action bar when your ready for it
import android.support.v7.app.ActionBar;
...
private ActionBar mActionbar;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionbar = getSupportActionBar();
mActionbar.hide()
}
...
somewhere when something cool happens
mActionbar.show();
In my Activity I want to show arrow to left of ActionBar icon, so in Activity I write:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
It works fine, but I decide move ActionBar initialization to another class and use it for all activities in my application, new class for this:
public class Utils {
public static void initActionBar(Activity activity, boolean homeIconNeeded) {
ActionBar actionBar = activity.getActionBar();
actionBar.setIcon(R.drawable.logo);
actionBar.setHomeButtonEnabled(homeIconNeeded);
actionBar.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.action_bar_background));
}
public static void initActionBar(ActionBar actionBar, boolean homeIconNeeded) {
actionBar.setIcon(R.drawable.logo);
actionBar.setHomeButtonEnabled(true);
actionBar.setBackgroundDrawable(SmartVmsApplication.getContext().getResources().getDrawable(R.drawable.action_bar_background));
}
}
Then in my activity I insert in onCreate() callback initActionBar(this, true), however arrow doesn’t appear, no matters I passed Activity or ActionBar as parameter and it is an issue.
You forgot to call the setDisplayHomeAsUpEnabled() method.