Mobile Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Unit 1.

Designing the User Interface


Create User interface with using following Ul widgets
Text Vieve: -Create an Account
Edit Text with inputs: Name, Email Mobile Number
Spinner: Items are-Permanent, Temporary, Contract, Freelancer
Checkbox: Work Hourly, Works Monthly
Available for overtime? Radio Button -Yes, No
Submit Button

activity_main.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:id="@+id/main"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/tvCreateAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/create_an_account"
android:textSize="24sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:paddingBottom="20dp" />

<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name"
android:inputType="textPersonName"
android:padding="10dp" />

<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:padding="10dp"
android:layout_marginTop="10dp" />
<EditText
android:id="@+id/etMobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/mobile_number"
android:inputType="phone"
android:padding="10dp"
android:layout_marginTop="10dp" />

<Spinner
android:id="@+id/spinnerEmploymentType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />

<CheckBox
android:id="@+id/cbWorkHourly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/work_hourly"
android:layout_marginTop="20dp" />

<CheckBox
android:id="@+id/cbWorkMonthly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/works_monthly"
android:layout_marginTop="5dp" />

<TextView
android:id="@+id/tvOvertime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/available_for_overtime"
android:layout_marginTop="20dp" />

<RadioGroup
android:id="@+id/rgOvertime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">

<RadioButton
android:id="@+id/rbYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yes" />

<RadioButton
android:id="@+id/rbNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no"
android:layout_marginStart="20dp" />
</RadioGroup>

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:padding="10dp" />
</LinearLayout>

MainActivity.java
package www.biplapneupane.com.np.practical_lab;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
Spinner spinnerEmploymentType = findViewById(R.id.spinnerEmploymentType);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.employment_type_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerEmploymentType.setAdapter(adapter);
}
}
employment_type.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="employment_type_array">
<item>Permanent</item>
<item>Temporary</item>
<item>Contract</item>
<item>Freelancer</item>
</string-array>
</resources>
Unit 2
Complete the following task
Create a new activity with: Edit Text Name, Edit Text Address, Edit Text-Email,
Edit Text-Mobile number with different input Type.
Submit Button with click event open another activity with:
TextView Name, TextView-Address, TextView-Email, TextView-Mobile number,
Display all data in Textview from previous activities user inputs

activityMain2.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:id="@+id/main"
android:padding="16dp">

<TextView
android:id="@+id/tvCreateAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/intent_example"
android:textSize="24sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:paddingBottom="20dp" />

<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name"
android:inputType="textPersonName" />

<!-- EditText for Address -->


<EditText
android:id="@+id/etAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/address"
android:inputType="textPostalAddress"
android:layout_marginTop="10dp" />

<!-- EditText for Email -->


<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:layout_marginTop="10dp" />

<!-- EditText for Mobile Number -->


<EditText
android:id="@+id/etMobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/mobile_number"
android:inputType="phone"
android:layout_marginTop="10dp" />

<!-- Submit Button -->


<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:layout_gravity="center"
android:layout_marginTop="20dp" />
</LinearLayout>

MainActivity.java
package www.biplapneupane.com.np.practical_lab;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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;

public class MainActivity2 extends AppCompatActivity {


EditText etName, etAddress, etEmail, etMobile;
Button btnSubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main2);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

etName = findViewById(R.id.etName);
etAddress = findViewById(R.id.etAddress);
etEmail = findViewById(R.id.etEmail);
etMobile = findViewById(R.id.etMobile);
btnSubmit = findViewById(R.id.btnSubmit);

btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity2.this, Activity2_display.class);
intent.putExtra("NAME", etName.getText().toString());
intent.putExtra("ADDRESS", etAddress.getText().toString());
intent.putExtra("EMAIL", etEmail.getText().toString());
intent.putExtra("MOBILE", etMobile.getText().toString());
startActivity(intent);
}
});
}
}

Activity2Display.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"
android:background="@android:color/white">

<!-- Title TextView -->


<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Details"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:layout_gravity="center"
android:layout_marginBottom="24dp"/>

<!-- Name TextView -->


<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textSize="18sp"
android:textColor="@android:color/black"/>

<!-- Address TextView -->


<TextView
android:id="@+id/tvAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address:"
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_marginTop="8dp"/>

<!-- Email TextView -->


<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_marginTop="8dp"/>

<!-- Mobile Number TextView -->


<TextView
android:id="@+id/tvMobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile Number:"
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_marginTop="8dp"/>

</LinearLayout>
activity2_display.java
package www.biplapneupane.com.np.practical_lab;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class Activity2_display extends


