Action Bar with navigation drawer - android

This is very similar to many question already posted on stackoverflow, but I still haven't found the right solution. The problem I'm facing is how to implement a custom actionbar.xml file and add a navigation drawer function to the top left icon.
I'm trying to create an action bar that looks like this with two ImageButtons on side and an ImageView as a logo in the center. When user presses on the left ImageButton from actionbar.xml, I would like navigation drawer coming from the left, like in this tutorial. The last thing that troubles me, is how to put a title of pressed fragment in the center of the action bar instead of a logo.
I've already written all xml and java files, but I just can't put the pieces together. Please help.

I believe you have solution to create navigation drawer.
Now for your question HOW TO CREATE A CUSTOM ACTION BAR
Try like this ..
actionBar = getSupportActionBar(); // Get Action Bar reference.
actionBar.setCustomView(R.layout.action_bar_confirm_cabs); Set your action bar view like this
fare_ll = (TextView) actionBar.getCustomView().findViewById(R.id.fare_ll); Get id of your custom view like this ...
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); // Set property for Custom view like this.
fare_ll.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
That's it.... You are good to go.

Related

Android: Can i add Tabs within actionBar?

I have done practice adding menu on action bar. But for this case I have to add tab in actionbar (not below the actionbar) like below figure:
Please help me to find out the solution. Thanks in advance and sorry for bad English.
For your purpose you can try a simple stuff by creating custom ActionBar like this
You can create custom Action bar like this ..
// gets actionBar reference ..
actionBar = getSupportActionBar();
// This sets custom layout to action bar
actionBar.setCustomView(R.layout.action_provider);
// this is how... .... you get id of layout defined
iv_d = (ImageView) actionBar.getCustomView().findViewById(R.id.action_menu);
Now if you need to show stuff like ActionBar tabs then simply provide background on every layout where each layout contains a text and Image... This will provide a look n feel of ActionBar Tab over ActionBar.
Hope it clears your doubt. Let me know if still you have any concern in same.
Thanks.

Android custom action bar gobal

I have a problem with the AppCombat.Light action bar. I want to customize my actionbar but I want to reference the layout globaly. Currently I would have to change the actionbar in each Activity like this:
private void setupActionBar() {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar_layout);
ImageButton tmp = (ImageButton)findViewById(R.id.action_bar_button);
tmp.setImageResource(R.drawable.ico_nav);
tmp.setBackgroundColor(Color.TRANSPARENT);
}
XML:
But I want to specify the layout parameters in my style sheet. Is this possible?
Currently my action bar looks like this:
but I want an action bar which looks like this:
Now my question is:
Can I customize my aciton bar in a xml file? If so how can I apply it on all activities?
PS: I dont want to edit the action bar programatically!!!
You should create an Activity class and make all of your activities inherit from it. Then you just write your code once.

Android: ActionBar glitch on show and hide

I would like to show/hide the action bar upon a click.
It does show and hide but it is not smooth...the bottom part hides but a different background for a little while before disappearing.
I even tried it in a simple hello world app and the result is the same.
Here's the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.shit);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getActionBar().hide();
}
});
}
Use overlay mode instead of showing and hiding the actionbar.
From the actionbar documentation:
Beware that hiding and removing the action bar causes your activity to
re-layout in order to account for the space consumed by the action
bar. If your activity regularly hides and shows the action bar (such
as in the Android Gallery app), you might want to use overlay mode.
Overlay mode draws the action bar on top of your activity layout
rather than in its own area of the screen. This way, your layout
remains fixed when the action bar hides and re-appears. To enable
overlay mode, create a theme for your activity and set
android:windowActionBarOverlay to true. For more information, see the
section about Styling the Action Bar.

Android custom action bar with back and next button

I've got a problem. I wanted to create something that looks like this:
action bar
Left icon would be a back button (I mean it would return to previous tab, the left one), right icon would be next button (it would go to next tab, the right one). I wanted also to make it repeatedly so when I'm on tab A (there are for example 3 tabs: A, B, C) and when I use right button, go to B, then C, and then again A and so on. The text in the middle would be a name of a tab. I would also like to make it compatible with earlier versions of android (like 2.something).
To achieve this, you need to implement custom action bar.
And to target devices with lower android versions use support library. Action bar is supported only in android versions 2.1 and above, with the help of the library.
Refer below link for setting up action bar.
https://developer.android.com/training/basics/actionbar/setting-up.html
The below piece of code will help you to get more idea on this.
// Inflate your custom layout
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.action_bar,
null);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
To create above action bar, use relative layout with two image views for direction buttons, and one textview for tabname. For a better design, avoid using tabs in this case. Use Fragments. On each click of the images, just inflate the fragments, each time.
if (openTabIndex=totalNumofTabs-1) {
//go to Tab index 0
} else {
//go to Tab index openTabIndex+1
}
I never worked with tabs before, but this is one if statement that if absent, you cannot do what you want.

Sherlock action bar's menu item background

i am having issue with rendering custom view in Sherlock action bar. what happen is i've successfully set the custom view in action bar designed to set the Title textview in center, but when i set the other menu items like refresh then it's background appears with black color. same things happens with home button. please go through the following snapshot.
as you can see in this image when i set home button visible or set the refresh menu item it should not displayed like this with black background.
in my Activity screen
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_activity);
....
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_navigation, null);
TextView textView = (TextView)customNav.findViewById(R.id.screen_title);
textView.setText("Category");
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(customNav);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,Constants.REFRESH_ITEM_ID,0,Constants.LABEL_REFRESH)
.setIcon(R.drawable.ic_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
I've tried a lot but fail to fix it.Please help me.
Thanks in Advance.
It seems you use Theme.Sherlock.Dark in manifest file for whole application or this activity.
It is normal behavior of ActionBar -- it is defined by style, but your custom view-element has is own backround, text style, etc...
From setCustomView() javadoc:
Custom navigation views appear between the application icon and any action buttons and may use any space available there.
This method just add new view to bar, but doesn't set 'view for whole bar'.
Take a look at http://jgilfelt.github.com/android-actionbarstylegenerator -- this is a style generator for action bar. Pay attention to the toggle button "Style compatibility".
I have found the solution of this not exactly the way i wanted but the other way. actually i've created the images with centred screen title for various screens and setting it as the action bar background rather than creating a custom view and SherlockActionBar is flexible enough to hide the default title displayed on the upper-left corner. all i have done is.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar()
.setBackgroundDrawable(getResources().getDrawable(R.drawable.menu_head));

Categories

Resources