Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display custom view in ActionBar?

I want to display custom search in actionbar (I'm using ActionBarSherlock for that).

I got that:

enter image description here

But I want make custom layout (edittext field) to occupy the entire available width.

I've implemented custom layout as suggested here.

There is my custom layout search.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     style="?attr/actionButtonStyle"     android:layout_width="fill_parent"     android:layout_height="match_parent"     android:layout_gravity="fill_horizontal"     android:focusable="true" >      <FrameLayout         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_gravity="center_vertical|fill_horizontal" >          <EditText             android:id="@+id/search_query"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:layout_gravity="left|center"             android:background="@drawable/bg_search_edit_text"             android:imeOptions="actionSearch"             android:inputType="text" />          <ImageView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="right|center_vertical"             android:src="@drawable/ic_search_arrow" />      </FrameLayout>  </LinearLayout> 

And in MyActivity:

ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayShowCustomEnabled(true); actionBar.setDisplayShowTitleEnabled(false); actionBar.setIcon(R.drawable.ic_action_search);  LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflator.inflate(R.layout.search, null);  actionBar.setCustomView(v); 

How can I make custom layout to occupy all the available width of actionBar?

Help, please.

like image 288
Sabre Avatar asked Oct 14 '12 15:10

Sabre


People also ask

How do I customize my action bar?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is the difference between ActionBar and toolbar?

An Action bar is traditionally a part of an Activity opaque window decor controlled by the framework but a Toolbar may be placed at any level of nesting within a view hierarchy. The toolbar provides more feature than ActionBar . A Toolbar may contain a combination of elements from start to end.

How do I get the toolbar from action bar?

if you are using custom toolbar or ActionBar and you want to get reference of your toolbar/action bar from Fragments then you need to first get instance of your Main Activity from Fragment's onCreateView Method like below. ImageView vRightBtn = activity. toolbar.


1 Answers

There is a trick for this. All you have to do is to use RelativeLayout instead of LinearLayout as the main container. It's important to have android:layout_gravity="fill_horizontal" set for it. That should do it.

like image 177
Tomik Avatar answered Sep 21 '22 05:09

Tomik