Skip to main content

Android

S1Q1


Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:layout_height="match_parent"
 tools:context=".MainActivity"/>

//main.java

package com.example.activitylifecycle;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity{
 private static final String TAG = "LifecycleActivity";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 }
 protected void onStart() {
 super.onStart();
 Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
 
 }
protected void onResume() {
 super.onResume();
 Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
 }
protected void onPause() {
 super.onPause();
 Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
 }
protected void onStop() {
 super.onStop();
 Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show();
 }
protected void onDestroy() {
 super.onDestroy();
 Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
 }


S2Q1
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_height="match_parent"
 tools:context=".MainActivity">
 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintVertical_bias="0.5">
 <EditText
 android:id="@+id/editText"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:hint="Enter A number"
 android:inputType="number" />
 <Button
 android:id="@+id/button"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

//main.java
package com.example.s2q1;
import android.content.Intent;
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;
public class MainActivity extends AppCompatActivity {
 private EditText editText;
 private Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 editText = findViewById(R.id.editText);
 button = findViewById(R.id.button);

 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 String input = editText.getText().toString();

 if(input.isEmpty()){
 Toast.makeText(MainActivity.this, "Enter a Number",
Toast.LENGTH_SHORT).show();
 }

 int number = Integer.parseInt(input);
 if(number <= 0){
 Toast.makeText(MainActivity.this, "Enter a positive number ",
Toast.LENGTH_SHORT).show();
 }
 int factorial =1;
 for(int i=1;i<number;i++){
 factorial = factorial*i;
 }
 Intent intent = new Intent(MainActivity.this,MainActivity2.class);
 intent.putExtra("factorial",factorial);
 startActivity(intent);
 }
 });
 }
}
// Activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_height="match_parent"
 tools:context=".MainActivity2">
 <TextView
 android:id="@+id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="24sp"
 android:textStyle="bold"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
//main2.java
package com.example.s2q1;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity2 extends AppCompatActivity {
 private TextView textView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main2);
 textView = findViewById(R.id.textView);
 int factorial = getIntent().getIntExtra("factorial",0);
 textView.setText("factorial "+factorial);
 }
}

S3Q1 :-

Main.java
package com.example.s3q1;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 textView = findViewById(R.id.textView);
 button = findViewById(R.id.button);
 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 int color = Color.parseColor("#ff0000");
 textView.setTextColor(color);
 textView.setTextSize(45);
 textView.setTypeface(null,Typeface.BOLD_ITALIC);
 }
});
 }
}
//xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/main_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity" >
 <TextView
 android:id="@+id/textView"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="KTHM"
 android:textSize="24sp" />
 <Button
 android:id="@+id/button"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
</LinearLayout>

S5Q1 :-
Xml 1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="16dp"
 tools:context=".MainActivity">
 <EditText
 android:id="@+id/num1_edittext"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:hint="Enter Number 1"
 android:inputType="numberDecimal"
 android:padding="8dp" />
 <EditText
 android:id="@+id/num2_edittext"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/num1_edittext"
 android:hint="Enter Number 2"
 android:inputType="numberDecimal"
 android:padding="8dp" />
<Button
 android:id="@+id/button"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@id/num2_edittext"
 android:text="Calculate"
 android:padding="16dp"/>
</RelativeLayout>
Java 1
package com.example.s5q1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
 // Declare variables for views
 private EditText num1EditText, num2EditText;
 private Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 // Initialize views
 num1EditText = findViewById(R.id.num1_edittext);
 num2EditText = findViewById(R.id.num2_edittext);
 button = findViewById(R.id.button);
 // Set click listener for calculate button
 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 // Get input from user
 String num1String = num1EditText.getText().toString();
 String num2String = num2EditText.getText().toString();
 // Check if input is empty
 if(num1String.isEmpty() || num2String.isEmpty()){
 return;
 }
 // Convert input to numbers
 double num1 = Double.parseDouble(num1String);
 double num2 = Double.parseDouble(num2String);
 // Calculate power and average
 double power = Math.pow(num1, num2);
 double average = (num1 + num2) / 2;
 // Create intent to start new activity
 Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("power", power);
 intent.putExtra("average", average);
 startActivity(intent);
 }
 });
 }
}
xml2
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/activity_main2"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity2">
 <TextView
 android:id="@+id/power_textview"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Power: "
 android:textSize="20sp"
 android:textStyle="bold"
 app:layout_constraintBottom_toTopOf="@id/average_textview"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent" />
 <TextView
 android:id="@+id/average_textview"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Average: "
 android:textSize="20sp"
 android:textStyle="bold"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@id/power_textview" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java 2
