0% found this document useful (0 votes)
7 views30 pages

Lab - 2 Bhupendra

The document outlines the development of an Android application that includes a menu-driven interface for calculating simple interest and the area of a triangle, utilizing fragments for each calculation. It also demonstrates the implementation of context and popup menus, as well as a custom dialog box for entering student details. The provided code snippets include XML layout files and Java classes necessary for the app's functionality.

Uploaded by

Bhupendra chand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views30 pages

Lab - 2 Bhupendra

The document outlines the development of an Android application that includes a menu-driven interface for calculating simple interest and the area of a triangle, utilizing fragments for each calculation. It also demonstrates the implementation of context and popup menus, as well as a custom dialog box for entering student details. The provided code snippets include XML layout files and Java classes necessary for the app's functionality.

Uploaded by

Bhupendra chand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

RR Campus Lab 2

5. Write android Program to:


a. Create a menu driven program android app that contains simple interest and
area of triangle.Create separate fragmentfor each menu and perform task as their
name.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

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

android:id="@+id/fragment_container"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

main_menu.xml
<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item

android:id="@+id/menu_si"

android:title="Simple Interest"/>

<item

android:id="@+id/menu_area"

android:title="Area of Triangle"/>

</menu>

Fragment_simple_interest.xml
<?xml version="1.0" encoding="utf-8"?>

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

android:orientation="vertical"

android:padding="16dp"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

Bhupendra chand Mobile Programming


RR Campus Lab 2
android:text="SimpleInterest Calculate"

android:textSize="30sp"

android:layout_gravity="center"

/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/etP"

android:hint="Enter the Principal"

android:inputType="numberDecimal"

android:textSize="25sp"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/etR"

android:hint="Enter the Rate"

android:inputType="numberDecimal"

android:textSize="25sp"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/etT"

android:hint="Enter the Time"

android:inputType="numberDecimal"

android:textSize="25sp"/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/btnCalc"

android:text="Calculate "

Bhupendra chand Mobile Programming


RR Campus Lab 2
android:layout_gravity="center"

android:textSize="25sp"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/tvResult"

android:textSize="23sp"/>

</LinearLayout>

Fragment_triangle.xml
<?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"

android:padding="16dp">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Area of Triangle Calculate"

android:textSize="30sp"

android:layout_gravity="center"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/etBase"

android:textSize="25sp"

android:hint="Enter the Base"

android:inputType="numberDecimal"/>

<EditText

android:layout_width="match_parent"

Bhupendra chand Mobile Programming


RR Campus Lab 2
android:layout_height="wrap_content"

android:id="@+id/etHeight"

android:textSize="25sp"

android:hint="Enter the Height"

android:inputType="numberDecimal"/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/btnCalc"

android:textSize="23sp"

android:text="Calculate "

android:layout_gravity="center"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/tvResult"

android:textSize="23sp"/>

</LinearLayout>

