Achraf Hamdi
Achraf Hamdi

Reputation: 75

Centering items in ListView (Android)

i have made a simple ListView, its working perfectly, the only thing i still nedd is to display the items in the centre of my ListView.

this is my explication.xml code :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Results"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#e33939"
    android:textSize="40dp" />

<ListView
    android:id="@+id/list"
    android:layout_width="180dp"
    android:layout_height="430dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:gravity="center"
    android:background="#e33939" >
</ListView>
</RelativeLayout>

this is my activity Learn.java:

public class Learn extends  Activity {
ListView listView ;

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

    // Get ListView object from xml
    listView = (ListView) findViewById(R.id.list);

    // Defined Array values to show in ListView
    String[] values = new String[] { 
        " a = أ - ى - ا ", " b = ب ", " c = س ", " d = د ", " f = ف ", " h = ه " , " i = ئ", " j = ج",
        " k = ك", " l = ل", " m = م ", " n = ن", " o = ؤ ", " p = ب", " q = ك", " r = ر",  " s = س",
        " t = ت - ث - ة ", " v = ف " , " w = و" , " x = اكس" , " y = ي", " z = ز ", " sh = ش" , 
        " ء - ؤ = 2  " ,  " ع = 3  ", " غ = 3′ " , " ذ = 4 ", " خ = 5 ", " ط = 6  ", " ظ = ‘6 ",
        " ح = 7 ", " ق = 8  ", "ص =  9 " , " ض = ‘9  "

                                    };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter); 

    // ListView Item Click Listener
    listView.setOnItemClickListener(new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, View view,
             int position, long id) {

           // ListView Clicked item index
           int itemPosition     = position;

           // ListView Clicked item value
           String  itemValue    = (String) listView.getItemAtPosition(position);

            // Show Alert 
            Toast.makeText(getApplicationContext(),""+"" +itemValue , Toast.LENGTH_LONG)
              .show();
          }
     }); 
}
}

the items are displayed in the left of the sceean. i have tried multiple things but no results. thank you

Upvotes: 3

Views: 4866

Answers (2)

msevgi
msevgi

Reputation: 4904

you must change row xml. For ex below

row.xml

 <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="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center" />
    </LinearLayout>

After you must updated one row only

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      R.layout.row, R.id.textItem, values);

Good luck there

Upvotes: 4

Ankit Khare
Ankit Khare

Reputation: 1385

Do it like this in your items.xml file

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true">


        </LinearLayout>
    </RelativeLayout>

This will create item in the center of listview.

Upvotes: 0

Related Questions