0% found this document useful (0 votes)
1K views52 pages

TyBCA Android Practical

This document contains code for multiple Android practical assignments involving different UI elements like text views, buttons, toast notifications, checkboxes, alert dialogs, spinners, and auto-complete text views. The code defines string resources, layout files, and Java classes to create simple Android apps demonstrating the use of these elements like displaying messages on button clicks, showing custom and basic toasts, validating form inputs, and displaying drop-down lists and auto-complete functionality.

Uploaded by

PRATIK MORE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views52 pages

TyBCA Android Practical

This document contains code for multiple Android practical assignments involving different UI elements like text views, buttons, toast notifications, checkboxes, alert dialogs, spinners, and auto-complete text views. The code defines string resources, layout files, and Java classes to create simple Android apps demonstrating the use of these elements like displaying messages on button clicks, showing custom and basic toasts, validating form inputs, and displaying drop-down lists and auto-complete functionality.

Uploaded by

PRATIK MORE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Practical No.

1
Introduction to Android and My first android application
Create an application with following functionalities: Print and show a simple message e.g. Hello Word
strings.xml
<resources>
<string name="app_name">TYBCA Prac</string>
<string name="helloWorld">Hello World</string>
</resources>

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tvHelloWorld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/helloWorld"
android:textSize="30sp" />

<Button
android:id="@+id/btnClickMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:text="Click Me" />

</FrameLayout>
MainActivity.java
package com.hinduja.tybcaprac;

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 com.hinduja.tybcaprac.R;

public class MainActivity extends AppCompatActivity

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

TextView tvHelloWorld =
findViewById(R.id.tvHelloWorld); Button btnClickMe =
findViewById(R.id.btnClickMe);

btnClickMe.setOnClickListener(view -> {
if (tvHelloWorld.getVisibility() ==
View.GONE)
tvHelloWorld.setVisibility(View.VISIBLE);
else
tvHelloWorld.setVisibility(View.GONE);

Toast.makeText(getApplicationContext(), "Hello
World", Toast.LENGTH_LONG).show();
});
}
}
Practical No. 3
Use of Toast & Custom Toast
Use of Toast & Custom Toast creates android application.

layout/custom_toast.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="#00FBD6"
android:gravity="center">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:layout_marginStart="24dp"
android:textSize="30sp" />

</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tvHelloWorld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/helloWorld"
android:textSize="30sp" />

<Button
android:id="@+id/btnClickMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:text="Click Me" />

</FrameLayout>

MainActivity.java
package com.hinduja.tybcaprac;

import android.os.Bundle;
import
android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.hinduja.tybcaprac.R;

public class MainActivity extends AppCompatActivity

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

Button btnClickMe = findViewById(R.id.btnClickMe);


btnClickMe.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{ showMsg();
}
});
}

public void showMsg() {


LayoutInflater layoutInflater = getLayoutInflater();
View cToast = layoutInflater.inflate(R.layout.custom_toast, null);

Toast toast = new Toast(this);


toast.setDuration(Toast.LENGTH_LONG);
toast.setView(cToast);
toast.setGravity(Gravity.TOP, 0,
200); toast.show();
}
}
Practical No. 4
CheckBox & AlertDialog Box
Create Android application using CheckBox & AlertDiaglog Box

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Form"
android:textSize="30sp" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Username" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Password"
android:inputType="textPassword" />

<CheckBox
android:id="@+id/cbTnC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Accept Term and Condition"
/>

<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Login" />

</LinearLayout>

MainActivity.java
package com.hinduja.tybcaprac;

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.hinduja.tybcaprac8.R;

public class MainActivity extends AppCompatActivity

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

CheckBox cbTnC = findViewById(R.id.cbTnC);


Button btnLogin =
findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(view -> {
boolean isTnCChecked = cbTnC.isChecked();

if(isTnCChecked)
showToast("Thank you accepting Terms and Condition");
else
showToast("Please accept Terms and Condition for login");
});
}

