I'm finding it impossible to center the text in my listview, tried wrap_content and layout_gravity=center on virtually everything vet the text doesn't move
here's my class agenceEco
package com.blabla;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.*;
public class agenceEco extends Activity {
ListView myList;
String[] listContent = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.agence);
myList = (ListView)findViewById(R.id.liste_agences);
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
R.layout.simple_list_item,
listContent);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList = (String)(myList.getItemAtPosition(myItemInt));
Toast.makeText(getBaseContext(),selectedFromList,Toast.LENGTH_SHORT).show();
}
});
}
}
Heres simple_list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center"
/>
here's agence.xml
<?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:orientation="vertical"
android:background="@color/white"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/black">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text=" "
android:layout_gravity="center"
/>
<TextView android:id="@+id/txt_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Choix de l'agence"
android:layout_gravity="center"
android:textStyle="bold"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text=" "
android:layout_gravity="center"
/>
</LinearLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text=" "
android:layout_gravity="center"
/>
<ListView android:id="@+id/liste_agences"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_gravity="center_horizontal"
></ListView>
</LinearLayout>
android:layout_gravity="center"
indicates to the parent of the textview how it should be placed. This is not what you want here because a ListView ignores that attribute. Replace it withandroid:gravity="center"
. This tells the TextView how to align the text inside it, in this case center it.This will have no effect when your textview doesn't fill the whole space though (since the text will match the size of the view, so align doesn't change much). Since you only have a textview in each entry it makes sense to set both
android:layout_width
andandroid:layout_height
tofill_parent
.try this
android:layout_centerhorizontal="true"
in place ofandroid:layout_gravity="center"
in your text viewapplication name center alignment in android`