CM-506 :: ANDROID PROGRAMMING LAB , Scheme :: C-23
EXPERIMENT 2 :: Create an android app to display TOAST MESSAGE
[Link] :: Program – Save application name as “helloapplication”
package [Link];
import [Link];
import [Link];
import [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);
[Link](this);
setContentView([Link].activity_main);
Button btn=findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link](getApplicationContext(),"T BHAVYA, KUPPAM", Toast.LENGTH_LONG).show();
}
});
[Link](findViewById([Link]), (v, insets) -> {
Insets systemBars = [Link]([Link]());
[Link]([Link], [Link], [Link], [Link]);
return insets;
});
}
}
activity_main.xml :: Program
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
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="Button"
tools:layout_editor_absoluteX="146dp"
tools:layout_editor_absoluteY="258dp" />
</RelativeLayout>
[Link]:: Program
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT :: SAMPLE SCREENS
EXPERIMENT No. :: 03
Create an android app to accept a number in text field and display the factorial of
it in a toast message on clicking a button
[Link] :: Program - Save application name as “factorial”
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText numberEditText;
private Button calculateButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
numberEditText = findViewById([Link]);
calculateButton = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View view) {
calculateFactorial();
}
});
}
private void calculateFactorial() {
String numberStr = [Link]().toString();
if ([Link]()) {
[Link](this, "Please enter a number",
Toast.LENGTH_SHORT).show();
return;
}
try {
int number = [Link](numberStr);
if (number < 0) {
[Link](this, "Factorial is not defined for negative
numbers", Toast.LENGTH_SHORT).show();
return;
}
long factorial = calculate(number);
[Link](this, "Factorial: " + factorial,
Toast.LENGTH_LONG).show();
} catch (NumberFormatException e) {
[Link](this, "Invalid input. Please enter a valid number.",
Toast.LENGTH_SHORT).show();
}
}
private long calculate(int n) {
if (n == 0) {
return 1;
} else {
long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
}
activity_main.xml :: Program
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/numberEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/calculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Factorial"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
[Link]:: Program
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT :: SAMPLE SCREENS
Enter the Number, Click on Claculate Factorial Button to get Factorial of given Or entered Number
EXPERIMENT No. :: 04
Create an android app to illustrate the use of Check Box widget
[Link] :: Program - Save application name as “checkboxexample”
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
CheckBox ch,ch1,ch2,ch3;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
ch=findViewById([Link]);
ch1=findViewById([Link].checkBox2);
ch2=findViewById([Link].checkBox3);
ch3=findViewById([Link].checkBox4);
}
public void Check(View v){
String msg="Selected Items are";
// Checking the selection
if([Link]())
msg=msg+" [Painting]";
if([Link]())
msg=msg+", [Reading]";
if([Link]())
msg=msg+", [Singing]";
if([Link]())
msg=msg+", [Cooking]";
// Executing the Toast
[Link](this,msg,Toast.LENGTH_LONG).show();
// Clearing all the selection
[Link](false);
[Link](false);
[Link](false);
[Link](false);
}
}
activity_main.xml :: Program
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:text="Choose your hobbies:"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Painting"
android:layout_marginTop="16dp"
android:textSize="16sp" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reading"
android:layout_marginTop="16dp"
android:textSize="16sp" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Singing"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="@+id/textView"
tools:layout_editor_absoluteX="382dp" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cooking"
android:layout_marginTop="16dp"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="@+id/checkBox"
tools:layout_editor_absoluteX="386dp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="Check"
android:text="submit" />
</LinearLayout>
[Link]:: Program
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT :: SAMPLE SCREENS
EXPERIMENT No. :: 04
[Link] :: Program - Save application name as “Toggle Button”
package [Link];
import [Link];
import [Link];
import [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);
[Link](this);
setContentView([Link].activity_main);
ToggleButton toggleButton=findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
if([Link]()){
[Link]([Link], "Toggle Button is ON",
Toast.LENGTH_SHORT).show();
}
else{
[Link]([Link], "Toggle Button is OFF",
Toast.LENGTH_SHORT).show();
}
}
});
[Link](findViewById([Link]), (v,
insets) -> {
Insets systemBars =
[Link]([Link]());
[Link]([Link], [Link], [Link],
[Link]);
return insets;
});
}
}
activity_main.xml :: Program
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
[Link]:: Program
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT :: SAMPLE SCREENS