ListView In Android Studio Androidpro.in

Step -1 : Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step -2 : Add the following code to res ⇒ layout ⇒ activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>

<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">

 

     <ListView

         android:id="@+id/list"

         android:layout_width="match_parent"

         android:layout_height="match_parent"/>

</RelativeLayout>

In the above code, we have taken a List view. When users click on the list, they can see the details story.

Step -3 : Add the following code to res ⇒ values ⇒ strings.xml.

<resources>

    <string-array name="title_story">

        <item>story1</item>

        <item>story2</item>

        <item>story3</item>

        <item>story4</item>

        <item>story5</item>

        <item>story6</item>

        <item>story7</item>

        <item>story8</item>

        <item>story9</item>

        <item>story10</item>

    </string-array>

 

    <string-array name="details_story">

        <item>Your detail story 1</item>

        <item>Your detail story 2</item>

        <item>Your detail story 3</item>

        <item>Your detail story 4</item>

        <item>Your detail story 5</item>

        <item>Your detail story 6</item>

        <item>Your detail story 7</item>

        <item>Your detail story 8</item>

        <item>Your detail story 9</item>

        <item>Your detail story 10</item>

    </string-array>

</resources>

In the above code, we have stored title of the story and details story.

Step -4 : Create a row to show title list, so go to res ⇒ Right click on layout ⇒ New ⇒Layout Resource File.

Take file name row ⇒ Click on Ok.

Step -5 : Add the following code to row.xml.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:padding="5dp">

<TextView

    android:id="@+id/rowTxt"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="text"

    android:textColor="#ffffff"

    android:background="#E91E63"

    android:textSize="25sp"

    android:padding="10dp"

    android:gravity="center_horizontal"/>

 

    <Space

        android:layout_below="@id/rowTxt"

        android:layout_width="match_parent"

        android:layout_height="5dp"/>

</RelativeLayout>

Step -6 : Add the following code to MainActivity.java.

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

 

public class MainActivity extends AppCompatActivity {

 

    ListView listView;

    String[] tStory;

    String[] dStory;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        listView=findViewById(R.id.list);

        tStory=getResources().getStringArray(R.array.title_story);

        dStory=getResources().getStringArray(R.array.details_story);

 

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.row,R.id.rowTxt,tStory);

        listView.setAdapter(adapter);

 

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

 

                String t=dStory[position];

                Intent intent=new

Intent(MainActivity.this,Main2Activity.class);

                intent.putExtra("story",t);

                startActivity(intent);

            }

        });

    }

}

Step -7 : Add the following code ⇒ Right-click on java ⇒ new ⇒ Activity ⇒ Empty Activity
Leave as it is and click on Finish.

Let assume Let assume you newly created Activity_main2.xml and Main2Activity.java.

Step -8 : Now add the following code to Activity_main2.xml.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="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:padding="10dp">

<ScrollView

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <TextView

    android:id="@+id/txt"

        android:gravity="center"

    android:text="text"

        android:textSize="20sp"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

</TextView>

</ScrollView>

</RelativeLayout>

In the above code, we have added one scroll view and a text view. The scroll view for the long story will be scrollable and text view for showing details story.

Step -9 : Now add the following code to Main2Activity.java.

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

 

public class Main2Activity extends AppCompatActivity {

TextView textView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);

      

        textView=findViewById(R.id.txt);

        String dStory=getIntent().getStringExtra("story");

        textView.setText(dStory);

 

    }

}

Now Run and test your app.