Loading, please wait...

Design a custom layout-based application which will be able to display your contacts details including Serial number, Name and Contact number.

Sep 30, 2019 Android, 96 Views
In this article, Design a custom layout-based application which will be able to display your contacts details including Serial number, Name , and Contact number.

Main.XML

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" tools:context=".MainActivity">

<ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list_item"></ListView>

</LinearLayout>


Main.JAVA

package com.example.exp6;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.widget.ListView;
public class MainActivity extends AppCompatActivity { ListView list_item; Myadapter myadapter;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list_item = findViewById(R.id.list_item); myadapter = new Myadapter(this); list_item.setAdapter(myadapter); } }

List.XML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent">

<TextView android:id="@+id/sn" android:layout_width="wrap_content" android:layout_height="86dp" android:gravity="center" android:text="sno." android:textSize="40sp" />

<LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="vertical">

<TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name" android:textSize="30sp" />

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/num" android:text="Number" android:textSize="30sp"/>
</LinearLayout>
</LinearLayout>



List.java

package com.example.exp6;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class List extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); } }

 

Sarthak.java

package com.example.exp6;
public class sarthak { private String name; private String num; private String sn;
public sarthak(String name, String num, String sn) { this.name = name; this.num = num; this.sn = sn; }
public String getName() { return name; }
public void setName(String name) {
this.name = name; }
public String getNum() { return num; }
public void setNum(String num) { this.num = num; }
public String getsn() { return sn; }
public void setImg(String img) { this.sn = img; } }


MyAdapter.java

package com.example.exp6;

import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView;

import java.util.ArrayList;

public class Myadapter extends BaseAdapter {

String name[] = {"Sarthak","sarthak","sarthak","sarthak"}; String num[] = {"8954450517","9897300999","8077019772","9412545135"}; String sn[] = {"1","2","3","4"};

Activity context; ArrayList<sarthak> list;

Myadapter(Activity context) { this.context = context; list = new ArrayList<>(); for (int i = 0; i< name.length;i++) { list.add(new sarthak(name[i],num[i],sn[i])); } }

@Override public int getCount() { return list.size(); }

@Override public Object getItem(int i) { return list.get(i); }

@Override public long getItemId(int i) { return i; }

@Override public View getView(int i, View view, ViewGroup viewGroup) { LayoutInflater inflater = context.getLayoutInflater(); View row = inflater.inflate(R.layout.activity_list,null,true); TextView name = row.findViewById(R.id.name); TextView num = row.findViewById(R.id.num); TextView sn = row.findViewById(R.id.sn); name.setText(list.get(i).getName()); num.setText(""+list.get(i).getNum()); sn.setText(""+list.get(i).getsn()); return row; } }


Output:-