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.
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.
Comments