Android Studio Development. EditText - java

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.

Related

Create a list dynamically of editText in android

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.

How can I define an ID for my EditText field programmatically?

I have an Activity in my App that adds more EditText fields to my view when a button is clicked.
Now I want to create a new ID for every created EditText-Field. Then I want to use
editText.setId(createdID);
And then I want to add this editText to an ArrayList (thats why I need the ID!)
editTextList.add((EditText) findViewById(createdID));
Any ideas? Thanks!
You can set it with TextView.setId(int id).
Try to see this question for more details.
Android: View.setID(int id) programmatically - how to avoid ID conflicts?
Rather confusing idea. But if you really need - you can set tags to yours EditTexts

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));

Android EditText does not show all text

I have a code which retrieves HTML source code of a page. I can retrieve and display the full source code on a TextView without any problem. Then, I want to pass all content of TextView to EditText in order to enable Editing the code. But when I pass source code to EditText, EditText does not show all of the code. The code is broken from random locations. It shows some part of the code. I am totally tilted.
Some details:
TextView is in another activity and EditText is in another activity, I am passing TextView content to EditText with Intents.
Do you have any idea?
Thanks in advance.
BR,
Ali
Try this:
EditText text = new EditText(this);
text.setText(Html.fromHtml(html));
text.setMovementMethod(LinkMovementMethod.getInstance());
All the time, I have counted the number of characters that I am passing to the edittext, it is revealed out that whatever website source code I am previewing the count is 10000. And the solution is so simple:
android:maxLength="100000"
That is it! Now I can view up to 100000 characters.

Update TextView numerous times with Buttons

I am making a simple shopping cart application where the user can select items (via buttons) and their running total will be displayed in a text view.
I am fine with having the text view being updated on a single click but I am struggling to figure out how to write my code if the same button is pressed more than once. For example a button writes a value of £3 into the TextView, if this button was clicked again I want the TextView to increase to £6 and so on.
Further to this I want to be able to have more than value added to the TextView from different buttons. I imagine this is more of a Java question as opposed to Android but seeing as I'm a bit of a newbie to both all advice is welcome!
Into your class if you have price value add an incerementPriceValue and make get/set functions for the last one.
If user press the button
setIncrementValue(getIncrementValue()+priceValue);
setText(getIncrementValue+simbolstring);
Create a int variable count and on the click increase the count value and set that value on the textView

Categories