Reputation: 364
I'm doing some tests to simply change text color based off of three sliders as my RGB values in the main activity. The problem lies in being unable to set my custom listener on my sliders. Here's my code. Also, everything that needs to be is imported, just didn't include them because the code insertion thing on here doesn't like it;
public class MainActivity extends AppCompatActivity {
int[] currentCol = {0, 0, 0};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
SeekBar Red = (SeekBar) findViewById(R.id.R);
SeekBar Blue = (SeekBar) findViewById(R.id.B);
SeekBar Green = (SeekBar) findViewById(R.id.G);
TextView textBox = (TextView) findViewById(R.id.colorText);
OnSeekBarChangeListener customListener = new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar name, int progress, boolean fromUser) {
if (name == Red) {
currentCol[0] = progress;
} else if (name == Blue) {
currentCol[1] = progress;
} else if (name == Green) {
currentCol[2] = progress;
}
textBox.setTextColor(Color.rgb(currentCol[0], currentCol[1], currentCol[2]));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
public void setCustomListener(OnSeekBarChangeListener customListener) {
this.customListener = customListener;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 1
Views: 1216
Reputation: 75788
At first add implements OnSeekBarChangeListener{
public class MainActivity extends AppCompatActivity implements OnSeekBarChangeListener{
Call this in oncreate() function
SeekBar Red = (SeekBar) findViewById(R.id.R);
SeekBar Blue = (SeekBar) findViewById(R.id.B);
SeekBar Green = (SeekBar) findViewById(R.id.G);
Red.setOnSeekBarChangeListener(this); // set seekbar listener.
For practice Demo
Upvotes: 1