14

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 >

Roy Lee
  • 10,572
  • 13
  • 60
  • 84

1 Answers1

30

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);
Heath Borders
  • 30,998
  • 16
  • 147
  • 256
hardartcore
  • 16,886
  • 12
  • 75
  • 101
  • Thanks for showing up! :) The first option is not working. The width seems to takeover the title space. Which does not happen to navigationbar: http://stackoverflow.com/questions/16274077/how-to-increase-icon-size-of-actionbutton-in-sherlockactionbar – Roy Lee May 13 '13 at 08:24
  • can you see what is happening when setting width to your relativelayout – hardartcore May 13 '13 at 08:26
  • Nothing happens. But if I would to set it as linearlayout, the title shown, but I just couldn't align the layout to right. – Roy Lee May 13 '13 at 08:30
  • 1
    You can't align the imageview or layout? – hardartcore May 13 '13 at 08:31
  • I could send you the test program. – Roy Lee May 13 '13 at 08:39
  • 1
    Ok, do it and I will check. – hardartcore May 13 '13 at 08:40
  • Now it works like charm! Gosh You are wizard! Thanks for the help, again. :) I learn something new today! – Roy Lee May 13 '13 at 09:27
  • I have tested to write in xml, but it doesn't work :) Any reason why this only work in programmically way. – Roy Lee May 13 '13 at 09:47
  • 2
    Because in your xml you can't set layout params to your root layout - `LinearLayout`, but in code you can do that while adding custom view to ActionBar. That's the whole idea. – hardartcore May 13 '13 at 10:00