Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 48

TRIBHUVAN UNIVERSITY

LAB REPORT ON MOBILE PROGRAMMING


(CACS 351)

Submitted by: Submitted to:

Level: BCA Sixth Semester

1. Write a program to implement LinearLayout.


<?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="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Field 1"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Field 2"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Field 3"
android:textStyle="bold"/>
</LinearLayout>

2. Write a program to implement RelativeLayout.


<?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">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_below="@+id/button1"
android:layout_toLeftOf="@+id/button1"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_below="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"/>
</RelativeLayout>
Output:

3. Write a program to implement TableLayout.


<TableLayout
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow> <TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
</TableRow>
</TableLayout>

4. Write a program to implement Android Widgets.


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


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>

<!-- EditText -->


<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText"/>

<!-- Button -->


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>

<!-- CheckBox -->


<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"/>
<!-- RadioButton -->
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton 1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton 2"/>
</RadioGroup>
</LinearLayout>

Output:

5. Write a program to implement Event Handling.


package com.example.widgets;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button; // Declare the button here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button); // Initialize the button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Button Clicked!",
Toast.LENGTH_SHORT).show();
}
});
}}
Output:

6. Write a program to implement Activity Life Cycle.


package com.example.activitylifecycle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this, "Activity Created!",
Toast.LENGTH_SHORT).show();
}
protected void onStart() {
super.onStart();
Toast.makeText(MainActivity.this, "Activity Started!",
Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(MainActivity.this, "Activity Restarted!",
Toast.LENGTH_SHORT).show();
}
protected void onPause() {
super.onPause();
Toast.makeText(MainActivity.this, "Activity Paused!",
Toast.LENGTH_SHORT).show();
}
protected void onResume() {
super.onResume();
Toast.makeText(MainActivity.this, "Activity Resumed!",
Toast.LENGTH_SHORT).show();
}
protected void onStop() {
super.onStop();
Toast.makeText(MainActivity.this, "Activity Stopped!",
Toast.LENGTH_SHORT).show();
}
protected void onDestroy() {
super.onDestroy();
Toast.makeText(MainActivity.this, "Activity Destroyed!",
Toast.LENGTH_SHORT).show();
}
}

7. Write a program to implement multiple Activity using Intent.


MainActivity.java:
package com.example.multipleactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
}
}
Activity_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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is first activity"
android:layout_gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/text1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="20sp"
android:layout_gravity="center"
android:id="@+id/btn"
/>
</LinearLayout>
MainActivity2.java:
package com.example.multipleactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity2 extends AppCompatActivity {


Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btn = findViewById(R.id.btn2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
startActivity(intent);
}
});
}
}
Activity_main2.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"
android:orientation="vertical"
tools:context=".MainActivity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is Second Activity"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:id="@+id/text2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="20sp"
android:layout_gravity="center"
android:id="@+id/btn2"
/>
</LinearLayout>

8. Creating a Fragment Class with wiring widgets.


Modifying strings.xml:
<resources>
<string name="app_name">GfG | Fragment in Android</string>
<string name="heading">Two Fragments in One Activity</string>
<string name="fragment1_button">Display First Fragment</string>
<string name="fragment2_button">Display Second Fragment</string>
<string name="fragment1_text1">Displaying contents of the First Fragment</string>
<string name="fragment2_text1">Displaying contents of the Second
Fragment</string>
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#168BC34A"
android:orientation="vertical"
tools:context=".MainActivity">

<!-- Heading of the activity -->


<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:text="@string/heading"
android:textAlignment="center"
android:textColor="@android:color/holo_green_light"
android:textSize="24sp"
android:textStyle="bold" />
<!-- Button to display first fragment -->
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:background="#4CAF50"
android:onClick="selectFragment"
android:text="@string/fragment1_button"
android:textColor="@android:color/background_light"
android:textSize="18sp"
android:textStyle="bold" />
<!-- Button to display second fragment -->
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#4CAF50"
android:onClick="selectFragment"
android:text="@string/fragment2_button"
android:textColor="@android:color/background_light"
android:textSize="18sp"
android:textStyle="bold" />
<!-- Adding Fragment element in the activity -->
<fragment
android:id="@+id/fragment_section"
android:name="com.example.frag.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
tools:layout="@layout/fragment_one" />
</LinearLayout>
FirstFragment class:
package com.example.frag;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// inflating the layout of the fragment
// and returning the view component
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
SecondFragment class:
package com.example.frag;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// inflating the layout of the fragment
// and returning the view component
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
fragment_one.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:background="#5C52CC57"
android:orientation="vertical">
<!-- Text to be displayed inside the Fragment -->
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/fragment1_text1"
android:textAlignment="center"
android:textColor="@android:color/background_light"
android:textSize="24sp"
android:textStyle="bold" />

</LinearLayout>

fragment_two.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:background="#5C3473A6"
android:orientation="vertical">

<!-- Text to be displayed inside the Fragment -->


