MAD22617prg4-16codes
MAD22617prg4-16codes
MainActivity.java
package com.example.helloworld;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView.setText("Hello World!");
}
activity_main.xml (Layout file)
<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">
<TextView
android:id="@+id/textViewHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
android:textStyle="bold"/>
</LinearLayout>
2. Program to Display Student's Name and Marks
MainActivity.java
package com.example.studentinfo;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Student Data
textViewStudent.setText(displayText);
}
activity_main.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">
<TextView
android:id="@+id/textViewStudent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Details"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
1. Program to Place Name, Age, and Mobile Number Vertically using Linear Layout
MainActivity.java
package com.example.linearlayoutapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
activity_main.xml (Linear Layout)
<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="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age: 22"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile: 9876543210"
android:textSize="18sp"
android:textStyle="bold"/>
</LinearLayout>
2. Program to Place Name, Age, and Mobile Number Centrally using Absolute
Layout
MainActivity.java
package com.example.absolutelayoutapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
activity_main.xml (Absolute Layout)
package com.example.tablelayoutapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:stretchColumns="1">
<TableRow>
</TableRow>
<TableRow>
<TextView android:text="1" android:padding="5dp"/>
</TableRow>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
</TableLayout>
MainActivity.java
package com.example.framelayoutapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
activity_main.xml (Frame Layout)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Integer: 100"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Float: 10.5"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Character: 'A'"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Boolean: true"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="200dp"/>
</FrameLayout>
1. Program to Accept Username and Password Using TextView and EditText
MainActivity.java
package com.example.loginapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
});
}
activity_main.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="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Username:"
android:textSize="18sp" />
<EditText
android:id="@+id/editUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Password:"
android:textSize="18sp"
android:layout_marginTop="10dp" />
<EditText
android:id="@+id/editPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:layout_marginTop="20dp"/>
</LinearLayout>
2. Program to Accept and Display Student's Personal Information
MainActivity.java
package com.example.studentinfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
txtDisplay.setText(result);
});
activity_main.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="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"
android:paddingBottom="10dp"/>
<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/editAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number" />
<EditText
android:id="@+id/editEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:inputType="phone" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/txtDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:layout_marginTop="20dp"/>
</LinearLayout>
1. Program: First Display Screen of a Search Engine using AutoCompleteTextView
MainActivity.java
package com.example.searchengine;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] searchSuggestions = {
};
searchBox.setAdapter(adapter);
activity_main.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:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search Engine"
android:textSize="24sp"
android:textStyle="bold"
android:layout_marginBottom="20dp" />
<AutoCompleteTextView
android:id="@+id/autoCompleteSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search..."
android:completionThreshold="1"/>
</LinearLayout>
2. Program: Display Sixth Semester Subjects using AutoCompleteTextView
MainActivity.java
package com.example.sixthsemsubjects;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] subjects = {
};
subjectsBox.setAdapter(adapter);
}
activity_main.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:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textStyle="bold"
android:layout_marginBottom="20dp" />
<AutoCompleteTextView
android:id="@+id/autoCompleteSubjects"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"/>
</LinearLayout>
1. Program: Toggle Button to Turn Bluetooth ON/OFF
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetooth_toggle">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
android:usesPermissionFlags="neverForLocation"/>
<application
android:allowBackup="true"
android:theme="@style/Theme.BluetoothToggle"
android:label="Bluetooth Toggle"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.bluetooth_toggle;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = findViewById(R.id.toggleButton);
statusText = findViewById(R.id.statusText);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
toggleButton.setChecked(bluetoothAdapter.isEnabled());
updateStatusText();
} else {
if (isChecked) {
updateStatusText();
});
if (bluetoothAdapter.isEnabled()) {
statusText.setText("Bluetooth is ON");
} else {
statusText.setText("Bluetooth is OFF");
activity_main.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:padding="20dp">
<TextView
android:id="@+id/statusText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth Status"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="20dp"/>
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Turn ON Bluetooth"/>
</LinearLayout>
MainActivity.java
package com.example.calculator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
result = findViewById(R.id.result);
if (input1.isEmpty() || input2.isEmpty()) {
return;
double n1 = Double.parseDouble(input1);
double n2 = Double.parseDouble(input2);
double output = 0;
switch (operator) {
case '/':
if (n2 == 0) {
output = n1 / n2;
break;
activity_main.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:padding="20dp">
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"/>
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"/>
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<Button
android:id="@+id/subtractButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract"/>
<Button
android:id="@+id/multiplyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"/>
<Button
android:id="@+id/divideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="18sp"
android:layout_marginTop="20dp"/>
</LinearLayout>
1⃣ Social Networking Site Login Form
<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:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginBottom="20dp"/>
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="10dp"/>
</LinearLayout>
package com.example.socialnetworklogin;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
Button btnLogin;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
btnLogin = findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
} else {
});
}
2⃣ Student Registration System Login Form
<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:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Login"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center"
android:layout_marginBottom="20dp"/>
<EditText
android:id="@+id/etStudentID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<EditText
android:id="@+id/etStudentPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"/>
<Button
android:id="@+id/btnStudentLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="10dp"/>
</LinearLayout>
package com.example.studentlogin;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
Button btnStudentLogin;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etStudentID = findViewById(R.id.etStudentID);
etStudentPassword = findViewById(R.id.etStudentPassword);
btnStudentLogin = findViewById(R.id.btnStudentLogin);
btnStudentLogin.setOnClickListener(new View.OnClickListener() {
@Override
} else {
});
}
1. Write a program to show five checkboxes and toast selected checkboxes.
<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="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textStyle="bold"
android:layout_marginBottom="10dp"/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected"
android:layout_marginTop="20dp"/>
</LinearLayout>
Java Code (MainActivity.java)
package com.example.checkboxexample;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
Button btnSubmit;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox1 = findViewById(R.id.checkBox1);
checkBox2 = findViewById(R.id.checkBox2);
checkBox3 = findViewById(R.id.checkBox3);
checkBox4 = findViewById(R.id.checkBox4);
checkBox5 = findViewById(R.id.checkBox5);
btnSubmit = findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
if (selectedOptions.toString().equals("Selected: ")) {
Toast.makeText(MainActivity.this, selectedOptions.toString(),
Toast.LENGTH_SHORT).show();
});
}
XML Layout (activity_main.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="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginBottom="10dp" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<Button
android:id="@+id/btnShowSelected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW SELECTED"
android:layout_marginTop="20dp"/>
</LinearLayout>
Java Code (MainActivity.java)
package com.example.radiobuttonexample;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
RadioGroup radioGroup;
Button btnShowSelected;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioButton1 = findViewById(R.id.radioButton1);
radioButton2 = findViewById(R.id.radioButton2);
radioGroup = findViewById(R.id.radioGroup);
radioMale = findViewById(R.id.radioMale);
radioFemale = findViewById(R.id.radioFemale);
// Initialize Button
btnShowSelected = findViewById(R.id.btnShowSelected);
btnShowSelected.setOnClickListener(new View.OnClickListener() {
@Override
if (selectedOptions.toString().equals("Selected: ")) {
Toast.makeText(MainActivity.this, selectedOptions.toString(),
Toast.LENGTH_SHORT).show();
});
}
1. Circular Progress Bar
<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:padding="20dp">
<ProgressBar
android:id="@+id/progressBarCircular"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
package com.example.circularprogressbar;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
ProgressBar progressBarCircular;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize ProgressBar
progressBarCircular = findViewById(R.id.progressBarCircular);
progressBarCircular.postDelayed(new Runnable() {
@Override
progressBarCircular.setVisibility(View.GONE);
}, 5000);
}
2. Progress Dialog with Horizontal ProgressBar
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="@+id/btnDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</LinearLayout>
package com.example.progressdialogexample;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
ProgressDialog progressDialog;
int progressStatus = 0;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Button
btnDownload = findViewById(R.id.btnDownload);
btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
// Show ProgressDialog
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.show();
@Override
@Override
progressDialog.setProgress(progressStatus);
});
try {
} catch (InterruptedException e) {
e.printStackTrace();
progressDialog.dismiss();
}).start();
});
}
1. ListView Example
<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="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ListView"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.example.listviewexample;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
String[] languages = {"Android", "Java", "PHP", "Hadoop", "SAP", "Python", "Ajax", "C++",
"Ruby", "Rails"};
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize ListView
// Set Adapter
listView.setAdapter(adapter);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/image1" />
<Button
android:id="@+id/btnChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image"
android:marginTop="20dp"/>
</LinearLayout>
package com.example.imagechange;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
Button btnChange;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Views
imageView = findViewById(R.id.imageView);
btnChange = findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
if (isImage1) {
imageView.setImageResource(R.drawable.image2);
} else {
imageView.setImageResource(R.drawable.image1);
isImage1 = !isImage1;
});
}
3. Display 15 Buttons using GridView
<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:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"/>
package com.example.gridviewbuttons;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
String[] buttonNames = {
"Button 1", "Button 2", "Button 3", "Button 4", "Button 5",
"Button 6", "Button 7", "Button 8", "Button 9", "Button 10",
"Button 11", "Button 12", "Button 13", "Button 14", "Button 15"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize GridView
// Set Adapter
gridView.setAdapter(adapter);
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec
odio. Praesent libero. Sed cursus ante dapibus diam. ..."
android:textSize="16sp"
android:padding="20dp"/>
</ScrollView>
package com.example.textscroll;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
1. Display Toast Message
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="@+id/btnToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"/>
</LinearLayout>
Java Code (MainActivity.java)
package com.example.toastmessage;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnToast.setOnClickListener(new View.OnClickListener() {
@Override
});
}
2. Checkboxes with Order Button
<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="20dp">
<CheckBox
android:id="@+id/checkPizza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pizza - ₹100"/>
<CheckBox
android:id="@+id/checkCoffee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coffee - ₹50"/>
<CheckBox
android:id="@+id/checkBurger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Burger - ₹70"/>
<Button
android:id="@+id/btnOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ORDER"
android:layout_marginTop="20dp"/>
</LinearLayout>
package com.example.checkboxorder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
Button btnOrder;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Views
checkPizza = findViewById(R.id.checkPizza);
checkCoffee = findViewById(R.id.checkCoffee);
checkBurger = findViewById(R.id.checkBurger);
btnOrder = findViewById(R.id.btnOrder);
btnOrder.setOnClickListener(new View.OnClickListener() {
@Override
if (checkPizza.isChecked()) {
orderSummary.append("Pizza - ₹100\n");
totalPrice += 100;
if (checkCoffee.isChecked()) {
orderSummary.append("Coffee - ₹50\n");
totalPrice += 50;
if (checkBurger.isChecked()) {
orderSummary.append("Burger - ₹70\n");
totalPrice += 70;
orderSummary.append("Total: ₹").append(totalPrice);
Toast.makeText(MainActivity.this, orderSummary.toString(),
Toast.LENGTH_LONG).show();
});
}
1. TimePicker with Spinner Mode
<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:padding="20dp">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner"/>
</LinearLayout>
package com.example.timepickerspinner;
import android.os.Bundle;
import android.widget.TimePicker;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
});
<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="20dp">
<TextView
android:id="@+id/textViewDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Date & Time"
android:textSize="18sp"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/btnSelectDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT DATE"/>
<Button
android:id="@+id/btnSelectTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT TIME"
android:layout_marginTop="10dp"/>
</LinearLayout>
package com.example.datetimepicker;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
TextView textViewDateTime;
Calendar calendar;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewDateTime = findViewById(R.id.textViewDateTime);
btnSelectDate = findViewById(R.id.btnSelectDate);
btnSelectTime = findViewById(R.id.btnSelectTime);
calendar = Calendar.getInstance();
btnSelectDate.setOnClickListener(new View.OnClickListener() {
@Override
datePickerDialog.show();
});
btnSelectTime.setOnClickListener(new View.OnClickListener() {
@Override
timePickerDialog.show();
});