Figen Güngör
Figen Güngör

Reputation: 12569

ActionBar Customization

I am trying to create a custom layout which centers the title and changes the background color of actionbar but that' what I am getting:

enter image description here

Could you please tell me what I am doing wrong?

MainActivity.java

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getSupportActionBar();
    //actionBar.setDisplayHomeAsUpEnabled(true);
     actionBar.setIcon(
                new ColorDrawable(getResources().getColor(android.R.color.transparent)));
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    //actionBar.setIcon(R.drawable.ic_launcher);

    LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.actionbar_layout, null);

    actionBar.setCustomView(v);
}
}

actionbar_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:background="#00ffff"
android:orientation="horizontal">

<TextView
    android:text="title"
    android:textColor="#000000"
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_height="wrap_content"/> 

</RelativeLayout>

Upvotes: 0

Views: 142

Answers (1)

Cruceo
Cruceo

Reputation: 6834

I've run into this one wayyy too many times (only on 3.0+ devices, to my knowledge), and, as a result, you should try adding these calls with the ones you're already using (which I've included):

    actionBar.setDisplayShowHomeEnabled(false);     
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setHomeButtonEnabled(false); 
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);    

That should remove the left-spacing that's automatically applied.

Upvotes: 1

Related Questions