How can I change Action Bar actions dynamically?
Since the actions are populated by the activity's options menu you can use Activity#invalidateOptionsMenu()
. This will dump the current menu and call your activity's onCreateOptionsMenu
/onPrepareOptionsMenu
methods again to rebuild it.
If you're using action bar tabs to change your fragment configuration there's a better way. Have each fragment manage its own portion of the menu. These fragments should call setHasOptionsMenu(true)
. When fragments that have options menu items are added or removed the system will automatically invalidate the options menu and call to each fragment's onCreateOptionsMenu
/onPrepareOptionsMenu
methods in addition to the activity's. This way each fragment can manage its own items and you don't need to worry about performing menu switching by hand.
Changing action bar title dynamically not working
actionBar
is deprecated and replaced withsupportActionBar
.- Using
activity
within a fragment returnsFragmentActivity
which doesn't directly referencesupportActionBar
object; instead you need to cast that toAppCompatActivity
) or to your custom name of the activity that hosts this fragment.
To fix this:
(requireActivity() as AppCompatActivity).supportActionBar?.setDisplayShowTitleEnabled(true)
(requireActivity() as AppCompatActivity).supportActionBar?.title = ...
Change text dynamically for Android Actionbar Button
First of all you don't set "TextView of a Button", because Button extends TextView, you set text of the Button itself. You can try to do this the following way in your activity's onCreate()
:
//I suppose you create custom ActionBar like this
final View addView = getActivity().getLayoutInflater().inflate(R.layout.my_action_bar, null);
bar.setCustomView(addView);
button=(Button)addView.findViewById(R.id.button);
button.setText("Whatever");
If you use menu items, then you should try this approach:
getSupportMenuInflater().inflate(R.menu.activitymenu, menu);
MenuItem menuItem=menu.findItem(R.id.menuSearch);
Also try looking here,here and here. It should be helpful.
ActionBar Title dynamically change with fragment
Use this method in activity to change the Fragment
and set the title programmatically:
private void displayFragment(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
String title = "";
switch (position) {
case 0:
fragment = new Home();
title = "Home";
break;
case 1:
fragment = new Login();
title = "Login";
break;
case 2:
fragment = new RestorePass();
title = "Restore Password";
break;
default:
break;
}
// update selected fragment and title
if (fragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container, fragment).commit();
getSupportActionBar().setTitle(title);
// change icon to arrow drawable
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow);
}
}
For example, you want Fragment Home
to be displayed:
displayFragment(0);
How to: dynamic change action Buttons icon or put in a text
You have two issues on your hands. First is displaying text within a circle, or bubble. Second, you want to be able to display that text in your Toolbar
(or ActionBar
), with the ability to change the text.
To solve your first issue, I would suggest creating a ShapeDrawable
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF000000" />
<corners android:bottomRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:topRightRadius="8dp"
android:topLeftRadius="8dp"/>
</shape>
You may need to mess with the radius of 8dp
, to make it look more circular, but this will allow the view to expand no matter how long the text gets. Lastly, create a layout that uses this ShapeDrawable
as a background:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="12sp" android:textColor="?android:attr/textColorPrimary"
android:background="@drawable/label_shape_drawable"/>
To solve your second issue, you can use this TextView
as an ActionLayout
for your MenuItem
:
<item android:id="@+id/mex_box" android:orderInCategory="100"
android:title="@string/mex_counter" android:visible="true"
app:showAsAction="always" app:actionLayout="@layout/view_label"/>
Then you can obtain your TextView
in onCreateOptionsMenu
:
private TextView mLabelTextView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_action_menu, menu);
MenuItem menuItem = menu.findItem(R.id.mex_box);
mLabelTextView = (TextView) menuItem.getActionView();
return super.onCreateOptionsMenu(menu);
}
Then, with your TextView
on hand, you can change the text whenever you like:
mLabelTextView.setText(newText);
Dynamic change of items title in Action Bar
You have to call InvalidateOptionsMenu
when you want to make changes to the ActionBar menu.
You then override onCreateOptionsMenu
to rebuild your menu with your changes in place. You cannot edit a menu outside of the methods provided for building menus.
Every time you call InvalidateOptionsMenu
your menu will be rebuilt by the onCreateOptionsMenu
method so you need to make all of your changes there.
http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu()
http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)
This is true whether you are using ActionBarSherlock or not.
e.g:
Set a variable to hold the title
String MyMenuTitle = "Default Menu";
Your OnClick
public void onClick(View view) {
MyMenuTitle = "My New Menu Title";
}
Your Menu Building Code.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.option_menu, menu);
MenuItem item = menu.findItem(R.id.state);
item.setTitle(MyMenuTitle);
return super.onCreateOptionsMenu(menu);
}
Dynamically change an ImageView source on a custom action bar
Why you used below line twice?
View mActionBarView = getLayoutInflater().inflate(R.layout.my_action_bar, null);
you set up view and again initialize that again. you code should like below
mageView universityLogo;
Global global = new Global();
View v2 = View.inflate(this, R.layout.my_action_bar, null);
universityLogo = (ImageView)v2.findViewById(R.id.buttonLeft);
switch(global.getLocation()){
case "33613":
universityLogo.setImageResource(R.drawable.diploma);
}
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Dynamically change action bar divider color (android:bottom for programmatically generated ShapeDrawable)?
As per earlier comment:
Did you give setLayerInset(int index, int l, int t, int r, int b)
a try? The docs say:
Specify modifiers to the bounds for the drawable[index]. left += l top
+= t; right -= r; bottom -= b;
Related Topics
Multiple Apps Use Same Content Provider
Android Studio- Program Type Already Present: Com.Google.Android.Gms.Internal.Measurement.Zzwp
How to Get the Current Dns Servers for Android
Send Request Over Wifi (Without Connection) Even If Mobile Data Is on (With Connection) on Android M
Access_Coarse_Location Permission Gives a Cell Tower Precision on Android
How to Change Actionbar Tab Indicator Programmatically
Edittext Settext Not Working with Fragment
How to Integrate Matlab Code Library with Android
How to Copy Selected Files from Android with Adb Pull
Display Toolbar for Google Maps Marker Automatically
How Might I Add a Watermark Effect to an Image in Android
Triggering Event When Button Is Pressed Down in Android
Moving from One Activity to Another Activity in Android