1

As I mentioned in the title ,I want to add text to a Textview without replacing the previous text .

In my application I have a TextView and 7 buttons .On every button click I set the text of button to the TextView.

If the button is clicked on first time ,Setting the text to TextView ,and if the same button is clicked 2nd time I am removing that button's text from TextView.

Here What I want to do is for 7 buttons I want to set positions(uniqueness for sun to sat) in TextView and when the respective button is clicked that text is set to the TextView and if the button is clicked 2nd time that specific position of the text should remove .

Here text shouldn't replace the previous text that is important to have and if some button's are selected and again that are deselected means TextView should show the default text as "Never"

I tried to get source from SO but I can't find a clear solution for this .

If anyone helps me to come out from this ,that's much helpful for me .

coding

public class CreateAlarm extends Activity implements View.OnClickListener {

    private Button mbtn_Sun, mbtn_Mon, mbtn_Tue, mbtn_Wed, mbtn_Thu, mbtn_Fri, mbtn_Sat;

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

        mRepeat = (TextView) findViewById(R.id.mRepeat);

        mbtn_Sun = (Button) findViewById(R.id.mbtn_Sun);
        mbtn_Mon = (Button) findViewById(R.id.mbtn_Mon);
        mbtn_Tue = (Button) findViewById(R.id.mbtn_Tue);
        mbtn_Wed = (Button) findViewById(R.id.mbtn_Wed);
        mbtn_Thu = (Button) findViewById(R.id.mbtn_Thu);
        mbtn_Fri = (Button) findViewById(R.id.mbtn_Fri);
        mbtn_Sat = (Button) findViewById(R.id.mbtn_Sat);

        mbtn_Sun.setOnClickListener((View.OnClickListener) this);
        mbtn_Mon.setOnClickListener((View.OnClickListener) this);
        mbtn_Tue.setOnClickListener((View.OnClickListener) this);
        mbtn_Wed.setOnClickListener((View.OnClickListener) this);
        mbtn_Thu.setOnClickListener((View.OnClickListener) this);
        mbtn_Fri.setOnClickListener((View.OnClickListener) this);
        mbtn_Sat.setOnClickListener((View.OnClickListener) this);

        int hours = mTimePicker.getCurrentHour();
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mbtn_Sun:
                if (mRepeat.getText().toString().contains("Sun")) {
                    mRepeat.setText("");
                } else
                    mRepeat.setText("Sun");
                break;
            case R.id.mbtn_Mon:
                if (mRepeat.getText().toString().contains("Mon")) {
                    mRepeat.setText("");
                } else
                    mRepeat.setText("Mon");
                break;
            case R.id.mbtn_Tue:
                if (mRepeat.getText().toString().contains("Tue")) {
                    mRepeat.setText("");
                } else
                    mRepeat.setText("Tue");
                break;
            case R.id.mbtn_Wed:
                if (mRepeat.getText().toString().contains("Wed")) {
                    mRepeat.setText("");
                } else
                    mRepeat.setText("Wed");
                break;
            case R.id.mbtn_Thu:
                if (mRepeat.getText().toString().contains("Thu")) {
                    mRepeat.setText("");
                } else
                    mRepeat.setText("Thu");
                break;
            case R.id.mbtn_Fri:
                if (mRepeat.getText().toString().contains("Fri")) {
                    mRepeat.setText("");
                } else
                    mRepeat.setText("Fri");
                break;
            case R.id.mbtn_Sat:
                if (mRepeat.getText().toString().contains("Sat")) {
                    mRepeat.setText("");
                } else
                    mRepeat.setText("Sat");
                break;
            default:
                    mRepeat.setText("Never");
        }
    }
}

Image :

enter image description here

By default the TextView text is "Never".

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Nikson
  • 900
  • 2
  • 21
  • 51

3 Answers3

2

You can define a TreeMap as:

private TreeMap<Integer, String> mAlarmDays = new TreeMap<>();

