Skip to content

Instantly share code, notes, and snippets.

@sharish
Created July 28, 2015 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharish/767a7c1431804ae017da to your computer and use it in GitHub Desktop.
Save sharish/767a7c1431804ae017da to your computer and use it in GitHub Desktop.
package com.cooltechworks.materialcolorshades;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private float mRange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
refresh();
}
};
((EditText)findViewById(R.id.from_color_et)).addTextChangedListener(watcher);
((EditText)findViewById(R.id.to_color_et)).addTextChangedListener(watcher);
SeekBar seekbar = (SeekBar) findViewById(R.id.seek_bar);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mRange = (progress/100f);
((TextView) findViewById(R.id.seek_bar_label)).setText(getString(R.string.seek, mRange));
refresh();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
refresh();
}
private void refresh() {
String fromColor = ((EditText)findViewById(R.id.from_color_et)).getText().toString();
String toColor = ((EditText)findViewById(R.id.to_color_et)).getText().toString();
int fromColorLength = fromColor.length();
int toColorLength = toColor.length();
if(((fromColorLength == 6 && !fromColor.startsWith("#")) || (fromColorLength == 7 && fromColor.startsWith("#"))) &&
((toColorLength == 6 && !toColor.startsWith("#")) || (toColorLength == 7 && toColor.startsWith("#")))) {
if (!fromColor.startsWith("#")) {
fromColor = "#" + fromColor;
}
if (!toColor.startsWith("#")) {
toColor = "#" + toColor;
}
// replace any other '#' char in the string.
fromColor = "#" + fromColor.substring(1).replace("#", "");
toColor = "#" + toColor.substring(1).replace("#", "");
ColorShades colorShades = new ColorShades();
colorShades.
setFromColor(fromColor)
.setToColor(toColor);
int outputColor = colorShades.generateColor(mRange);
int outputColorInverted = colorShades.generateColorInverted(mRange);
String outputColorString = colorShades.generateColorString(mRange);
TextView outputView = (TextView) findViewById(R.id.output_color_code);
outputView.setBackgroundColor(outputColor);
outputView.setTextColor(outputColorInverted);
outputView.setText(outputColorString);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment