Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
a. Design Book Entry interface.
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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Book Entry"
android:layout_gravity="center"
android:textSize="30sp"
android:layout_marginTop="100sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Book Title"
android:textSize="25sp"
android:layout_marginTop="20sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the book title"
android:textSize="15sp"
android:id="@+id/title"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Author Name"
android:textSize="25sp"
android:layout_marginTop="20sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the author name"
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
android:textSize="15sp"
android:id="@+id/author"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Book Description"
android:textSize="25sp"
android:layout_marginTop="20sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the book description"
android:textSize="15sp"
android:id="@+id/description"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Book Price"
android:textSize="25sp"
android:layout_marginTop="20sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the book price"
android:textSize="15sp"
android:id="@+id/price"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="20sp"
android:layout_marginTop="20sp"
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
android:layout_marginLeft="40sp"
android:id="@+id/submit"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40sp"
android:layout_marginTop="20sp"
android:text="Cancel"
android:textSize="20sp"
android:id="@+id/cancel"
/>
</LinearLayout>
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.layout);
}
}
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
b. Calculate Simple Interest and Display the Result in Same Activity.
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="SI Calculator"
android:textSize="40sp"
android:textAlignment="center"
android:layout_margin="10sp" />
<EditText
android:id="@+id/pri"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Principle"
android:textSize="25sp"
android:layout_margin="10sp" />
<EditText
android:id="@+id/rate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Rate"
android:textSize="25sp"
android:layout_margin="10sp" />
<EditText
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Time"
android:textSize="25sp"
android:layout_margin="10sp" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Calculate"
android:textSize="25sp"
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
android:layout_gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textAlignment="center"
android:layout_margin="10sp"
android:id="@+id/txt1" />
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
onClick();
}
public void onClick() {
Button btn;
EditText pri, rate, time;
TextView txt1;
btn = findViewById(R.id.btn);
pri = findViewById(R.id.pri);
rate = findViewById(R.id.rate);
time = findViewById(R.id.time);
txt1 = findViewById(R.id.txt1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int p =Integer.parseInt(pri.getText().toString());
int r=Integer.parseInt(rate.getText().toString());
int t=Integer.parseInt(time.getText().toString());
int si=(p*t*r)/100;
int e=si+p;
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
txt1.setText("Total Amount with interest = "+String.valueOf(e));
}
});
}
c. Design Employee Entry Form insert the data and show to another activity
entry.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="Employee Entry Form"
android:textSize="24sp"
android:layout_gravity="center"
android:paddingBottom="10dp"
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
android:layout_marginTop="30sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Employee Name"
android:id="@+id/empName"
android:layout_marginTop="10sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Employee Age"
android:inputType="number"
android:id="@+id/empAge"
android:layout_marginTop="10sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Department"
android:id="@+id/empDept"
android:layout_marginTop="10sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Salary"
android:inputType="numberDecimal"
android:id="@+id/empSalary"
android:layout_marginTop="10sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginTop="20dp"
android:id="@+id/submitBtn"/>
</LinearLayout>
display.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
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="Employee Details"
android:textSize="24sp"
android:layout_gravity="center"
android:paddingBottom="10dp"
android:layout_marginTop="30sp"/>
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText empName, empAge, empDept, empSalary;
Button submitBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entry);
empName = findViewById(R.id.empName);
empAge = findViewById(R.id.empAge);
empDept = findViewById(R.id.empDept);
empSalary = findViewById(R.id.empSalary);
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
submitBtn = findViewById(R.id.submitBtn);
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = empName.getText().toString();
int age = Integer.parseInt(empAge.getText().toString());
String department = empDept.getText().toString();
double salary = Double.parseDouble(empSalary.getText().toString());
Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
intent.putExtra("name", name);
intent.putExtra("age", age);
intent.putExtra("department", department);
intent.putExtra("salary", salary);
startActivity(intent);
}
});
}
}
DisplayActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class DisplayActivity extends AppCompatActivity {
TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
resultText = findViewById(R.id.resultText);
Intent data = getIntent();
String name = data.getStringExtra("name");
int age = data.getIntExtra("age", 0);
String department = data.getStringExtra("department");
double salary = data.getDoubleExtra("salary", 0.0);
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
String result = "Employee Details:\n\n" +
"Name: " + name + "\n" +
"Age: " + age + "\n" +
"Department: " + department + "\n" +
"Salary: " + salary;
resultText.setText(result);
}
}
d. Implement Three Fragment in one Activity.
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="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment Example"
android:textSize="24sp"
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
android:layout_gravity="center"
android:paddingBottom="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/btnFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"/>
<Button
android:id="@+id/btnFragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2"
android:layout_marginLeft="10dp"/>
<Button
android:id="@+id/btnFragment3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 3"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"/>
</LinearLayout>
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"
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
android:layout_height="match_parent"
android:gravity="center"
android:background="@android:color/holo_blue_light"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment One"
android:textSize="24sp"/>
</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:gravity="center"
android:background="@android:color/holo_green_light"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment Two"
android:textSize="24sp"/>
</LinearLayout>
fragment_three.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:gravity="center"
android:background="@android:color/holo_red_light"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment Three"
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
android:textSize="24sp"/>
</LinearLayout>
FragmentOne.java
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FragmentOne extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
FragmentTwo.java
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FragmentTwo extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
FragmentThree.java
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FragmentThree extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_three, container, false);
}
}
MainActivity.java
package com.example.fragment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity {
Button btnFragment1, btnFragment2, btnFragment3;
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
btnFragment1 = findViewById(R.id.btnFragment1);
btnFragment2 = findViewById(R.id.btnFragment2);
btnFragment3 = findViewById(R.id.btnFragment3);
// Load the first fragment by default
loadFragment(new FragmentOne());
btnFragment1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new FragmentOne());
}
});
btnFragment2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new FragmentTwo());
}
});
btnFragment3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new FragmentThree());
}
});
}
private void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment);
fragmentTransaction.commit();
}
}
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
e. Transfer data from one fragment to another Fragment.
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
fragment_1.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:padding="16dp">
<EditText
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
android:id="@+id/editTextProductId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Product ID"
android:inputType="number" />
<EditText
android:id="@+id/editTextProductName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Product Name" />
<EditText
android:id="@+id/editTextProductPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Product Price"
android:inputType="numberDecimal" />
<Button
android:id="@+id/buttonSendData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Product Data" />
</LinearLayout>
Fragment1.java
package com.example.tranferdatalab1_5;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
public class Fragment1 extends Fragment {
private EditText editTextProductId, editTextProductName, editTextProductPrice;
private Button buttonSendData;
public Fragment1() {}
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
editTextProductId = view.findViewById(R.id.editTextProductId);
editTextProductName = view.findViewById(R.id.editTextProductName);
editTextProductPrice = view.findViewById(R.id.editTextProductPrice);
buttonSendData = view.findViewById(R.id.buttonSendData);
buttonSendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String productId = editTextProductId.getText().toString();
String productName = editTextProductName.getText().toString();
String productPrice = editTextProductPrice.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("product_id", productId);
bundle.putString("product_name", productName);
bundle.putString("product_price", productPrice);
Fragment2 fragment2 = new Fragment2();
fragment2.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment2);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
fragment_2.xml
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
public class Fragment2 extends Fragment {
private TextView textViewProductDetails;
public Fragment2() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_2, container, false);
textViewProductDetails = view.findViewById(R.id.textViewProductDetails);
Bundle bundle = getArguments();
if (bundle != null) {
String productId = bundle.getString("product_id");
String productName = bundle.getString("product_name");
String productPrice = bundle.getString("product_price");
String productDetails = "Product ID: " + productId + "\n" +
"Product Name: " + productName + "\n" +
"Product Price: " + productPrice;
textViewProductDetails.setText(productDetails);
}
return view;
}
}
MainActivity.java
package com.example.tranferdatalab1_5;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi
Lab Report 1 Mobile Programming Prepared By: Muna Kumari Kafle
if (savedInstanceState == null) {
Fragment1 fragment1 = new Fragment1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment1);
transaction.commit();
}
}
}
Lab Report 1 Mobile Programming Prepared By: Bishal Regmi