private void showToast(String msg) {


Toast.makeText(this, msg,
Toast.LENGTH_LONG).show();
}

@Override
public void onBackPressed()
{ showOnBackPressed();
}

public void showOnBackPressed() {


AlertDialog.Builder builder = new
AlertDialog.Builder(this); builder.setTitle("Attention");
builder.setMessage("Do you want exit?");
builder.setPositiveButton("Yes", new
DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{ finish();
}
});

builder.setNegativeButton("Nope", new DialogInterface.OnClickListener()


{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{ dialogInterface.dismiss();
}
});

AlertDialog alert =
builder.create(); alert.show();
}
}
Practical No. 5
Spinner & AutoCompleteTextView
Create Android application using Spinner & Auto complete test view
strings.xml
<resources>
<string name="app_name">Tybca Prac 5</string>

<string-array name="fruits">
<item>Mango</item>
<item>Banana</item>
<item>Apple</item>
<item>Grapes</item>
<item>Grapes</item>
<item>Bananas</item>
<item>Dates</item>
<item>Guava</item>
<item>Oranges</item>
<item>Lemons</item>
<item>Water Melons</item>
<item>Papaya</item>
<item>Pear</item>
<item>Apricots</item>
<item>Cherries</item>
<item>Strawberries</item>
</string-array>

</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:orientation="vertical"
android:padding="24dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please select your favorite Fruit "
/>

<Spinner
android:id="@+id/spinnerFruits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:entries="@array/fruits" />

<AutoCompleteTextView
android:id="@+id/actvFruits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Search for fruit name"
/>

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="24dp"
android:text="Submit" />

</LinearLayout>

MainActivity.java
package com.hinduja.tybcaprac5;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity

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

Spinner spinnerFruits = findViewById(R.id.spinnerFruits);


AutoCompleteTextView actvFruits =
findViewById(R.id.actvFruits); Button btnSubmit =
findViewById(R.id.btnSubmit);

String[] fruits =
getResources().getStringArray(R.array.fruits);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fruits);
actvFruits.setAdapter(adapter);

btnSubmit.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View view)
{ String strSpinnerValue =
spinnerFruits.getSelectedItem().toString();
String strActvValue = actvFruits.getText().toString();
String strMsg = "Spinner value : " + strSpinnerValue
+ "\nAutoComplete value : " + strActvValue;

Toast.makeText(getApplicationContext(),
strMsg,
Toast.LENGTH_LONG).show();
}
});

}
}
Practical No. 6
Calculator
Create an application with following functionalities: Calculator for Basic mathematical
operations.
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:orientation="vertical"
android:padding="24dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/etInputFirstNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input First Number"
android:inputType="numberDecimal" />

<EditText
android:id="@+id/etInputSecondNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Input Second Number"
android:inputType="numberDecimal" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp">

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="+" />

<Button
android:id="@+id/btnSubstract"
android:layout_width="wrap_content"
android:layout_height="wrap_content
" android:layout_margin="2dp"
android:layout_weight="1"
android:text="-" />

<Button
android:id="@+id/btnMulti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="x" />
<Button
android:id="@+id/btnDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="÷" />

</LinearLayout>

<TextView
android:id="@+id/tvOutput"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="24dp"
android:background="#eee"
android:gravity="center"
android:padding="4dp"
android:textSize="30sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Note : If you don't insert a value in above edit box
then it will be consider as Zero (0)"
android:textColor="#f00" />

</LinearLayout>

MainActivity.java
package com.hinduja.tybcaprac6;

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;

public class MainActivity extends AppCompatActivity

EditText et1, et2;


Button btnAdd, btnSubstract, btnMulti,
btnDivide; TextView tvOutput;

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

