There is an easy way for listing contents. Also with the help of onListItemClick() method the selected list item can be captured.
-------------------Your Xml code--------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#64B5F6"
android:padding="20dp" >
<TextView
android:id="@+id/textView1"
android:layout_marginBottom="5dp"
android:textStyle="italic|bold"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Android Versions"/>
<TextView
android:id="@+id/showVersionTV"
android:background="#ffffff"
android:gravity="center"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ff0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
</LinearLayout>
------------------------------------------------------
-----------------------Now the Java code------------------------
package com.apps.yourappname;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MyListActivity extends ListActivity {
private TextView showVersionTV;
private String[] items = {"a","b","c","d","e","f","g","h","i","j","k"};
private String[] versions = {"alpha","beta","cup cake","donut","eclare","froyo",
"ginger bread","honey comb","ice cream","jelly bean","kitkat"};
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.my_list_activity);
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,items));
showVersionTV = findViewById(R.id.showVersionTV);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
showVersionTV.setText(versions[position]);
}
}
----------------------------------------------------------------------------------
----------------------------------------Changes in the Manifest file------------------------------------------
Just add the ActivityName in the Manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apps.yourappname">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MyListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-------------------------No Changes in the build.gradle (Module:app)------------------
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com..app.yourappname"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
No comments:
Post a Comment