Monday, August 27, 2012

Android | Seekbar | Color Maker

This is another example of Seekbar. i used it to develop color maker.

As you know, color is combination of 4 elements, Alpha, Red, Green and Blue. Alpha is used to set value between transparency and opaque states.

I have verified the code in Android 4.0.3 emulator.

Code of Layout File:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/llcolormaker" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvalphacolor"
        android:textColor="#FFFFFFFF"
        />  

    <SeekBar
        android:id="@+id/sbalpha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="255"  />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvredcolor"
        android:textColor="#FFFFFFFF"
        />

    <SeekBar
        android:id="@+id/sbred"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0"  />
       
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvgreencolor"
        android:textColor="#FFFFFFFF"
        />

    <SeekBar
        android:id="@+id/sbgreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="0"  />  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvbluecolor"
        android:textColor="#FFFFFFFF"
        />

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

Code of Strings.xml file:


<resources>

    <string name="app_name">ColorMakerExample</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_color_maker_example">Color Maker Example</string>

    <string name="tvalphacolor">Select Alpha :</string>
    <string name="tvredcolor">Select Red Color :</string>
    <string name="tvgreencolor">Select Green Color :</string>
    <string name="tvbluecolor">Select Blue Color :</string>
</resources>

code of Java file:



package com.example.colormakerexample;

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

public class ColorMakerExample extends Activity implements OnSeekBarChangeListener {

SeekBar sbAlpha, sbRed, sbGreen, sbBlue;
LinearLayout llColorMaker;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_maker_example);
       
        sbAlpha = (SeekBar) findViewById(R.id.sbalpha);
        sbRed = (SeekBar) findViewById(R.id.sbred);
        sbGreen = (SeekBar) findViewById(R.id.sbgreen);
        sbBlue = (SeekBar) findViewById(R.id.sbblue);
        llColorMaker = (LinearLayout) findViewById(R.id.llcolormaker);
       
        sbAlpha.setOnSeekBarChangeListener(this);
        sbRed.setOnSeekBarChangeListener(this);
        sbGreen.setOnSeekBarChangeListener(this);
        sbBlue.setOnSeekBarChangeListener(this);
       

        llColorMaker.setBackgroundColor(Color.argb(sbAlpha.getProgress(), sbRed.getProgress(), sbGreen.getProgress(), sbBlue.getProgress()));
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_color_maker_example, menu);
        return true;
    }


public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub

        llColorMaker.setBackgroundColor(Color.argb(sbAlpha.getProgress(), sbRed.getProgress(), sbGreen.getProgress(), sbBlue.getProgress()));

}


public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
sbAlpha = sbRed = sbGreen = sbBlue = null;
super.onDestroy();
}
}



Result:




No comments:

Post a Comment