package com.example.s5q1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
private TextView powerTextView, averageTextView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main2);
 // Find the TextViews in the layout
 powerTextView = findViewById(R.id.power_textview);
 averageTextView = findViewById(R.id.average_textview);
 // Get the power and average values passed from MainActivity
 double power = getIntent().getDoubleExtra("power", 0);
 double average = getIntent().getDoubleExtra("average", 0);
 // Set the text of the TextViews to display the power and average values
 powerTextView.setText("Power: " + power);
 averageTextView.setText("Average: " + average);
 }
}

S7Q1 :-
Xml 1
<?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">
 <RadioGroup
 android:id="@+id/radio_group"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true">
 <RadioButton
 android:id="@+id/radio_button_1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Option 1" />
 <RadioButton
 android:id="@+id/radio_button_2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Option 2" />
 <RadioButton
 android:id="@+id/radio_button_3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Option 3" />
 </RadioGroup>
</RelativeLayout>
Java 1
package com.example.s7q1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
 private RadioGroup radioGroup;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 // Get reference to the radio group in the layout
 radioGroup = findViewById(R.id.radio_group);
 // Set an on checked change listener for the radio group
 radioGroup.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
 @Override
 public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
 // Get the selected radio button from the radio group
 RadioButton radioButton = findViewById(checkedId);
 // Get the text of the selected radio button
 String text = radioButton.getText().toString();
 // Display a toast message showing the selected option
 Toast.makeText(MainActivity.this, "Selected option: " + text,
Toast.LENGTH_SHORT).show();
 }
 });
 }
}


S8Q1 :-
Xml1
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
android:layout_height="match_parent"
 android:stretchColumns="0">
 <TableRow>
 <TextView
 android:text="Username:"
 android:textSize="20sp"/>
 <EditText
 android:id="@+id/username_edittext"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:inputType="text"/>
 </TableRow>
 <TableRow>
 <TextView
 android:text="Password:"
 android:textSize="20sp"/>
 <EditText
 android:id="@+id/password_edittext"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:inputType="textPassword"/>
 </TableRow>
 <TableRow>
 <Button
 android:id="@+id/login_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Login"/>
 </TableRow>
</TableLayout>
Java 1
package com.example.s8q1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
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 usernameEditText, passwordEditText;
 private Button loginButton;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 usernameEditText = findViewById(R.id.username_edittext);
 passwordEditText = findViewById(R.id.password_edittext);
 loginButton = findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 String username = usernameEditText.getText().toString();
 String password = passwordEditText.getText().toString();
 if(username.equals("admin") && password.equals("password")){
 Toast.makeText(MainActivity.this, "Login successful, go to next
activity", Toast.LENGTH_SHORT).show();
 Intent intent = new Intent(MainActivity.this,
MainActivity2.class);
 startActivity(intent);
 } else {
 Toast.makeText(MainActivity.this, "Invalid username or
password", Toast.LENGTH_SHORT).show();
 }
 }
 });
 }
}
xml2
<?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:gravity="center"
 android:orientation="vertical">
 <TextView
 android:id="@+id/textView"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Next Activity"
 android:textSize="30sp"/>
</LinearLayout>
Java 2
package com.example.s8q1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main2);
}
}


