0% found this document useful (0 votes)
13 views16 pages

Mep Java

Uploaded by

recec59667
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)
13 views16 pages

Mep Java

Uploaded by

recec59667
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/ 16

3.

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;

public class MainActivity extends AppCompatActivity {


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

EditText name = findViewById(R.id.Name);


Button submit = findViewById(R.id.submit);
TextView output = findViewById(R.id.result);

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;

public class MainActivity extends AppCompatActivity {


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

EditText userName = findViewById(R.id.userName);


EditText passw = findViewById(R.id.passw);
EditText addr = findViewById(R.id.addr);
EditText age = findViewById(R.id.age);
EditText dob = findViewById(R.id.dob);
Spinner stateSpinner = findViewById(R.id.stateSpinner);
Button sub = findViewById(R.id.sub);
RadioGroup gender = findViewById(R.id.gender);
TextView printer = findViewById(R.id.printer);

// 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);

DatePickerDialog datePickerDialog = new DatePickerDialog(


MainActivity.this,
(view, selectedYear, selectedMonth, selectedDay) ->
dob.setText(selectedDay + "/" + (selectedMonth + 1) + "/" +
selectedYear),
year,
month,
day
);
datePickerDialog.show();
});

sub.setOnClickListener(v -> {
String selectedGender;
if (gender.getCheckedRadioButtonId() == R.id.ma) {
selectedGender = "Male";
} else if (gender.getCheckedRadioButtonId() == R.id.fe) {
selectedGender = "Female";
} else {
selectedGender = "";
}

String selectedState = stateSpinner.getSelectedItem().toString();

printer.setText("Name: " + userName.getText().toString() + "\n" +


"Password: " + passw.getText().toString() + "\n" +
"Address: " + addr.getText().toString() + "\n" +
"State: " + selectedState + "\n" +
"Gender: " + selectedGender + "\n" +
"Age: " + age.getText().toString() + "\n" +
"Date of Birth: " + dob.getText().toString() + "\n");
});
}
}
5. Design an android application to create page using Intent and one Button and
pass the Values from one Activity to second Activity.

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;

public class MainActivity extends AppCompatActivity {


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

EditText editTextName = findViewById(R.id.editTextName);


Button buttonSend = findViewById(R.id.buttonSend);

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;

public class SecondActivity extends AppCompatActivity {


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

TextView textViewName = findViewById(R.id.textViewName);


String name = getIntent().getStringExtra("EXTRA_NAME");
textViewName.setText(name);
}
}
AndroidManifest.xml

Add this line


<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>
//paina code already aa file lo untadi just kinda 3 lines ni copy chesi paina unna code kinda paste cheyandi
<activity
android:name=".SecondActivity">
</activity>
6. Design an android application Send SMS using Intent

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

Add these lines


<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-feature android:name="android.hardware.telephony" />
7. Create an android application using Fragments

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;

public class MainActivity extends AppCompatActivity {


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

Button firstButton = findViewById(R.id.firstButton);


Button secondButton = findViewById(R.id.secondButton);

firstButton.setOnClickListener(v -> {
replaceFragment(new FirstFragment());
});

secondButton.setOnClickListener(v -> {
replaceFragment(new SecondFragment());
});
}

private void replaceFragment(Fragment fragment) {


getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
}
FirstFragment.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;

public class FirstFragment extends Fragment {


@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
}

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;

public class SecondFragment extends Fragment {


@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container,
false);
}
}
8. Design an android application Using Radio buttons

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;

public class MainActivity extends AppCompatActivity {


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

RadioGroup gender = findViewById(R.id.radioGroup);


Button sub = findViewById(R.id.buttonSubmit);
TextView res = findViewById(R.id.res);

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";
}

res.setText("Selected : " + selected);


});
}
}

You might also like