Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android studio – Align the text to center in ListView

Posted on November 14, 2022

Solution 1 :

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

Problem :

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);
    }
}
READ  [ANSWERED] android - Why do the components move around when I switch one bitmap for another of the same size?
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress