I can't center the text in my listview

chility

I have this xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".AllJokes" 
    android:background="@drawable/background">

    <ListView
              android:id="@+id/allJokesList"
              android:layout_width="fill_parent"
              android:layout_height="0dp"
              android:layout_weight="1"
              android:gravity="center">
    </ListView>


              <com.google.android.gms.ads.AdView android:id="@+id/adView"

                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    ads:adUnitId="ca-app-pub-code/code"
                    ads:adSize="BANNER"
                />

</LinearLayout>

As you can see I'm trying to center the text of the listView by using android:gravity="center_vertical", but without a success. I have also tried to do it by: android:gravity="center", but it is always left alligned.

Also in the java code I'm setting a white background to the listView by:

listView.setBackgroundColor(Color.WHITE);

Is it messing the things up?

Why is this happening?

I know that I'm missing something really small and basic, but as a beginner, I'm not able to spot it.

EDIT:

Here is the code I'm using to add items to the list view:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
        adapter.clear();
        adapter.addAll(globals.getMyStringArray());

        listView = (ListView) findViewById(R.id.allJokesList);
        listView.setAdapter(adapter);
        listView.setBackgroundColor(Color.WHITE);

Where globals.getMyStringArray() is already populated with values.

Libin

Use this. I copied the same android.R.layout.simple_list_item_1 layout, but changed android:gravity="center"

R.layout.custom_simple_list_item

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

change the code ..

 adapter = new ArrayAdapter<String>(this, R.layout.custom_simple_list_item, new ArrayList<String>());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I can't center my menu on HTML

From Dev

how can I center text in my bootstrap webpage?

From Dev

I can't align the div to the center of my webpage

From Dev

I can't change the text of my buttons

From Dev

how I can mange my text in child-listview?

From Dev

I can't clear the listview in my fragment before populating it again?

From Java

UWP: ListView Can't align item in center

From Dev

Why can't I center my image even when I set img as block element?

From Dev

Can't get my footer content to center

From Dev

Can't center the title of my webpage

From Dev

Can't vertically center my logo

From Dev

Can't center my social media icon

From Dev

Can't center my html input areas

From Dev

How can I center my button on radiogroup?

From Dev

How can i center these images on my screen

From Dev

How can I center this datepicker in my form?

From Dev

Can't get text to center in Windows Safari

From Dev

Can't center text in a fixed position column

From Dev

Can´t center textview´s text vertically

From Dev

Can't center text HTML / CSS

From Dev

Can't center text when using colored

From Dev

Can't get text to center in Windows Safari

From Dev

Can't center text in Outlook 2013

From Dev

Can't center some text in a Bootstrap row

From Dev

Can't vertically center text in a table cell

From Dev

How can I center the text in a table?

From Dev

How can i center the slideshow in my website to be in the center of the screen?

From Dev

(Android) How can I make the text in my listview item to stay on one row?

From Dev

How can I create a listView of all TEXT file from my sdcard?