0

in my android actionbar i have 2 custom buttons ill post the code below but my problem is the buttons get stretched in the actionbar how do i set it to fit the text and not strech?? here is my code for

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_alignParentLeft="true"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    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: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:text="reload" />
</LinearLayout>    
</RelativeLayout>

and 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);

}
}

Oh almost forgot it is android even tho it looks like the screenshot was from ios .. i rooted my phone to look like that :) androids about customization right?

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jacob Anthony Tonna
  • 515
  • 3
  • 13
  • 27

2 Answers2

1
<Button
    android:layout_width="fill_parent"
... />

Should be

<Button
    android:layout_width="wrap_content"
... />
Adam S
  • 16,144
  • 6
  • 54
  • 81
  • If you do the same for the height attribute of the buttons (set `layout_height="wrap_content"`) that should work. – Adam S May 27 '13 at 23:51
  • how do i make the buttons smaller like in the IOS actionbar the buttons are smaller then the actionbar but the buttons i have leave no space from them and the actionbar? – Jacob Anthony Tonna May 28 '13 at 03:04
  • Have a read about setting a margin on your buttons - [this](http://stackoverflow.com/a/4619943/1217087) answer is a good place to start, but it sounds like you really need to have a read of the Android [Layout documentation](http://developer.android.com/guide/topics/ui/declaring-layout.html#SizePaddingMargins). That page covers all of what you're asking here and much more. – Adam S May 28 '13 at 04:44
1

You should always use 9 patch images for buttons. Check this site to create your 9 patches. And also check this video.

Basim Sherif
  • 5,384
  • 7
  • 48
  • 90