0% found this document useful (0 votes)
3 views

Andrioid Programming

Uploaded by

Ansari Faisal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Andrioid Programming

Uploaded by

Ansari Faisal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

NAME- Ansari Mohd Faisal.

Dilshad
ROLL NO- 238

ASSIGNMENT- 3

SET-A

Q1-Create an Android Appliction to demonstrate DatePickerDialog


and TimePickerDialog.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
tools:context=".MainActivity" >

<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Date" />

<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Time"
/>

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="TextView"
/>
</LinearLayout>
JAVA
package com.example.dateandtime;

import androidx.appcompat.app.AppCompatActivity;

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.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


Calendar c;
DatePickerDialog dp;
TimePickerDialog tp;
Button btn1,btn2;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=findViewById(R.id.b1); btn2=findViewById(R.id.b2);
tv=findViewById(R.id.textView);
btn1.setOnClickListener(new View.OnClickListener() {
@Override public void
onClick(View v) { c=
Calendar.getInstance();
int dd = c.get(Calendar.DAY_OF_MONTH);
int mm = c.get(Calendar.MONTH); int yy
= c.get(Calendar.YEAR);
dp = new DatePickerDialog(MainActivity.this, new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month,
int dayOfMonth) {
tv.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
}
}, dd, mm, yy);
dp.show();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override public void
onClick(View v)
{ c=Calendar.getInstance(); int
hrs=c.get(Calendar.HOUR_OF_DAY); int
minutes=c.get(Calendar.MINUTE);
tp=new TimePickerDialog(MainActivity.this, new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int
minute) {
tv.setText(hourOfDay+":"+minute);
}
}, hrs, minutes, true);
tp.show();
}
});
}
}

Q2- Create an Android Application that demontrate Switch and Toggle


Button.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ToggleButton android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="94dp"
android:layout_marginBottom="574dp"
android:text="Wifi"
android:textOff="Wifi" />

<Switch android:id="@+id/s1"
android:layout_width="wrap_content"
android:layout_height="237dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="143dp"
android:layout_marginBottom="182dp"
android:text="Switch" />

<ToggleButton android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="270dp"
android:layout_marginBottom="580dp"
android:text="Bluetooth"
android:textOff="Bluetooth" />
</RelativeLayout>

JAVA
package com.example.switchtoggle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import


android.widget.CompoundButton; import
android.widget.Switch; import
android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {


ToggleButton tg1,tg2;
Switch s1;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tg1 = findViewById(R.id.t1);
tg2 = findViewById(R.id.t2); s1 = findViewById(R.id.s1);
tg1.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, "Bletooth on",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Bluettoth off",
Toast.LENGTH_SHORT).show();
}
}
});
tg2.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked){
Toast.makeText(MainActivity.this,"Wifi
on",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"Wifi
off",Toast.LENGTH_SHORT).show();
}
}
});
s1.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked){
Toast.makeText(MainActivity.this,"Switch is
on",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"Switch is
off",Toast.LENGTH_SHORT).show();
}
}
});
}

Q3-Create an Android Application that accept the number from user to


find factorial and display result on AlertDialog Box.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>

JAVA
package com.example.factalert;

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

import android.content.DialogInterface;
import android.os.Bundle; import
android.view.View; import
android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


EditText t;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=findViewById(R.id.t1); b=findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
int i,n,fact=1;
n=Integer.parseInt(t.getText().toString());
for (i=1;i<=n;i++){ fact=fact*i; }
AlertDialog.Builder d=new AlertDialog.Builder(MainActivity.this);
d.setTitle("Factorial");
d.setMessage("The factorial of number is "+fact);
d.setNegativeButton("okay", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
});
}
}

Q4-Create an Android Application to change the image on the screen.


XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" tools:context=".MainActivity"
>

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="464dp"
tools:srcCompat="@tools:sample/backgrounds/scenic" />

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="168dp"
android:layout_marginBottom="285dp"
android:text="Button" />

<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="164dp"
android:layout_marginBottom="195dp"
android:text="Button2" />

<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="161dp"
android:layout_marginBottom="106dp"
android:text="Button3" />
</RelativeLayout>

JAVA
package com.example.changeimage;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import


android.view.View; import
android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


ImageView iv;
Button bn1,bn2,bn3;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=findViewById(R.id.imageView2);
bn1=findViewById(R.id.b1); bn2=findViewById(R.id.b2);
bn3=findViewById(R.id.b3);
bn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv.setImageResource(R.drawable.ic_launcher_background);
}
});
bn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv.setImageResource(R.drawable.ic_launcher_foreground);
}
});
bn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{ iv.setImageResource(R.drawable.ic_launcher_background);
}
});
}
}

Q5-Create an Android Application that creates a custom Alert Dialog


containing Friends Name and on click of Friends Name Button greet
accodingly.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:text="Button"
/>
</LinearLayout>

JAVA
package com.example.customdialog;

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

import android.app.Dialog; import


