Professional Documents
Culture Documents
Exercise:
2]Program:
activity_main:
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download File"
android:layout_marginTop="300dp"
android:layout_marginStart="120dp"
android:id="@+id/btn"/>
</RelativeLayout>
MainActivity:
package com.example.madpractical_nishant;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = new ProgressDialog(v.getContext());
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(0);
progressDialog.setMax(100);
progressDialog.setMessage("File Downloading...");
CountDownTimer countDownTimer = new
CountDownTimer(20000,1000) {
@Override
public void onTick(long millisUntilFinished) {
progressDialog.incrementProgressBy(5);
}
@Override
public void onFinish() {
}
}.start();
progressDialog.show();
}
});
}
}
Output:
Practical 14:
Exercise:
1]Program:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
list_item:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txt_view"
android:textSize="18sp"
android:padding="10dp"/>
MainActivity:
package com.example.madpractical_nishant;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
String Subjects[] =
{"Android","Java","PHP","Hadoop","Sap","Python","Ajax","C++","Ruby","Rails"
};
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list);
ArrayAdapter<String> arr;
arr = new
ArrayAdapter<String>(this,R.layout.list_item,R.id.txt_view,Subjects);
listView.setAdapter(arr);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String selectedItem = Subjects[position];
Toast.makeText(MainActivity.this,selectedItem,Toast.LENGTH_SHORT).show();
}
});
}
}
Output:
2]Program:
activity_main:
<?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">
<ImageView
android:id="@+id/Image1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/google"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image"
android:layout_marginTop="200dp"
android:layout_marginStart="150dp"/>
</RelativeLayout>
MainActivity:
package com.example.madpractical_nishant;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.Image1);
button = findViewById(R.id.btn);
imageView.setImageResource(R.drawable.google);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isImageDisplayed){
imageView.setImageResource(R.drawable.yahoo);
}
else {
imageView.setImageResource(R.drawable.google);
}
isImageDisplayed = !isImageDisplayed;
}
});
}
}
Output:
3]Program:
activity_main:
<?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">
<GridView
android:id="@+id/GV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="6dp"
android:numColumns="2"
android:verticalSpacing="6dp" />
</RelativeLayout>
list_items:
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txt_view"
android:textSize="18sp"
android:padding="10dp"/>
MainActivity:
package com.example.madpractical_nishant;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Output:
4]Program:
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Napoleon Bonaparte (born Napoleone di Buonaparte;[1][b]
15 August 1769 – 5 May 1821), later known by his regnal name Napoleon I,
was a French emperor and military commander who rose to prominence
during the French Revolution and led successful campaigns during the
Revolutionary Wars. He was the leader of the French Republic as First Consul
from 1799 to 1804, then of the French Empire as Emperor of the French from
1804 until 1814, and briefly again in 1815. His political and cultural legacy
endures as a celebrated and controversial leader. He initiated many enduring
reforms, but has been criticized for his authoritarian rule. He is considered one
of the greatest military commanders in history and his wars and campaigns are
still studied at military schools worldwide. However, historians still debate the
degree to which he was responsible for the Napoleonic Wars, in which
between three and six million people died."
android:textSize="30sp"/>
</ScrollView>
</RelativeLayout>
Output:
Practical 15:
Exercise:
1]Program:
activity_main:
<?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, Toast Example"
android:layout_marginTop="10dp"
android:layout_marginStart="20dp"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_marginTop="40dp"
android:layout_marginStart="20dp"
android:textSize="20dp"/>
</RelativeLayout>
customtoast:
<?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:background="#000"
android:padding="10dp"
android:id="@+id/custtoast"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message for you:"
android:textSize="30sp"
android:textColor="#fff"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You have got mail!"
android:textSize="20sp"
android:textColor="#fff"/>
</LinearLayout>
MainActivity:
package com.example.madpractical_nishant;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater li = getLayoutInflater();
View layout =
li.inflate(R.layout.customtoast,findViewById(R.id.custtoast));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.setGravity(Gravity.CENTER_HORIZONTAL |
Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
});
}
}
Output:
2]Program:
activity_main:
<?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">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pizza"
android:text="Pizza"
android:textSize="20sp"
android:layout_marginTop="100dp"
android:layout_marginStart="150dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/burger"
android:text="Burger"
android:textSize="20sp"
android:layout_marginTop="150dp"
android:layout_marginStart="150dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/coffee"
android:text="Coffee"
android:textSize="20sp"
android:layout_marginTop="200dp"
android:layout_marginStart="150dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="ORDER"
android:layout_marginTop="500dp"
android:layout_marginStart="150dp"/>
</RelativeLayout>
MainActivity:
package com.example.madpractical_nishant;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (pizza.isChecked()){
pPrice=100;
str += "\n Pizza : " +pPrice+" RS";
}
if (coffee.isChecked()){
cPrice=50;
str += "\n Coffee : "+cPrice+" RS";
}
if (burger.isChecked()){
bPrice=120;
str += "\n Burger : "+bPrice+" RS";
}
total = pPrice + bPrice + cPrice;
str += "\n\nTotal"+total;
pPrice = cPrice = bPrice = 0;
str += "\nSelected items: ";
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_LONG).show();
}
});
}
}
Output:
Practical 16:
Exercise:
1]Program:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:timePickerMode="spinner"
android:layout_marginTop="400dp"/>
<TimePicker
android:id="@+id/timepicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:timePickerMode="spinner"
android:layout_marginTop="550dp"/>
</RelativeLayout>
MainActivity:
package com.example.madpractical_nishant;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TimePicker;
}}
Output:
2]Program:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<EditText
android:id="@+id/edtDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="200dp"/>
<Button
android:id="@+id/btnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT DATE"
android:layout_marginTop="200dp"
android:layout_marginStart="250dp"/>
<EditText
android:id="@+id/edtTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="200dp"/>
<Button
android:id="@+id/btnTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT TIME"
android:layout_marginTop="300dp"
android:layout_marginStart="250dp"/>
</RelativeLayout>
TimePickerFragment:
package com.example.madpractical_nishant;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.EditText;
import android.widget.TimePicker;
import androidx.fragment.app.DialogFragment;
import java.util.Calendar;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
EditText tv = getActivity().findViewById(R.id.edtTime);
if (tv != null) {
tv.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
}
}
}
MainActivity:
package com.example.madpractical_nishant;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import java.util.Calendar;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtdate = findViewById(R.id.edtDate);
txttime = findViewById(R.id.edtTime);
Button btnDate = findViewById(R.id.btnDate);
Button btnTime = findViewById(R.id.btnTime);
btnDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
}
});
btnTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "Timepicker");
}
});
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
month = month+1;
String date = dayOfMonth+"-"+month+"-"+year;
txtdate.setText(date);
}
}
Output: