Q8

Question 8

Create an application with 3 buttons vertically aligned, on selecting a button color of the screen will change.

App Insights:

Made a vertical layout for buttons, aligned them and added margin, wrote methods for each button to change bgcolor based on stored values in colors.xml

Browse Source Code

Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/b1"
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_weight="0"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="75dp"
        android:minWidth="200dp"
        android:onClick="goRed"
        android:text="Red" />

    <Button
        android:id="@+id/b2"
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_weight="0"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="125dp"
        android:onClick="goBlue"
        android:text="Blue" />

    <Button
        android:id="@+id/b3"
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_weight="0"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="125dp"
        android:onClick="goGreen"
        android:text="Green" />


</LinearLayout>

Output

Studio Screenshot Main Activity (1/8)
App Screenshot Main Activity (2/8)
Studio Screenshot After Pressing Red Button (3/8)
App Screenshot After Pressing Red Button (4/8)
Studio Screenshot After Pressing Blue Button (5/8)
App Screenshot After Pressing Blue Button (6/8)
Studio Screenshot After Pressing Green Button (7/8)
App Screenshot After Pressing Green Button (8/8)

Last updated