android.content.DialogInterface; import
android.os.Bundle; import
android.view.View; import
android.widget.Button; import
android.widget.DatePicker; import
android.widget.Toast;

public class MainActivity extends AppCompatActivity {


Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); b=
findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
AlertDialog a = b.create();
a.setButton(Dialog.BUTTON1, "SANA", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Hello sana",
Toast.LENGTH_SHORT).show();
}
});
a.setButton(Dialog.BUTTON2, "prajo", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "HIII prajo",
Toast.LENGTH_SHORT).show();
}
});
a.show();
}
});
}}

SET-B

Q1-Create an Android Application to create a List View of Fruits and


Display an Alert Dialog Box after an item is selected from the list.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/lv"
android:layout_width="409dp"
android:layout_height="729dp"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

JAVA
package com.example.listview;

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

import android.content.DialogInterface;
import android.os.Bundle; import
android.view.View; import
android.widget.AdapterView; import
android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


ListView lv; String
con[]={"mango","apple","banana","strawberry","lichi","pineapple","chikoo","
blueberries"}; @Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.lv);
ArrayAdapter<String> adp=new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1,con);
lv.setAdapter(adp);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i,
long id) {
AlertDialog.Builder a=new AlertDialog.Builder(MainActivity.this);
a.setTitle(con[i]);
a.setMessage("fruit list is:"+con[i]);
a.setNegativeButton("Okay", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
});
}
}

Q2-Create an Android Application to take one spinner and add a string


array of size to the Spinner and display Toast message of selected size.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/lv"
android:layout_width="409dp"
android:layout_height="729dp"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

JAVA
package com.example.spinner;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import


android.view.View; import
android.widget.AdapterView; import
android.widget.ArrayAdapter; import
android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


Spinner sp;
String size[]={"small","medium","large"};
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=findViewById(R.id.spinner);
ArrayAdapter<String> adp=new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,size);
sp.setAdapter(adp);
sp.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i,
long id) {
float ts;
switch (i)
{ case 0:
ts=14;
break; case 1:
ts=28;
break; case
2:ts=35;
break;
default:
ts=18;
}
Toast.makeText(MainActivity.this,"Size is"+ts
,Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}

Q3-Create an Android application that demonstrate Radio Button.


XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
tools:context=".MainActivity" >

<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Male" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
</LinearLayout>

JAVA
package com.example.radiogroup;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import


android.widget.RadioButton; import
android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


RadioGroup rg;
RadioButton r1,r2;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg=findViewById(R.id.radiogroup);
r1=findViewById(R.id.radioButton);
r2=findViewById(R.id.radioButton2);
rg.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(r1.isChecked()){
Toast.makeText(MainActivity.this, "Male is selected",
Toast.LENGTH_SHORT).show();
}
if ((r2.isChecked())) {
Toast.makeText(MainActivity.this, "Female is selected",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

Q4-Create an android application that accept number and find square


of the number using context view.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
tools:context=".MainActivity">

<EditText android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

JAVA
package com.example.contextmenu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import


android.view.ContextMenu; import
android.view.MenuItem; import
android.view.View; import
android.widget.Button; import
android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText t1;
Button btn;
@Override protected void onCreate(Bundle
savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=findViewById(R.id.t1);
btn=findViewById(R.id.button);
registerForContextMenu(btn);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo)
{ super.onCreateContextMenu(menu, v, menuInfo);
menu.add("Square");
}

@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
int a=Integer.parseInt(t1.getText().toString());
if ("Square".equals(item.getTitle())){ int
b=a*a;
Toast.makeText(MainActivity.this, "Square is"+b,
Toast.LENGTH_SHORT).show();
}
return super.onContextItemSelected(item);
}
}

Q5-Create an android application that accept number and swap of the


number using context view.

//XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
tools:context=".MainActivity" >

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="Name" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="Name" />

<Button android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

</LinearLayout>

//JAVA
package com.example.contextmenusqure;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import


android.view.ContextMenu; import
android.view.MenuItem; import
android.view.View; import
android.widget.Button; import
android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText t1,t2;
Button b1;

@Override protected void onCreate(Bundle


savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=findViewById(R.id.editText1);
t2=findViewById(R.id.editText2);
b1=findViewById(R.id.b1);
registerForContextMenu(b1);

}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo)
{ super.onCreateContextMenu(menu, v, menuInfo);
menu.add("Square");
menu.add("swap");
}

@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
int a= Integer.parseInt(t1.getText().toString());
int b= Integer.parseInt(t2.getText().toString());
if("Square".equals(item.getTitle())){ int b1=
a*a;
Toast.makeText(this, "Square is :"+b1,
Toast.LENGTH_SHORT).show();
}
if("swap".equals(item.getTitle())){
int temp; temp=a; a=b;
b=temp;
Toast.makeText(this, "Swaping of number1:a="+a+ "b="+b,
Toast.LENGTH_SHORT).show();
}

return super.onContextItemSelected(item);
}
}

You might also like