Learner
Learner

Reputation: 43

Align the text to center in ListView

I've created a stopwatch app when the user clicks the Lap button just gives the current time the timer is at. My problem is text in ListView displays at the left corner. I'm finding it impossible to center the text in my listview. Here I attached my XML and Java code.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_margin="100dp"
        android:text="format"
        android:textSize="40dp"
        android:textStyle="bold" />


    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="110dp"
        android:text="Start" />

    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/start"
        android:text="Reset" />


    <ListView
        android:id="@+id/listview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/start"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="9dp"
        android:layout_marginRight="50dp"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll" />


</RelativeLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity
{
    TextView time;
    Button reset, start;
    ListView listView;
    long startTime, TimeBuff, millisecondsTime, UpdateTime = 0L;
    int seconds, milliseconds, minutes;
    Handler handler;
    List<String> ListElementsArrayList;
    ArrayAdapter<String> adapter;
    String[] ListElements = new String[]{};///creating arrays of string type

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

        time = (TextView) findViewById(R.id.textView);
        start = (Button) findViewById(R.id.start);
        reset = (Button) findViewById(R.id.reset);
        listView = (ListView) findViewById(R.id.listview1);

        handler = new Handler();

        //Getting the list of array
        ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
        adapter = new ArrayAdapter<String>(MainActivity.this,
                                           android.R.layout.simple_list_item_1,
                                           ListElementsArrayList);

        listView.setAdapter(adapter);
    }
}

Upvotes: 1

Views: 238

Answers (1)

iknow
iknow

Reputation: 9862

To do it You have to create a layout in Your res/layout folder. With name e.g. list_item_layout.xml. In this layout add this:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textSize="20sp" />

In this file, You can make custom TextView which You want to display (change font, size etc.). To center text use android:gravity="center"

Next, when You create an ArrayAdapter, use this layout:

adapter = new ArrayAdapter<String>(
    MainActivity.this,
    R.layout.list_item_layout, // here You use created layout
    ListElementsArrayList
);

Result:

enter image description here

Upvotes: 2

Related Questions