Mad Programs
Mad Programs
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Java file :
package com.example.simpletoast;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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);
Button button = findViewById(R.id.buttonToast);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Hello, this is a toast!", Toast.LENGTH_SHORT).show();
}
});
}
}
2.Develop a program to create an activity using Log.d
What Is Log.d() in Android?
Log.d() is used to print debug messages to the Logcat window in Android Studio.
It's very useful when you're developing apps and want to check what's happening in your code — like
when a button is clicked or when an activity starts.
XML file:
<?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:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/logButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
</LinearLayout>
Java file :
package com.example.logdemo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Activity created"); // Log when activity is created
3.Develop a program to implement new activity using explicit intent and implicit intent.
xml file
<?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:id="@+id/main1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start Dialer" />
</LinearLayout>
Java file :
package com.example.exp3;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{
Button b1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.btn1);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("Tel :"));
startActivity(i);
}
});
}
}
<AutoCompleteTextView
android:id="@+id/a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Select Subject"
android:completionThreshold="1"/>
</RelativeLayout>
Java file :
package com.example.pdf1;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
Xml file :
<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton 1"
android:checked="false"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton 2"
android:checked="false"
android:textSize="20dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Button inside RadioGroup"
android:textSize="20dp"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Female"
android:textSize="20dp"/>
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Male"
android:textSize="20dp"/>
<Button
android:id="@+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="20dp"/>
</RadioGroup>
</LinearLayout>
Java file :
package com.example.pdf1;
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;
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selected=rg.getCheckedRadioButtonId();
if(selected!=1)
{
rb=findViewById(selected);
Toast.makeText(getApplicationContext(),rb.getText(),Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"Please select an
Option",Toast.LENGTH_LONG).show();
}
}
});
}
}
Xml file :
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAD"
android:layout_marginTop="30dp"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MGT"
android:layout_marginTop="60dp"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WBP"
android:layout_marginTop="90dp"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PWP"
android:layout_marginTop="120dp"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ETI"
android:layout_marginTop="150dp"
android:textSize="20dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:layout_marginTop="200dp"
android:textSize="20dp"/>
</RelativeLayout>
Java file :
package com.example.exp10;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
Button b1;
String s;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c1=(CheckBox) findViewById(R.id.checkBox1);
c2=(CheckBox) findViewById(R.id.checkBox2);
c3=(CheckBox) findViewById(R.id.checkBox3);
c4=(CheckBox) findViewById(R.id.checkBox4);
c5=(CheckBox) findViewById(R.id.checkBox5);
b1=(Button) findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
s=" ";
if(c1.isChecked())
s=s+c1.getText().toString()+"\n";
if(c2.isChecked())
s=s+c2.getText().toString()+"\n";
if(c3.isChecked())
s=s+c3.getText().toString()+"\n";
if(c4.isChecked())
s=s+c4.getText().toString()+"\n";
if(c5.isChecked())
s=s+c5.getText().toString()+"\n";
}
Toast.makeText(getApplicationContext(),"Selected Subjects : \
n"+s,Toast.LENGTH_LONG).show();
});
<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:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Java file :
package com.example.exp11;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
ListView l;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView) findViewById(R.id.listView);
ArrayAdapter adapter=new
ArrayAdapter<String>(this,androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,city);
l.setAdapter(adapter);
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
Toast.makeText(MainActivity.this,((TextView)view).getText(),Toast.LENGTH_LONG).show();
});
<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:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show" />
</LinearLayout>
Java file :
package com.example.exp12;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
Button b;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
p=findViewById(R.id.progressBar);
b=findViewById(R.id.button);
b.setOnClickListener(View::invalidate);
p.setVisibility(View.VISIBLE);
<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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Bluetooth is OFF"
android:textOn="Bluetooth is ON"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:layout_marginTop="60sp"/>
</RelativeLayout>
Java file :
package com.example.exp13;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
ToggleButton t;
Button btn;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(ToggleButton) findViewById(R.id.toggleButton);
btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
if(t.isChecked())
Toast.makeText(getApplicationContext(),"Bluetooth is ON",Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(),"Bluetooth is OFF",Toast.LENGTH_LONG).show();
}
});
<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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridView
android:id="@+id/g"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Java file :
package com.example.exp14;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;
import android.widget.GridView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.view.View;
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
GridView gv=findViewById(R.id.g);
for(int i=0;i<15;i++)
ArrayAdapter<String>adapter=new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,labels);
gv.setAdapter(adapter);
gv.setOnItemClickListener(new AdapterView.OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String buttonlabel=((String)parent.getItemAtPosition(position));
Toast.makeText(MainActivity.this,buttonlabel+"Clicked",Toast.LENGTH_LONG).show();
});
}
1)Write a program to display following output using suitable layout.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#e0f0ff"
android:padding="16dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Row1"
android:background="#007bff"
android:textColor="#fff"
android:layout_marginBottom="16dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginBottom="16dp">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Row2\nCol1"
android:background="#007bff"
android:textColor="#fff"
android:layout_marginEnd="8dp"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Row2\nCol2"
android:background="#007bff"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:layout_marginBottom="16dp">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Row3\nCol1"
android:background="#007bff"
android:textColor="#fff"
android:layout_marginEnd="8dp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Row3\nCol2"
android:background="#007bff"
android:textColor="#fff"
android:layout_marginEnd="8dp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Row3\nCol3"
android:background="#007bff"
android:textColor="#fff" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Row4"
android:background="#007bff"
android:textColor="#fff" />
</LinearLayout>
</ScrollView>
2) Develop the registration form using the following GUI.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white"
android:padding="20dp"
android:gravity="center_horizontal"
android:elevation="4dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginHorizontal="8dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/sample_image"
android:layout_gravity="center"
android:layout_marginBottom="24dp"
android:scaleType="centerCrop" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:padding="12dp"
android:layout_marginBottom="12dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email ID"
android:inputType="textEmailAddress"
android:padding="12dp"
android:layout_marginBottom="12dp" />
<!-- Password -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="12dp"
android:layout_marginBottom="12dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Confirm password"
android:inputType="textPassword"
android:padding="12dp"
android:layout_marginBottom="12dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Mobile"
android:inputType="phone"
android:padding="12dp"
android:layout_marginBottom="12dp" />
<!-- Gender -->
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="20dp">
<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"
android:layout_marginStart="20dp" />
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:backgroundTint="#03A9F4"
android:textColor="#ffffff" />
</LinearLayout>
</ScrollView>