MainActivity.java
package com.cfsuman.androidtutorials;
import android.os.Bundle;
import android.app.Activity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the widgets reference from XML layout
ListView listView = findViewById(R.id.listView);
// Initializing a new String Array
String[] fruits = new String[] {
"Cape Gooseberry",
"Capuli cherry",
"Chinese Quince",
"Cocky apple",
"Coconut"
};
// Create a List from String Array elements
List<String> fruits_list = new ArrayList<String>
(Arrays.asList(fruits));
// Create an ArrayAdapter from List
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(
this,
android.R.layout.simple_list_item_1,
fruits_list
);
// DataBind ListView with items from ArrayAdapter
listView.setAdapter(arrayAdapter);
// Set the list view item click listener
listView.setOnItemClickListener((
parent, view, position, id) -> {
// Start an alpha animation for clicked item
Animation animation1 = new AlphaAnimation(0.3f, 1.0f);
animation1.setDuration(4000);
view.startAnimation(animation1);
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="24dp">
<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>


- How to scroll to the bottom of a ListView programmatically in Android
- How to scroll at the bottom of a ListView in Android
- How to animate ListView item when clicked in Android
- How to change ListView selected item background color in Android
- How to add items to ListView programmatically in Android
- How to change ListView item text color in Android
- Android TextClock Example
- How to change CheckBox size in Android
- How to add padding to a CheckBox in Android
- How to set CheckBox on the right side of text in Android
- How to hide/remove circle from RadioButton in Android
- How to add RadioButton to a RadioGroup programmatically in Android
- How to create a RadioGroup programmatically in Android
- How to create a background color animation in Android
- How to animate scale property of a View using ObjectAnimator in Android