Android Color Picker SeekBar
Color picker seekBar |
Here is Code for Color Picker SeekBar
Define SeekBar in activity_main.xml. Just Like normal SeekBar.
<?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:gravity="center"
tools:context="techamongus.com.testapplication.MainActivity">
<SeekBar
android:id="@+id/colorSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxHeight="10dp"
/>
</RelativeLayout>
Define MainActivity.java
Here define LinearGradient and shader.
package techamongus.com.testapplication;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
public class MainActivity extends Activity {
Dialog customDialog;
Button ok,cancel;
int width;
// Button showDialog;
CustomView customView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
width=getWindowManager().getDefaultDisplay().getWidth();
LinearGradient test = new LinearGradient(0.f, 0.f, width, 0.0f,
new int[] { 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF,
0xFFFF0000, 0xFFFF00FF, 0xFFFFFF00, 0xFFFFFFFF},
null, Shader.TileMode.CLAMP);
ShapeDrawable shape = new ShapeDrawable(new RectShape());
shape.getPaint().setShader(test);
SeekBar seekBarFont = (SeekBar)findViewById(R.id.colorSeekbar);
seekBarFont.setProgressDrawable( (Drawable)shape );
seekBarFont.setMax(256*7-1);
seekBarFont.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
int r = 0;
int g = 0;
int b = 0;
if(progress < 256){
b = progress;
} else if(progress < 256*2) {
g = progress%256;
b = 256 - progress%256;
} else if(progress < 256*3) {
g = 255;
b = progress%256;
} else if(progress < 256*4) {
r = progress%256;
g = 256 - progress%256;
b = 256 - progress%256;
} else if(progress < 256*5) {
r = 255;
g = 0;
b = progress%256;
} else if(progress < 256*6) {
r = 255;
g = progress%256;
b = 256 - progress%256;
} else if(progress < 256*7) {
r = 255;
g = 255;
b = progress%256;
}
// seekBarFont.setBackgroundColor(Color.argb(255, r, g, b));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
Leave a Comment