Mep Java
Mep Java
Mep Java
Create an application that takes the name from a textbox and shows hello
message along with the name entered in text box, when the user clicks the OK
button.
activity_main.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:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Name"
android:textSize="22sp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:id="@+id/Name">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="submit"
android:id="@+id/submit">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:id="@+id/result">
</TextView>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
submit.setOnClickListener(v -> {
output.setText("Hello " + name.getText().toString());
});
}
}
4. Create a screen that has input boxes for Username, Password, and Address,
Gender (radio buttons for male and female), Age (numeric), Date of Birth (Date
Picker), State (Spinner) and a Submit button. On clicking the submit button, print
all the data below the Submit Button (use any layout)
activity_main.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:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UserName:"
android:textSize="22sp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter UserName"
android:id="@+id/userName">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password:"
android:textSize="22sp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter password"
android:id="@+id/passw"
android:inputType="textPassword">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Address:"
android:textSize="22sp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Adrdress"
android:id="@+id/addr">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gender:"
android:textSize="22sp">
</TextView>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/gender">
<RadioButton
android:id="@+id/ma"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Male"
android:textSize="18sp"/>
<RadioButton
android:id="@+id/fe"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Female"
android:textSize="18sp"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Date of Birth:"
android:textSize="22sp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Select Date of Birth"
android:focusable="false"
android:id="@+id/dob">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="15px">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Age"
android:textSize="22sp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number"
android:id="@+id/age">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="State:"
android:textSize="22sp">
</TextView>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/stateSpinner">
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="18sp"
android:textColor="@color/white"
android:id="@+id/sub">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:id="@+id/printer">
</TextView>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
// State spinner
String[] states = {"state-A", "AP", "Bihar"};
ArrayAdapter<String> stateAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, states);
stateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
stateSpinner.setAdapter(stateAdapter);
// DatePickerDialog
dob.setOnClickListener(v -> {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
sub.setOnClickListener(v -> {
String selectedGender;
if (gender.getCheckedRadioButtonId() == R.id.ma) {
selectedGender = "Male";
} else if (gender.getCheckedRadioButtonId() == R.id.fe) {
selectedGender = "Female";
} else {
selectedGender = "";
}
activity_main.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"
android:fitsSystemWindows="true">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />
<Button
android:id="@+id/buttonSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
activity_second.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"
android:fitsSystemWindows="true">
<TextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
buttonSend.setOnClickListener(v -> {
String name = editTextName.getText().toString();
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
intent.putExtra("EXTRA_NAME", name);
startActivity(intent);
});
}
}
SecondActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_marginTop="140dp"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter number"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter message"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:text="SEND" />
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private EditText phoneNumber;
private EditText message;
private Button send;
private static final int SMS_PERMISSION_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = findViewById(R.id.button);
phoneNumber = findViewById(R.id.editText);
message = findViewById(R.id.editText2);
send.setOnClickListener(v -> {
String number = phoneNumber.getText().toString();
String msg = message.getText().toString();
if (!number.isEmpty() && !msg.isEmpty()) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS) ==
PackageManager.PERMISSION_GRANTED) {
sendSMS(number, msg);
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
SMS_PERMISSION_CODE);
}
} else {
Toast.makeText(this, "Please enter both phone number and message",
Toast.LENGTH_SHORT).show();
}
});
}
private void sendSMS(String phone, String message) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phone, null, message,
null, null);
Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Failed to send message",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) {
super.onRequestPermissionsResult(requestCode,
permissions, grantResults);
if (requestCode == SMS_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
String number =
phoneNumber.getText().toString();
String msg = message.getText().toString();
sendSMS(number, msg);
} else {
Toast.makeText(this, "SMS permission denied",
Toast.LENGTH_SHORT).show();
}
}
}
}
AndroidManifest.xml
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">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/buttonGroup" />
<LinearLayout
android:id="@+id/buttonGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/firstButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="First Fragment" />
<Button
android:id="@+id/secondButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Second Fragment" />
</LinearLayout>
</RelativeLayout>
fragment_first.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:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Fragment"
android:textSize="24sp"/>
</LinearLayout>
fragment_second.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:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Fragment"
android:textSize="24sp"/>
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import androidx.fragment.app.Fragment;
firstButton.setOnClickListener(v -> {
replaceFragment(new FirstFragment());
});
secondButton.setOnClickListener(v -> {
replaceFragment(new SecondFragment());
});
}
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
SecondFragment.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
activity_main.xml
<!-- activity_main.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Submit" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:layout_centerHorizontal="true"
android:id="@+id/res">
</TextView>
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
sub.setOnClickListener(v -> {
int selectedId = gender.getCheckedRadioButtonId();
String selected;
if (selectedId == R.id.radioButton1) {
selected = "option1";
} else if (selectedId == R.id.radioButton2) {
selected = "option2";
} else if (selectedId == R.id.radioButton3) {
selected = "option3";
} else {
selected = "Nothing";
}