0% found this document useful (0 votes)
6 views46 pages

Mad Programs

The document provides instructions for creating Android applications that display personalized greetings and collect user input through various layouts. It includes Java code for MainActivity and XML layouts for Linear, Relative, and Grid layouts, allowing users to enter their details and view the results. The applications demonstrate basic Android UI components such as TextView, EditText, Button, RadioButton, DatePicker, and Spinner.

Uploaded by

sonusurepally
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)
6 views46 pages

Mad Programs

The document provides instructions for creating Android applications that display personalized greetings and collect user input through various layouts. It includes Java code for MainActivity and XML layouts for Linear, Relative, and Grid layouts, allowing users to enter their details and view the results. The applications demonstrate basic Android UI components such as TextView, EditText, Button, RadioButton, DatePicker, and Spinner.

Uploaded by

sonusurepally
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/ 46

1.

a) Create an Android application that shows Hello + name of the user and run
it on an emulator.
MainActivity.java
package com.example.hellouserapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Assuming you have a TextView with the id 'textView' in your layout
String username = "Sampath";
TextView textView = findViewById(R.id.textView);
textView.setText("Hello, " + username + "!");
}
}
activity_main.xml
<?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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="24sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
1b) Create an application that takes the name from a text box and shows hello
message along with the name entered in text box, when the user clicks the OK
button.
<?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">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"/>
<Button
android:id="@+id/buttonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_below="@id/editTextName"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:onClick="showHelloMessage"/>
<TextView
android:id="@+id/textViewMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="@id/buttonOK"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"/>
</RelativeLayout>
MainActivity.java
package com.example.hellonameapp;
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 {
private EditText editTextName;
private TextView textViewMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = findViewById(R.id.editTextName);
textViewMessage = findViewById(R.id.textViewMessage);
}
public void showHelloMessage(View view) {
String name = editTextName.getText().toString();
if (!name.isEmpty()) {
String helloMessage = "Hello, " + name + "!";
textViewMessage.setText(helloMessage);
}
}
}

2. Create a screen that has input boxes for User Name, Password, Address,
Gender (radio buttons for male and female), Age (numeric), Date of Birth (Date
Picket), State (Spinner) and a Submit button. On clicking the submit button,
print all the data below the Submit Button. Use (a) Linear Layout (b) Relative
Layout and (c) Grid Layout or Table Layout.(any one of the layouts).
Linear layout:
MainActivity.java
package com.example.userinputlayouts;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextUserName, editTextPassword, editTextAddress,
editTextAge;
private RadioButton radioButtonMale, radioButtonFemale;
private DatePicker datePicker;
private Spinner spinnerState;
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_linear);
editTextUserName = findViewById(R.id.editTextUserName);
editTextPassword = findViewById(R.id.editTextPassword);
editTextAddress = findViewById(R.id.editTextAddress);
editTextAge = findViewById(R.id.editTextAge);
radioButtonMale = findViewById(R.id.radioButtonMale);
radioButtonFemale = findViewById(R.id.radioButtonFemale);
datePicker = findViewById(R.id.datePicker);
spinnerState = findViewById(R.id.spinnerState);
textViewResult = findViewById(R.id.textViewResult);
setupSpinner();
}
private void setupSpinner() {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.states_array,
android.R.layout.simple_spinner_item
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdo
wn_
item);
spinnerState.setAdapter(adapter);
}
public void onSubmitClick(View view) {
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
String address = editTextAddress.getText().toString();
String age = editTextAge.getText().toString();
String gender = radioButtonMale.isChecked() ? "Male" : "Female";
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1; // Months are 0-indexed
int year = datePicker.getYear();
String dateOfBirth = String.format("%02d-%02d-%04d", day, month, year);
String state = spinnerState.getSelectedItem().toString();
String result = "User Name: " + userName +
"\nPassword: " + password +
"\nAddress: " + address +
"\nAge: " + age +
"\nGender: " + gender +
"\nDate of Birth: " + dateOfBirth +
"\nState: " + state;
textViewResult.setText(result);
}
}
res/values/strings.xml
<resources>
<string-array name="states_array">
<item>State 1</item>
<item>State 2</item>
<item>State 3</item>
<!-- Add more states as needed -->
</string-array>
</resources>
res/layout/activity_main_linear.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="16dp">
<EditText
android:id="@+id/editTextUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"/>
<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButtonMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>
<RadioButton
android:id="@+id/radioButtonFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
</RadioGroup>
<DatePicker
android:id="@+id/datePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Spinner
android:id="@+id/spinnerState"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="onSubmitClick"
android:layout_gravity="center"/>
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>
</LinearLayout>
b)Relative layout:
MainActivity.java
package com.example.userinputlayouts;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextUserName, editTextPassword, editTextAddress,
editTextAge;
private RadioButton radioButtonMale, radioButtonFemale;
private DatePicker datePicker;
private Spinner spinnerState;
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_relative);
editTextUserName = findViewById(R.id.editTextUserName);
editTextPassword = findViewById(R.id.editTextPassword);
editTextAddress = findViewById(R.id.editTextAddress);
editTextAge = findViewById(R.id.editTextAge);
radioButtonMale = findViewById(R.id.radioButtonMale);
radioButtonFemale = findViewById(R.id.radioButtonFemale);
datePicker = findViewById(R.id.datePicker);
spinnerState = findViewById(R.id.spinnerState);
textViewResult = findViewById(R.id.textViewResult);
setupSpinner();
}
private void setupSpinner() {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.states_array,
android.R.layout.simple_spinner_item
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdo
wn_
item);
spinnerState.setAdapter(adapter);
}
public void onSubmitClick(View view) {
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
String address = editTextAddress.getText().toString();
String age = editTextAge.getText().toString();
String gender = radioButtonMale.isChecked() ? "Male" : "Female";
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1; // Months are 0-indexed
int year = datePicker.getYear();
String dateOfBirth = String.format("%02d-%02d-%04d", day, month, year);
String state = spinnerState.getSelectedItem().toString();
String result = "User Name: " + userName +
"\nPassword: " + password +
"\nAddress: " + address +
"\nAge: " + age +
"\nGender: " + gender +
"\nDate of Birth: " + dateOfBirth +
"\nState: " + state;
textViewResult.setText(result);
}
}
res/layout/activity_main_relative.xml
<?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"
android:padding="16dp">
<EditText
android:id="@+id/editTextUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:layout_alignParentTop="true"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_below="@id/editTextUserName"
android:layout_marginTop="16dp"
android:inputType="textPassword"/>

