Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SherlockActionBar: How to adjust CustomView against actionBar

Question:

How can I have the title back?


Here's the screenshot (Missing title):

enter image description here

Actionbar setting:

 ActionBar actionBar = getSupportActionBar();
 actionBar.setTitle("My Title");
 actionBar.setIcon(drawable.dropdown_user);

 View mLogoView = LayoutInflater.from(this).inflate(R.layout.mLogoView, null);
 actionBar.setCustomView(mLogoView);

 actionBar.setDisplayShowCustomEnabled(true);
 actionBar.setDisplayShowTitleEnabled(true);
 actionBar.setDisplayHomeAsUpEnabled(true);

mLogoView.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/img_merchant"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true"
        android:src="@drawable/infoflex">
</ImageView>
</RelativeLayout >

like image 809
Roy Lee Avatar asked May 13 '13 06:05

Roy Lee


People also ask

How to set Custom ActionBar in Android?

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.


1 Answers

The first thing which I can think a way to fix that is try to set

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right" >

to your RelativeLayout. Because as default in Android every View is added from left to right (if you doesn't specify something else in your code).

And if that doesn't work, I would add android:layout_width="90dip" or some other value depending on the image and your tests. The second option is more like a workaround, but I think it will work on all devices.

Edit:

As I found there is a little problem using RelativeLayout and Custom Views in ActionBar, so the better option is to use LinearLayout and set LayoutParams via code. Change your xml to look like this :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/asus"    />
</LinearLayout >

and in your MainActivity.class use this :

import com.actionbarsherlock.app.ActionBar.LayoutParams;
....
ActionBar actionBar = getSupportActionBar();

actionBar.setTitle("OMG");

LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
View customNav = LayoutInflater.from(this).inflate(R.layout.img, null); // layout which contains your button.
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);
like image 62
hardartcore Avatar answered Oct 05 '22 14:10

hardartcore