pjamesrud

pjamesrud

Android app, using a loop value to set visibilty and add data to textview?

I am creating an app that allows user to enter values and depending on the value a number of textviews are changed programmatically from gone to visible.
In the xml file there is TV!,TV2 and TV3, The user enters a couple of numbers from previous activity and these are used to set the visibility of the TV1,TV2 and TV3 textviews

Example:
            User enters 2.
Using a for loop 
        for (int i = 0; i<= userinput; i++){
          showTV = "TV"+(i);
          showTV.setVisibility(View.VISIBLE);
          showTV.setText(String.valueOf(list1.get(1)));

But I can not get the textview’s Id to be recognized using the i value

Can it be done this way or is this the wrong direction.

First Post!

Veeran

Veeran

It seems like you’re on the right track, but there’s a small mistake in your approach. When trying to dynamically set the visibility of TextViews based on user input, you should use the resource identifier (R.id) to access the TextViews rather than concatenating a string.

You can achieve this by creating an array of TextViews in your Java code that corresponds to the TextViews in your XML layout. Then, you can iterate through this array to set the visibility of TextViews based on the user input. Here’s a simplified example:

// Assuming you have TextViews in your XML with IDs TV1, TV2, and TV3
TextView tvArray = new TextView{findViewById(R.id.TV1), findViewById(R.id.TV2), findViewById(R.id.TV3)};

int userInput = 2; // Example user input

for (int i = 0; i <= userInput; i++) {
tvArray[i].setVisibility(View.VISIBLE);
tvArray[i].setText(String.valueOf(list1.get(i))); // Assuming list1 contains the values you want to set
}

Best Regards

Popular Frontend topics Top

david-j-m
Hi I have a gallery site which displays paintings according to selected category (oils, water-color, etc.) “on:click” event. When this e...
/js
New
sona11
I have a 1D array of numbers and need help splitting them into groups using a jagged array so that I can perform a series of computations...
New
harwind
First have a look at the code: function mainfunc(func, par3, par2){ window[func](par3, par2); } function calledfunc(par3, par2){ ...
/js
New
Fl4m3Ph03n1x
Background I have a a fresh umbrella project with a Phoenix app inside. To create the app I used the following commands: mix new test_a...
New
JessicaW33
Hello All, How would you implement a navigation system in React Native? Could not find a way so I ask the community can someone help me ...
New
JessicaW33
Hello everyone, I’m working on optimizing the performance of React Native applications and would like to gather insights and suggestions...
New
Fl4m3Ph03n1x
Background I have a Phoenix application, where all pages (expect the login page) have a menu at the top. This menu will therefore only a...
New
Fl4m3Ph03n1x
Background I have a custom component in my LiveView, which is basically a group of checkboxes. I want to add a new attribute to my custo...
New
gameengineer
We are developing on Samsung Tab Active 4 Pro using Android Studio, kotlin and java. We are getting what we think are app deadlocks. The ...
New
PickyBiker
I have done small amounts of programming for IOS and for Android, but now I need to create something that works with both. What are the ...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
New
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New

Latest in Questions

View all threads ❯