RCO
RCO

Reputation: 21

Implement a method to set some TextView properties

I need to implement a custom method to switch some TextView background colors according to the position of the seekbar (except the first and last position , they are fixed).

I need to do it to 10 views. So I made this class SwitchColor to try to automate my work. The constructor uses 4 parameters: the TextView I want to change the background, the seekbar position, the initial color, and the final color.

I'm not sure how to call the internal method SwitchTextViewBackground() from the new object SwitchColor.

My MainActivity:

public class MainActivity extends AppCompatActivity {

private static SeekBar seekbar;
private static TextView textview;

private static TextView topSqr;
private static TextView middleSqr;
private static TextView middleSqrContentA;
private static TextView MiddleSqrContentB;
private static TextView bottomSqr;
private static TextView stripe1;
private static TextView stripe2;
private static TextView stripe3;
private static TextView stripe4;
private static TextView stripe5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    seekBarAction();
}


private void seekBarAction() {

    seekbar = (SeekBar) findViewById(R.id.seekBar);
    seekbar.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener() {
                int seekBarProgressValue = 0;

                SwitchColor switchColor = new SwitchColor(topSqr,seekBarProgressValue,"#FFF","#000");
                switchColor.SwitchTextViewBackground(); // cannot resolve Symbol

                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                seekBarProgressValue = i;
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }

            }
    );

}}

And my custom class:

public class SwitchColor{
private TextView view;
private int colorIndex;
private String initialColor;
private String finalColor;

public SwitchColor(TextView view, int colorIndex, String initialColor, String finalColor) {
    this.view = view;
    this.colorIndex = colorIndex;
    this.initialColor = initialColor;
    this.finalColor = finalColor;
}

public void SwitchTextViewBackground(){

    if(colorIndex <= 10 && colorIndex < 0) {
        view.setBackgroundColor(Color.parseColor(initialColor));
    } else {
        if (colorIndex <= 90 && colorIndex <=100){
            view.setBackgroundColor(Color.parseColor(finalColor));
        }
        else {
            Random rnd = new Random();
            int color = Color.argb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
            view.setBackgroundColor(color);
        }
    }
}}

Upvotes: 0

Views: 73

Answers (1)

user4696837
user4696837

Reputation:

Edit your SwithchColor class like this

public class SwitchColor{
    private TextView view;
    private int colorIndex;
    private String initialColor;
    private String finalColor;

    public SwitchColor(TextView view, int colorIndex, String initialColor,   String finalColor) {
        this.view = view;
        this.colorIndex = colorIndex;
        this.initialColor = initialColor;
        this.finalColor = finalColor;
        SwitchTextViewBackground();//Change is here
    }

    public void SwitchTextViewBackground(){

        if(colorIndex <= 10 && colorIndex < 0) {
            view.setBackgroundColor(Color.parseColor(initialColor));
        } else {
            //if (colorIndex <= 90 && colorIndex <=100){ 
            if (colorIndex >= 90 && colorIndex <=100){//Here is an edit
                view.setBackgroundColor(Color.parseColor(finalColor));
            }
            else {
                Random rnd = new Random();
                int color = Color.argb(rnd.nextInt(256), rnd.nextInt(256),     rnd.nextInt(256), rnd.nextInt(256));
                view.setBackgroundColor(color);
            }
        }
    }
}

Then Edit inside seekBarAction like this

private void seekBarAction() {

    seekbar = (SeekBar) findViewById(R.id.seekBar);
    seekbar.setOnSeekBarChangeListener(
            new SeekBar.OnSeekBarChangeListener() {
                //int seekBarProgressValue = 0;

                //SwitchColor switchColor = new SwitchColor(topSqr,seekBarProgressValue,"#FFF","#000");
               // switchColor.SwitchTextViewBackground(); // cannot resolve Symbol

                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //seekBarProgressValue = i;
                //Edit is here 
                SwitchColor switchColor = new SwitchColor(topSqr,i,"#FFFFFF","#000000");
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }

            }
    );

}}

Upvotes: 1

Related Questions