Sunday, 16 April 2017

Three Buttons Vertically Aligned & Background Color Usage in Android

FileName: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.abbu.button_color.MainActivity">

    <Button
        android:text="GAMBOGE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonPatina"
        android:layout_alignStart="@+id/buttonPatina"
        android:layout_marginTop="30dp"
        android:id="@+id/buttonGamboge" />

    <Button
        android:text="AMBER"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonGamboge"
        android:layout_alignStart="@+id/buttonGamboge"
        android:layout_marginTop="30dp"
        android:id="@+id/buttonAmber" />

    <Button
        android:text="PATINA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonPatina"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

FileName: MainActivity.java

package com.example.abbu.button_color;

import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;


public class MainActivity extends AppCompatActivity {

    Button btn1,btn2,btn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = (Button) findViewById(R.id.buttonPatina);
        btn2 = (Button) findViewById(R.id.buttonGamboge);
        btn3 = (Button) findViewById(R.id.buttonAmber);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RelativeLayout rl = (RelativeLayout)findViewById(R.id.activity_main);
                    rl.setBackgroundColor(Color.parseColor("#407A52"));
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RelativeLayout rl = (RelativeLayout)findViewById(R.id.activity_main);
                rl.setBackgroundColor(Color.parseColor("#FFB61E"));
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RelativeLayout rl = (RelativeLayout)findViewById(R.id.activity_main);
                rl.setBackgroundColor(Color.parseColor("#CA6924"));
            }
        });
    }
}

Output:

Three Buttons Vertically Aligned & Background Color Usage in Android
Three Buttons Vertically Aligned & Background Color Usage in Android
After clicking on PATINA Button, the background color changes to:
Three Buttons Vertically Aligned & Background Color Usage in Android
PATINA (#407A52)
After clicking on GAMBOGE Button, the background color changes to:
Three Buttons Vertically Aligned & Background Color Usage in Android
GAMBOGE (#FFB61E)
After clicking on AMBER Button, the background color changes to:
AMBER (#CA6924)

No comments:

Post a Comment