Fragment_SimpleInterest.java
package com.bhupendra.trianglesimpleinterest;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class FragmentSimpleInterest extends Fragment {

Bhupendra chand Mobile Programming


RR Campus Lab 2
EditText etP, etR, etT;

Button btnCalc;

TextView tvResult;

// TODO: Rename parameter arguments, choose names that match

// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

private static final String ARG_PARAM1 = "param1";

private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters

private String mParam1;

private String mParam2;

public FragmentSimpleInterest() {

// Required empty public constructor

// TODO: Rename and change types and number of parameters

public static FragmentSimpleInterest newInstance(String param1, String param2) {

FragmentSimpleInterest fragment = new FragmentSimpleInterest();

Bundle args = new Bundle();

args.putString(ARG_PARAM1, param1);

args.putString(ARG_PARAM2, param2);

fragment.setArguments(args);

return fragment;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if (getArguments() != null) {

mParam1 = getArguments().getString(ARG_PARAM1);

mParam2 = getArguments().getString(ARG_PARAM2);

Bhupendra chand Mobile Programming


RR Campus Lab 2
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)


{

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

etP = view.findViewById(R.id.etP);

etR = view.findViewById(R.id.etR);

etT = view.findViewById(R.id.etT);

btnCalc = view.findViewById(R.id.btnCalc);

tvResult = view.findViewById(R.id.tvResult);

btnCalc.setOnClickListener(v -> {

double p = Double.parseDouble(etP.getText().toString());

double r = Double.parseDouble(etR.getText().toString());

double t = Double.parseDouble(etT.getText().toString());

double si = (p * r * t) / 100;

tvResult.setText("The Simple Interest is: " + si);

});

return view;

Fragment_Triangle.java
package com.bhupendra.trianglesimpleinterest;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class FragmentTriangle extends Fragment {

Bhupendra chand Mobile Programming


RR Campus Lab 2
EditText etBase, etHeight;

Button btnCalc;

TextView tvResult;

// TODO: Rename parameter arguments, choose names that match

// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

private static final String ARG_PARAM1 = "param1";

private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters

private String mParam1;

private String mParam2;

public FragmentTriangle() {

// Required empty public constructor

public static FragmentTriangle newInstance(String param1, String param2) {

FragmentTriangle fragment = new FragmentTriangle();

Bundle args = new Bundle();

args.putString(ARG_PARAM1, param1);

args.putString(ARG_PARAM2, param2);

fragment.setArguments(args);

return fragment;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if (getArguments() != null) {

mParam1 = getArguments().getString(ARG_PARAM1);

mParam2 = getArguments().getString(ARG_PARAM2);

@Override

Bhupendra chand Mobile Programming


RR Campus Lab 2
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

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

etBase = view.findViewById(R.id.etBase);

etHeight = view.findViewById(R.id.etHeight);

btnCalc = view.findViewById(R.id.btnCalc);

tvResult = view.findViewById(R.id.tvResult);

btnCalc.setOnClickListener(v -> {

double b = Double.parseDouble(etBase.getText().toString());

double h = Double.parseDouble(etHeight.getText().toString());

double area = 0.5 * b * h;

tvResult.setText("The Area of Triangle is: " + area);

});

return view;

MainActivity.java
package com.bhupendra.trianglesimpleinterest;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

@Override

public boolean onCreateOptionsMenu(Menu menu) {

Bhupendra chand Mobile Programming


RR Campus Lab 2
getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

@Override

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

Fragment fragment = null;

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

fragment = new FragmentSimpleInterest();

} else if (id == R.id.menu_area) {

fragment = new FragmentTriangle();

if (fragment != null) {

getSupportFragmentManager().beginTransaction()

.replace(R.id.fragment_container, fragment)

.commit();

return true;

Bhupendra chand Mobile Programming


RR Campus Lab 2
b. Demonstrate context menu and popup menu.
Layout.xml
<?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">

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Menus are Here"

android:textSize="35sp"

android:layout_margin="15dp"

android:textAlignment="center"/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="PopUp Menu"

android:id="@+id/btn"

android:textSize="25sp"

android:layout_margin="10dp"

android:layout_gravity="center"/>

</LinearLayout>

Popup.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item

android:title="Copy"

android:id="@+id/copy"/>

Bhupendra chand Mobile Programming


RR Campus Lab 2
<item

android:title="Paste"

android:id="@+id/paste"/>

<item

android:title="Cut"

android:id="@+id/cut"/>

</menu>

MainActivity.java
package com.bhupendra.contextpopupmenu;

import android.os.Bundle;

import android.view.ContextMenu;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.PopupMenu;

import android.widget.Toast;

import androidx.activity.EdgeToEdge;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.graphics.Insets;

import androidx.core.view.ViewCompat;

import androidx.core.view.WindowInsetsCompat;

publicclass MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener


{

Button btn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Bhupendra chand Mobile Programming


RR Campus Lab 2
setContentView(R.layout.layout);

btn=findViewById(R.id.btn);

registerForContextMenu(btn);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

PopupMenu popup=new PopupMenu(getApplicationContext(),v);

popup.inflate(R.menu.popup);

popup.setOnMenuItemClickListener(MainActivity.this);

popup.show();

});

public boolean onMenuItemClick(MenuItem item){

if (item.getItemId()==R.id.copy){

Toast.makeText(this, "Copy is Clicked", Toast.LENGTH_SHORT).show();

else if (item.getItemId()==R.id.paste){

Toast.makeText(this, "Paste is Clicked", Toast.LENGTH_SHORT).show();

else if (item.getItemId()==R.id.cut){

Toast.makeText(this, "Cut is Clicked", Toast.LENGTH_SHORT).show();

return true;

@Override

public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater mf = new MenuInflater(this);

mf.inflate(R.menu.popup, menu);

return super.onCreateOptionsMenu(menu);

Bhupendra chand Mobile Programming


RR Campus Lab 2
}

@Override

public boolean onOptionsItemSelected(@NonNull MenuItem item) {

if (item.getItemId()==R.id.copy){

Toast.makeText(this, "Copy is Clicked!", Toast.LENGTH_SHORT).show();

else if (item.getItemId()==R.id.paste){

Toast.makeText(this, "Paste is Clicked!", Toast.LENGTH_SHORT).show();

else if (item.getItemId()==R.id.cut){

Toast.makeText(this, "Cut is Clicked!", Toast.LENGTH_SHORT).show(); }

return super.onOptionsItemSelected(item); }

@Override

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo


menuInfo) {

super.onCreateContextMenu(menu, v, menuInfo);

MenuInflater mf = new MenuInflater(this);

mf.inflate(R.menu.popup, menu);}

@Override

public boolean onContextItemSelected(@NonNull MenuItem item) {

if (item.getItemId()==R.id.copy){

Toast.makeText(this, "Copy is Clicked", Toast.LENGTH_SHORT).show(); }

else if (item.getItemId()==R.id.paste){

Toast.makeText(this, "Paste is Clicked", Toast.LENGTH_SHORT).show(); }

else if (item.getItemId()==R.id.cut){

Toast.makeText(this, "Cut is Clicked", Toast.LENGTH_SHORT).show(); }

return super.onContextItemSelected(item);

}}

Bhupendra chand Mobile Programming


RR Campus Lab 2

c. Create an android app with custom dialog box.Dialog box should ask for
Student details (Name,roll,MP,NP,AJ,AE,DS).When save button is pressed. Student
details should be displayed in activity along with result and grade.
Activity.xml
<?xml version="1.0" encoding="utf-8"?>

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

android:orientation="vertical"

android:padding="24dp"

android:layout_width="match_parent"

android:layout_height="match_parent">

<Button

android:id="@+id/btnShowDialog"

android:text="Student Details"

android:layout_width="wrap_content"

Bhupendra chand Mobile Programming


RR Campus Lab 2
android:textSize="25sp"

android:layout_gravity="center"

android:layout_height="wrap_content"/>

<TextView

android:id="@+id/tvStudentDetails"

android:layout_marginTop="20dp"

android:textSize="20sp"

android:textColor="#000000"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

</LinearLayout>

Customdialog_student.xml
<?xml version="1.0" encoding="utf-8"?>

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

android:layout_width="match_parent"

android:layout_height="wrap_content">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:padding="20dp">

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/etName"

android:textSize="25dp"

android:hint="Name"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

Bhupendra chand Mobile Programming


RR Campus Lab 2
android:id="@+id/etRoll"

android:textSize="25dp"

android:hint="Roll Number"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/etMP"

android:textSize="25dp"

android:hint="Mobile Programming Marks"

android:inputType="number"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/etNP"

android:textSize="25dp"

android:hint="Network Programming Marks"

android:inputType="number"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/etAJ"

android:textSize="25dp"

android:hint="Advance Java Marks"

android:inputType="number"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/etAE"

android:textSize="25dp"

android:hint="Applied Economics Marks"

Bhupendra chand Mobile Programming


RR Campus Lab 2
android:inputType="number"/>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/etDS"

android:textSize="25dp"

android:hint="Distributed System Marks"

android:inputType="number"/>

</LinearLayout>

</ScrollView>

MainActivity.java
package com.bhupendra.studentcustomdialog;

import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.app.AlertDialog;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.*;

public class MainActivity extends AppCompatActivity {

Button btnShowDialog;

TextView tvStudentDetails;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity);

btnShowDialog = findViewById(R.id.btnShowDialog);

tvStudentDetails = findViewById(R.id.tvStudentDetails);

btnShowDialog.setOnClickListener(v -> showStudentDialog());

private void showStudentDialog() {

Bhupendra chand Mobile Programming


RR Campus Lab 2
View view = LayoutInflater.from(this).inflate(R.layout.customdialog_student, null);

EditText etName = view.findViewById(R.id.etName);

EditText etRoll = view.findViewById(R.id.etRoll);

EditText etMP = view.findViewById(R.id.etMP);

EditText etNP = view.findViewById(R.id.etNP);

EditText etAJ = view.findViewById(R.id.etAJ);

EditText etAE = view.findViewById(R.id.etAE);

EditText etDS = view.findViewById(R.id.etDS);

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Enter Student Details");

builder.setView(view);

builder.setPositiveButton("Save", (dialog, which) -> {

String name = etName.getText().toString();

String roll = etRoll.getText().toString();

int mp = Integer.parseInt(etMP.getText().toString());

int np = Integer.parseInt(etNP.getText().toString());

int aj = Integer.parseInt(etAJ.getText().toString());

int ae = Integer.parseInt(etAE.getText().toString());

int ds = Integer.parseInt(etDS.getText().toString());

int total = mp + np + aj + ae + ds;

float avg = total / 5f;

boolean pass = mp >= 40 && np >= 40 && aj >= 40 && ae >= 40 && ds >= 40;

String result = pass ? "Pass" : "Fail";

String grade;

if (avg >= 90) grade = "A+";

else if (avg >= 80) grade = "A";

else if (avg >= 70) grade = "B";

else if (avg >= 60) grade = "C";

else grade = "D";

String output = "Name: " + name + "\nRoll: " + roll + "\nMarks:\nMobile Programming="

Bhupendra chand Mobile Programming


RR Campus Lab 2
+ mp + "\nNetwork Programming=" + np + "\nAdvance Java=" + aj + "\nApplied Economics="

+ ae + "\nDistributed System=" + ds + "\nResult: " + result + "\nGrade: " + grade;

tvStudentDetails.setText(output);

});

builder.setNegativeButton("Cancel", null);

builder.show();

d. Create a Custom List View with icon.


Layout.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<ListView

android:layout_width="match_parent"
android:layout_height="wrap_content" android:textSize="30sp"
android:id="@+id/list"/>
Bhupendra chand Mobile Programming
RR Campus Lab 2
</LinearLayout>

Listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView android:layout_width="100sp"
android:layout_height="100sp"
android:contentDescription="TODO"
android:src="@drawable/pic1" android:layout_margin="10sp"
android:id="@+id/image"/>

<TextView

android:layout_width="match_parent"
android:layout_height="wrap_content"

android:text="main title"

android:textSize="30sp"
android:layout_margin="8sp"
android:id="@+id/main" android:layout_toRightOf="@+id/image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sub title"
android:textSize="25sp"
android:layout_margin="50sp"
android:id="@+id/sub_main"
android:layout_toRightOf="@+id/image"/>
</RelativeLayout>
MainActivity.java

import android.os.Bundle;

import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

Bhupendra chand Mobile Programming


RR Campus Lab 2
ListView list; @Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.layout);

list = findViewById(R.id.list); // Correct initialization

String[] main = {"Php", "JavaScript", "React"};

int[] image = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3};

mylistadapter adapter = new mylistadapter(this, main, image);

list.setAdapter(adapter);

MyListAdapter.java

import android.app.Activity;

import android.content.Context; import


android.view.LayoutInflater; import
android.view.View;

import android.view.ViewGroup; import


android.widget.ArrayAdapter; import
android.widget.ImageView; import
android.widget.TextView;

public class mylistadapter extends ArrayAdapter<String> {

Activity context;

String[] title; String[] sub_main; int[]


image;

public mylistadapter(Activity context, String[] title, int[] image) {

super(context, R.layout.listitem, title);

this.context = context;

this.title = title;
Bhupendra chand Mobile Programming
RR Campus Lab 2
this.image = image;

}
public View getView(int position, View view, ViewGroup parent) {

LayoutInflater inflater = context.getLayoutInflater();

View rowView = inflater.inflate(R.layout.listitem, null, true);

TextView tmain = (TextView) rowView.findViewById(R.id.main);

ImageView timage = (ImageView) rowView.findViewById(R.id.image); tmain.setText(title[position]);

timage.setImageResource(image[position]);
tsub_main.setText(sub_main[position]); return rowView;

}
}

PHP

JavaScript

React

Bhupendra chand Mobile Programming


RR Campus Lab 2

e. Create a custom Grid View with icon.


Grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background"
android:layout_gravity="center"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="30sp"
android:textColor="#000000"
android:gravity="center"/>
</LinearLayout>

ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"

Bhupendra chand Mobile Programming


RR Campus Lab 2

android:numColumns="auto_fit"
android:columnWidth="100dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"/>
</RelativeLayout>

MyGridAdapter.java
import android.app.Activity;
import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView; import
android.widget.TextView;
import androidx.annotation.NonNull;

class MyGridAdapter extends ArrayAdapter<String> {

private final Activity context; private final String[] text;


private final int[] image;
public MyGridAdapter(Activity context, String[] text, int[] image)
{ super(context, R.layout.grid_item, text);
this.context = context;
this.text = text;
this.image = image;

}
@NonNull @Override
public View getView(int position, @NonNull View convertView, @NonNull ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater(); convertView =

Bhupendra chand Mobile Programming


RR Campus Lab 2

inflater.inflate(R.layout.grid_item, parent, false);

holder = new ViewHolder();


holder.imageView = convertView.findViewById(R.id.image);
holder.textView = convertView.findViewById(R.id.text);
convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}

holder.imageView.setImageResource(image[position]);
holder.textView.setText(text[position]);
return convertView;
}

static class ViewHolder {

ImageView imageView;

TextView textView; } }

PHP JavaScript

React

Bhupendra chand Mobile Programming


RR Campus Lab 2

f. Create a Recycler View with icon and event

<?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">

<androidx.recyclerview.widget.RecyclerView

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scrollbars="vertical" android:id="@+id/list"/>

</LinearLayout>

list_item.xml
<?xml version="1.0" encoding="utf-8"?>

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

android:layout_width="match_parent"

android:layout_height="wrap_content">

<ImageView

android:layout_width="100sp"

android:layout_height="100sp"

android:layout_alignParentTop="true"

android:layout_margin="10sp"

android:id="@+id/image"/>

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="30sp"

android:layout_alignParentTop="true"

android:layout_toRightOf="@+id/image"

android:id="@+id/txtName" />
Bhupendra chand Mobile Programming
RR Campus Lab 2

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="25sp"

android:layout_below="@id/txtName"

android:layout_toRightOf="@+id/image"

android:id="@+id/txtAddress" />

</RelativeLayout>

MainActivity.java
package com.example.recyclerview;

import androidx.appcompat.app.AppCompatActivity;

import androidx.recyclerview.widget.LinearLayoutManager;

import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

RecyclerViewAdapter adapter;

RecyclerView.LayoutManager layoutManager;

RecyclerView list;

@Override

protected void onCreate(Bundle savedInstanceState)

{ super.onCreate(savedInstanceState);

setContentView(R.layout.layout); list =findViewById(R.id.list);

String[] name={"Php","JavaScript","React"};

int[] image = {R.drawable.img1,R.drawable.img2,R.drawable.img3};

layoutManager = new LinearLayoutManager(MainActivity.this);

list.setLayoutManager(layoutManager);

adapter = new RecyclerViewAdapter(MainActivity.this,name,address,image);

list.setAdapter(adapter);

}
Bhupendra chand Mobile Programming
RR Campus Lab 2

RecyclerViewAdapter.java package

com.example.recyclerview; import

android.app.Activity; import

android.view.LayoutInflater; import

android.view.View; import

android.view.ViewGroup; import

android.widget.ImageView; import

android.widget.TextView; import

androidx.annotation.NonNull; import

androidx.recyclerview.widget.RecyclerView; public

class RecyclerViewAdapter extends

RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{

Activity context;

String[] name;

image;

public RecyclerViewAdapter(Activity context,String[] name,String[] address, int[]

image){ this.context =context; this.name = name;

this.image = image;

@NonNull

@Override

public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

LayoutInflater inflater = LayoutInflater.from(context);

View listItem = inflater.inflate(R.layout.list_item,parent,false);

ViewHolder viewHolder = new ViewHolder(listItem); return

viewHolder;

@Override

public void onBindViewHolder(@NonNull ViewHolder holder, int position)

{ holder.txtName.setText(name[position]);
Bhupendra chand Mobile Programming
RR Campus Lab 2

holder.txtAddress.setText(address[position]);

holder.imageView.setImageResource(image[position]);

@Override public int

getItemCount() { return

name.length;

public static class ViewHolder extends RecyclerView.ViewHolder{

TextView txtName;

ImageView

ViewHolder(View

listItem){ super(listItem);

txtName = listItem.findViewById(R.id.txtName);

imageView = listItem.findViewById(R.id.image);

Bhupendra chand Mobile Programming


Bhupendra chand Mobile Programming

You might also like