<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:layout_below="@id/editTextPassword"
android:layout_marginTop="16dp"/>

<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:layout_below="@id/editTextAddress"
android:layout_marginTop="16dp"
android:inputType="number"/>

<RadioGroup
android:id="@+id/radioGroupGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/editTextAge"
android:layout_marginTop="16dp">

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

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

<DatePicker
android:id="@+id/datePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroupGender"
android:layout_marginTop="16dp"/>
<Spinner
android:id="@+id/spinnerState"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/datePicker"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="onSubmitClick"
android:layout_below="@id/spinnerState"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSubmit"
android:layout_marginTop="16dp"/>
</RelativeLayout>
c)Grid Layout:
MainActivity.java
package com.example.userinputlayouts;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextUserName, editTextPassword, editTextAddress,
editTextAge;
private RadioButton radioButtonMale, radioButtonFemale;
private DatePicker datePicker;
private Spinner spinnerState;
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_grid);
editTextUserName = findViewById(R.id.editTextUserName);
editTextPassword = findViewById(R.id.editTextPassword);
editTextAddress = findViewById(R.id.editTextAddress);
editTextAge = findViewById(R.id.editTextAge);
radioButtonMale = findViewById(R.id.radioButtonMale);
radioButtonFemale = findViewById(R.id.radioButtonFemale);
datePicker = findViewById(R.id.datePicker);
spinnerState = findViewById(R.id.spinnerState);
textViewResult = findViewById(R.id.textViewResult);
setupSpinner();
}
private void setupSpinner() {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.states_array,
android.R.layout.simple_spinner_item
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdo
wn_
item);
spinnerState.setAdapter(adapter);
}
public void onSubmitClick(View view) {
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
String address = editTextAddress.getText().toString();
String age = editTextAge.getText().toString();
String gender = radioButtonMale.isChecked() ? "Male" : "Female";
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1; // Months are 0-indexed
int year = datePicker.getYear();
String dateOfBirth = String.format("%02d-%02d-%04d", day, month, year);
String state = spinnerState.getSelectedItem().toString();
String result = "User Name: " + userName +
"\nPassword: " + password +
"\nAddress: " + address +
"\nAge: " + age +
"\nGender: " + gender +
"\nDate of Birth: " + dateOfBirth +
"\nState: " + state;
textViewResult.setText(result);
}
}
res/layout/activity_main_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="10"
android:padding="16dp">
<EditText
android:id="@+id/editTextUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:layout_columnSpan="2"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_columnSpan="2"
android:inputType="textPassword"/>

<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:layout_columnSpan="2"/>

