3

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>
Mike Bryant
  • 2,455
  • 9
  • 39
  • 60

2 Answers2

10

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 with android: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 and android:layout_height to fill_parent.


<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="#ff000000"
          android:background="#ffffffff"
          android:gravity="center"
        />
  • it doesn't have any effect, in simple_list_item.xml (before and after) in appears centered but when being implemented by the listview it aligns left – Mike Bryant Feb 28 '12 at 14:45
  • I tested it, added my code to the answer. I changed the attributes as mentioned. Apart from that I removed the paddingLeft which might cause trouble. Didn't mention that though, so try it after removing that. This works for me (on Android 4.0.3, should do on any other version). –  Feb 28 '12 at 14:47
  • tried the edited version too with no more luck, do I need to change anything in agence.xml? also if it helps the padding works, but it can't be a fixed value => different screens need different values – Mike Bryant Feb 28 '12 at 14:49
  • I updated my code in the first post to what it is now after the unsuccessfull edits – Mike Bryant Feb 28 '12 at 14:55
  • I don't know whether it's possible to do something like a padding left 50%, I tried that but maybe something similar because the padding-left 10dp moved the text – Mike Bryant Feb 28 '12 at 14:58
  • Ok got it, you need to change `android:layout_width` to `fill_parent` on both your listview and your outer LinearLayout. Then it works. The issue here is similar to the one described with the textview, if the listview has the same width as the text, there is no space to center anything. As for the percentages, these should not work because they are not supported, for a list of the available units see [the doc](http://developer.android.com/guide/topics/resources/more-resources.html#Dimension). –  Feb 28 '12 at 15:00
  • thanks that did the trick, I really appreciate you taking the time to help. – Mike Bryant Feb 29 '12 at 13:17
0

try this android:layout_centerhorizontal="true" in place of android:layout_gravity="center" in your text view

application name center alignment in android`

Community
  • 1
  • 1
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98