as a field of your class and add/remove the days to/from the TreeMap when the corresponding button is clicked. So the implementation of onClick method will be:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.mbtn_Sun:
            if (mRepeat.getText().toString().contains("Sun")) {
                mAlarmDays.remove(0);
            } else
                mAlarmDays.put(0, "Sun");
            break;
        case R.id.mbtn_Mon:
            if (mRepeat.getText().toString().contains("Mon")) {
                mAlarmDays.remove(1);
            } else
                mAlarmDays.put(1, "Mon");
            break;
        case R.id.mbtn_Tue:
            if (mRepeat.getText().toString().contains("Tue")) {
                mAlarmDays.remove(2);
            } else
                mAlarmDays.put(2, "Tue");
            break;
        case R.id.mbtn_Wed:
            if (mRepeat.getText().toString().contains("Wed")) {
                mAlarmDays.remove(3);
            } else
                mAlarmDays.put(3, "Wed");
            break;
        case R.id.mbtn_Thu:
            if (mRepeat.getText().toString().contains("Thu")) {
                mAlarmDays.remove(4);
            } else
                mAlarmDays.put(4, "Thu");
            break;
        case R.id.mbtn_Fri:
            if (mRepeat.getText().toString().contains("Fri")) {
                mAlarmDays.remove(5);
            } else
                mAlarmDays.put(5, "Fri");
            break;
        case R.id.mbtn_Sat:
            if (mRepeat.getText().toString().contains("Sat")) {
                mAlarmDays.remove(6);
            } else
                mAlarmDays.put(6, "Sat");
            break;
    }
    StringBuilder repeatDays = new StringBuilder();
    if (mAlarmDays.size() == 0) {
        repeatDays = new StringBuilder("Never");
    } else {
        for (String day:mAlarmDays.values()) {
            repeatDays.append(day).append(" ");
        }
    }
    mRepeat.setText(repeatDays.toString());
}
Abdul Wadood
  • 1,161
  • 1
  • 10
  • 18
  • @Nikson Welcome – Abdul Wadood May 12 '18 at 16:21
  • Although this solution is better than mine in some perspective, I just want to make it the best one. `Map` should be avoid in Android, `SparseArray` is born as a replacement because of better memory allocation only if you use `int` as the key. Pros and cons are mentioned here https://stackoverflow.com/questions/25560629/sparsearray-vs-hashmap. Android also suggest this approach so consider using `SparseArray` if you can. – Tam Huynh May 12 '18 at 17:17
  • @TamHuynh We need the map to be sorted by keys. – Abdul Wadood May 12 '18 at 17:24
  • Yes I understand, it can be done by a for loop through index 0-6 and get the values. Trade off case again. But ok, it's up to each case and the developer style, I just think too much – Tam Huynh May 13 '18 at 00:18
0

You should set each button id first,add this to your xml for each specific button : android:id="sun" and ...

samy2k9
  • 16
  • 3
0

My suggestion is: use a single TextView can make your logic quite complex

Use a horizontal LinearLayout instead, you will have 7 TextView inside with predefine text and position. Just simply show/hide them according to which button is clicked and you don't have to deal with any complex string analize.

Tam Huynh
  • 2,026
  • 1
  • 16
  • 20
  • Don't you think this will lead to more code that has to be maintained? Also, more views will consume more memory. – Abdul Wadood May 12 '18 at 16:18
  • `More code to be maintained`: My instinct says no, your solution currently keep all the logic in 1 string only so it has to be a complex if-else code: find, replace, insert string into string is kinda painful. `more views consume more memory`: my experience says No matter. This is a trade-off situation, performance trade with memory and vice versa. Also I had to deal with a very big activity with nearly hundred views plus fragments before, and they are still in good shape. In your case I assume this is a Setting activity so it's not a main one, everything will be cleared when finish – Tam Huynh May 12 '18 at 16:30
  • 7-views-with-easy-logic trade with 1-view-with-painful-logic is worthy to me – Tam Huynh May 12 '18 at 16:33
  • Of course, you can go with it if it pleases you. We developers sometimes have different preferences. :) – Abdul Wadood May 12 '18 at 16:38