AppCompatActivity {
TextView tvName,tvAddress,tvEmail,tvMobile;

@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

EdgeToEdge.enable(this);

setContentView(R.layout.activity_activity2_display);
ViewCompat.setOnApplyWindowInsetsListener
(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets
(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top,
systemBars.right, systemBars.bottom);
return insets;
});

// Retrieve data from Intent


String name = getIntent().getStringExtra("NAME");
String address =
getIntent().getStringExtra("ADDRESS");
String email =
getIntent().getStringExtra("EMAIL");
String mobile =
getIntent().getStringExtra("MOBILE");

// Find TextViews and set the data


tvName = findViewById(R.id.tvName);
tvAddress = findViewById(R.id.tvAddress);
tvEmail = findViewById(R.id.tvEmail);
tvMobile = findViewById(R.id.tvMobile);

tvName.setText("Name: " + name);


tvAddress.setText("Address: " + address);
tvEmail.setText("Email: " + email);
tvMobile.setText("Mobile Number: " + mobile);
}
}
Unit 3.

Ut Fragments, Menus and Dialogs


Create an options menu with the following items
a) Search, Save, Edit, and Delete
b) Display toast items while the select item.
Create a button and display context menu while button long click. The menu should be
a) Tittle Choose the colors
b) Menu items Black Red, Yellow, White and Green
Display toast items while the select item tittle
Create a button and display the Popup menu while the button clicks. The menu item
should be
a) Search, Save, Edit, and Delete
Create a button and display an alert dialog with title, message, and two negative and
positive buttons

Activity_color_choose.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/main"
android:padding="16dp">

<Button
android:id="@+id/btn_choose_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Color" />
<Button
android:id="@+id/btn_popup_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup Menu" />
<Button
android:id="@+id/btn_alert_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"/>
</LinearLayout>
chooseColor.java

package www.biplapneupane.com.np.practical_lab;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
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.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ColorChoose extends AppCompatActivity {


Button btn_choose_color,btn_alert_dialog,btn_popup_menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_color_choose);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
btn_choose_color=findViewById(R.id.btn_choose_color);
btn_popup_menu=findViewById(R.id.btn_popup_menu);
btn_alert_dialog=findViewById(R.id.btn_alert_dialog);
registerForContextMenu(btn_choose_color);
btn_popup_menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showpupupmenu();
}
private void showpupupmenu() {
PopupMenu popupMenu = new PopupMenu(ColorChoose.this,btn_popup_menu);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new
PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
return true;
}

});
popupMenu.show();
}
});

btn_alert_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(ColorChoose.this);
AlertDialog alertDialog = builder.create();
builder.setTitle("Are you sure ?");
builder.setMessage("Do you want to continue enter ok other wise click cancel");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(ColorChoose.this,"OK
Selected",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Toast.makeText(ColorChoose.this,"Cancel
Selected",Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
});

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Choose Menu");
// Dynamically adding menu items
menu.add("Black");
menu.add("Red");
menu.add("Yellow");
menu.add("White");
menu.add("Green");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
String title = item.getTitle().toString();

if (title.equals("Black")) {
Toast.makeText(this, "Black selected", Toast.LENGTH_SHORT).show();
} else if (title.equals("Red")) {
Toast.makeText(this, "Red selected", Toast.LENGTH_SHORT).show();
} else if (title.equals("Yellow")) {
Toast.makeText(this, "Yellow selected", Toast.LENGTH_SHORT).show();
} else if (title.equals("White")) {
Toast.makeText(this, "White selected", Toast.LENGTH_SHORT).show();
} else if (title.equals("Green")) {
Toast.makeText(this, "Green selected", Toast.LENGTH_SHORT).show();
} else {
return super.onContextItemSelected(item);
}
return true;
}

options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/edit"
android:title="edit"
app:showAsAction="always"/>

<item
android:id="@+id/save"
android:title="save"
app:showAsAction="always"/>

<item
android:id="@+id/update"
android:title="update"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/delete"
android:title="delete"
app:showAsAction="ifRoom"/>
</menu>
popup_menu.xml

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


<menu xmlns:android=
"http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add"
android:title="add" />
<item
android:id="@+id/edit"
android:title="edit" />
<item
android:id="@+id/delete"
android:title="delete"
/>
</menu>
Unit 4

Listview, Gridview and Recyclerview


Write an activity class to add any 10 food items within Listview.
Create Grid View with display following items.
Country Name
Country flag
Create Recycler view with display the following items
Name of country
Country Flag
Short Description
Capital
Continent

ListView_Activity.java

