0

I am trying to have the content of the ListView centered when displayed in a dialog. This is my code to show the ListView

String[] myItems = {"item1", "item2", "item3"}; 

ArrayAdapter<String> adapter = new ArrayAdapter<String>( this,  R.layout.setting_layout, myItems);

ListView list = (ListView) deleteDialog.findViewById(R.id.listView1);
list.setAdapter(adapter);
list.setFadingEdgeLength(5);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> paret, View viewClicked, int position, long id) {
        // TODO Auto-generated method stub
            }
 }

And this is the xml part:

 TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:layout_gravity="center"
    android:textSize="25sp"

and this is the layout of delete dialog:

  <?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"
    android:layout_gravity="center_vertical|center_horizontal" >

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_gravity="center"
    android:dividerHeight="5dp"
    android:gravity="center">

</ListView>

Its running but the content is always displayed to left. I tried this: How to set the text at center in ListView android application? but it didn't work.

Hope anyone can guide me through.

Community
  • 1
  • 1
coder
  • 5,200
  • 3
  • 20
  • 45
  • What happens if you set `android:gravity="center"` to ListView? – gunar Nov 25 '13 at 12:43
  • In fact I haven't tried it. The thing is when I write android: it starts giving the options that I could use, and gravity was not among them so I just assumed that I couldn't use. I just tried it, it didn't give an error, but still its giving items to the left – coder Nov 25 '13 at 12:48
  • 1
    change ur linearlayout's width to fill_parent instead of wrap_content – Kaushik Nov 25 '13 at 13:10
  • welcome i have written it as an answer – Kaushik Nov 25 '13 at 13:16

3 Answers3

2

this will work for u

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_gravity="center"
    android:dividerHeight="5dp"
    android:gravity="center">

</ListView>
Kaushik
  • 6,150
  • 5
  • 39
  • 54
1

Change your LinearLayout to:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:layout_gravity="center_vertical|center_horizontal">
ajacian81
  • 7,419
  • 9
  • 51
  • 64
1

You can try with

android:layout_gravity="center_vertical|center_horizontal"

I hope it helps

Crash
  • 338
  • 1
  • 2
  • 12