<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:layout_columnSpan="2"
android:inputType="number"/>

<RadioGroup
android:id="@+id/radioGroupGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_columnSpan="2">

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

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

<DatePicker
android:id="@+id/datePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="2"/>

<Spinner
android:id="@+id/spinnerState"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="2"/>
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:onClick="onSubmitClick"
android:layout_gravity="center"/>
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="2"/>
</GridLayout>
3. Develop an application that shows names as a list and on selecting a name it
should show the details of the candidate on the next screen with a “Back”
button.
MainActivity.java:
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {

ListView nameListView;

String[] names = {"Candidate 1", "Candidate 2", "Candidate 3"};

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

nameListView = findViewById(R.id.nameListView);

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1, names);
nameListView.setAdapter(adapter);

nameListView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String selectedName = names[position];
String[] details = getCandidateDetails(selectedName);
Intent intent = new Intent(MainActivity.this,
CandidateDetailsActivity.class);
intent.putExtra("name", selectedName);
intent.putExtra("rollNo", details[0]);
intent.putExtra("age", details[1]);
intent.putExtra("experience", details[2]);
startActivity(intent);
}
});
}

// Method to get details of a candidate


private String[] getCandidateDetails(String name) {
String[] details = new String[3];
// Sample data - Replace with actual data retrieval logic
if(name.equals("Candidate 1")) {
details[0] = "101"; // Roll Number
details[1] = "25"; // Age
details[2] = "3 years"; // Experience
} else if (name.equals("Candidate 2")) {
details[0] = "102";
details[1] = "28";
details[2] = "5 years";
} else if (name.equals("Candidate 3")) {
details[0] = "103";
details[1] = "30";
details[2] = "7 years";
}
return details;
}
}
Candidatedetailsactivity.java:
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CandidateDetailsActivity extends AppCompatActivity {

TextView nameTextView, rollNoTextView, ageTextView, experienceTextView;


Button backButton;

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

nameTextView = findViewById(R.id.nameTextView);
rollNoTextView = findViewById(R.id.rollNoTextView);
ageTextView = findViewById(R.id.ageTextView);
experienceTextView = findViewById(R.id.experienceTextView);
backButton = findViewById(R.id.backButton);

Intent intent = getIntent();


if (intent != null) {
String name = intent.getStringExtra("name");
String rollNo = intent.getStringExtra("rollNo");
String age = intent.getStringExtra("age");
String experience = intent.getStringExtra("experience");

nameTextView.setText(name);
rollNoTextView.setText("Roll No: " + rollNo);
ageTextView.setText("Age: " + age);
experienceTextView.setText("Experience: " + experience);
}

backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
Activity_main.xml:
<?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">

<ListView
android:id="@+id/nameListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp" />

</RelativeLayout>
Activity_candidatedetails:
<?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">

<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"/>

<TextView
android:id="@+id/rollNoTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/nameTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>

<TextView
android:id="@+id/ageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rollNoTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>

<TextView
android:id="@+id/experienceTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ageTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>

<Button
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/experienceTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Back"/>

</RelativeLayout>

4. Develop an application that uses a menu with 3 options for dialing a number,
opening a website and to send an SMS. On selecting an option, the appropriate
action should be invoked using intents.
package com.example.myapplication;

import android.os.Bundle;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {

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

Button dialButton = findViewById(R.id.dial_button);


Button websiteButton = findViewById(R.id.website_button);
Button smsButton = findViewById(R.id.sms_button);

dialButton.setOnClickListener(this);
websiteButton.setOnClickListener(this);
smsButton.setOnClickListener(this);
}

public void onClick(View v) {


int id = v.getId();
if (id == R.id.dial_button) {
// Dial a number
Intent dialIntent = new Intent(Intent.ACTION_DIAL);
dialIntent.setData(Uri.parse("tel:123456789")); // Replace with your
phone number
startActivity(dialIntent);
} else if (id == R.id.website_button) {
// Open a website
Intent websiteIntent = new Intent(Intent.ACTION_VIEW);
websiteIntent.setData(Uri.parse("https://www.example.com")); //
Replace with your website URL
startActivity(websiteIntent);
} else if (id == R.id.sms_button) {
// Send an SMS
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse("smsto:")); // This ensures only SMS apps
respond
smsIntent.putExtra("sms_body", "Hello, this is a test message!"); //
Replace with your message
startActivity(smsIntent);
}
}

}
<?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:gravity="center"
android:padding="16dp"
tools:context=".MainActivity">

<Button
android:id="@+id/dial_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dial a Number" />
<Button
android:id="@+id/website_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Website" />

