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

Sally_san
So I’ve got a client that wants to build a site like this: Specifically 3 things from the website: The fullscreen sections that chang...
New
Zuber
How to make a website like webnovel and wattpadd where subscribers and logged in user post stories like their own Novel in a website and ...
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
tomcatttttt
I’m trying to take the API from the site to get a seamless online game through the HTML5 API. It works in all browsers except Google Chro...
New
Fl4m3Ph03n1x
Background I have a button that may be disabled or not, depending on a set of conditions. I want to disable/enable the button without hav...
New
Fl4m3Ph03n1x
Background I have Phoenix umbrella application. When inside said application, I can run it without issues if MIX_ENV=prod. However, if I ...
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
Julien0577
Hi all, Anybody knows how to do this menu animation? (from BBVA APP, they have the same for both android and iOS app). Is it custom?...
New
avipal
I have an application where it is three layer 1st layer- A legacy core routines 2nd layer- built on top of the Core routine using Dotno...
New
hosseinkhosromanesh
hello , i should code a cluster like image bellow we have no challenge in coding backend but in front need some clue to do this its a dy...
/js
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1139 25467 754
New
New
sir.laksmana_wenk
I’m able to do the “artistic” part of game-development; character designing/modeling, music, environment modeling, etc. However, I don’t...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New