0% found this document useful (0 votes)
37 views20 pages

MAD QB Program

The document contains code and XML layouts for Android activities. It includes examples of using toggle buttons, radio buttons, progress bars and calculating age from birthdate. The code demonstrates how to set onClick listeners and find views by ID.

Uploaded by

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

MAD QB Program

The document contains code and XML layouts for Android activities. It includes examples of using toggle buttons, radio buttons, progress bars and calculating age from birthdate. The code demonstrates how to set onClick listeners and find views by ID.

Uploaded by

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

Chapter 4

Q 2.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth: "
android:textSize="30dp" />
<ToggleButton
android:id="@+id/tb"
android:layout_toRightOf="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON" />
</RelativeLayout>
package com.example.exp_9;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
ToggleButton T;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
T=findViewById(R.id.tb);
T.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
if(T.isChecked())
{
Toast.makeText(MainActivity.this,"BLUETOOTH IS
ON",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this,"BLUETOOTH IS
OFF",Toast.LENGTH_LONG);
}
}
});
}
}
Q 5.
<?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:layout_gravity="center"
android:gravity="center">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Single Radio Button"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Radio Button 1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="Radio Button 2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Radio Button 3" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Radio Button inside RadioGroup"
android:textSize="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
<RadioGroup
android:id="@+id/radiogroup"
android:checkedButton="@+id/radioButton3"
android:layout_width="96dp"
android:layout_height="99dp"
android:layout_marginTop="64dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="Show Selected"
/>
</LinearLayout>
package com.example.exp11;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup rg;
RadioButton r1,r2,r3;
Button b;
String str="";
String gender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg = findViewById(R.id.radiogroup);
b = findViewById(R.id.button);
r1=findViewById(R.id.radioButton1);
r2=findViewById(R.id.radioButton2);
r3=findViewById(R.id.radioButton3);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
int checkedRadioButtonId = rg.getCheckedRadioButtonId();
if(r1.isChecked())
str=str+r1.getText().toString()+",";
if(r2.isChecked())
str=str+r2.getText().toString();
if(r3.isChecked())
str=str+r3.getText().toString();
if (checkedRadioButtonId != -1){
RadioButton selectedRadioButton =findViewById(checkedRadioButtonId);
gender = selectedRadioButton.getText().toString();
str=str+","+gender;
}
Toast.makeText(MainActivity.this,"Selected
Option:"+str,Toast.LENGTH_LONG).show();
}
}); }
}
Q 6.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/pb"
android:visibility="invisible"
android:layout_width="87dp"
android:layout_height="80dp"
android:layout_below="@+id/button"
android:layout_alignParentEnd="true"
android:layout_gravity="center"
android:layout_marginTop="87dp"
android:layout_marginEnd="170dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="109dp"
android:layout_marginTop="43dp"
android:layout_marginEnd="134dp"
android:text="Start The Progress" />
</RelativeLayout>
package com.example.exp11;
import static android.view.View.VISIBLE;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends
AppCompatActivity {
Button b;
ProgressBar pb;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=findViewById(R.id.button);
pb=findViewById(R.id.pb);
b.setOnClickListener(new
View.OnClickListener(){
public void onClick(View view)
{
pb.setVisibility(VISIBLE);
}
});
}
}
Q 10.

<?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:layout_gravity="center"
android:gravity="center">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Date of Birth: " />
<EditText
android:id="@+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Current Date" />
<EditText
android:id="@+id/edittext2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="Calculate"/>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Your Age is:" />

</LinearLayout>
package com.example.exp11;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
Button b;
TextView t1, t2, t3;
EditText e1, e2;
String birth, current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = findViewById(R.id.button);
t1 = findViewById(R.id.textView1);
t2 = findViewById(R.id.textView2);
e1 = findViewById(R.id.edittext1);
e2 = findViewById(R.id.edittext2);
t3 = findViewById(R.id.textView3);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
birth = e1.getText().toString();
current = e2.getText().toString();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try
{
Date birthDate = format.parse(birth);
Date currentDate = format.parse(current);
long result = currentDate.getTime() - birthDate.getTime();
long days = result / (1000 * 60 * 60 * 24);
long years = days / 365;
String str = "Your Age is: " +years+" Years";
t3.setText(str);
}
catch(Exception e)
{
Toast.makeText(MainActivity.this, "Invalid date format. Please use
YYYY-MM-DD", Toast.LENGTH_SHORT).show();
}
}
});
}
Q 13.

<?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:layout_gravity="center"
android:gravity="center">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="Display"/>
</LinearLayout>
package com.example.exp11;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
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) {
Toast.makeText(MainActivity.this,"Good
Morning",Toast.LENGTH_SHORT).show();
}
});
}
}
Chapter 5

Q4
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Start Dialer" />
</RelativeLayout>

package com.example.exp18_2;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
Button b;
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
b=findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View view)
{
i=new Intent(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:"+"9529592099"));
startActivity(i);
}
});
}
}
Q8
<?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:padding="40dp"
android:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CAMERA"
android:id="@+id/text"
android:textSize="20dp"
android:gravity="center"/>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_marginTop="81dp"
android:src="@drawable/lion"/>
<Button
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="TAKE PHOTO" />
</RelativeLayout>
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
package com.example.camera;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button b1;
ImageView imageView;
int CAMERA_REQUEST=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.photo);
imageView=findViewById(R.id.image);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,CAMERA_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==CAMERA_REQUEST)
{
Bitmap image= (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(image);}
}
}
Q.9.

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

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/sensorListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
package com.example.senser_list;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView sensorListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorListView = findViewById(R.id.sensorListView);
// Get the sensor manager
SensorManager sensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
// Get the list of available sensors
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
// Extract sensor names
List<String> sensorNames = new ArrayList<>();
for (Sensor sensor : sensorList) {
sensorNames.add(sensor.getName());
}
// Create adapter for ListView
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
sensorNames);
// Set adapter to ListView
sensorListView.setAdapter(adapter);
}
}
Capter 5

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:hint="Mobile No" />
<EditText
android:id="@+id/edittext2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:hint="Message" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
package com.example.send_sms;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
EditText e1,e2;
Button b1;
e1=findViewById(R.id.edittext1);
e2=findViewById(R.id.edittext2);
b1=findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String no=e1.getText().toString();
String msg=e2.getText().toString();
Intent i=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pi= PendingIntent.getActivity(getApplicationContext(),0,i,0);
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(no,null,msg,pi,null);
Toast.makeText(MainActivity.this,"Message Sent
Sucessfully",Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like