private void addListeners() {


btnAdd.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
String strIp1 =
et1.getText().toString(); String strIp2
= et2.getText().toString();

double dIp1 =
parseDouble(strIp1); double dIp2
= parseDouble(strIp2);

double dOp = dIp1 + dIp2;


tvOutput.setText("" +
dOp);
}
});

btnSubstract.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View view) {
String strIp1 =
et1.getText().toString(); String strIp2
= et2.getText().toString();

double dIp1 =
parseDouble(strIp1); double dIp2
= parseDouble(strIp2);

double dOp = dIp1 - dIp2;


tvOutput.setText("" +
dOp);
}
});

btnMulti.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View view) {
String strIp1 =
et1.getText().toString(); String strIp2
= et2.getText().toString();

double dIp1 =
parseDouble(strIp1); double dIp2
= parseDouble(strIp2);

double dOp = dIp1 * dIp2;


tvOutput.setText("" +
dOp);
}
});
btnDivide.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View view) {
String strIp1 =
et1.getText().toString(); String strIp2
= et2.getText().toString();
double dIp1 =
parseDouble(strIp1); double dIp2
= parseDouble(strIp2);

double dOp = dIp1 / dIp2;


tvOutput.setText("" +
dOp);
}
});
}

private void registerUI() {


et1 = findViewById(R.id.etInputFirstNo);
et2 =
findViewById(R.id.etInputSecondNo);

btnAdd = findViewById(R.id.btnAdd);
btnSubstract =
findViewById(R.id.btnSubstract); btnMulti =
findViewById(R.id.btnMulti); btnDivide =
findViewById(R.id.btnDivide);

tvOutput = findViewById(R.id.tvOutput);
}

private double parseDouble(String ip)


{ try {
return Double.parseDouble(ip);
} catch (Exception e)
{ return 0;
}
}
}
Practical No. 7
Rating Bar, Web view, Seek Bar
Create Android application using Rating Bar, Web view, Seek Bar
android_manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hinduja.tybcaprac7">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TybcaPrac5678">

<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>

<activity
android:name=".WebActivity"
android:exported="false"
android:label="@string/title_activity_web"
android:theme="@style/Theme.TybcaPrac5678.NoActionBar" />

</application>

</manifest>
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:gravity="center_horizontal"
android:orientation="vertical"
android:padding="24dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please rate your App out of 5 stars"
/>

<RatingBar
android:id="@+id/rbFeedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:rating="3"
android:stepSize="1" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Please rate your App out of 100"
/>

<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp" />

<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Submit" />

</LinearLayout>

activity_web.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".WebActivity">

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" /
>

</FrameLayout>

MainActivity.java
package com.hinduja.tybcaprac7;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import
android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity

RatingBar
rbFeedback; SeekBar
seekBar; Button
btnSubmit;

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

registerUI();
addListeners();
}

private void registerUI() {


rbFeedback =
findViewById(R.id.rbFeedback); seekBar =
findViewById(R.id.seekBar); btnSubmit =
findViewById(R.id.btnSubmit);
}

private void addListeners() {


btnSubmit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
float rating =
rbFeedback.getRating(); int progress
= seekBar.getProgress();
String strMsg = "RatingBar value : " + rating + "/5"
+ "\n SeekBar value : " + progress + "/100";
Toast.makeText(getApplicationContext(),
strMsg, Toast.LENGTH_LONG).show();

Intent intent = new


Intent(getApplicationContext(), WebActivity.class);
startActivity(intent);
}
});
}
}

WebActivity.java
package com.hinduja.tybcaprac7;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import
android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;

public class WebActivity extends AppCompatActivity

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

WebView webView = findViewById(R.id.webView);

webView.setWebViewClient(new WebViewClient());
WebSettings webSettings =
webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://google.com");
}
}
Practical No. 8
Android UI Design
Design an android application for “Registration Form” using different layout such as
table layout, linear layout etc. Use Drawable Resources, option menu, List Views
and Adapters.
res/menu/menu_registration.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/itemSubmit"
android:title="Submit"
app:showAsAction="always" />

