I've been working with ActionBar for a while, but was constantly frustrated by its customizaton work. Also I've been playing with ActionBarSherlock and ActionBarCompat but finally get down to the later one.
I saw many apps combining the leftmost up button with the title into one clickable button on a ActionBar. How can that be achieved without supplying a custom layout for ActionBar? Is that a way to customize only the up button to include the title as well?
Thanks in advance!
The up button is an ImageView not button. So you can not apply text using setText();
You can set an image with text to that ImageView.
Sample code:
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setIcon(R.drawable.YourImage);
OR
getActionBar().setDisplayShowHomeEnabled(true);
ImageView imgHome=(ImageView)findViewById(android.R.id.home);
imgHome.setImageResource(R.drawable.YourImage);
Related
Im making an app for android, without actionbar.
What is best practice for showing titles without actionbar?
The best practice is basically never using an action bar. This gives you a complete device screen to play with.
You can use images or different text styles for showing the title. You can also use interesting icons.
To remove action bar -
Go to res > values > styles.xml
Here is an illustration of styles file -
You can use the Toolbar (it's like the action bar, but more flexible). For example, action bar needs to be on top of the screen, but toolbar can have place anywhere in the layout.
You title can be shown as text or a beautiful asset.
If, for some reason, you don't want to use the toolbar, you can think about something like having a scrollView with the title on top of it.
Use a regular LinearLayout with a TextView inside. Style according to your needs, and set the text in the xml, or programmaticaly using TextView's setText() method.
I need an information about Toolbar. I would like to add four images in my Toolbar like home, news, search and message. So i've putting Imageviews in the Toolbar. Now I don't know how to add listener at these images...how could I do that?
I have also a navigation drawer and I want that, if you press the Imageview in the Toolbar, you go in a new Fragment and not in a new Activity.
So how could I do these two things?
Thank you for the answers.
i guess you know how to add your imageViews to your ToolBar if not check this .. After that get a reference to your ToolBar use findViewById
Toolbar tool = finViewById(R.id.mytoolbar);//cast it to ToolBar
ImageView im1 = tool.findViewById(R.id.myimageviewone); // cast it
//same for the others
// now you can set your click listeners
//in your onclick listeners you use fragmentmanager along with fragmenttransaction
//to start a new fragment in your layout or on your layout, you can google
// for that
You should just be using the regular MenuItem functionality built into the Toolbar (and same as ActionBar). Look up any ActionBar tutorial, and look into using ,creating MenuItems (you will do most of this in XML). You will then be able to use the OS framework, to capture onClickListenters.
Here are the docs (you will notice the MenuItem has an icon attribute, which will place the images into the Toolbar):
android:icon="#drawable/ic_new_game"
Docs:
http://developer.android.com/guide/topics/ui/menus.html
I would recomend to use ImageButtons. You can set image sources which will be displayed as a Button and they behave like normal Buttons with OnClickListeners.
I'm trying to place a logo that will act as the actionbar title and then have dynamic fragment based subtitles with text below.
Can you either set text below logo or the title as an image?
Is it possible?
Thanks!
ActionBar does not provides APIs for setting title as an image, you could try find the actual TextView in layout but I would not recommend that due Android fragmentation. It will not work on all devices.
You can however use a custom view in ActionBar, you will have to call
getActionBar().setDisplayShowCustomEnabled(true);
I've been wondering about this question quite long time. How do they do that? Is the title just disabled and the HomeAsUp button is custom and contains the logo, arrow and text?
Do you know any other ways how to accomplish this?
You can achieve this by making a single image that contain both
- arrow.
- text.
- icon.
And the work on this single image action
I'm developing an application in which I have to put drawables on top of the actionbar and have to update then from time to time. Here is a layout what I want it to look like.
I want to put a drawable as shown in the picture atttached(The green color drawable not the notification thing). Is that possible to achieve this kind of screen in android? If, so please guide me how to achieve this.
Any kind of help will be appreciated.
This is easy using a custom view. Let me explain you the steps briefly:
Create your custom view in the layout folder. For example, an ImageButton.
Set this view as the custom view of the action bar in your activity. You can do this by calling
getSupportActionBar().setCustomView(R.layout.your_custom_view);
(or getActionBar() instead of getSupportActionBar())
Call setDisplayOptions() with the ActionBar.DISPLAY_SHOW_HOME and ActionBar.DISPLAY_SHOW_CUSTOM options.
If you have other standard buttons in the action bar, inflate the menu.
You can finally set the OnClickListener for your custom view.
This worked for me, hope it helps you!
Create custom view
Use it as ActionView in ActionBar
In onCreateOptionsMenu get a reference to your view and update it when needed