It's possible to add imageview in a toolbar? - android

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.

Related

Custom Toolbar in Navigation Component

I have used Google's solution for multiple back stacks in Navigation Component. I want to setup a custom AppBar at the activity level(Idea is to have a Toolbar, A FragmentContainerView, and BottomNavigationView)
This works fine except this sets the title of the Toolbar automatically (doesn't call any particular method in the file I have linked as far as I know) .
I want to build a custom toolbar which has a TextView with multiple colors(As in half of the title is black , other half is yellow).
Any solution how to build a custom toolbar in this solution would be greatly appreciated
You can read this document about handle navigation component back stack with appBar configuration
https://developer.android.com/guide/navigation/navigation-ui
and also you can watch this course for make custom toolbar
https://m.youtube.com/watch?v=e5_C9e_gKOM

Is possible have to different menu in Action Bar? (Kotlin)

I have an app, which have an action menu item on the right (2).
I need an ulterior menu in the left(1) or a button in the action bar, is that possible?
I attach a image.
enter image description here
You may create a custom toolbar. The standard height of a toolbar is 89dp.
To create a custom toolbar you should make your activity view container RelativeLayout. Then create a custom toolbar view (it may also be RelativeLayout) which height is 89dp. Then add your toolbar to the main container and set alignParentTop to true. So you have a custom flexible toolbar and you can add any view to it.
This way is very comfortable to use any custom views on your toolbar.
I also faced the same situation of customizing action bar. After a lot of searching I got the solution which is Toolbar.
Checkout: https://developer.android.com/training/appbar/setting-up
I think from now on, you should start using Toolbar as the default action bar for your apps. It provides high level of customization and material design features.

how to put a profile image and a searchbar in a toolbar?

I am new to Android development I am building an app but from last three days, I am stuck while making toolbar I am very confused. I want to make toolbar like this
The widget elements in your design are the following from left to right:
Navigation Drawer Indicator
ImageView
SearchView
Toggle
Overflow Menu
These can all be implemented using default widgets. It is not common to put a profile image to the right of the title, but you could do this by putting a custom layout inside the toolbar view.
It is also not common to place a toggle in the action bar, but it is possible using an ActionLayout.

How to achieve the same appbar-design as Periscope's Android app?

Here is a screenshot of the toolbar. It has three navigation items to the left which looks just like a tab-layout. When you select the third tab, a menu item pops up from right side of the toolbar.
How can i achieve this look? Is this a tablayout inside a toolbar? Or a tablayout and a toolbar next to each other? I've tried some stuff but failed.
I would use TabBarView. Then, in the Fragment you want a MenuItem to show (like the last tab in Periscope), add setHasOptionsMenu(true) and have the MenuItem's visibility be app:showAsAction="always". Wally also uses this UI pattern.

Setting Navigation Icon on Android ActionBar

So I'm working on adding ActionBarSherlock and the Navigation Drawer to a project that previously implemented a custom (very poorly written) "action bar". Instead of using fragments and a backstack of activities for navigation, some activities show and hide different layouts. (That is, suppose I am in a list mode and then select a button to go into an edit screen. The app currently hides the list layout and shows another layout.).
So I've added actionbar sherlock and a navigation drawer to all the activities. I want to be able to programmatically switch the navigation icon from the 3 lines to the arrow when certain buttons are pressed.
I can't figure out how to do this though. Any ideas?
Thanks!
The solution to this problem is to use the method:
setDrawerIndicatorEnabled(boolean enable)
inside the ActionBarDrawerToggle class.
After:
drawer.setDrawerListener(toggle);
Use this code:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.select);
It depends how wedded you are to built-in actionbar artifacts. You can always redraw the current actionbar by inflating a layout of your choosing, then calling
getSherlockActivity().getSupportActionBar().setDisplayShowTitleEnabled(false);
getSherlockActivity().getSupportActionBar().setDisplayShowHomeEnabled(false);
getSherlockActivity().getSupportActionBar().setDisplayShowCustomEnabled(true);
// Inflate and do whatever you need to your view...
getSherlockActivity().getSupportActionBar().setCustomView(abView);
getSherlockActivity().getSupportActionBar().show();
When you want to go back to your standard (assuming you're using a DrawerLayout to do your navigation drawer), you can just set make a call to setDisplayShowCustomEnabled(false) (re-enable showHome and showTitle as you please).
As far as I know, customization of the back button can only be done via themes. Besides, swapping the drawer icon for the back icon (within the same Activity) doesn't make sense, since users would still be able to access the navigation drawer by sliding the left most edge to the right. It just wouldn't make sense.
If you absolutely need the back icon, then it would make the most sense to make that screen a new Activity since you would indeed be adding another "level" to the stack, which is what the back icon represents.

Categories

Resources