-3

I have four TextViews in my activity. Based on an incoming message, I want to change the format of these four TextViews so that one is larger and bold while the rest are smaller and regularly styled. To do this, I create two methods, setTextNormal and setTextBig. In order to change the numbers I am using this bit of code:

                if (tokens[2].equals("0")) {
                    setTextBig(text1);
                    setTextNormal(text2);
                    setTextNormal(text3);
                    setTextNormal(text4);
                }
                if (tokens[2].equals("1")) {
                    setTextBig(text2);
                    setTextNormal(text1);
                    setTextNormal(text3);
                    setTextNormal(text4);
                }
                if (tokens[2].equals("2")) {
                    setTextBig(text3);
                    setTextNormal(text2);
                    setTextNormal(text1);
                    setTextNormal(text4);
                }
                if (tokens[2].equals("3")) {
                    setTextBig(text4);
                    setTextNormal(text2);
                    setTextNormal(text3);
                    setTextNormal(text1);
                }

Notice that for each if statement, one of the TextViews are being set to big and the rest to normal. It works, but is there a more efficient way for me to set these TextViews?

AreM
  • 97
  • 10
  • 2
    You could setTextNormal all views before the ifs then setTextBig the view you want in the if block. – assylias Aug 26 '15 at 16:07
  • @AreM Please work harder to make the title of your Question specific and meaningful. You can use the Edit link/button to change it. – Basil Bourque Aug 26 '15 at 16:31
  • 1
    If your two methods are already written, I would recommend using my solution. If they have not yet been written, I would go with the solution @Andreas proposed. – cadams Aug 26 '15 at 16:51

7 Answers7

2

Make an array of your text variables like [text1, text2, text3, text4]

Set all texts normal at the start, as other users have suggested, and then do: setTextBig(texts[tokens[2]]);

cadams
  • 1,299
  • 1
  • 11
  • 21
2

I would suggest changing your code to have a setTextSize(text, boolean big) method, and use it as follows:

String token = tokens[2];
setTextSize(text1, /*big*/token.equals("0"));
setTextSize(text2, /*big*/token.equals("1"));
setTextSize(text3, /*big*/token.equals("2"));
setTextSize(text4, /*big*/token.equals("3"));
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I wonder why I got a down-vote. The question stated that `setTextNormal` and `setTextBig` was created by OP, so going with a different implementation is a very valid answer. --- But just in case it's because someone don't like that you have to look at the signature of `setTextSize` to see what parameter 2 means, I'll add comments to help any reader of the code. That's a good pattern anyway, especially for boolean parameters. – Andreas Aug 26 '15 at 16:22
  • Very clean and elegant. Great answer! – Phantômaxx Aug 27 '15 at 07:08
0

Well not sure if more efficent by much really but just setting all text to normal first and then only changing the specific text to large works such as.

setTextNormal(text4);
setTextNormal(text2);
setTextNormal(text3);
setTextNormal(text1);
if (tokens[2].equals("0")) {
    setTextBig(text1);
} else if (tokens[2].equals("1")) {
    setTextBig(text2);
} else if (tokens[2].equals("2")) {
    setTextBig(text3);
} else if (tokens[2].equals("3")) {
    setTextBig(text4);
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
Ashley Alvarado
  • 1,078
  • 10
  • 17
0

Probably you can do something like that.

 setTextNormal(text1);
 setTextNormal(text2);
 setTextNormal(text3);
 setTextNormal(text4);

 if (tokens[2].equals("0")) {
     setTextBig(text1);
 } else if (tokens[2].equals("1")) {
     setTextBig(text2);
 } else if (tokens[2].equals("2")) {
     setTextBig(text3);
 } else if (tokens[2].equals("3")) {
     setTextBig(text4);
 }

Check if it is possible to set a text as normal and after set it as big.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0
void setNormal(){
    setTextNormal(text1);
    setTextNormal(text2);
    setTextNormal(text3);
    setTextNormal(text4);
}

switch(tokens[2]){
    case "0":
        setNormal();
        setTextBig(text1);
        break;
    case "1":
        setNormal();
        setTextBig(text2);
        break;
    case "2":
        setNormal();
        setTextBig(text3);
        break;
    case "3":
        setNormal();
        setTextBig(text4);
        break;
    default:
        break;
}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

Extract the repeated similarity into a method

setText(String large, String ... small)

// note 2nd arg is vararg which is one or more arguments

//setTextLarge(large)

//Iterate over small which is a vararg array and setTextSmall

Then to use the extracted method

If token is 0 setText(text1, text2, text3)

If token is 2 setText(text2, text1, text3)

If token is 2 setText(text3, text1, text2)

0
    if (tokens[2].equals("0")) {
            setTextsOrder(text1, text2, text3, text4);
        } else if (tokens[2].equals("1")) {
            setTextsOrder(text2, text1, text3, text4);
        } else if (tokens[2].equals("2")) {
            setTextsOrder(text3, text2, text1, text4);
        } else if (tokens[2].equals("3")) {
            setTextsOrder(text4, text2, text3, text1);
        }
    }
    . 
    .

    private static void setTextsOrder(String text, String... texts) {
        setTextBig(text);
        for (String textIt : texts) {
            setTextNormal(textIt);
        }
    }
Ahmad Al-Kurdi
  • 2,248
  • 3
  • 23
  • 39