<Button
android:id="@+id/sms_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS" />

</LinearLayout>

Manifests:
<uses-permission
android:name="android.permission.CALL_PHONE" />
<uses-permission
android:name="android.permission.SEND_SMS" />

5. Develop an application to implement broadcast receivers.


Mainactivity.java:
package com.example.myapplication;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

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

// Broadcast a custom intent.


public void broadcastIntent(View view){
Intent intent = new Intent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
intent.setComponent(new ComponentName(getPackageName(),
MyReceiver.class.getName())); // Set the component explicitly
sendBroadcast(intent);
}

}
Myreceiver.java:
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
* Created by TutorialsPoint7 on 8/23/2016.
*/
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
}
}
Activity_main.xml:
<?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">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of Broadcast"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Broadcast Intent"
android:onClick="broadcastIntent"
android:layout_below="@id/textView1"
android:layout_centerHorizontal="true" />

</RelativeLayout>
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<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>
<receiver android:name="MyReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
</application>

</manifest>
6.Create an application that uses a text file to store user names and passwords
(tab separated fields and one record per line). When the user submits a login
name and password through a screen, the details should be verified with the
text file data and if they match, show a dialog saying that login is successful.
Otherwise, show the dialog with Login Failed message.
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {


EditText editTextUsername, editTextPassword;
Button buttonLogin;

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

editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);

buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();

if (validateCredentials(username, password)) {
Toast.makeText(MainActivity.this, "Login Successful!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Login Failed. Please check your
username and password.", Toast.LENGTH_SHORT).show();
}
}
});
}

private boolean validateCredentials(String username, String password) {


try {
InputStream inputStream =
getResources().getAssets().open("user_credentials.txt");
BufferedReader reader = new BufferedReader(new
InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\t");
if (parts.length == 2) {
String storedUsername = parts[0];
String storedPassword = parts[1];
if (storedUsername.equals(username) &&
storedPassword.equals(password)) {
reader.close();
return true;
}
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
<?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">
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginTop="50dp"
android:layout_marginHorizontal="20dp"/>

<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_below="@id/editTextUsername"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="20dp"/>

<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@id/editTextPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
</RelativeLayout>

7. Create a user registration application that stores the user details in a


database table.
Mainactivity.java:
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText userNameEdt, userEmailEdt, userPasswordEdt;


private Button registerBtn;
private DBHandler dbHandler;

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

userNameEdt = findViewById(R.id.idEdtUserName);
userEmailEdt = findViewById(R.id.idEdtUserEmail);
userPasswordEdt = findViewById(R.id.idEdtUserPassword);
registerBtn = findViewById(R.id.idBtnRegister);

dbHandler = new DBHandler(MainActivity.this);

registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = userNameEdt.getText().toString();
String userEmail = userEmailEdt.getText().toString();
String userPassword = userPasswordEdt.getText().toString();

if (userName.isEmpty() || userEmail.isEmpty() ||
userPassword.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter all the data..",
Toast.LENGTH_SHORT).show();
return;
}

dbHandler.addNewUser(userName, userEmail, userPassword);

Toast.makeText(MainActivity.this, "User registered successfully.",


Toast.LENGTH_SHORT).show();
userNameEdt.setText("");
userEmailEdt.setText("");
userPasswordEdt.setText("");
}
});
}
}
Activity_mainxml:
<?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"
tools:context=".MainActivity">

<EditText
android:id="@+id/idEdtUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Username" />

<EditText
android:id="@+id/idEdtUserEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Email" />
<EditText
android:id="@+id/idEdtUserPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Password"
android:inputType="textPassword" />

<Button
android:id="@+id/idBtnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Register"
android:textAllCaps="false" />

</LinearLayout>
DBHandler.java:
package com.example.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHandler extends SQLiteOpenHelper {

private static final String DB_NAME = "userdb";


private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "users";
private static final String ID_COL = "id";
private static final String NAME_COL = "name";
private static final String EMAIL_COL = "email";
private static final String PASSWORD_COL = "password";

public DBHandler(Context context) {


super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME + " ("
+ ID_COL + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAME_COL + " TEXT,"
+ EMAIL_COL + " TEXT,"
+ PASSWORD_COL + " TEXT)";

db.execSQL(query);
}

public void addNewUser(String userName, String userEmail, String


userPassword) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME_COL, userName);
values.put(EMAIL_COL, userEmail);
values.put(PASSWORD_COL, userPassword);
db.insert(TABLE_NAME, null, values);
db.close();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}

You might also like