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

/*Create an application with three buttons vertically aligned, on selecting a button color of the screen will change*/

File Name: 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/RL1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="Red"
        android:layout_centerHorizontal="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn1"
        android:layout_centerHorizontal="true"
        android:id="@+id/btn2"
        android:layout_marginTop="30dp"
        android:text="Blue"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn3"
        android:text="Green"
        android:layout_below="@+id/btn2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"/>

</RelativeLayout>    


File Name: MainActivity.java

package com.ducskecode.color_changer_vertical;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button b1,b2,b3;
    RelativeLayout r1;

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

        b1=(Button)findViewById(R.id.btn1);
        b2=(Button)findViewById(R.id.btn2);
        b3=(Button)findViewById(R.id.btn3);
        r1=(RelativeLayout)findViewById(R.id.RL1);

        b1.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v)
            {
                r1.setBackgroundColor(Color.RED);
            }
        });

        b2.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v)
            {
                r1.setBackgroundColor(Color.BLUE);
            }
        });

        b3.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v)
            {
                r1.setBackgroundColor(Color.GREEN);
            }
        });
    }
        public void send()
        {
            Toast.makeText(this,"color will change now",Toast.LENGTH_LONG).show();
        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}        


No comments:

Post a Comment

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...