ductran
ductran

Reputation: 10203

set text of ListView in center - android

I try to set text of ListView in center of layout. I use LinearLayout and set gravity = center, but it always shows in left

Here is layout:

<?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="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="center">
        <ListView android:id="@+id/lv_function" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:gravity="center" />
    </LinearLayout>
</LinearLayout>

In my code, I use:

String[] functions = { "Login", "Register", "Setting", "About", "Exit" };
lvMenu = (ListView) findViewById(R.id.lv_function);
ArrayAdapter<String> arr = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, functions);
// set adapter for ListView.
lvMenu.setAdapter(arr);

Here is result: result

Do you have any idea? Please help me, thanks!

Upvotes: 2

Views: 13379

Answers (2)

anop72
anop72

Reputation: 41

  • layout_gravity = Impact with layout in your child views
  • gravity - Impact with content in your child views

Upvotes: 0

Shaun
Shaun

Reputation: 5531

You have to create your own layout for your listview item. Soemthing like this

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >
        <TextView
            android:id="@id/textItem"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
</LinearLayout>

Then, in your code, your going to have to do this

 new ArrayAdapter<String>(this, R.layout.yourRowItemLayoutHere, R.id.textItem, functions);

Hope that helped

Upvotes: 13

Related Questions