<item
android:id="@+id/itemAbout"
android:icon="@drawable/ic_about"
app:showAsAction="ifRoom"
android:title="About Us" />

<item
android:id="@+id/itemExit"
android:title="Exit this App"
/>

</menu>

strings.xml
<resources>
<string name="app_name">TYBCA Prac 8</string>

<string-array name="genders">
<item>Male</item>
<item>Female</item>
<item>Others</item>
</string-array>

<string-array name="country">
<item>India</item>
<item>China</item>
<item>Australia</item>
<item>Japan</item>
<item>Other</item>
</string-array>

</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:orientation="vertical"
android:padding="24dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_registration"
android:drawablePadding="8dp"
android:text="Registration Form"
android:textSize="24sp" />

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="#eee"
android:padding="8dp">

<TableRow>

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name" />

<EditText
android:id="@+id/etName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</TableRow>

<TableRow android:layout_marginTop="8dp">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Age" />

<EditText
android:id="@+id/etAge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:inputType="number"
android:maxLength="2" />
</TableRow>

<TableRow android:layout_marginTop="8dp">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gender" />

<Spinner
android:id="@+id/spinnerGender"
android:layout_width="0dp"
android:layout_height="38dp"
android:layout_weight="2"
android:entries="@array/genders"
android:text="Gender" />
</TableRow>

<TableRow>
<TextView
android:layout_marginTop="24dp"
android:layout_weight="1"
android:text="Select Country"
/>

<TextView
android:id="@+id/tvSelectedCountry"
android:layout_marginTop="24dp"
android:layout_weight="1"
android:gravity="end"
android:text="Nothing Selected" />
</TableRow>

<ListView
android:id="@+id/lvCountries"
android:layout_height="200dp" /
>
</TableLayout>
</LinearLayout>

MainActivity.java
package com.hinduja.tybcaprac8;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import
androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity

{ EditText etName, etAge;


Spinner spinnerGender;
ListView lvCountries;
TextView
tvSelectedCountry;
String selectedCountry = "Not
Selected"; String[] arrayCountries;

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

private void registerUI() {


etName =
findViewById(R.id.etName); etAge =
findViewById(R.id.etAge);
spinnerGender = findViewById(R.id.spinnerGender);
lvCountries = findViewById(R.id.lvCountries);
tvSelectedCountry =
findViewById(R.id.tvSelectedCountry);
}

private void initObject() {


arrayCountries =
getResources().getStringArray(R.array.country); ArrayAdapter
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
arrayCountries);
lvCountries.setAdapter(adapter);
}

private void addListener() {


lvCountries.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int i, long l) {
selectedCountry = arrayCountries[i];
tvSelectedCountry.setText("Selected : " +
selectedCountry);
}
});
}

private void onClickSubmit() {


String strName =
etName.getText().toString(); String strAge =
etAge.getText().toString();
String strGender = spinnerGender.getSelectedItem().toString();

String msg = "Name : " + strName


+ "\nAge : " + strAge +
"\nCountry : " + selectedCountry
+ "\nGender : " + strGender;

showAlert(msg);
}

private void showAlert(String msg) {


AlertDialog.Builder builder =
new
AlertDialog.Builder(MainActivity.this);
builder.setMessage(msg);
builder.setPositiveButton("Ok",
null); builder.create().show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_registation,
menu); return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{ int id = item.getItemId();
switch (id) {
case
R.id.itemSubmit:
onClickSubmit();
return true;

case R.id.itemAbout:
String strAbout = "This app is to demonstrate Android
Practical
for " +
"Designing an android application for
'Registration Form' using different layout such as TableLayout,
LinearLayout etc. " +
"\nUsed Drawable Resources, Option menu, ListView and
Adapters.";

showAlert(strAbout);
return true;

case
R.id.itemExit:
finish();
return true;

default:
return super.onOptionsItemSelected(item);
}
}
}
Practical No. 9
Date picker & Time Picker
Create Android application using Date picker & Time Picker
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">

