0% found this document useful (0 votes)
21 views8 pages

GUI Design Exercises for Android Apps

The document contains multiple exercises for designing Android GUIs using XML and Java code. It includes a layout for a simple form with text views and edit texts, a color grid layout, a button to open a website, and a BMI calculator app with input fields and result displays. Each exercise provides the XML layout and corresponding Java code for functionality.

Uploaded by

abekum21
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)
21 views8 pages

GUI Design Exercises for Android Apps

The document contains multiple exercises for designing Android GUIs using XML and Java code. It includes a layout for a simple form with text views and edit texts, a color grid layout, a button to open a website, and a BMI calculator app with input fields and result displays. Each exercise provides the XML layout and corresponding Java code for functionality.

Uploaded by

abekum21
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

1.

Exercise 1: design the GUI shown below using 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">

<!-- Top Text -->


<TextView
android:id="@+id/textViewTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TEXT"
android:textSize="20sp"
android:gravity="center"
android:layout_marginBottom="16dp"/>

<!-- First Row -->


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="16dp">

<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TEXT"
android:gravity="center"/>

<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="EDIT TEXT"/>
</LinearLayout>

<!-- Second Row -->


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="16dp">

<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TEXT"
android:gravity="center"/>

<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="EDIT TEXT"/>
</LinearLayout>

<!-- Button -->


<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BUTTON"
android:gravity="center"/>
</LinearLayout>
Exercise 2: design the GUI shown below using XML

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<TextView
android:id="@+id/red"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="RED"
android:gravity="center"
android:background="#FF0000"
android:textColor="#FFFFFF"/>

<TextView
android:id="@+id/orange"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="ORANGE"
android:gravity="center"
android:background="#FFA500"
android:textColor="#FFFFFF"
android:layout_toRightOf="@id/red"/>

<TextView
android:id="@+id/yellow"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="YELLOW"
android:gravity="center"
android:background="#FFFF00"
android:textColor="#000000"
android:layout_toRightOf="@id/orange"/>

<TextView
android:id="@+id/green"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="GREEN"
android:gravity="center"
android:background="#008000"
android:textColor="#FFFFFF"
android:layout_below="@id/red"/>

<TextView
android:id="@+id/blue"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="BLUE"
android:gravity="center"
android:background="#0000FF"
android:textColor="#FFFFFF"
android:layout_toRightOf="@id/green"
android:layout_below="@id/orange"/>

<TextView
android:id="@+id/indigo"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="INDIGO"
android:gravity="center"
android:background="#4B0082"
android:textColor="#FFFFFF"
android:layout_toRightOf="@id/blue"
android:layout_below="@id/yellow"/>

<TextView
android:id="@+id/violet"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="VIOLET"
android:gravity="center"
android:background="#EE82EE"
android:textColor="#FFFFFF"
android:layout_below="@id/green"
android:layout_below="@id/blue"
android:layout_below="@id/indigo"/>
</RelativeLayout>
Exercise3 : Create an app that links to ASTU Website

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

<Button
android:id="@+id/buttonOpenWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open ASTU Website"
android:layout_centerInParent="true"
android:onClick="openWebsite"/>
</RelativeLayout>

Java code
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);
}

public void openWebsite(View view) {


String url = "[Link] // Replace with the actual ASTU website URL
Intent intent = new Intent(Intent.ACTION_VIEW);
[Link]([Link](url));
startActivity(intent);
}
}
Exercise 4: Learn to create a BMI Calculator App for Android

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"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI Calculator"
android:textSize="24sp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"/>

<EditText
android:id="@+id/editTextWeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Weight (kg)"
android:inputType="numberDecimal"
android:layout_below="@id/textViewTitle"
android:layout_marginBottom="16dp"/>

<EditText
android:id="@+id/editTextHeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Height (cm)"
android:inputType="numberDecimal"
android:layout_below="@id/editTextWeight"
android:layout_marginBottom="32dp"/>

<Button
android:id="@+id/buttonCalculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate BMI"
android:layout_below="@id/editTextHeight"
android:layout_marginBottom="16dp"
android:onClick="calculateBMI"/>

<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI: "
android:textSize="18sp"
android:layout_below="@id/buttonCalculate"
android:layout_marginBottom="16dp"/>

<TextView
android:id="@+id/textViewStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status: "
android:textSize="18sp"
android:layout_below="@id/textViewResult"/>
</RelativeLayout>

Java
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private EditText editTextWeight, editTextHeight;


private TextView textViewResult, textViewStatus;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

editTextWeight = findViewById([Link]);
editTextHeight = findViewById([Link]);
textViewResult = findViewById([Link]);
textViewStatus = findViewById([Link]);
}

public void calculateBMI(View view) {


String weightStr = [Link]().toString();
String heightStr = [Link]().toString();

if (![Link]() && ![Link]()) {


float weight = [Link](weightStr);
float height = [Link](heightStr) / 100; // convert height from cm to meters

float bmi = weight / (height * height);


[Link]([Link]("BMI: %.2f", bmi));

String bmiStatus = getBMIStatus(bmi);


[Link]([Link]("Status: %s", bmiStatus));
}
}

private String getBMIStatus(float bmi) {


if (bmi < 18.5) {
return "Underweight";
} else if (bmi >= 18.5 && bmi <= 24.9) {
return "Normal weight";
} else if (bmi >= 25 && bmi <= 29.9) {
return "Overweight";
} else if (bmi >= 30 && bmi <= 34.9) {
return "Obesity Class I";
} else if (bmi >= 35 && bmi <= 39.9) {
return "Obesity Class II";
} else {
return "Obesity Class III";
}
}
}

You might also like