S10Q1 :-
Xml1
<?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"
 tools:context=".MainActivity">
 <Switch
 android:id="@+id/switch_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Switch"
 android:layout_marginBottom="20dp"/>
 <ToggleButton
 android:id="@+id/toggle_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Toggle"
 android:layout_marginBottom="20dp"/>
 <TextView
 android:id="@+id/text_view"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="20sp"/>
</LinearLayout>
Java1 :
package com.example.s10q1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
 private Switch switchButton;
 private ToggleButton toggleButton;
 private TextView textView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 switchButton = findViewById(R.id.switch_button);
 toggleButton = findViewById(R.id.toggle_button);
 textView = findViewById(R.id.text_view);
 switchButton.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
 @Override
 public void onCheckedChanged(CompoundButton compoundButton, boolean
isChecked) {
 if (isChecked) {
 textView.setText("Switch is ON");
 } else {
 textView.setText("Switch is OFF");
 }
 }
 });
 toggleButton.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
 @Override
 public void onCheckedChanged(CompoundButton compoundButton, boolean
isChecked) {
 if (isChecked) {
 textView.setText("Toggle is ON");
 } else {
 textView.setText("Toggle is OFF");
 }
 }
 });
 }
}


S11Q1 :-
Xml1
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="16dp"
 tools:context=".MainActivity">
 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintVertical_bias="0.5">
 <TextView
android:id="@+id/text_view"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center|center_horizontal"
 android:foregroundGravity="center_vertical|center"
 android:gravity="center|center_horizontal"
 android:text="Hello World!"
 android:textSize="14sp" />
 <Button
 android:id="@+id/size_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Increase Text Size" />
 <Button
 android:id="@+id/color_button"
 android:layout_width="175dp"
 android:layout_height="wrap_content"
 android:layout_marginStart="8dp"
 android:layout_marginTop="8dp"
 android:text="Change Text Color" />
 <Button
 android:id="@+id/font_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginStart="4dp"
 android:layout_marginTop="4dp"
 android:text="Change Text Font" />
 </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java 1
package com.example.s11q1;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
 private TextView textView;
 private Button size_button, color_button, font_button;
 private int textSize = 14;
 private int textColor = Color.BLACK;
 private Typeface typeface = Typeface.DEFAULT;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 // Get references to UI elements
 textView = findViewById(R.id.text_view);
size_button = findViewById(R.id.size_button);
 color_button = findViewById(R.id.color_button);
 font_button = findViewById(R.id.font_button);
 // Set click listener for size button
 size_button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 // Increase text size by 2
 textSize += 2;
 textView.setTextSize(textSize);
 }
 });
 // Set click listener for color button
 color_button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 // Toggle between black and red text color
 if (textColor == Color.BLACK) {
 textColor = Color.RED;
 } else {
 textColor = Color.BLACK;
 }
 textView.setTextColor(textColor);
 }
 });
 // Set click listener for font button
 font_button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 // Toggle between default and monospace font
 if (typeface == Typeface.DEFAULT) {
 typeface = Typeface.MONOSPACE;
 } else {
 typeface = Typeface.DEFAULT;
 }
textView.setTypeface(typeface);
 }
 });
 }
}

S12Q1
Xml1
<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/edittext"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_margin="16dp"
 android:hint="Enter your message here"
 android:inputType="text" />
<Button
 android:id="@+id/button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/edittext"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="24dp"
 android:text="Send Message" />
</RelativeLayout>
Java 1
package com.example.s12q1;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
 private EditText editText;
 private Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 editText = findViewById(R.id.edittext);
 button = findViewById(R.id.button);
 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 String message = editText.getText().toString();
 Intent intent = new Intent(MainActivity.this,MainActivity2.class);
 intent.putExtra("message", message);
 startActivity(intent);
 }
 });
 }
}
xml 2
<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=".SecondActivity">
 <TextView
 android:id="@+id/textview"
android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:text="Message will be displayed here"
 android:textSize="24sp" />
