Create a list dynamically of editText in android - java

I have an EditText for the user to write how many players will be in a game. Then I have a ScrollView with a vertical LinearLayout where I want to create as many EditTexts (for the player names) as the user gave in the first editText. How can I do that?

Its really simple. Instead of using xml layout view you have to create it programmatically in java file.
Eg. to create an edit text you can use:
EditText myEditText = new EditText(context); // Pass it an Activity or Context
myEditText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
myLayout.addView(myEditText);
And after that add a button which fetch No. of players (lets say 5) from edit text and hence create 5 edit text using a for loop.

Related

Spinner inandroid studio

I have 2 spinners and 1 edit text. How to get selected spinner item. I need to know witch ones are selected, so I could show conversion resoult in other activity.
First spinner have 2 items ( Celsius and Fahrenheit) second also. How to call one of 2 methods depending on spinner item select.
Create a reference for your spinners. Extract the selected value of the spinners like this:
String spinnerValue = String.valueOf(spinnerReference.getSelectedItem());
Considering that spinnerValue can contain either "Farenheit" or "Celsisus", you can use a switch block to call the appropriate method.
If you want to check currently selected item in the spinner you should have an instance of it. If you don't make sure you assign an id (for example "unitSpinner") to it in the layout file and then in the Activity's onCreate method call Spinner spinner = (Spinner) findViewById(R.id.<idYouAssigned>). Then you can call spinner.getSelectedItem() which will return either "Celsuius" or "Fahrenheit" depending on which one the user selected.

Android Studio Development. EditText

Im looking to make a simple app that involves the user to input an integer and depending on that integer, the same number of EditText will appear. For example, if the user inputs 5, then 5 EditTexts will materialize right below. I dont need help with the layout, just the java code. Im new to programming and im not quite sure how to approach this. thanks.
You can go for creating EditTexts programmatically inside a loop. Use
EditText editText = new EditText(this); // declare the editText
linearLayout.addView(editText); // add the editText to the parent view, wchich is a linear layout here. inside a loop with the number entered in editText as limit.
For more reference and example about creating views dynamically, refer this link.

Dynamically Creating/Removing Buttons in Android

Firstly, my apologies if this answer is already on here, as I've been searching for a few weeks and haven't found anything yet.
I am working on an Android app which needs to allow the user to create and remove buttons. I know how to normally create buttons statically through adding the button the XML file and creating it's functionality in the JAVA file.
Instead, I have a static button which I'll refer to as "Create Button". When the user presses on the Create Button, they should be given the option to add a new button to the current activity, allowing them to change the title of said button etc. When they close the app and open it back up; the button they added should still be there. Similarly, they should be given an option to remove buttons.
Can someone point me in the right direction? Most of the sources that I've come across only explain how to statically create buttons, like I first mentioned.
Thanks for the help!
EDIT: I was able to figure some stuff out based off of the feedback I've been given.
So far I have the following code in the onOptionsItemSelected( ) method:
if (id == R.id.add_button)
{
Button myButton = new Button(this);
myButton.setText("Push Me");
//myButton.setVisibility(View.VISIBLE);
return true;
}
I am still a little confused about how this can get added to the layout. Mainly, I am confused about the findViewById call:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.?);
Which id should I be using? In the app's main XML file, there is no ID for the layout itself. It's just a "RelativeLayout".
EDIT2:
Actually, I solved the problem. Thanks for the advice! I just needed to give my layout an ID in the XML file. I knew that I could give buttons etc an ID, but never knew that I was able to do so for the actual layout itself!
Creating a button -
Button myButton = new Button(this);
Adding text to it -
myButton.setText("Push Me");
To make the button visible, you need to add it to a view like this. You can also add it to a statically created view -
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
Removing button -
ll.removeView(myButton);
For additional customizations, check documentation.
If you are creating multiple buttons, then I recommend setting id. This example makes it clear.
For making buttons visible after closing the app, you need to store the data on memory. The simplest way to do this is to maintain a record of the buttons and their specifications and storing them before closing the app. After opening the app, you can read the stored data and create the buttons accordingly.
For more details, check Data Storing.
ViewGroup mViewGroup = (ViewGroup) findViewById(R.id.main_layout_id);
mViewGroup.addView(yourButton, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

How can i add dynamically Images in a Activity in Android

im coding an android app for a board game. The user can choose in the main activity how many dices he want to roll(1 up to 3).
I would like to use only one activity to display the result of the dices as images. The user can go back to the main activity and choose a other amount of dices.
My problem is, how can i dynamically add the needed imageviews for the result images into the second activity where i want to display the results of the choosen amount of dices?
Create a new ImageView object, set its properties, and then add it to an existing layout. Here is the most basic form, but you will need to add LayoutParams as well to get the desired location.
ImageView iv = new ImageView(getApplicationContext());
iv.setImageResource(R.id.myimage);
someView.add(id);
See the ImageView Documentation for more usage details.

Android varying number of textviews to display

For exmaple I have int variable "number" that is passed throughout the program
and if the number = 7 I want to create 7 identical textviews and create 7 IDs for each of them
if the number = 5 I want to create 5 textviews etc.
My guess is that I should not create any textviews in my xml file and rather create views in my java code. Is this the right approach?
I would do this with a XML holding the EditText and a ListView in another xml, then in the Adapter of that ListView you can inflate and instanciate the EditText XML items.
This way you stay much cleaner and it might be less work if you change style etc. You do not need to take care of a scrollview yourself for more items.
Also you can have items and ListView adapting to screendensity, screensize, orientation etc. without going through a hell of code - if this is a requirement.
If the last item in the list needs to be a button, you could inflate and instanciate such an item layout too in the Adapter.
If your layout is unknown at compile time (for example, the number of Views is not known), then your only choice is to do it programatically.

Categories