<TextView
android:id="@+id/tvShowDateTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Will display current date and
time" android:textColor="@color/purple_500"
android:textSize="20sp" />

<DatePicker
android:id="@+id/datePickerExample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:endYear="2100"
android:maxDate="12/31/2100"
android:minDate="01/01/2000"
android:spinnersShown="true"
android:startYear="2000" />

<TimePicker
android:id="@+id/timePickerExample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:headerBackground="@color/purple_500"
android:timePickerMode="clock" />

</LinearLayout>
</ScrollView>
MainActivity.java
package com.hinduja.tybcaprac;

import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity


{ private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int
mMinute;

TextView tvShowDateTime;
DatePicker datePicker;
TimePicker timePicker;

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

registerUi();
initObjects();
initDatePicker();
initTimePicker();

showUserSelectDateTime();
}

private void registerUi() {


tvShowDateTime =
findViewById(R.id.tvShowDateTime); datePicker =
findViewById(R.id.datePickerExample); timePicker =
findViewById(R.id.timePickerExample);
}

private void initObjects() {


// Get current calendar date and time from java.util.Calender using
getInstance() method
Calendar currentCal = Calendar.getInstance();

// Initialize instance object


mYear = currentCal.get(Calendar.YEAR);
mMonth =
currentCal.get(Calendar.MONTH);
mDay =
currentCal.get(Calendar.DAY_OF_MONTH); mHour
= currentCal.get(Calendar.HOUR_OF_DAY);
mMinute = currentCal.get(Calendar.MINUTE);
}
private void initDatePicker() {
// Init DatePicker and Set DatePicker listener on Date Change by user
datePicker.init(mYear, mMonth, mDay,
new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker datePicker, int year,
int month, int day) {
mYear = year;
mMonth =
month; mDay =
day;

showUserSelectDateTime();
}
});
}

private void initTimePicker() {

// Init Time Picker


timePicker.setHour(this.mHour);
timePicker.setMinute(this.mMinute);

// Set TimePicker listener on Time Change by user


timePicker.setOnTimeChangedListener(new
TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker timePicker, int hour, int
minute) {
mHour = hour;
mMinute =
minute;

showUserSelectDateTime();
}
});
}

/* Show user selected date time. */


private void showUserSelectDateTime() {
// Generate the String to display Date time, later set this string to
TextView to Display on screen.
StringBuffer sb = new StringBuffer();
sb.append("You selected date time :
");

sb.append(mYear);
sb.append("-");
sb.append(mMonth +
1); sb.append("-");
sb.append(mDay);
sb.append(" ");
sb.append(mHour);
sb.append(":");
sb.append(mMinute);

tvShowDateTime.setText(sb.toString());
}
}
Practical No. 10
Progress Bar, Implicit Intend
Create Android application using Progress Bar, Implicit Intend
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="24dp">

<!-- Nested LinearLayout for showing ProgressBar and ToggleButton -->


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="24dp" />

<ToggleButton
android:id="@+id/tbProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:textOff="Hide Progress"
android:textOn="Show Progress" />

</LinearLayout>

<!-- EDIT TEXT with the hint of "Enter Contact No." -->
<EditText
android:id="@+id/etContactNo"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="8dp"
android:hint="Enter Contact No."
android:inputType="phone" />

<!-- BUTTON for opening Dial-Pad -->


<Button
android:id="@+id/btnDial"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Dialpad" />

<!-- BUTTON for opening Contacts -->


<Button
android:id="@+id/btnContact"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Contact" />

<!-- BUTTON for opening URL on Browser -->


<Button
android:id="@+id/btnBrowser"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Browser" />

<!-- BUTTON for opening system Call log -->


<Button
android:id="@+id/btnCallLog"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Call Log" />