package www.biplapneupane.com.np.practical_lab;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ListView_Activity extends AppCompatActivity {


ListView listView;
String[] foodItems = {
"Pizza", "Burger", "Pasta", "Sushi", "Tacos", "Sandwich", "Salad",
"Ice Cream", "Chocolate", "Fried Chicken"
};

int[] itemImages = {
R.drawable.pizza, R.drawable.burger, R.drawable.pasta,
R.drawable.sushi, R.drawable.tacos, R.drawable.sandwich,
R.drawable.salad, R.drawable.ice_cream, R.drawable.chocolate,
R.drawable.fried_chicken
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_list_view);

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main),
(v, insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
return insets;
});

listView = findViewById(R.id.listView);
CustomListAdapter customListAdapter = new
CustomListAdapter(ListView_Activity.this, foodItems, itemImages);
listView.setAdapter(customListAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(ListView_Activity.this, "Selected: " +
foodItems[position], Toast.LENGTH_SHORT).show();
}
});
}
}

CustomListAdapter.java

package www.biplapneupane.com.np.practical_lab;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListAdapter extends BaseAdapter {


TextView listTextView;
ImageView imageview;
Context mcontext;
String[] foodItems;
int[] images;
LayoutInflater layoutinflater;

public CustomListAdapter(Context context, String[]textItems, int[]imageItems)


{
this.mcontext = context;
this.foodItems = textItems;
this.images = imageItems;
layoutinflater = (LayoutInflater.from(context));
}
@Override
public int getCount() {
return foodItems.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
view = layoutinflater.inflate(R.layout.list_view, null);
listTextView = view.findViewById(R.id.listTextView);
imageview = view.findViewById(R.id.imageview);
listTextView.setText(foodItems[position]);
imageview.setImageResource(images[position]);
return view;
}
}

ActivityListView.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListView_Activity" >

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:divider="@color/black"
android:dividerHeight="5dp"
/>

</RelativeLayout>

listView.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"
>
<ImageView
android:layout_width="40dp"
android:layout_height="50dp"
android:id="@+id/imageview"
/>

<TextView
android:id="@+id/listTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:textColor="@color/black"
android:textStyle="bold" />
</LinearLayout>
Grid View
GridView.java

package www.biplapneupane.com.np.practical_lab;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class GridView extends AppCompatActivity {

android.widget.GridView gridView;

String[] countries = {
"USA", "Nepal", "Bhutan", "Shri Lanka.", "Pakistan", "Italy",
"Japan", "China", "India", "Brazil", "Australia"
};

int[] countryFlags = {
R.drawable.usa, R.drawable.nepal, R.drawable.bhutan,
R.drawable.shrilanka, R.drawable.pakistan, R.drawable.italy,
R.drawable.japan, R.drawable.china, R.drawable.india,
R.drawable.brasil, R.drawable.aus
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_grid_view);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
gridView= findViewById(R.id.gridView);
CustomGridAdapter customGridAdapter = new
CustomGridAdapter(GridView.this,countries,countryFlags);
gridView.setAdapter(customGridAdapter);
}
}

CustomGridAdapetr.java
package www.biplapneupane.com.np.practical_lab;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomGridAdapter extends BaseAdapter {

int[] imageItems;
Context mContext;
String[] countries;

int[] laanguageItems;

LayoutInflater layoutInflater;

public CustomGridAdapter (Context context, String[] countries,int[] imageItems){


this.mContext =context;
this.countries= countries;
this.imageItems= imageItems;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return countries.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= layoutInflater.inflate(R.layout.layout_grid,null);
TextView textView = convertView.findViewById(R.id.textItemGrid);
ImageView imageView = convertView.findViewById(R.id.imageItemGrid);
textView.setText(countries[position]);
imageView.setImageResource(imageItems[position]);

return convertView;
}
}
activity_grid_view.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GridView"
android:layout_marginTop="@dimen/cardview_compat_inset_shadow" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="15dp"
android:id="@+id/textView"/>

<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:id="@+id/gridView"
android:layout_below="@id/textView"
android:layout_marginTop="20dp"/>
</RelativeLayout>

layout_grid.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="100dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:id="@+id/imageItemGrid"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textItemGrid"
android:layout_centerHorizontal="true"
android:layout_below="@+id/imageItemGrid"
android:layout_marginBottom="20dp"
android:text="@string/app_name"
/>
</RelativeLayout>

RecyclerView
RecyclerView.java
package www.biplapneupane.com.np.practical_lab;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

public class RecyclerView extends AppCompatActivity {

androidx.recyclerview.widget.RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_recycler_view);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

