1

I have created a textview dynamically in android. When i clicked the textview the color changes from white to orange, but what i want is when i clicked another textview, the other textview that has been changed to orange will change back to white. This is the code to create the textview:

for (int i = 1; i <= n; i++) {

        final TextView mPageNumber = new TextView(getActivity());
        mPageNumber.setText("" + i);
        mPageNumber.setId(Integer.parseInt(String.valueOf(i)));
        mPageNumber.setTextColor(getResources().getColor(R.color.colorWhite));
        mPageNumber.setPadding(60,30,60,30);
        final int id_ = mPageNumber.getId();
        LinearLayout layout = (LinearLayout) getView().findViewById(R.id.pagination);
        layout.setBackgroundResource(R.color.colorPrimary);
        layout.addView(mPageNumber);

OnClickListener

mPageNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (toastMessage!= null) {
                    toastMessage.cancel();
                }

                toastMessage = Toast.makeText(getActivity().getApplicationContext(), "Button with id =" + id_ +
                        " is clicked",Toast.LENGTH_SHORT);
                current = id_;
                toastMessage.show(); mPageNumber.setTextColor(getResources().getColor(R.color.colorOrange));
EKN
  • 1,886
  • 1
  • 16
  • 29
wizzone
  • 127
  • 1
  • 15

4 Answers4

0

You can using like this :~

mPageNumber.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent)
    {
        switch (motionEvent.getAction()){
            case MotionEvent.ACTION_DOWN:
                textView.setTextColor(Color.RED);
                break;
            case MotionEvent.ACTION_UP:
                textView.setTextColor(Color.BLUE);
                break;
        }
        return false;
    }
});
Harshit Trivedi
  • 764
  • 9
  • 33
0

You can iterate through all the child in the layout and set the color as white and then the selected color as orange like this in below example.

LinearLayout layout;
private void setAllTextColorAsWhite() {
    if(layout == null) {
        return;
    }

    int childCount = layout.getChildCount();

    for (int i = 0; i < childCount; i++) {
        TextView textView = (TextView) layout.getChildAt(i);
        textView.setTextColor(getResources().getColor(R.color.white));
    }
}

public void setTextViews() {
    layout = (LinearLayout) getView().findViewById(R.id.pagination);

    layout.removeAllViews();

    for (int i = 1; i <= n; i++) {

        final TextView mPageNumber = new TextView(getActivity());
        mPageNumber.setText("" + i);
        mPageNumber.setId(Integer.parseInt(String.valueOf(i)));
        mPageNumber.setTextColor(getResources().getColor(R.color.colorWhite));
        mPageNumber.setPadding(60, 30, 60, 30);
        final int id_ = mPageNumber.getId();

        layout.setBackgroundResource(R.color.colorPrimary);
        layout.addView(mPageNumber);

        mPageNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (toastMessage!= null) {
                    toastMessage.cancel();
                }

                toastMessage = Toast.makeText(getActivity().getApplicationContext(), "Button with id =" + id_ +
                        " is clicked",Toast.LENGTH_SHORT);
                current = id_;
                toastMessage.show();

                setAllTextColorAsWhite();

                mPageNumber.setTextColor(getResources().getColor(R.color.colorOrange));
            }
        });
    }
}
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
0

you have to do like this

for (int i = 1; i <= n; i++) {

    final TextView mPageNumber = new TextView(getActivity());
    mPageNumber.setText("" + i);
    mPageNumber.setId(Integer.parseInt(String.valueOf(i)));
    mPageNumber.setTextColor(getResources().getColor(R.color.colorWhite));
    mPageNumber.setPadding(60,30,60,30);
    mPageNumber.setOnClickListener(this);
    final int id_ = mPageNumber.getId();
    LinearLayout layout = (LinearLayout) getView().findViewById(R.id.pagination);
    layout.setBackgroundResource(R.color.colorPrimary);
    layout.addView(mPageNumber);

now you have to implement onclick method like this

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.1:
            //code for click textview.
            break;
    }
}

happy coding.

KuLdip PaTel
  • 1,069
  • 7
  • 19
0

The easiest way is to save the TextView reference to an array, like this:

final TextView[] mPageNumbers;
for (int i = 1; i <= n; i++) {
    mPageNumbers[i] = new TextView(getActivity());
    mPageNumber[i].setText("" + i);
.
.
.
for (int i=0; i<mPageNumbers.length; i++) {
    if (view.getId() == mPageNumbers[i].getId()) {
    mPageNumber.setTextColor(getResources().getColor(R.color.colorOrange));
        } else {
        mPageNumber.setTextColor(getResources().getColor(R.color.colorWhite));
        }
    }

And in the end, you loop the TextView references, and then set the color manually, if the id is same, set orange, if different (means other textViews), set white.

Kharda
  • 1,318
  • 14
  • 23