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:
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);
}
}