recyclerView=findViewById(R.id.recyclerView);
RecyclerDTO[] recyclerDTOS=new RecyclerDTO[]{
new RecyclerDTO("Nepal", "Nepal is a landlocked country in Asia, known for its
stunning Himalayan landscapes and cultural heritage.", "Asia", "Kathmandu",
R.drawable.nepal),
new RecyclerDTO("Bhutan", "Bhutan is a landlocked country in Asia, known for its
mountainous terrain and rich culture.", "Asia", "Thimphu", R.drawable.bhutan),
new RecyclerDTO("Pakistan", "Pakistan is a country in South Asia known for its
diverse landscapes and rich history.", "Asia", "Islamabad", R.drawable.pakistan),
new RecyclerDTO("India", "India is a country in South Asia known for its diverse
culture, languages, and landscapes.", "Asia", "New Delhi", R.drawable.india),
new RecyclerDTO("Sri Lanka", "Sri Lanka is an island country in South Asia known
for its tropical beaches and cultural heritage.", "Asia", "Colombo", R.drawable.shrilanka),
new RecyclerDTO("USA", "The United States is a country in North America known
for its diverse culture and landscapes.", "North America", "Washington, D.C.", R.drawable.usa),
};
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(recyclerDTOS);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(recyclerViewAdapter);
}
}

RecyclerDTO.java

package www.biplapneupane.com.np.practical_lab;
public class RecyclerDTO {
String country;
String discription;
String continent;
String capital;
int images;

public RecyclerDTO(String country, String discription, String continent, String capital, int
images) {
this.country = country;
this.discription = discription;
this.continent = continent;
this.capital = capital;
this.images = images;
}

public String getcountry() {


return country;
}
public void setcountry(String country) {
this.country = country;
}
public String getDiscription() {
return discription;
}
public void setDiscription(String discription) {
this.discription = discription;
}
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
public int getImages() {
return images;
}
public void setImages(int images) {
this.images = images;
}
}

RecyclerViewAdapter.java

package www.biplapneupane.com.np.practical_lab;
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


androidx.recyclerview.widget.RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
RecyclerDTO[] recyclerDTOS;

public RecyclerViewAdapter(RecyclerDTO[] recyclerDTOS) {


this.recyclerDTOS = recyclerDTOS;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.recycler_view_items, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
RecyclerDTO recyclerDTO = recyclerDTOS[position];
holder.countryTextView.setText(recyclerDTO.getcountry());
holder.capitalTextView.setText(recyclerDTO.getCapital());
holder.continentTextView.setText(recyclerDTO.getContinent());
holder.descriptionTextView.setText(recyclerDTO.getDiscription());
holder.imageView.setImageResource(recyclerDTO.getImages());
}
@Override
public int getItemCount() {
return recyclerDTOS.length;
}

public static class ViewHolder extends RecyclerView.ViewHolder {


TextView countryTextView;
TextView capitalTextView;
TextView continentTextView;
TextView descriptionTextView;
ImageView imageView;

public ViewHolder(@NonNull View itemView) {


super(itemView);
countryTextView = itemView.findViewById(R.id.recyclerTextView);
capitalTextView = itemView.findViewById(R.id.capitalOfCountry);
continentTextView = itemView.findViewById(R.id.continentOfCountry);
descriptionTextView = itemView.findViewById(R.id.descriptionOfCountry);
imageView = itemView.findViewById(R.id.recyclerImageView);
}
}
}

Recycler_view_items.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"
android:padding="16dp">

<LinearLayout
android:id="@+id/leftColumn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentStart="true"
android:layout_marginEnd="16dp">

<ImageView
android:id="@+id/recyclerImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/editing"
android:layout_marginBottom="8dp" />

<TextView
android:id="@+id/recyclerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="18sp"
android:textColor="#000000"
android:gravity="center"
android:layout_marginBottom="4dp" />

<TextView
android:id="@+id/capitalOfCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capital"
android:textSize="14sp"
android:textColor="#757575"
android:gravity="center"
android:layout_marginBottom="4dp" />

<TextView
android:id="@+id/continentOfCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continent"
android:textSize="14sp"
android:textColor="#757575"
android:gravity="center" />
</LinearLayout>

<TextView
android:id="@+id/descriptionOfCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/leftColumn"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="Description"
android:textSize="16sp"
android:textColor="#000000"
android:gravity="center"
android:padding="16dp"
android:background="#FFFFFF"
android:elevation="2dp" />

<!-- Divider View -->


<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/leftColumn"
android:layout_marginTop="23dp"
android:padding="0dp"

android:background="@color/cardview_dark_background" />
</RelativeLayout>

Activity_recycler_view.xml

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


<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecyclerView" >
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Practical_Lab"
tools:targetApi="31">
<activity
android:name=".GridView"
android:exported="false" />
<activity
android:name=".ListView_Activity"
android:exported="false" />
<activity
android:name=".ColorChoose"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="false" />
<activity
android:name=".Activity2_display"
android:exported="false" />
<activity
android:name=".MainActivity2"
android:exported="false" />
<activity
android:name=".RecyclerView"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

You might also like