Wednesday, January 22, 2014

ANDROID : Set Red Green and Blue BackGround Color Using SeekBar.

.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/myScreen"
>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RED" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0" />

    <TextView
        android:id="@+id/countred"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="0" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="GREEN" />

    <SeekBar
        android:id="@+id/seekBar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0" />

    <TextView
        android:id="@+id/countgreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="0" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BLUE" />

    <SeekBar
        android:id="@+id/seekBar3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0" />

    <TextView
        android:id="@+id/countblue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="0" />

</LinearLayout>



.JAVA

 package com.bhavesh.colorchooser;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;


public class MainActivity extends Activity {

   
    SeekBar seekred,seekgreen,seekblue;
    LinearLayout layout1;
    TextView countred,countgreen,countblue;
  
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        seekred=(SeekBar)findViewById(R.id.seekBar1);
        seekgreen=(SeekBar)findViewById(R.id.seekBar2);
        seekblue=(SeekBar)findViewById(R.id.seekBar3);
        layout1=(LinearLayout)findViewById(R.id.myScreen);
       
        countred=(TextView)findViewById(R.id.countred);
        countgreen=(TextView)findViewById(R.id.countgreen);
        countblue=(TextView)findViewById(R.id.countblue);
       
       
       
        seekred.setOnSeekBarChangeListener(ChangeColor);
        seekgreen.setOnSeekBarChangeListener(ChangeColor);
        seekblue.setOnSeekBarChangeListener(ChangeColor);
    }
       
    private OnSeekBarChangeListener ChangeColor=new OnSeekBarChangeListener() {
           
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
               
               
            }
           
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
               
               
            }
           
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
       
               
                int R,G,B;
               
                 R = seekred.getProgress();
                 G = seekgreen.getProgress();
                 B = seekblue.getProgress();
                 layout1.setBackgroundColor(0xff000000 + R * 0x10000 + G * 0x100 + B);
               
                 countred.setText(String.valueOf(R));
                 countgreen.setText(String.valueOf(G));
                 countblue.setText(String.valueOf(B));
               
               
            }
    };

   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
       
       
       
       
       
    }
   
}

No comments:

Post a Comment

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...