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="[Link]
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>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
EditText name = findViewById([Link]);
Button submit = findViewById([Link]);
TextView output = findViewById([Link]);
[Link](v -> {
[Link]("Hello " + [Link]().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="[Link]
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>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
EditText userName = findViewById([Link]);
EditText passw = findViewById([Link]);
EditText addr = findViewById([Link]);
EditText age = findViewById([Link]);
EditText dob = findViewById([Link]);
Spinner stateSpinner = findViewById([Link]);
Button sub = findViewById([Link]);
RadioGroup gender = findViewById([Link]);
TextView printer = findViewById([Link]);
// State spinner
String[] states = {"state-A", "AP", "Bihar"};
ArrayAdapter<String> stateAdapter = new ArrayAdapter<>(this,
[Link].simple_spinner_item, states);
[Link]([Link].simple_spinner_dropdown_item);
[Link](stateAdapter);
// DatePickerDialog
[Link](v -> {
Calendar calendar = [Link]();
int year = [Link]([Link]);
int month = [Link]([Link]);
int day = [Link](Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(
[Link],
(view, selectedYear, selectedMonth, selectedDay) ->
[Link](selectedDay + "/" + (selectedMonth + 1) + "/" +
selectedYear),
year,
month,
day
);
[Link]();
});
[Link](v -> {
String selectedGender;
if ([Link]() == [Link]) {
selectedGender = "Male";
} else if ([Link]() == [Link]) {
selectedGender = "Female";
} else {
selectedGender = "";
}
String selectedState = [Link]().toString();
[Link]("Name: " + [Link]().toString() + "\n" +
"Password: " + [Link]().toString() + "\n" +
"Address: " + [Link]().toString() + "\n" +
"State: " + selectedState + "\n" +
"Gender: " + selectedGender + "\n" +
"Age: " + [Link]().toString() + "\n" +
"Date of Birth: " + [Link]().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="[Link]
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="[Link]
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>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
EditText editTextName = findViewById([Link]);
Button buttonSend = findViewById([Link]);
[Link](v -> {
String name = [Link]().toString();
Intent intent = new Intent([Link],
[Link]);
[Link]("EXTRA_NAME", name);
startActivity(intent);
});
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
TextView textViewName = findViewById([Link]);
String name = getIntent().getStringExtra("EXTRA_NAME");
[Link](name);
}
}
[Link]
Add this line
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</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="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
send = findViewById([Link]);
phoneNumber = findViewById([Link]);
message = findViewById([Link].editText2);
[Link](v -> {
String number = [Link]().toString();
String msg = [Link]().toString();
if (![Link]() && ![Link]()) {
if ([Link](this,
[Link].SEND_SMS) ==
PackageManager.PERMISSION_GRANTED) {
sendSMS(number, msg);
} else {
[Link](this,
new String[]{[Link].SEND_SMS},
SMS_PERMISSION_CODE);
}
} else {
[Link](this, "Please enter both phone number and message",
Toast.LENGTH_SHORT).show();
}
});
}
private void sendSMS(String phone, String message) {
try {
SmsManager smsManager = [Link]();
[Link](phone, null, message,
null, null);
[Link](getApplicationContext(), "Message Sent", Toast.LENGTH_LONG).show();
} catch (Exception e) {
[Link](getApplicationContext(), "Failed to send message",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) {
[Link](requestCode,
permissions, grantResults);
if (requestCode == SMS_PERMISSION_CODE) {
if ([Link] > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
String number =
[Link]().toString();
String msg = [Link]().toString();
sendSMS(number, msg);
} else {
[Link](this, "SMS permission denied",
Toast.LENGTH_SHORT).show();
}
}
}
}
[Link]
Add these lines
<uses-permission android:name="[Link].SEND_SMS"/>
<uses-feature android:name="[Link]" />
7. Create an android application using Fragments
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:tools="[Link]
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="[Link]
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="[Link]
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>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Button firstButton = findViewById([Link]);
Button secondButton = findViewById([Link]);
[Link](v -> {
replaceFragment(new FirstFragment());
});
[Link](v -> {
replaceFragment(new SecondFragment());
});
}
private void replaceFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.replace([Link], fragment)
.commit();
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class FirstFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return [Link]([Link].fragment_first, container, false);
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SecondFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return [Link]([Link].fragment_second, container,
false);
}
}
8. Design an android application Using Radio buttons
activity_main.xml
<!-- activity_main.xml -->
<LinearLayout
xmlns:android="[Link]
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>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
RadioGroup gender = findViewById([Link]);
Button sub = findViewById([Link]);
TextView res = findViewById([Link]);
[Link](v -> {
int selectedId = [Link]();
String selected;
if (selectedId == [Link].radioButton1) {
selected = "option1";
} else if (selectedId == [Link].radioButton2) {
selected = "option2";
} else if (selectedId == [Link].radioButton3) {
selected = "option3";
} else {
selected = "Nothing";
}
[Link]("Selected : " + selected);
});
}
}