UI: make all friend list in one screen with android studio

Do you want to make your all friend list in the application?

Create a UI such that, one screen has a list of all friends. On selecting any name, the next screen should show details of that friend like Name, Image, Interest, Contact details, etc.

For that, we have to create two activity.

Let’s Start how to create that…..

Steps Description
1 create a project in android studio with any name.
2 Modify src/MainActivity.java and add the code
3 Modify layout XML file res/layout/activity_main.xml
4 create src/Main2Activity.java
5 create res/layout/activity_main2.xml
6  Modify src/MainActivity2.java and add the code
7  Modify layout XML file res/layout/activity_main2.xml

Read This Post –> Display toast (Message) on the specific interval of Time

Content of src/MainActivity.java 

package com.example.kenilvavaliya.four;

import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
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
{
private ListView listview;
private String[] list;
int i;
@Override
protected void onCreate(Bundle savedInstanceState)

{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listview=(ListView)findViewById(R.id.lv);
list = getResources().getStringArray(R.array.list);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("Friends",listview.getItemAtPosition(i).toString());
startActivity(intent);
}
});

}
}

Content of res/layout/activity_main.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"
tools:context=".MainActivity">


<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="511dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp" />

</RelativeLayout>

After that left click on java folder go New > Activity > Empty Activity

It will create a new java and XML file.

Content of src/Main2Activity.java 

package com.example.kenilvavaliya.four;

import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {
TextView t1;
TextView t2;
TextView t3;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
t1 = (TextView)findViewById(R.id.textView4);
t2 = (TextView)findViewById(R.id.textView5);
t3 = (TextView)findViewById(R.id.textView6);
imageView = (ImageView)findViewById(R.id.imageView);
Bundle bundle = getIntent().getExtras();
if (bundle != null){
t1.setText(bundle.getString("Friends"));
if (t1.getText().toString().equalsIgnoreCase("Friend1")){
imageView.setImageDrawable(ContextCompat.getDrawable(Main2Activity.this,R.drawable.p));
t2.setText("8325602651");
t3.setText("Dancing");

}
else if (t1.getText().toString().equalsIgnoreCase("Friend2")){
imageView.setImageDrawable(ContextCompat.getDrawable(Main2Activity.this,R.drawable.c));
t2.setText("9624124552");
t3.setText("Learning");

}
else if (t1.getText().toString().equalsIgnoreCase("Friend3")){
imageView.setImageDrawable(ContextCompat.getDrawable(Main2Activity.this,R.drawable.jh));
t2.setText("954523621");
t3.setText("Painting");

}
else if (t1.getText().toString().equalsIgnoreCase("Friend4")){
imageView.setImageDrawable(ContextCompat.getDrawable(Main2Activity.this,R.drawable.m1));
t2.setText("841253534");
t3.setText("Learning");

}
else {
imageView.setImageDrawable(ContextCompat.getDrawable(Main2Activity.this,R.drawable.j));
t2.setText("753468920");
t3.setText("App Development");

}
}
}
}

Content of res/layout/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"
tools:context=".Main2Activity">

<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
app:srcCompat="@drawable/ic_launcher_background"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="87dp" />

<TextView
android:id="@+id/textView"
android:layout_width="104dp"
android:layout_height="32dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="15dp"
android:layout_marginTop="241dp"
android:text="Name"
android:textSize="24sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="156dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/textView"
android:layout_marginBottom="216dp"
android:text="Mobile No"
android:textSize="24sp" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/textView"
android:layout_marginBottom="145dp"
android:text="Interest"
android:textSize="24sp" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView"
android:layout_toEndOf="@+id/textView2"
android:text="TextView"
android:textSize="28sp" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView4"
android:layout_alignTop="@+id/textView3"
android:text="TextView"
android:textSize="28sp" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView4"
android:layout_alignTop="@+id/textView2"
android:text="TextView"
android:textSize="28sp" />
</RelativeLayout>

For changing the name of Friend go to values > strings.xml

and change the name of your friend

for a change, the photo of your friend right click on drawable > Show on Explorer and paste the photo of your friends

Output:

Stay Connect with our app: –¬†

https://play.google.com/store/apps/details?id=com.edu.easyengineer

For Assignment Questions and Explanation of a theory topic visit:

http://mycandal.com/mobile-application-devlopmentseit2021/

About easyengineering 38 Articles
Easyengineering.in provides you to all subject and exam related materials online like GPSC, UPSC, IES, GATE, etc. As well as we provide daily job notification, some life-related books and other online courses which are useful too in your study.

Be the first to comment

Leave a Reply

Your email address will not be published.


*