0

I am trying to display some textviews visible in runtime. Here's my code:

for (int i=0; i<arrBool.length; i++) {
    arrBool[i] = r.nextBoolean();
        if(arrBool[i]==true) {                  
            textView[i].setVisibility(View.VISIBLE);
 }
}

When I run the application, textviews should randomly be visible. It is working, but my problem is I have set the layout of those textviews. When I run Android Application, the visible textviews go to top left corner and loses the layout position.

How to deal with this?

David Dimalanta
  • 548
  • 6
  • 19
Mac
  • 27
  • 2
  • 9

3 Answers3

2

Change start visibility parameter of views to View.INVISIBLE It will hold their own places on the layout and prevent from taking this places by other views, which is normal behavior in case of View.GONE

teoREtik
  • 7,886
  • 15
  • 46
  • 65
  • But I want those textviews visible according to boolean array value. If textview[1] is true then only it will display the textview otherwise not. My problem is when I do that it just changes the x,y position and start from the top left. – Mac Nov 07 '12 at 17:44
  • How do you specify an amount of your `TextView`s. Does it change dynamically or it is constant value? – teoREtik Nov 07 '12 at 17:48
  • It is constant. There are 6 textviews. And it has layout set according to background image. How can I keep those layouts constant, when it runs. – Mac Nov 07 '12 at 17:49
  • Each textview is separately specified. I have specific layout for each one of those. – Mac Nov 07 '12 at 18:00
  • Could you make a screenshot? – teoREtik Nov 07 '12 at 18:05
2

Adding more to teoREtik solution.

In your layout do not specify the android:visible property.

for (int i=0; i<arrBool.length; i++) {
    arrBool[i] = r.nextBoolean();
    if(arrBool[i]==true) {                  
        textView[i].setVisibility(View.VISIBLE);
    else 
        textView[i].setVisibility(View.INVISIBLE);
    }
}
PravinCG
  • 7,688
  • 3
  • 30
  • 55
0

I don't think using the FOR loop will help you out making the random possibility of the visible text view everytime you open. Why don't you just use the integer (0 for false and 1 for true) and a Random like this:

Random rnd = new Random(1);  // --> This will randomize numbers up to one;
int enable =  rnd.nextInt(); // --> Get the random value from 0 to 1

if(enable == 1) // --> If visibility of the text field is enabled everytime you opened the app...
{
      textView.setVisibility(View.VISIBLE);
} else {
      textView[i].setVisibility(View.INVISIBLE);
}

You can modify more and experiment on the randomizer for the integer's value by visiting this example at " How can I generate random number in specific range in Android? " topic. Check all the possible answers. Don't mind the green check mark and focus these answered codes by investigate its comments. I'm sure all of these answers about "random" topic, the link I gave you, is guaranteed effective.

About the layout, I recommend not to use the relative layout and instead use the linear layout because using the linear layout stays on the original place proportionally since relative layout is screen resolution dependent on the coordinates. Also try practicing manipulate the string value dimensions under res folder to maintain the size of the text proportionally in different screen resolution (from HVGA to WVGA). Check at " Different font sizes for different screen sizes " for more details about text size proportion.

Community
  • 1
  • 1
David Dimalanta
  • 548
  • 6
  • 19