<!-- BUTTON for openning Gallery -->


<Button
android:id="@+id/btnGallery"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Gallery" />

<!-- BUTTON for opening Camera -->


<Button
android:id="@+id/btnCamera"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Camera" />

<!-- BUTTON for Finding Location on Map -->


<Button
android:id="@+id/btnMap"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Map" />

<!-- BUTTON for Sharing Text message -->


<Button
android:id="@+id/btnShare"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Share" />

</LinearLayout>

MainActivity.java
package com.unit5.implicitintenteg;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity

EditText etContactNo;

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

//Registering UI
ProgressBar progressBar =
findViewById(R.id.progressBar); ToggleButton tbProgress
= findViewById(R.id.tbProgress); etContactNo =
findViewById(R.id.etContactNo);
Button btnDial = findViewById(R.id.btnDial);
Button btnContact =
findViewById(R.id.btnContact); Button btnBrowser
= findViewById(R.id.btnBrowser); Button
btnCallLog = findViewById(R.id.btnCallLog);
Button btnGallery =
findViewById(R.id.btnGallery); Button btnCamera =
findViewById(R.id.btnCamera); Button btnMap =
findViewById(R.id.btnMap);
Button btnShare = findViewById(R.id.btnShare);

//Adding Listeners

tbProgress.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean
b) {
if (b)
progressBar.setVisibility(View.GONE);
else
progressBar.setVisibility(View.VISIBLE);
}
});

btnDial.setOnClickListener(v -> onClickDial());


btnContact.setOnClickListener(v ->
onClickContact()); btnBrowser.setOnClickListener(v -
> onClickBrowser()); btnCallLog.setOnClickListener(v
-> onClickCallLog());
btnGallery.setOnClickListener(v ->
onClickGallery());
btnCamera.setOnClickListener(v ->
onClickCamera()); btnMap.setOnClickListener(v ->
onClickMap()); btnShare.setOnClickListener(v ->
onClickShare());
}

private void onClickDial() {


//1st way of doing it
Intent i = new Intent();
i.setAction(Intent.ACTION_DIAL);
String strNo =
etContactNo.getText().toString();
i.setData(Uri.parse("tel:" + strNo));
startActivity(i);

//2nd way of doing it


/*
Intent i2 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" +
strNo));
startActivity(i2);
*/

//3rd way of doing it


/*
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" +
strNo)));
*/
}

private void onClickContact() {


Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://contacts/people/"));
startActivity(i);
}

private void onClickBrowser() {


Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.google.com/"));
startActivity(Intent.createChooser(i, "Choose a
browser"));
}

private void onClickCallLog() {


Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://call_log/calls"));
startActivity(i);
}

private void onClickGallery() {


Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
Uri uri =
Uri.parse("content://media/external/images/media/");
i.setData(uri);
startActivity(i);
}

private void onClickCamera()


{ Intent i = new
Intent();
i.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
startActivity(i);
}

private void onClickMap() {


Uri uri = Uri.parse("https://goo.gl/maps/KYRAP4e2rNv1CAHR7");
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
uri); startActivity(intent);
}

private void onClickShare() {


String subject = "Refer and earn with Implicit Intent
App"; String msg = "Hey friend \uD83D\uDC4B, " +
"\nCreate an account with referral 'IMPLICIT1234' "
+ "and get 50% cashback on first transaction, " +
"or else I will delete your contact number from my device
\uD83D\uDE08.";

Intent intent = new Intent(android.content.Intent.ACTION_SEND);


intent.setType("text/plain"); //The is MIME type for plain text.
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
intent.putExtra(android.content.Intent.EXTRA_TEXT, msg);

startActivity(Intent.createChooser(intent, "Choose an app to Refer"));


}

private void showMsg(String str) {


Toast.makeText(getApplicationContext(),
str,
Toast.LENGTH_LONG).show();
}
}
Practical No. 11
Progress Bar, Explicit Intend
Create Android application using Progress Bar, Explicit Intend
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:orientation="vertical"
android:padding="24dp"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="24dp" />