</RelativeLayout>
Java 2
package com.example.s12q1;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity2 extends AppCompatActivity {
 private TextView textView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main2);
 textView = findViewById(R.id.textview);
 Intent intent = getIntent();
 String message = intent.getStringExtra("message");
 textView.setText(message);
 }


S13Q1
Xml1
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_height="match_parent"
 tools:context=".MainActivity">
 <ScrollView
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <LinearLayout
android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical" >
 <Button
 android:id="@+id/button17"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button18"
android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button19"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 <Button
 android:id="@+id/button20"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="Button" />
 </LinearLayout>
 </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Java 1
package com.example.s13q1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
}

S14Q1 :-
Java 1
package com.example.s14q1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
 private static final String TAG = "MainActivity";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Log.d(TAG, "onCreate() called");
 }
 @Override
 protected void onStart() {
 super.onStart();
 Log.d(TAG, "onStart() called");
 }
 @Override
 protected void onResume() {
 super.onResume();
 Log.d(TAG, "onResume() called");
 }
 @Override
 protected void onPause() {
 super.onPause();
 Log.d(TAG, "onPause() called");
 }
 @Override
 protected void onStop() {
 super.onStop();
 Log.d(TAG, "onStop() called");
 }
 @Override
 protected void onDestroy() {
 super.onDestroy();
 Log.d(TAG, "onDestroy() called");
 }
}

S15Q1 :-
Xml1
<?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:background="@drawable/border"
 android:padding="16dp">
 <TextView
android:id="@+id/myText"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Hello World!"
 android:textSize="24sp"
 android:layout_centerInParent="true"/>
</RelativeLayout>
Border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
 <stroke
 android:width="30dp"
 android:color="@color/black"/>
 <corners
 android:radius="10dp"/>
</shape>
Java (main)1
package com.example.s15q1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
}

S16Q1:-
Xml1:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_height="match_parent"
 tools:context=".MainActivity">
 <LinearLayout
 android:layout_width="match_parent"
android:layout_height="match_parent"
 android:orientation="vertical">
 <EditText
 android:id="@+id/editText"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:hint="Name"
 android:inputType="textPersonName" />
 <EditText
 android:id="@+id/editText2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:hint="Surname"
 android:inputType="textPersonName" />
 <EditText
 android:id="@+id/editText3"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:hint="Class"
 android:inputType="textPersonName" />
 <EditText
 android:id="@+id/editText4"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:hint="Gender"
 android:inputType="textPersonName" />
 <EditText
 android:id="@+id/editText5"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:hint="Hobies"
 android:inputType="textPersonName" />
 <EditText
 android:id="@+id/editTextNumber"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10"
 android:hint="Marks"
 android:inputType="number" />
 <Button
 android:id="@+id/button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:text="Button" />
 </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java 1
package com.example.s16q1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText editText,editText2,editText3,editText4,editText5,editTextNumber;
private Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 editText = findViewById(R.id.editText);
 editText2 = findViewById(R.id.editText2);
 editText3 = findViewById(R.id.editText3);
 editText4 = findViewById(R.id.editText4);
 editText5 = findViewById(R.id.editText5);
 editTextNumber = findViewById(R.id.editTextNumber);
 button = findViewById(R.id.button);
 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 String Name = editText.getText().toString();
 String Surname = editText2.getText().toString();
 String Class = editText3.getText().toString();
 String Gender = editText4.getText().toString();
 String Hobbies = editText5.getText().toString();
 String input = editTextNumber.getText().toString();
 int Marks = Integer.parseInt(input);
 Intent intent = new Intent(MainActivity.this,MainActivity2.class);
 intent.putExtra("Name",Name);
 intent.putExtra("Surname",Surname);
 intent.putExtra("Class",Class);
 intent.putExtra("Gender",Gender);
 intent.putExtra("Hobbies",Hobbies);
 intent.putExtra("Marks",Marks);
 startActivity(intent);
 }
 });
 }
}
Xml2
<?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=".MainActivity2">
 <TableLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <TableRow
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <TextView
 android:id="@+id/textView11"
 android:layout_width="410dp"
 android:layout_height="match_parent"
 android:text="TextView"
 android:textSize="20sp" />
 </TableRow>
 <TableRow
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <TextView
 android:id="@+id/textView12"
 android:layout_width="410dp"
 android:layout_height="match_parent"
 android:text="TextView"
 android:textSize="20sp" />
 </TableRow>
 <TableRow
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <TextView
 android:id="@+id/textView13"
