Raj
Raj

Reputation: 506

How to setCustomView of actionbarsherlock in SherlockFragmentActivity class

I am using SherlockFragmentActivity as my main activity class. below is the code...

 public class MainActivity extends SherlockFragmentActivity{
  protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar ab = getSherlock().getActionBar();
    LayoutInflater li = LayoutInflater.from(this);
    View customView = li.inflate(R.layout.my_custom_layout, null);
    customView.setLayoutParams(new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT ,ActionBar.LayoutParams.MATCH_PARENT));
    ab.setCustomView(customView);
    ab.setDisplayShowHomeEnabled(false);}  }

I have also tried with

getSupportActionBar().setCustomView(customView);

and this is my my_custom_layout.xml file

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/light_gradient" >

<Button
    android:id="@+id/btn_actionbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:textSize="15sp"
    android:text="Settings" />

<TextView
    android:id="@+id/tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="20sp"
    android:textColor="@color/White"
    android:text="Home"/></RelativeLayout>

I have defined my activity in manifest file as below

<activity
        android:name="com.example.demo.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.Sherlock.Light" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="com.actionbarsherlock.sample.demos.EXAMPLE" />
        </intent-filter>
    </activity>

Now my problem is that I am not able to set custom view of my actiobar. can any one help me. Thanx in advance.

Upvotes: 2

Views: 2858

Answers (2)

Melbourne Lopes
Melbourne Lopes

Reputation: 4855

use this...

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.your_layout);

Upvotes: 2

Ketan Ahir
Ketan Ahir

Reputation: 6738

try this

 View mView=getLayoutInflater().inflate(R.layout.YOUR_LAYOUT, null);
 ActionBar ab=getSupportActionBar();
 ab.setDisplayShowCustomEnabled(true);
 ab.setCustomView(mView);

Upvotes: 6

Related Questions