Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add imageview on right hand side of action bar?

I want to add image view on action bar right hand side.I tried to do that but i couldn't. Can anyone help me?

This is my image view xml

 <ImageView         android:id="@+id/imageView4"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentRight="true"         android:layout_alignParentTop="true"         android:src="@drawable/adnace_search_i" /> 

This is the image of action bar that i want to create.

enter image description here

like image 472
anuruddhika Avatar asked Jan 17 '14 05:01

anuruddhika


People also ask

How do you add action items to the action bar in Android?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

Can ImageView be used as button?

You can turn any View , such as an ImageView , into a button by adding the android:onClick attribute in the XML layout. The image for the ImageView must already be stored in the drawable folder of your project.

Can ImageView be clickable?

For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.


1 Answers

try this...

ActionBar actionBar = getActionBar(); actionBar.setDisplayOptions(actionBar.getDisplayOptions()         | ActionBar.DISPLAY_SHOW_CUSTOM); ImageView imageView = new ImageView(actionBar.getThemedContext()); imageView.setScaleType(ImageView.ScaleType.CENTER); imageView.setImageResource(R.drawable.adnace_search_i); ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(         ActionBar.LayoutParams.WRAP_CONTENT,         ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT                 | Gravity.CENTER_VERTICAL); layoutParams.rightMargin = 40; imageView.setLayoutParams(layoutParams); actionBar.setCustomView(imageView); 
like image 142
Gopal Gopi Avatar answered Sep 20 '22 21:09

Gopal Gopi