<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:gravity="center"
android:text="@string/fragment2_text1"
android:textAlignment="center"
android:textColor="@android:color/background_light"
android:textSize="24sp"
android:textStyle="bold" />

</LinearLayout>

MainActivity.java:
package com.example.frag;

import android.os.Bundle;
import android.view.View;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFragment(View view) {
// creating object for Fragment
Fragment fr;
// displaying first fragment
// if button1 is clicked
if(view == findViewById(R.id.button1)) {
fr = new FragmentOne();
}
// displaying second fragment
// if button2 is clicked
else {
fr = new FragmentTwo();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_section, fr);
fragmentTransaction.commit();
}
}
Output:
9. Implement Option Menu.
Mymenu.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/item1"
android:title="first"></item>
<item android:id="@+id/item2"
android:title="second"></item>
<item android:id="@+id/item3"
android:title="third"
app:showAsAction="withText"></item>
</menu>
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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
MainActivity.java:
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

10. Implement Context Menu.


Mymenu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item1"
android:title="Item 1" />
<item
android:id="@+id/item2"
android:title="Item 2" />
<item
android:id="@+id/item3"
android:title="Item 3" />
</menu>
activity_main.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">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Long-press me"
android:longClickable="true" />
</RelativeLayout>
MainActivity.java:
package com.example.contextmenu;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
registerForContextMenu(btn);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
// Handle item 1 click
return true;
case R.id.item2:
// Handle item 2 click
return true;
case R.id.item3:
// Handle item 3 click
return true;
default:
return super.onContextItemSelected(item);
}
}
}
11. Implement alert Dialog box.
custom_dialog.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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a custom AlertDialog content." />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
activity_main.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">

<Button
android:id="@+id/showDialogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show AlertDialog"
android:layout_centerInParent="true" />
</RelativeLayout>
MainActivity.java:
package com.example.alertdialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button showDialogButton;

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

showDialogButton = findViewById(R.id.showDialogButton);

showDialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create an AlertDialog with a custom layout
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(MainActivity.this);
View customView =
LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_dialog, null);
alertDialogBuilder.setView(customView);
// Define button click actions
alertDialogBuilder.setPositiveButton("OK", null);
alertDialogBuilder.setNegativeButton("Cancel", null);

AlertDialog alertDialog = alertDialogBuilder.create();


alertDialog.show();
}
});
}
}
12. Write a program to implement ListView.
listview_example.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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/text"
android:layout_margin="10dp"></TextView>
</RelativeLayout>
activity_main.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">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mylist"></ListView>
</RelativeLayout>

MainActivity.java:
package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.mylist);
String names[]= {"Kushal","Nabin","Nirajan","Sumit","Sunny"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listview_example,R.id.text,names);
listView.setAdapter(adapter);
}
}
13. Write a program to implement GridView.
grid_item.xml:
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridItemImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />

activity_main.xml:
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:horizontalSpacing="4dp"
android:verticalSpacing="4dp"
android:padding="4dp" />

ImageAdapter.java:
package com.example.gridview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter{
private Context mContext;
private int[] imageIds = {R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.image5};
public ImageAdapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return imageIds.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200)); //
Adjust size as needed
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIds[position]);
return imageView;
}
}
MainActivity.java:
package com.example.gridview;
import android.os.Bundle;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


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

GridView gridView = findViewById(R.id.gridView);


