Mad Programs
Mad Programs
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameListView = findViewById(R.id.nameListView);
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);
}
});
}
@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);
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialButton.setOnClickListener(this);
websiteButton.setOnClickListener(this);
smsButton.setOnClickListener(this);
}
}
<?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" />
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
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" />
</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;
@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();
}
}
});
}
<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>
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;
@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);
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;
}
<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 {
@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);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}