coder
coder

Reputation: 5390

listView Centered in a dialog in Android

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.

Upvotes: 0

Views: 597

Answers (3)

ajacian81
ajacian81

Reputation: 7569

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">

Upvotes: 1

Kaushik
Kaushik

Reputation: 6162

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>

Upvotes: 2

Crash
Crash

Reputation: 338

You can try with

android:layout_gravity="center_vertical|center_horizontal"

I hope it helps

Upvotes: 1

Related Questions