0

I have 10 EditTexts defined as L1, L2 etc. All of them are initially hidden. Based on a integer value - say 4, I would like to unhide only L1 to L4. Is there any method for doing so?

Something like this:

public void displaydynamicdata(int count){
    int i= 1;
    if(i <= count){
        StringBuilder toto = new StringBuilder("L" + i);
        "toto".setVisibility(View.VISIBLE);
        i++;
    }
}
1
  • Your loop would not work: The if-statement is executed exactly once, and that's it! And don't call your TextEdits L1, L2, ... The names must start with a small letter. Commented Dec 8, 2023 at 9:58

2 Answers 2

1

You can solve your problem a bit more elegantly using a HashMap:

EditText et1 = findViewById(R.id.et1);
⋮   // Fill in the other 8 lines
EditText et10 = findViewById(R.id.et10);

HashMap<Integer, EditText> et = new HashMap<>();
et.put(1, et1);
⋮   // Fill in the other 8 lines
et.put(10, et10);

for (int i = 1; i<=4; i++) {
  et.get(i).setVisibility(View.VISIBLE);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I guess, this is the nearest and neat method. Once the initial definitions are done, it is easy for rest of manipulations. Accepted this as Answer
Update for not producing error. Objects.requireNonNull(et.get(i)).setVisibility(View.VISIBLE);
0

In this given scenario you can implement the when statement (switch-case kotlin) for reference here is the answer How to implement switch-case statement in Kotlin

You can set the flag in your XML and set true or false depending on the scenario example code provided below

<EditText
        android:id="@+id/yourTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Text"
        android:visibility="@{viewModel.l1 || viewModel.l2 ? View.VISIBLE : View.GONE}" />

Hope this helps.

3 Comments

If we use switch case, still we need to fill the commands manually. What I need is for L1 to L4 unhide the EditTexts
I'm trying. But how to enable for 10 different cases in Java? Can we get something like this - "toto".setVisibility(View.VISIBLE); maybe contents of toto (&toto or something like that). I use java
No that's not possible you have to write the cases

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.