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

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

Related

Set The Visibility of The Whole Box

So in my app I have a registration form used to register other users. In my app I have 3 ranks (Admin, Patient, HCP). In the 'New HCP' form I want to hide something that is in my .xml file for the TextView. I am well aware of setVisibilty(View.GONE); but this doesn't work for my options because I have the boxes in a Constraint Layout.
My Question is:
How do I hide the whole box including its contents?
I tried this (but it only hid the contents and left the drawable box behind:
TextView consent_TextView = (TextView) findViewById(R.id.Register_text_Activity_consent);
consent_TextView.setVisibility(View.GONE);
TextView remind_TextView = (TextView) findViewById(R.id.Register_text_Activity_remind);
remind_TextView.setVisibility(View.GONE);
Please remember that I want to change this in Java not XML because if I do it in XML I will hide the box for everyone. Thanks!
As said in the comments, you can get a reference to the box you want to set the visibility either through View#getParent() or you can set its id programatically via View#setId(), bind it and set its visibility.

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.

Change the look of an EditText prgrammatically

I'm making an app that works as follows:
when you click a button, there will appear an EditText and a Button and a TextView.
I have already done that, but the style is not the same as the style when I create the EditText in xml (it looks different, the colour is not the same).
There will always be a couple of EditTexts, so the user will see the difference very well.
I also prefer the colour of the EditText when I create it in xml.
How can I change the style of an EditText programmatically?
Thanks in advance!
http://i.stack.imgur.com/24dzI.png (picture from the difference)
The issue here is that the background is being set differently between the two fields. You can just use setBackground(...) to have it match the other fields.
If you need to get the proper background drawable, look up one of the existing fields, grab the background from there, and assign it to your dynamic field.
http://developer.android.com/reference/android/widget/EditText.html
You can use EditText.setTypeFace() or EditText.setTextAppearance(), depending on what you're trying to change. The link has all the methods that EditText can perform.

Get overall string text of different dynamic views

I have an android fragment that looks something like this:
Every time the user clicks add/remove a new row is added/removed. When they click okay, then I need to return a string for all the views above, for example "TextATextBTextCTextD".
What would be a good way to go about this? I thought about adding tags for each new view, then doing a for loop through each view. But because I have spinners and edit views, I wasn't sure how to get the view for each then get their text.
Any help would be appreciated. Thanks.
Well, your tag option isn't bad... everytime you change one of the view's values, you could set the tag of the view to the string value, then iterate over the views and just collect the tags.
Another way to do it, would be to create your own custom spinner / edittext & make them all implement an interface which has a method called String getDisplayText()
Then, whenever you create a view, place it in an array of that interface type, then iterate over all your objects and call getDisplayText() on all of them.
Hope this helps :)

Getting Button in Android Search Dialog

how can I access the button on the left in androids search dialog?
http://developer.android.com/images/search/search-ui.png
In the picture above I want to set an onClickListener onto the books icon.
Thanks in advance
Unfortunately, there's no way to do it. You could get to the layout and its elements if you had necessary ids. But the ids of the elements used by SearchDialog are not public, that's why you don't have access to them.

Categories