android:layout_width="410dp"
 android:layout_height="wrap_content"
 android:text="TextView"
 android:textSize="20sp" />
 </TableRow>
 <TableRow
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <TextView
 android:id="@+id/textView14"
 android:layout_width="410dp"
 android:layout_height="wrap_content"
 android:text="TextView"
 android:textSize="20sp" />
 </TableRow>
 <TableRow
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <TextView
 android:id="@+id/textView15"
 android:layout_width="410dp"
 android:layout_height="wrap_content"
android:text="TextView"
 android:textSize="20sp" />
 </TableRow>
<TableRow
 android:layout_width="match_parent"
 android:layout_height="match_parent" >
 <TextView
 android:id="@+id/textView16"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="TextView"
 android:textSize="20sp" />
 </TableRow>
 </TableLayout>
</LinearLayout>
Java 2
package com.example.s16q1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
private TextView textView11,textView12,textView13,textView14,textView15,textView16;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main2);
 textView11 = findViewById(R.id.textView11);
 textView12 = findViewById(R.id.textView12);
 textView13 = findViewById(R.id.textView13);
 textView14 = findViewById(R.id.textView14);
 textView15 = findViewById(R.id.textView15);
 textView16 = findViewById(R.id.textView16);
 String Name = getIntent().getStringExtra("Name");
 String Surname = getIntent().getStringExtra("Surname");
 String Class = getIntent().getStringExtra("Class");
 String Gender = getIntent().getStringExtra("Gender");
 String Hobbies = getIntent().getStringExtra("Hobbies");
 int Marks = getIntent().getIntExtra("Marks",0);
 textView11.setText("Name: "+Name);
 textView12.setText("Surname: "+Surname);
 textView13.setText("Class: "+Class);
 textView14.setText("Gender: "+Gender);
 textView15.setText("Hobbies: "+Hobbies);
 textView16.setText("Marks: "+Marks);
 }
}
S17Q1:-
Xml1
<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">
 <Button
 android:id="@+id/btn_call"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:text="Make Phone Call" />
</RelativeLayout>
Java 1
package com.example.slip17q1;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Button button = findViewById(R.id.btn_call);
 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 String phoneNumber = "1234567890"; // replace with your desired
phone number
 Intent intent = new Intent(Intent.ACTION_DIAL);
 intent.setData(Uri.parse("tel:" + phoneNumber));
 startActivity(intent);
 }
 });
 }
}
manifest :
<?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:supportsRtl="true"
 android:theme="@style/Theme.Slip17Q1"
 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>
 </application>
</manifest>

S18Q1 :-
Xml1
<?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">
 <Button
 android:id="@+id/button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Show Alert Dialog"
 android:layout_centerInParent="true"/>
</RelativeLayout>
Java 1:-
// Import necessary packages
package com.example.slip18q1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
 private Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState
setContentView(R.layout.activity_main);
 // Initialize button variable and link with UI element
 button = findViewById(R.id.button);
 // Set button click listener
 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 // Create an alert dialog
 AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
 // Set dialog title
 builder.setTitle("Hey Boss");
 // Set dialog message
 builder.setMessage("This is an alert dialog!");
 // Set positive button and its click listener
 builder.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
 @Override
public void onClick(DialogInterface dialog, int which) {
 // Do nothing when OK button is clicked
 }
 });
 // Show the alert dialog
 builder.show();
 }
 });
 }
}

Comments