0% found this document useful (0 votes)
10 views

Android Development

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Android Development

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

Button Click Event:


○ Objective: Create an Android application with a button that changes the text of a
TextView when clicked.
○ Tasks:
1. Create a new Android Studio project.
2. Add a Button and a TextView to the main layout file.
3. Write an onClick event handler in the MainActivity class that changes the
TextView text when the button is clicked.
4. Run the application and test the functionality.
2. Simple Calculator:
○ Objective: Develop a basic calculator app that can perform addition of two
numbers.
○ Tasks:
1. Create a new Android project.
2. Add two EditText fields for user input, a Button to perform the addition,
and a TextView to display the result.
3. Write the logic in MainActivity to read input values, perform addition, and
display the result when the button is clicked.
4. Handle cases where the input fields are empty or contain invalid
numbers.
3. Basic ListView:
○ Objective: Implement a simple ListView that displays a list of items.
○ Tasks:
1. Create a new Android project.
2. Add a ListView to the main layout file.
3. Create an array of strings and set up an ArrayAdapter to populate the
ListView with these strings.
4. Run the application to ensure the list displays correctly.
4. Intent and Activity Navigation:
○ Objective: Create an app with two activities and navigate between them using
intents.
○ Tasks:
1. Create a new Android project with a main activity (MainActivity) and a
second activity (SecondActivity).
2. Add a Button to MainActivity. When the button is clicked, it should
navigate to SecondActivity.
3. In SecondActivity, add a Button that navigates back to MainActivity.
4. Implement the necessary intents and onClick event handlers to handle the
navigation.
5. Test the app to ensure the navigation works correctly.

5. Text Input and Display:


● Objective: Create an Android application that takes user input from an EditText field and
displays it in a TextView when a button is clicked.
● Tasks:
1. Set up a new Android Studio project.
2. Modify the main layout file (activity_main.xml) to include an EditText, a Button, and
a TextView.
3. Write an onClick event handler in the MainActivity class that retrieves the text
from the EditText and sets it to the TextView when the button is clicked.
4. Run the application on an emulator or p

Lab Exercise 1:

<?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">

<EditText

android:id="@+id/editText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="Enter text"

android:layout_centerHorizontal="true"

android:layout_marginTop="50dp"/>

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Display"
android:layout_below="@id/editText"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp"/>

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Your text will appear here"

android:layout_below="@id/button"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp"/>

</RelativeLayout>

package com.example.textinputdisplay;

import android.os.Bundle;

import android.view.View;

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 editText = findViewById(R.id.editText);

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

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

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String inputText = editText.getText().toString();

textView.setText(inputText);

});

Lab Exercise 2
<?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">

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click Me"

android:layout_centerInParent="true"/>

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello, World!"

android:layout_below="@id/button"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp"/>

</RelativeLayout>

package com.example.buttonclickevent;
import android.os.Bundle;

import android.view.View;

import android.widget.Button;

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

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

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

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

textView.setText("Button Clicked!");

});
}

Lab Exercise 3

<?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">

<EditText

android:id="@+id/number1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="Enter first number"

android:inputType="number"

android:layout_centerHorizontal="true"

android:layout_marginTop="50dp"/>

<EditText

android:id="@+id/number2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="Enter second number"

android:inputType="number"
android:layout_below="@id/number1"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp"/>

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Add"

android:layout_below="@id/number2"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp"/>

<TextView

android:id="@+id/result"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Result will be displayed here"

android:layout_below="@id/button"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp"/>

</RelativeLayout>
package com.example.simplecalculator;

import android.os.Bundle;

import android.view.View;

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 number1 = findViewById(R.id.number1);

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

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

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

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

String num1Str = number1.getText().toString();

String num2Str = number2.getText().toString();

if (!num1Str.isEmpty() && !num2Str.isEmpty()) {

double num1 = Double.parseDouble(num1Str);

double num2 = Double.parseDouble(num2Str);

double sum = num1 + num2;

result.setText("Result: " + sum);

} else {

result.setText("Please enter both numbers");

});

Lab Exercise 4

<?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">

<ListView
android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</RelativeLayout>

package com.example.basiclistview;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ListView listView = findViewById(R.id.listView);

String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,

android.R.layout.simple_list_item_1, items);

listView.setAdapter(adapter);

Lab Exercise 5

package com.example.intentnavigation;

import android.content.Intent;

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 buttonNavigate = findViewById(R.id.buttonNavigate);

buttonNavigate.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, SecondActivity.class);

startActivity(intent);

});

You might also like