Android ActionBar custom behaviour - java

I created fully custom action bar, removed all native control from there and added button in left side.
This is done in order to get full control over action bar layout.
Now when user clicks on this button i want to grant the same functionality as Native Up button.
In other words i from my button click event i need the same functionality as default code here.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return(super.onOptionsItemSelected(item));
}
How this can be achieved?
Thanks.

Switch android.R.id.home with the id of the custom button, R.id.menu_button_name.
ActionBar is now being replaced with Toolbar which gives you more control and customization options than ActionBar. Here are some examples of what you can do with the Toolbar these days.

Related

Disabling Contextual Action Bar Crosswalk-Cordova

Contextual Action Bar (CAB) won't disable using Crosswalk as a plugin in Cordova.
Tried everything, starting with the CSS, all the way through HTML & JavaScript, and even MainActivity.java & XWalkCordovaView.java.
I must disable the Contextual Action Bar. It cannot show up. It's messing my entire app. It needs to be gone, completely. Or at least, I settle with not seeing it in my app.
Here are some pictures:
As you see, we need to select text and show that toolbar (the black one, and disable the blue contextbar, prevent it from showing at all). It's not allowing us to use our toolbar. I mean, with Contextual Action Bar, NO WYSIWYG will work nicely.
I even have problems with the arrow select, which I'd like to disable as well, or at least change the color, or not mess up the sidenav.
So, what I tried:
MainActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
// disable the context menu and all long clicks
super.appView.getView().setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v)
{ return true; }
});
super.appView.getView().setLongClickable(false);
}
}
From here:
How to disable long-click which opens the Android top menu bar with copy/paste/etc. buttons in Cordova Crosswalk apps?
Pull Request: https://github.com/crosswalk-project/crosswalk/pull/3193
Now, please, I need to disable the Contextual Action Bar when I select some text. Thank you so much in advance. I've searched the whole web and nothing seems to work.
I've filled an issue on Crosswalk Project's Issues:
https://crosswalk-project.org/jira/browse/XWALK-7206

Action Bar Title Not Updating onNavigationItemSelected

I am trying to learn using Android Studio's built in templates. I have the Navigation Drawer variant selected currently, and I am trying to use the onNavigationItemSelected method determine the supportActionBar title using the following code:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
getSupportActionBar().setTitle(item.getTitle()); ...
The issue is that once I start loading fragments, the title isn't updating whenever I select a new navigation item. I have checked to see if the method is firing every time an item in the drawer is selected, and it does, so I can't tell why this isn't updating.
Any help would be greatly appreciated!

Why my back button does nothing?

I have a AppCompatActivity activity named MainActivity with the following code placed on onCreate method to show/hide back and menu button
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
toggle.setDrawerIndicatorEnabled(
getSupportFragmentManager().getBackStackEntryCount() == 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(
getSupportFragmentManager().getBackStackEntryCount() > 0);
}
});
This is my only activity, I use fragments for the different views. The back button shows perfectly when appropiated but does nothing when I click on it.
Do I have to put some code on fragments? I have checked many other similar questions but I'm not able to detect what's missed
EDIT
Many solutions ask to override onOptionsItemSelected on Fragment or Activity but this method is not called when I click on the back button on toolbar.
EDIT 2
If I comment line
toggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0);
then back button click opens navigation menu.
You'll have to manually handle the home button as shown here :
catch toolbar home button click event
then load the previous fragment from backstack:
Get Fragment from backstack

How to give different toolbar settings to different tabs?

I recently tried something different for my on development app but don't know how it works. I'm trying to add different actions (eg. Search)to each Tabs in more than one toolbar. I don't know how to explain my view well but the images below can make you understand what I'm trying to.
In the Contacts tab there is a add user icon in toolbar and in the calls tab you can see add calls icon in toolbar and in the Chats tab you can see chats icon in toolbar. My question is how to give different ifRoom settings to different tabs like the above app. Hope it is well explained to get the right answer :)
Thanks in Advance :)
Follow this code.
Add toolbar in your Activity, like you did.
Create menu for each fragment res/menu/menu_one.xml and ...
Add onCreateOptionsMenu and onOptionsItemSelected in each fragment, like this:
.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);// add menu which you created in step 2
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
// do stuff, like showing settings fragment
return true;
}
return super.onOptionsItemSelected(item); // important line
}
And in onCreate add this like: setHasOptionsMenu(true);
And it is it! :)

Displaying menu items in the Android ICS action bar

I've got an app that I've written targeting Android 2.1 which makes use of the menu button to expose some options. I'd like to have this menu available through the action bar overflow button in ICS, but I'm having trouble with getting it to show up.
If I change my target API to 15, the legacy menu button in the bottom bar disappears in ICS, but the icon in the top right of the action bar doesn't replace it, so there is no way to access the menu. I've tried adding the showAsAction attribute to the menu items, which did nothing. I'm definitely targeting 4.0.3 in my Eclipse build options.
All I want is for that menu to be accessible somewhere whilst using the ICS Holo theme, but still backwards compatible to older devices. How do I go about that?
Usually you add those Action Items simply by implementing the onCreateOptionsMenu(Menu menu) and adding the android:showAsAction="ifRoom" for the desired items in the menu's XML file.
e.g.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_save"
android:icon="#drawable/ic_menu_save"
android:title="#string/menu_save"
android:showAsAction="ifRoom|withText" />
</menu>
Did you consider this fact?
Edit:
Here is a simple implemantation which worked for me a while ago:
#Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
createMenu(menu);
return true;
}
private void createMenu(Menu menu){
MenuItem mnu1 = menu.add(0, 0, 0, "Logout");
{
mnu1.setAlphabeticShortcut('a');
mnu1.setIcon(R.drawable.icon);
mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
I was having this issue 5 minutes ago. Dont know if this can be still helpful, but this is how I got it solved:
Add super.setBooleanProperty("showTitle", true); to your onCreate method
#Override
public void onCreate(Bundle savedInstanceState) {
super.setBooleanProperty("showTitle", true);
super.onCreate(savedInstanceState);
//more code...
This will force the action bar to show and the menu button replacement will be there.
I am targeting android 4 in eclipse. Older devices show the bar as title only and of course are listening to default menu button action.

Categories

Resources