<ToggleButton
android:id="@+id/tbProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:textOff="Hide Progress"
android:textOn="Show Progress" />

</LinearLayout>

<EditText
android:id="@+id/etFullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Full name"
android:inputType="textPersonName" /
>

<EditText
android:id="@+id/etContactNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Contact No."
android:inputType="phone" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Gender : " />

<RadioGroup
android:id="@+id/rgGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full name"
android:orientation="horizontal">

<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male" />

<RadioButton
android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:text="Female" />
</RadioGroup>

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="24dp"
android:text="Submit" />

</LinearLayout>

activity_display.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:orientation="vertical"
android:padding="24dp"
tools:context=".DisplayActivity">

<TextView
android:id="@+id/tvFullNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Full name" />

<TextView
android:id="@+id/tvFullNameValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp"
tools:text="Xxxxxxxx Xxxxxxxxxx"
/>

<TextView
android:id="@+id/tvContactNoLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Contact No." />

<TextView
android:id="@+id/tvContactNoValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp"
tools:text="Xxxxxxxx Xxxxxxxxxx" />

<TextView
android:id="@+id/tvGenderLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Gender" />

<TextView
android:id="@+id/tvGenderValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp"
tools:text="Xxxxxxxx Xxxxxxxxxx" />

</LinearLayout>

MainActivity.java
package com.hinduja.tybcaprac;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity

{
ProgressBar
progressBar;
ToggleButton
tbProgress;

EditText etFullname;
EditText
etContactNo;
RadioButton rbMale;
Button btnSubmit;

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

registerUI();
addListeners();
}

private void registerUI() {


progressBar =
findViewById(R.id.progressBar); tbProgress =
findViewById(R.id.tbProgress);

etFullname = findViewById(R.id.etFullName);
etContactNo =
findViewById(R.id.etContactNo); rbMale =
findViewById(R.id.rbMale);
btnSubmit = findViewById(R.id.btnSubmit);
}

private void addListeners() {


btnSubmit.setOnClickListener(view ->
startNextActivity());

tbProgress.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean
b) {
if (b)
progressBar.setVisibility(View.GONE);
else
progressBar.setVisibility(View.VISIBLE);
}
});
}

private void startNextActivity() {


long lContactNo =
getContactNo();

if (lContactNo == 0) {
etContactNo.setError("Please enter proper Contact
No."); return;
}
Intent i = new Intent(getApplicationContext(),
DisplayActivity.class); i.putExtra("fn",
etFullname.getText().toString());
i. putExtra("cn", lContactNo);
i.putExtra("gender",
rbMale.isChecked());
startActivity(i);
}

private long getContactNo()


{ try {
String strContact =
etContactNo.getText().toString(); long l =
Long.parseLong(strContact);
return l;
} catch (Exception e)
{ return 0L;
}
}
}

DisplayActivity.java
package com.hinduja.tybcaprac;

import android.content.Intent;
import android.os.Bundle;
import
android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class DisplayActivity extends AppCompatActivity

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

Intent i = getIntent();
String strFullname =
i.getStringExtra("fn"); long lContact =
i.getLongExtra("cn", 0L);
boolean isMale = i.getBooleanExtra("gender", true);

TextView tvFullname =
findViewById(R.id.tvFullNameValue); TextView tvContact =
findViewById(R.id.tvContactNoValue); TextView tvGender =
findViewById(R.id.tvGenderValue);
tvFullname.setText(strFullname);
tvContact.setText("" + lContact);
tvGender.setText(isMale ? "Male" :
"Female");
}
}

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TYBCAPrac8">
<activity
android:name="com.hinduja.tybcaprac.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>

<activity
android:name=".DisplayActivity"
android:exported="false" />

</application>

</manifest>

You might also like