📝
Android Programming
  • Introduction
  • Contents
  • Android
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
    • End
Powered by GitBook
On this page
  • Create an application with 3 buttons horizontally aligned, on selecting a button color of the screen will change.
  • Code
  • Output
  1. Android

Q9

Question 9

PreviousQ8NextQ10

Last updated 4 years ago

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

App Insights:

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

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="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>
<?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>
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);
    }
}

Output

Download to Run and Test Locally

Q9_Three_Color_Buttons_Horizontal.apk
Browse Source Code
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)