0

I'm not understanding whats going on, I've been on this for hours. I have a ListView of buttons with an adapter. I have a background set for the buttons, but the buttons don't want to align to the middle of the listView, instead it aligns to the left. All the layout-gravity and gravity settings apply to the title text of the button, not the button itself. Is there anyway I can align the background button, or maybe the whole button to the middle of the listView? Heres my code of such things that are relevant

sports_list.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Button xmlns:android="http://schemas.android.com/apk/res/android"
       style="@style/SportsList"
       android:id="@+id/sport_item"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:paddingTop="15dp"
       android:paddingBottom="15dp"
       android:layout_gravity="center"/>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--<item name="android:background">@drawable/default_background</item> -->
</style>

<style name="SportsList" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">@drawable/custom_button</item>
    <item name="android:textSize">20dp</item>
    <item name="android:textColor">#fff</item>
</style>

</resources>

activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:paddingTop="15dp"
         android:paddingLeft="15dp"
         android:paddingRight="15dp"
         tools:context=".MainActivity"
         android:orientation="vertical"
         android:gravity="center"
         android:background="@drawable/default_background">

        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:text="@string/choose"
          android:textColor="#fff"
          android:layout_gravity="center"/>

        <ListView
          android:id="@+id/list_view_sports"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:divider="@android:color/transparent"
          android:layout_marginTop="30dp"
          android:dividerHeight="30dp"
          android:layout_gravity="center_horizontal" />
 </LinearLayout>

and the only important section in my mainactivity.java

public static ArrayAdapter adapter;
String[] sports = {"Baseball", "Football", "Soccer"};

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

    adapter = new ArrayAdapter(
            this,
            R.layout.sports_list,
            R.id.sport_item,
            sports);

    ListView listView = (ListView) findViewById(R.id.list_view_sports);
    listView.setAdapter(adapter);
}

i would like to center the buttons enter image description here

Steven R
  • 323
  • 2
  • 6
  • 18

4 Answers4

0

Try to add the attribute layout_gravity: center your buttons.

If this doesn't work, you could change your LinearLayout to a Relative layout, and then use the android:layout_centerInParent="true" attribute to center your buttons.

Here is a link to an answer that should help you.

You will probably need to add some attributes to your buttons/textview in order to make them look the way you want (as they did in linear layout). I suggest using the layout_below attribute to get things lined up the way you want them vertically.

Community
  • 1
  • 1
ryguy
  • 481
  • 2
  • 7
  • this doesn't work either, I looked everywhere and came across that link as well and didn't work. My belief is that the ListView is getting centered, but the contents inside are not. – Steven R Jun 25 '15 at 21:14
  • Did you try to use a RelativeLayout? – ryguy Jun 25 '15 at 21:17
  • Yup, and with `android:layout_centerInParent="true"` and still nothing – Steven R Jun 25 '15 at 21:20
  • But whats really weird is in the preview for the sport_list.xml, the button will get centered, but during runtime on the phone, it doesnt center, it goes to the left – Steven R Jun 25 '15 at 21:21
  • That is strange. Do you set any attributes on any of your layouts programmatically during runtime? – ryguy Jun 26 '15 at 12:00
  • Nope, everything is done through the xml's. But I got what I wanted by creating multiple buttons in the xml's. Not what I wanted to do, but it works that way. – Steven R Jun 26 '15 at 15:26
  • Well I'm glad you got it to work.. sorry I couldn't have been more help. – ryguy Jun 26 '15 at 16:05
  • Thank you for the help though! Dont be sorry :) – Steven R Jun 26 '15 at 19:24
0

Maybe try to put your button inside the another layout, for example:

<RelativeLayout
    android:id="@+id/button_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        style="@style/SportsList"
        android:id="@+id/sport_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>

Let me know, if it's working.

kodeusz
  • 178
  • 3
  • 10
0

You may want to change the layout_width of listview to "match_parent"

km86
  • 475
  • 3
  • 6
0

Try adding ListView inside a RelativeLayout as follows : -

 <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
 <ListView
      android:id="@+id/list_view_sports"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:divider="@android:color/transparent"
      android:layout_marginTop="30dp"
      android:dividerHeight="30dp"
      android:layout_gravity="center_horizontal" />
 </RelativeLayout>
devcodes
  • 1,038
  • 19
  • 38