# Q9

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

{% hint style="info" %}
**App Insights:**

Twitched the previous vertical layout code and switched the margin pattern and perform same functions as before

[Browse Source Code ](https://github.com/MJK618/AndroidProgramming/tree/main/Source%20Code%20for%20Questions/Q9_Three_Color_Buttons_Horizontal)
{% endhint %}

![](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZLc_ZwDT-hcDx1Uw-e%2F-MZLsQQLFPpnhzMmethY%2Fimage.png?alt=media\&token=f307f87b-24dd-40cd-8103-cbface2a67d6)

## **Code**

{% tabs %}
{% tab title="activity\_main.xml" %}

```markup
<?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="horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/b1"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="300dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:minWidth="150dp"
        android:onClick="goRed"
        android:text="Red" />

    <Button
        android:id="@+id/b2"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="300dp"
        android:layout_marginRight="5dp"
        android:minWidth="200dp"
        android:onClick="goBlue"
        android:text="Blue" />

    <Button
        android:id="@+id/b3"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxHeight="20dp"
        android:layout_marginTop="300dp"
        android:layout_marginRight="10dp"
        android:onClick="goGreen"
        android:text="Green" />


</LinearLayout>
```

{% endtab %}

{% tab title="colors.xml" %}

```markup
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="gray">#616161</color>
    <color name="red">#ff0000</color>
    <color name="blue">#0000ff</color>
    <color name="green">#00ff00</color>
</resources>
```

{% endtab %}

{% tab title="MainActivity.java" %}

```java
package com.example.q9_three_color_buttons_horizontal;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view = this.getWindow().getDecorView();
        view.setBackgroundResource(R.color.gray);
    }
    public void goRed(View v) {
        view.setBackgroundResource(R.color.red);
    }
    public void goBlue(View v) {
        view.setBackgroundResource(R.color.blue);
    }
    public void goGreen(View v) {
        view.setBackgroundResource(R.color.green);
    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="success" %}
Download [Q9\_Three\_Color\_Buttons\_Horizontal.apk](https://github.com/MJK618/AndroidProgramming/raw/main/APK's/Q9_Three_Color_Buttons_Horizontal.apk) to Run and Test Locally
{% endhint %}

## **Output**

![Studio Screenshot Main Activity (1/8)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZLc_ZwDT-hcDx1Uw-e%2F-MZLtL9lBBTd3D1wEKj0%2Fimage.png?alt=media\&token=d26539fd-4f02-492d-95c7-9ca286708d67)

![App Screenshot Main Activity (2/8)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZLc_ZwDT-hcDx1Uw-e%2F-MZLuWAJ9MtsAxv7FOV3%2Fimage.png?alt=media\&token=3e5b1e81-64b0-4e50-bd15-7d21fcd38e87)

![Studio Screenshot After Pressing Red Button (3/8)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZLc_ZwDT-hcDx1Uw-e%2F-MZLtQas-ScSLi9Ypk-W%2Fimage.png?alt=media\&token=1d33968d-2f52-47ba-ab97-375cef5d3506)

![App Screenshot After Pressing Red Button (4/8)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZLc_ZwDT-hcDx1Uw-e%2F-MZLtwn8QWu42KSIYPyv%2Fimage.png?alt=media\&token=823842e3-aefd-407e-8385-df2a602c0242)

![Studio Screenshot After Pressing Blue Button (5/8)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZLc_ZwDT-hcDx1Uw-e%2F-MZLtUJSaQajQBTuPzQe%2Fimage.png?alt=media\&token=23996cda-bb14-4e3d-bc22-ee16b160ed43)

![App Screenshot After Pressing Blue Button (6/8)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZLc_ZwDT-hcDx1Uw-e%2F-MZLtqihlBoU-k_ODGtr%2Fimage.png?alt=media\&token=8f4a5043-3e89-41ba-8bd2-e568593de4ca)

![Studio Screenshot After Pressing Green Button (7/8)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZLc_ZwDT-hcDx1Uw-e%2F-MZLtYII_9k6FKp5trFz%2Fimage.png?alt=media\&token=2c1aae35-7fa7-46ae-8997-c9920c414935)

![App Screenshot After Pressing Green Button (8/8)](https://3448212806-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZCvUCwpAEgH8ewpyoU%2F-MZLc_ZwDT-hcDx1Uw-e%2F-MZLth4xANa2lCjxEJ0B%2Fimage.png?alt=media\&token=a24c189b-c141-457d-86f4-bb224eb7c408)
