Simple ListView in Android

AndroidListViewis a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using anAdapterthat pulls content from a source such as an array or database.

Create New Project Name it as ListViewProject. It create MainActivity.java and activity_main.xml file.

Drag and drop listview from palette. Give ID=listView and layout_height = match_parent and layout_width=match_parent.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
RelativeLayoutxmlns:android="
xmlns:app="
xmlns:tools="
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.thebrogrammers.listviewproject.MainActivity"
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout

It will look on your design something like this - >

Now at MainActivity.java class

package com.thebrogrammers.listviewproject;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivityextends Activity {
// Array of strings...
String[] contactlistArray= {"9846355555","9846366666","9846477777","9816187025"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListViewlistView = (ListView) findViewById(R.id.listView);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactlistArray);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, intposition, long id) {
Toast.makeText(MainActivity.this, "Position: "+position+"\n"+adapter.getItem(position), Toast.LENGTH_LONG).show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public booleanonItemLongClick(AdapterView<?> parent, View view, intposition, long id) {
// on long pressing it will throw the number in dial pad
//for that you need to add permission on AndroidManifest.xml file
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + adapter.getItem(position)));
startActivity(intent);
return true;
}
});
}
}

My AndroidManifest.xml look like this:

You need to add permission of phone call before <application> as shown below

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
manifest xmlns:android="
package="com.thebrogrammers.listviewproject"
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
<activity android:name=".MainActivity"
<intent-filter
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter
</activity
</application
</manifest

Now all set, you can deploy on your android device. And see at onClick and onLongClick

Figure: ListView at ScreenFigure: at OnItem clickedFig.at OnitemLongClick