Android get text from button - android

I have 6 Buttons on my screen
I need to check the text on each Button and if the result is a capital "X" then I need to make the Button invisible
I can get the text when a Button is clicked using this
Button b = (Button)view;
String text = b.getText().toString();
but how do you get it without clicking a Button?
Any help appreciated
mark

There are different options. If that is the only type of View then you can iterate through the layout's children with something like
for (int i=0; i<myLL.getChildCount(); i++) // assuming you have a LinearLayout named myLL
{
Button btn = (Button) myLL.getChildAt(i); // cast the View to a Button
String text = btn.getText().toString();
// check the text here and do what you need
}
Or you could put them in an ArrayList at the beginning then iterate through that and check the text with something like
ArrayList<Button> myBtns = new ArrayList<Button>();
myBtns.add(R.id.someBtnId);
// iterate through them when needed.
There may be easier ways with listeners and such but without more information this is the best I can give you.

In the onCreate of your activity (or the onCreateView of your fragment) you need to use findViewById to get the views on your app.
Once you have the views you can query their contents and set up listeners to see when they change.

Related

Set Id to dynamically created edittext

I'm dynamically creating Edittext on a button click and saving this edittext to an ArrayList of type Edittext and then getting values of all edittext in an ArrayList of type String. While creating new Edittext Iam also creating a BUTTON with cross image which delete that edittext when user click on it. and then Iam passing these edittexts to Options of CheckBox question which is also Iam creating dynamically.
Issue is when I create multiple edittext and randomly delete someone and click ok arraylist remove the last index of it .Iam also setting ids to Edittext but couldnt able to understand how to get the Id of the same Edittext whose value I want to delete and then remove him from ArrayList too.
Here I'am creating the Edittext and adding them in "moreOptList" with there ids.
EditText checkBoxMoreEdt = new EditText(this);
checkBoxMoreEdt.setId(i);
moreOptList.add(i, checkBoxMoreEdt);
Here Iam deleting the view on Button clickListener,but instead of deleting the the exact value from arrayList also its only delete the view and the last index of array from the list.For example if I have enter 4 options
a
b
c
d
and then deleted the option 2 which is b and click ok,it will show me
a
b
c
what its doing is removing the last save value from arrayList now How should I get the Id of exact edittext I want to delete and delete it from arrayList also.
final Button button = new Button(this);
button.setId(b);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chkbParentLayout.removeView(ll);
moreOptList.remove(i);
}
});
Most probably the problem is in ArrayList. When you remove element by index all elements are shifted, hence indexes do not match your views:
list.add(0, 0) //[0]
list.add(1, 1) //[0, 1]
list.remove(0) //[1]
list.remove(1) //throws IndexOutOfBoundsException
One options is to replace ArrayList with HashMap:
map = HashMap<Integer, View>()
map.put(i, view)
...
group.remove(map.remove(i))
Another useful thing is to use setTag/getTag methods of View, you can store your indexes there, so you might not need to have external indexes storage:
view.setTag(index)
index = view.getTag()

How to refer to a dynamically generated button?

I am trying to interact with dynamically generated buttons. I want to update text and background color for those player clicked and for those who is near by horizontal or vertical axis at the moment of a click.
What I've tried. I've found an id property of a button in XML which led me to idea that I can make a text key to refer to a programmatically generated button. But when I've tried to assign an id - IDE was expecting a number (Int), not a string. Since buttons form a square array - I decided to encode each button via 4 digit number where first 2 digits stand for a row number and other two stand for a column number. Though when I tried to use findViewById IDE told me that it was expecting a special id data type, not a number.
That's how it looks for now:
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
button.id = i*100 + j
constraintLayout.addView(button)
}
}
What idea or method could I look at?
If you created it dynamically you can save it in a variable (or an array) for later use.
val myButtons = ArrayList<Button>()
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
myButtons.add(button)
constraintLayout.addView(button)
}
}
if you have a layout with dynamically created views and you know their order you can get them with getChildAt(index).
Alternatively you can assign ids saved in xml like this.

How to get user inputs from multiple fragments added to single activity

As shown in image, I have added the same fragment 5 times in activity by performing on click operation on ADD FRAGMENT button:
Now I want to get all user entered data from those 5 edittext on click of GET DATA button. Is this possible?
(Both buttons is in Main Activity)
First we need to traverse to the dynamic edit text linear layout and then only we can count how many edit texts are avaliable then read the values of corresponding edit text :)
//step:1
LinearLayout layout = (LinearLayout)findViewById(R.id.LinearDynamicEditTextID);// change as your //dynamic EditTexts' parent Linear Layout id.
//step:2
for(int i=0;i<layout.getChildCount();i++)// Count how many children avaliable
{
View v = (View)layout.getChildAt(i);// get the child view
if (v instanceof EditText)// verifying whether the child is EditText for secure.
{
EditText editText = (EditText)layout.getChildAt(i);//thats it.
Log.w("Edit Text "+i+" Value" , editText.getText());
}
}
Now I want to get all user entered data from those 5 edittext on click
of GET DATA button. Is this possible?
Answer: Yes, this is possible.
How?
Solution: You can make public variables in the activity and you can access those variables from the fragment which you are adding. On click of GET DATA button, you can read those variables set by the different fragment and show them in the same Activity or in different Fragment.

List of Strings in multiple buttons

I have a list of Strings (Video tags) and I wanna show each of them in a button (same as when you want to send a text for multiple users) and when I click on each an action happens for that box. I took a look at:
https://github.com/kpbird/chips-edittext-library
It generate each string in a box but finally I don't have access to specific item onclick.
Does anyone has any suggestion what to use or how to make it?!
Read out the array of strings and
programmatically add a button with the text of each
string to for example a LinearLayout.
pseudo code:
ArrayList<string> list = getResources.array("id");
LinearLayout l = (Linearlayout) findViewById(R.id.linearlayout);
foreach(String s in list)
{
l.addView(new Button(s));
}
Something like that.

how to display text in edit text in the list view

My team and I are making an android chat application. We want to display the messages sent by the users in a ListView. How can we display the text entered in an EditText in the list view. Or is there any other way to achieve our goal?
It all depends on what type of adapter you're using to back the view. If you're using an ArrayAdapter, just add the text to the array each time you have a new message you want to display (for instance, every time a button was clicked). If you're using a CursorAdapter, you'd then have to add the text to the ContentProvider that the Cursor is viewing. Beyond that, I can't tell you much else. We'd need more details about what you're doing in order to answer any further.
I've done this with some different by your needs. I just get the editText values to my database and printed into EditText. Just see this code -
EditText b = (EditText)findViewById(R.id.editText1);
b.setVisibility(1);
output = (TextView) this.findViewById(R.id.editText1);
output1 = (TextView) this.findViewById(R.id.editText2);
String s1= output1.getText().toString();
this.dh = new DataHelper(this);
this.dh.insert(s1);
List<String> names = this.dh.selectAll();
StringBuilder sb = new StringBuilder();
sb.append("Names in database:\n\n");
for (String name : names)
{
sb.append("\t - \t" + (name) + "\n");
}
this.output.setText(sb.toString());
Just change this with your needs. Try out using this
You can do this using the custom list view. In the custom list view use the custom adapter in which you can take an edit TextView. You can see this how to implement custom list view here. In this list view a TextView is used but you can replace this TextView with EditText view.

Categories

Resources