ImageAdapter adapter = new ImageAdapter(this);
gridView.setAdapter(adapter);
}
}
14. Write a program to implement RecyclerView.
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">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerview">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
item_view.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_marginTop="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:id="@+id/imageview"></ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_toEndOf="@+id/imageview"
android:textSize="20dp"
android:textColor="@color/black"
android:text="Name"></TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email"
android:layout_toEndOf="@+id/imageview"
android:layout_below="@+id/name"
android:textColor="@color/black"
android:text="Email"></TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_below="@id/imageview"
android:layout_marginTop="10dp"
android:background="#DAD8D8"></LinearLayout>
</RelativeLayout>
myViewHolder.java:
package com.example.relativelayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class myViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView nameView,emailView;
public myViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageview);
nameView = itemView.findViewById(R.id.name);
emailView = itemView.findViewById(R.id.email);
}
}
myAdapter.java:
package com.example.relativelayout;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class myAdapter extends RecyclerView.Adapter<myViewHolder> {
Context context;
List<item> items;
public myAdapter(Context context, List<item> items) {
this.context = context;
this.items = items;
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
return new
myViewHolder(LayoutInflater.from(context).inflate(R.layout.item_view,parent,false));
}
@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
holder.nameView.setText(items.get(position).getName());
holder.emailView.setText(items.get(position).getEmail());
holder.imageView.setImageResource(items.get(position).getImage());
}
@Override
public int getItemCount() {
return items.size();
}
}
MainActivity.java:
package com.example.relativelayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<item> items = new ArrayList<item>();
items.add(new item("Sabina Ghimire","sg@gmail.com,",R.drawable.woman));
items.add(new item("Sargoon Ghimire","rs@gmail.com,",R.drawable.man2));
items.add(new item("sita dhakal","sd@gmail.com,",R.drawable.woman));
items.add(new item("Sabina Ghimire ","sg@gmail.com,",R.drawable.woman));
items.add(new item("Sargoon Ghimire ","rs@gmail.com,",R.drawable.man2));
items.add(new item("sita dhakal","sd@gmail.com,",R.drawable.woman));
items.add(new item("Sabina Ghimire","sg@gmail.com,",R.drawable.woman));
items.add(new item("Sargoon Ghimire ","rs@gmail.com,",R.drawable.man2));
items.add(new item("sita dhakal","sd@gmail.com,",R.drawable.woman));
recyclerView.setAdapter(new myAdapter(getApplicationContext(),items));
}
}
15. Write a program to implement data Manipulation using CRUD operation.

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"
android:layout_margin="5dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter ID"
android:inputType="number"
android:id="@+id/edtId"></EditText>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:layout_below="@id/edtId"
android:id="@+id/edtName"></EditText>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Address"
android:layout_below="@id/edtName"
android:id="@+id/edtAddress"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edtAddress"
android:id="@+id/btnInsert"
android:text="Insert"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edtAddress"
android:layout_toRightOf="@id/btnInsert"
android:id="@+id/btnSelect"
android:text="Select"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edtAddress"
android:layout_toRightOf="@id/btnSelect"
android:id="@+id/btnUpdate"
android:text="Update"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edtAddress"
android:layout_toRightOf="@id/btnUpdate"
android:id="@+id/btnDelete"
android:text="Delete"></Button>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Data:"
android:layout_below="@id/btnSelect"
android:layout_marginTop="10dp"
android:id="@+id/txtData"></TextView>
</RelativeLayout>
myDbHelper.java:
package com.example.dbsql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class myDbHelper extends SQLiteOpenHelper {


private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydb";
public myDbHelper(Context context){
super (context, DATABASE_NAME, null, DATABASE_VERSION);
}
//creating tables
@Override
public void onCreate(SQLiteDatabase db){
String createQuery = "CREATE TABLE mytable (id INTEGER PRIMARY
KEY,name TEXT, address TEXT)";
db.execSQL(createQuery);
}
//upgrading database
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
onCreate(db);
}
//code to insert data
public void insertData(int id, String name, String address){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name",name);
contentValues.put("address",address);
//inserting row
db.insert("mytable", null, contentValues);
db.close();
}
//code to select data
public Cursor selectData(){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM mytable";
Cursor cursor = db.rawQuery(query, null);
return cursor;
}
//code to update data
public void updateData(String id, String name, String address){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name",name);
contentValues.put("address",address);
//updating row
db.update("mytable",contentValues,"id=?", new String[] {id});
db.close();
}
//code to delete data
public void deleteData(String id){
SQLiteDatabase db = this.getWritableDatabase();
//deleting row
db.delete("mytable","id=?",new String[]{id});
}
}

MainActivity.java:
package com.example.dbsql;

import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


EditText edtId, edtName, edtAddress;
Button btnInsert,btnSelect,btnUpdate,btnDelete;
TextView txtData;
myDbHelper myDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creating objetc of MYDBHelper class
myDBHelper = new myDbHelper(this);
edtId = findViewById(R.id.edtId);
edtName = findViewById(R.id.edtName);
edtAddress = findViewById(R.id.edtAddress);
btnDelete = findViewById(R.id.btnDelete);
btnInsert = findViewById(R.id.btnInsert);
btnSelect = findViewById(R.id.btnSelect);
btnUpdate = findViewById(R.id.btnUpdate);
txtData = findViewById(R.id.txtData);
btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = Integer.parseInt(edtId.getText().toString());
String name = edtName.getText().toString();
String address = edtAddress.getText().toString();
//calling insert function
myDBHelper.insertData(id,name,address);
Toast.makeText(getApplicationContext(),"Data Inserted
Successfully",Toast.LENGTH_SHORT).show();
}
});
btnSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//retriving data
int id=0;
String name = "", address = "";
//calling seletc function
Cursor cursor = myDBHelper.selectData();
while(cursor.moveToNext()){
id = cursor.getInt(0);
name = cursor.getString(1);
address = cursor.getString(2);
}
//displaying data ni TextView
txtData.setText("Id = "+id+"\t Name="+name+"\tAddress="+address);
}
});
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = edtId.getText().toString();
String name = edtName.getText().toString();
String address = edtAddress.getText().toString();
//calling insert function
myDBHelper.updateData(id,name,address);
Toast.makeText(getApplicationContext(),"Data Updated
Successfully",Toast.LENGTH_SHORT).show();
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id=edtId.getText().toString();
//calling delete function
myDBHelper.deleteData(id);
Toast.makeText(getApplicationContext(),"Data deleted
Successfully",Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like