ListView using ListFragment Android

Photo by Muhammad Rosyid Izzulkhaq on Unsplash

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create a Blank Activity and click on Finish button.
Step 2 : Open res -> layout ->activity_main.xml and add following code :

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

android : paddingBottom = “@dimen/activity_vertical_margin”

android : paddingLeft = “@dimen/activity_horizontal_margin”

android : paddingRight = “@dimen/activity_horizontal_margin”

android : paddingTop = “@dimen/activity_vertical_margin”

tools : context =”com.skholingua.android.listfragment.MainActivity” >

< fragment

android : id = “@+id/fragment1”

android : name =”com.skholingua.android.listfragment.MyListFragment”

android : layout_width = “match_parent”

android : layout_height = “match_parent” / >

< / RelativeLayout >

Step 3 : Open src -> package ->MainActivity.java and add following code :

package com . skholingua . android . listfragment ;

import android . os . Bundle ;

import android . support . v4 . app . FragmentActivity ;

import android . view . Menu ;

import android . view . MenuItem ;

public class MainActivity extends FragmentActivity {

@Override

protected void onCreate ( BundlesavedInstanceState ) {

super . onCreate ( savedInstanceState ) ;

setContentView ( R . layout . activity_main ) ;

}

@Override

public boolean onCreateOptionsMenu ( Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater ( ) . inflate ( R . menu . main ,menu ) ;

return true ;

}

@Override

public boolean onOptionsItemSelected ( MenuItemitem ) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item . getItemId ( ) ;

if ( id == R . id . action_settings ) {

return true ;

}

return super . onOptionsItemSelected ( item ) ;

}

}

Step 4 : Open res -> layout ->Create new xml file, list_fragment.xml and add following code :

<? xml version = “1.0” encoding = “utf-8” ?>

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

android : layout_width = “match_parent”

android : layout_height = “match_parent”

android : orientation = “vertical” >

< ListView

android : id = “@android:id/list”

android : layout_width = “match_parent”

android : layout_height = “wrap_content” >

< / ListView >

< TextView

android : id = “@android:id/empty”

android : layout_width = “match_parent”

android : layout_height = “wrap_content” >

< / TextView >

< / LinearLayout >

Step 5 : Open src -> package -> Create new class, MyListFragment.java and add following code :

package com . skholingua . android . listfragment ;

import android . os . Bundle ;

import android . support . v4 . app . ListFragment ;

import android . view . LayoutInflater ;

import android . view . View ;

import android . view . ViewGroup ;

import android . widget . AdapterView ;

import android . widget . AdapterView .OnItemClickListener ;

import android . widget . ArrayAdapter ;

import android . widget . Toast ;

public class MyListFragment extends ListFragmentimplements OnItemClickListener {

// String items = {};

@Override

public View onCreateView ( LayoutInflater inflater ,ViewGroup container ,

Bundle savedInstanceState ) {

View view = inflater . inflate ( R . layout .list_fragment , container , false ) ;

return view ;

}

@Override

public void onActivityCreated ( BundlesavedInstanceState ) {

super . onActivityCreated ( savedInstanceState ) ;

ArrayAdapter <CharSequence> adapter =ArrayAdapter . createFromResource (

getActivity ( ) , R . array . array_list ,

android . R . layout . simple_list_item_1 ) ;

setListAdapter ( adapter ) ;

getListView ( ) . setOnItemClickListener ( this ) ;

}

@Override

public void onItemClick ( AdapterView < ? > parent ,View view , int position ,

long id ) {

Toast . makeText ( getActivity ( ) , “Item: “ + (position + 1 ) ,

Toast . LENGTH_SHORT ) . show ( ) ;

}

}

Step 6 : Open AndroidManifest.xml and add following code :

<? xml version = “1.0” encoding = “utf-8” ?>

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

package = “com.skholingua.android.listfragment”

android : versionCode = “1”

android : versionName = “1.0” >

< uses — sdk

android : minSdkVersion = “16”

android : targetSdkVersion = “19” / >

< application

android : allowBackup = “true”

android : icon = “@drawable/ic_launcher”

android : label = “@string/app_name”

android : theme = “@style/AppTheme” >

< activity

android : name = “.MainActivity”

android : label = “@string/app_name” >

< intent — filter >

< action android : name =”android.intent.action.MAIN” / >

< category android : name =”android.intent.category.LAUNCHER” / >

< / intent — filter >

< / activity >

< / application >

< / manifest >

--

--

--

I’m a good husband, father and Senior Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Understanding Kotlin Coroutines for Beginners

Smartphone

Skeleton screens in an Android app

How to make Note App in Android Studio Part 1

ListView using Custom ArrayAdapter Android

Part 1: Kotlin Coroutines for Beginners

Android -Working with Bottom Sheet

Part 2: Kotlin Coroutines for Beginners

Flutter — Sharing PDF File(Text and Image)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CodingwithSaud

CodingwithSaud

I’m a good husband, father and Senior Developer.

More from Medium

Context and Shared Preferences in Android — An Intro

How to get started with Android Development

Bypassing Android 12 SplashScreen API

How to perform search using channel for Paging 3