1

how can i align the button named Reload to the right of my actionbar ? i know this is possible i have tryed to put android:gravity="right" but that didnt work

activity_main_ab.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"
android:gravity="fill_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >

<Button
android:id="@+id/action_bar_button_about"
android:layout_width="fill_parent"
android:layout_height="match_parent"           
android:layout_weight="1"
android:background="@drawable/ios_btn"
android:textColor="#ffffff"
android:text="about" />

<Button
android:id="@+id/action_bar_button_reload"
android:layout_width="fill_parent"
android:layout_height="match_parent" 
android:background="@drawable/ios_btn"          
android:layout_weight="1"
android:textColor="#ffffff"
android:text="reload" />
</LinearLayout>    
</RelativeLayout>

here is my MainActivity.java

package jb.cydia;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   
    final ActionBar ab = getActionBar();
    ab.setDisplayShowHomeEnabled(false);
    ab.setDisplayShowTitleEnabled(false);     
    final LayoutInflater inflater =    (LayoutInflater)getSystemService("layout_inflater");
    View view = inflater.inflate(R.layout.activity_main_ab,null); 
    ab.setCustomView(view);
    ab.setDisplayShowCustomEnabled(true);

}
}

enter image description here

yes it looks like IOS i know androids about customization right??

Jacob Anthony Tonna
  • 515
  • 3
  • 13
  • 27

3 Answers3

2

You do not need the LinearLayout. Stick to the RelativeLayout and use android:layout_alignParentRight="true"

Look at this answer which shows a nice example: Android Layout Right Align

<?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"
android:gravity="fill_horizontal"
android:orientation="horizontal" >


<Button
android:id="@+id/action_bar_button_about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"           
android:layout_weight="1"
android:background="@drawable/ios_btn"
android:textColor="#ffffff"
android:text="about" />

<Button
android:id="@+id/action_bar_button_reload"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:background="@drawable/ios_btn"          
android:layout_weight="1"
android:textColor="#ffffff"
android:text="reload" 
android:layout_alignParentRight="true"/>

</RelativeLayout>

Here is what I did:

  • removed the Linear layout
  • added android:layout_alignParentRight="true"
  • changed android:layout_width and layout_height to wrap_content
Community
  • 1
  • 1
Enno
  • 131
  • 5
1

The parent of an ActionBar custom view appears to be a FrameLayout. Thus, the quickest solution is:

android:layout_width="wrap_content"
android:layout_gravity="end"

at the top level view or container.

Since this makes an and assumption about the implementation of the ActionBar's view, Enno's is probably more robust. That said, I'd consider this a pretty safe assumption.

Anm
  • 3,291
  • 2
  • 29
  • 40
1

You should do something like this:

actionBar.setCustomView(view, new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
     ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL));
actionBar.setDisplayShowCustomEnabled(visible);
Oleksandr Albul
  • 1,611
  • 1
  • 23
  • 31