I have a string with the name of my button. Say it is called String A.
String A = myButtonName;
Now, if I want to remove the button by doing:
layout.removeView(myButtonName);
This would work, but, I can't do that on a string.
How can I do it on my string?
Like this, right now I am getting an error, since it is a string:
layout.removeView(A);
How can I remove a view with a string which corresponds to a view?
Theoretically, I want to typecast my string to a ViewGroup
Simplified question:
I have a string. That string is also the variable name of my button.
Can I remove the button using the string?
Why won't you do it this way??
View namebar = view.findViewById(R.id.namebar);
((ViewGroup) namebar.getParent()).removeView(namebar);
You cannot directly cast String to View or ViewGroups but you can try this method:
Check all button name and compare it with you button name . Use
button.getText() to get the text of button and then compare it with
your button name "myButton".
If a match found, remove that button using your removeView(Button).
Else, ignore.
Firstly, I couldn't understand that why you have to reversely to find a view from a string. Still, whatever condition you are in, I am answering your question.
I read your answer too, it might give you desired output, but I feel the approach is wrong.
So, you have a String having the button's id name. So, you can get it's resource_identifier(int) from the string. like
String btnName = "btn_test";
int id = getResources().getIdentifier(btnName, "id", getPackageName());
Button btnTest = (Button)findViewById(id);
So, once you got a btnTest holding the View, you can remove it. With this method you won't need to hardcode anything. Hope this helps..
I got it! So, because I have only a string, I can just check to see if that string equals the object:
if(myButtonName.equals("Blue")){
View myView = (View) findViewById(R.id.Blue);
((ViewManager)v.getParent()).removeView(v);
}
Then I can manually remove the object itself. Because, as Sai mentioned, it is impossible to remove a view with a string object; I just removed the view after checking it through an if statement, and replacing the object.
You can also use switch:
switch(hello) {
case "Blue":
View myView = (View) findViewById(R.id.Blue);
((ViewManager) v.getParent()).removeView(v);
break;
Related
I have textview where user are asked to enter some information and that information is uploaded in Firebase Data Structure and then is Displayed on another activity
Here is the code I'm using to getText from Textview
etAuthor = (EditText) findViewById(com.nepalpolice.bookbazaar.R.id.editText1);
String bauthor = etAuthor.getText().toString();
and it does job pretty well.
and it is added to firebase.
But what if I want to add predefined Text like
Author:getText()
Here I have added author.
and This will be upudated on Database as well and will Displayed to user
instead of Consider Author is
J. K. Rowling
It will show
Author:J.K Rowling
Any help is appreciated.
Thanks in advance.
you can concatenate the desired string, in you case:
String bauthor ="Author:"+etAuthor.getText().toString();
String bauthor = "Auther : "+etAuthor.getText().toString(); //use this, '+' use for concatenate
#Bir Nepali, you have to just append while getting the text.
etAuthor = (EditText) findViewById(com.nepalpolice.bookbazaar.R.id.editText1);
String bauthor = "Author:" + etAuthor.getText().toString();
Now you can populate this bauhtor in the view.
I am trying to create a boolean from an Edit Text field.
I've already converted it into a string but need to make a boolean from the answer. I want to assign the correct answer as 1 and 2 as the wrong answer. If you can give me a tip how to make sure they enter only 1 or 2 that would be great!
This is what i have so far:
public void EnterAnswer(Editable insertAnswer) {
EditText enterAnswer = (EditText) findViewById(R.id.editText_question_six);
String answer = enterAnswer.getText().toString().trim();
}
First and foremost, using an EditText, there is really no way of specifying the values a user can input. Best you can do is to use the input type attribute by setting the input type to "number" as shown below. This way, a user would only be able to input numbers.
android:inputType="number"
You can also use the setError() method of the EditText to display an error if the user inputs a value that is neither 0 nor 1. This is illustrated below:
if(!(editText.getText().toString().equals("0") || editText.getText().toString().equals("1"))){
editText.setError("Wrong input, you can only input 0 or 1");
}
Follow this procedure:
Get the text from your textview
Check whether the text is one or two
assign a value to your boolean variable based on the result above.
Illustration:
public boolean EnterAnswer(Editable answerView){
boolean result = false;
String answer = answerView.getText().toString().trim();
if(answer.equals("1")){
result = true;
}
return result;
}
Following the logic above, the method will only return true if the answer (from the EditText) is one.
I hope this helps... Merry coding!
I was implementing the following Java code in Android Studio:
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(number);
...
}
This is a part of a larger application.
As you can see, I've passed only an integer value to the quantityTextView.setText(number) method.
When running the app, it crashes as soon as this method is called. Can you tell me why such a thing is happening?
Yes, use String.valueOf(), like this:
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(String.valueOf(number));
}
Because setText() accepts only String values or Resource ID of a String (which is infact int).
Check here: setText() Method
You can use String.valueOf(number); as input parameter of setText() or you can refer to String ID in XML with getResources().getString(R.string.number) as input value.
Convert the integer to string before putting it in the TextView:
quantityTextView.setText(Integer.toString(number));
or simply
quantityTextView.setText(number+"");
The reason your code is crashing is that setText(int) expects a resource ID. It's not very well documented, so you'd be forgiven for thinking that you could pass it an integer and have the TextView convert it to text.
You should first convert it to a String, for example with:
String.valueOf(number)
and then it will be alright.
setText() method of TextView accepts CharSequence, not integers. So, you must convert your number to String before.
Try to use this:
quantityTextView.setText(Integer.toString(x));
The reason is that, setText() only expects string or char[].
So either you can perform type casting or you can add quotes with the number
(1). by type casting
String.valueOf(number)
(2). by adding "" with the number
quantityTextView.setText(""+number);
or
quantityTextView.setText(number+"");
So my app needs to read text from a text box with a tag of "inText" do stuff to it (that stuff works) then write the output to a box with the id of "outView". I've been doing this with setText() and getText().
setText() was for writing the output below is what I used:
(TextView)findViewById(R.id.outView.setText(textoutput));
getText() was for reading the input text then writing it to a variable and below is what I used:
String mEdit = (EditText)findViewById(R.id.inText.getText()).toString();
You're chaining the method at the wrong spot. Remove .getText() from R.id.inText and place it after the brackets like (same thing for TextView):
String mEdit = ((EditText)findViewById(R.id.inText)).getText().toString();
Though this is an uncommon way to do thing. Rather initialize the EditText first and then get the text, it's much clearer that way:
EditText mEdit = (EditText)findViewById(R.id.inText);
String mText = mEdit.getText().toString();
You set the getText() and setText() method in wrong place.
getText() and setText() are methods of TextView and EditText classes.
But here you used it as a method of id. That's why it's showing "Can't resolve method getText/setText()". As id has no such methods.
You can do the following.
((TextView) findViewById(R.id.outView)).setText(textoutput);
and
String mEdit = ((EditText) findViewById(R.id.inText)).getText().toString();
It's not working since you need to first set the TextView:
TextView tvOut = (TextView) findViewById(R.id.outView);
TextView tvIn = (TextView) findViewById(R.id.inText);
String out = tvOut.getText().toString();
String in = tvIn.setText(out);
If you use a field check the compiler didn't automatically set it as a view instead of EditText type.
Also the casts are redundant now so you better not use them if you don't have to
I am looping through a list of checkboxes upon click of a button. What I am looking to do is grab the name of the checkbox at runtime to strip out an integer value specified within the name. I am not looking to get the value nor the id. So in the strings.xml file, <string name="checkList1">Pain assessment.</string>, I am trying to get checkList1 at run time. I can get the text without a problem. Currently I am looping through the view elements with the code below:
RelativeLayout root = (RelativeLayout)findViewById(R.id.Root);
for (int i = 0; i < root.getChildCount(); i++)
{
View v1 = root.getChildAt(i);
Class c = v1.getClass();
if (c == CheckBox.class)
{
CheckBox thisBox = (CheckBox)v1;
if (thisBox.isChecked())
{
String text = (String)thisBox.ge;
DoDailyCheckListUpdate(thisBox.isChecked(),checkBoxCount);
countItemsFinished++;
}
checkBoxCount++;
}
}
What I am looking for is to somehow get the name of Checkbox thisBox. So when it loops through and hits the Pain Assessment checkbox, I want to be able to pull out checkList1. Without going as far as ripping through the strings.xml file based on the text I find to get the name, I was hoping maybe there was a simpler solution that I maybe overlooking.
Thank You in advance.
CheckBox extends from TextView so to retrieve a text from it is quite simple :
String text = thisBox.getText().toString();
http://developer.android.com/reference/android/widget/CheckBox.html
If you want to retrieve the key name of the string. I suggest you put it into the tag of the object :
thisBox.setTag(getResources().getResourceEntryName(R.string. checkList1);
Retrieve it like that :
String text = (String)thisBox.getTag();
that should do the trick.