Android compare Button with Textview value - android

Hell i am trying to compare textview value with button how can i do it?
i tried this way but not working.
button xml - android:tag = "1"
Java
Button btn = (Button) findViewById(id);
string valu = btn.getTag();
string txt = textView.getText();
if(txt.equals(valu))
{
// do what you want
}

try this:
textView.getText().toString();
getText() returns a CharSequence not a String.
Because of that your equals must be false.

Simply use this.
if (b.getTag().toString().equalsIgnoreCase(tv.getText().toString())) {
Toast.makeText(getApplicationContext(), "HI", 9000).show();
}

Related

How to manipulate the contents of a button without using id; Android?

I'm new in android world and I have a problem, well, I'm making a project very simple it is about an activity where i have a button and an EditText. The button has an event onClick in XML.
My problem is it: I need the button value and send this value to
EditText but my button don't have a id. Help me I don't know how manipulate a element if it dont have a id.
XML Code:
<View
android:layout_height="#dimen/cell_height"
android:background="#color/red"/>
<Button
android:layout_marginLeft="#dimen/button_margin"
android:layout_gravity="center_vertical"
android:text="#string/hex_red"
android:textColor="#color/red"
android:onClick="copy"/>`
Java code:
public void copy(View boton){
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
String color = boton; <-- here need the button value
txtSelected.setText(color);
}
I need your help, Thanks
you can say boton.getText().toString()
Modify your copy() function like this:
public void copy(View boton) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button) boton; // << key point.
String color = btn.getText().toString();
txtSelected.setText(color);
}`
public void onClickBtn(View view) {
EditText txtSelected = (EditText)this.findViewById(R.id.txtColor)
Button btn = (Button)(view);
String value = (String) btn.getText();
txtSelected.setText(value);
txtSelected.setSelection(value.length()); // cursor will be at the end of the text
}

How to display a parsed integer string in a textview

I'm having difficulties getting a string containing the user's input and displaying it in a textview when the button is clicked. For example, if the user types 10 and click the button, I want the textview to display 10. I get a warning saying that answerINT is never used.
The code I'm having difficulties with
//Fetches user answer and converts it into an integer
EditText answer = (EditText)findViewById(R.id.editanswer); {
if (answer.getText().toString().length() > 0){
int answerInt = Integer.parseInt(answer.getText().toString());
//Display answer in textview on click
TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer);
displayanswer.setText(answer);
}
The coode in it's context
findViewById(R.id.btnok).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("onClick", "OK button was clicked");
//Fetches user answer and converts it into an integer
EditText answer = (EditText)findViewById(R.id.editanswer); {
if (answer.getText().toString().length() > 0){
int answerInt = Integer.parseInt(answer.getText().toString());
//Display answer in textview on click
TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer);
displayanswer.setText(answer);
}
//Displaying a toast if no answer is provided
else
{
Toast.makeText(getApplicationContext(), "No answer provided!",
Toast.LENGTH_SHORT).show();
}
}
Try this
displayanswer.setText(" "+answerInt);
OR
displayanswer.setText(String.valueOf(answerInt));
Try this:
displayanswer.setText(""+answerInt);
You can use this:
String.valueOf(yourint);
You can simply do it this way:
if (answer.getText().toString().length() > 0){
//Display answer in textview on click
TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer);
displayanswer.setText(answer.getText().toString());
}

How can I change Text of Textview from other funcion?

this is my first question so I hope to make it clear.
I have one textView with some numerical text and next to it one button with one click listener and what I want is that when you click on the button the numerical value (>=0) of the TextView decrements in one.
Here is part of my code:
TextView Counter = new TextView(this);
if (intSeries != 0)
Counter.setText(Integer.toString(intSeries));
else
Counter.setText("0");
Counter.setId(4);
tablaContador.addView(Counter,Tr);
Button Done = new Button(this);
Done.setText("-1");
if (intSeries != 0)
Done.setVisibility(View.VISIBLE);
else
Done.setVisibility(View.GONE);
Done.setId(6);
Done.setOnClickListener(this);
And this is the onClick funcion (part of it):
#Override
public void onClick(final View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case 6:{
TextView text = (TextView)findViewById(4);
int series = Integer.parseInt(text.getText().toString());
series--;
text.setText(series);
if (series==0){
Button boton = (Button)findViewById(6);
boton.setVisibility(View.GONE);
}
}
}
}
The error is when I try to make the setText inside the onClick function, I hope it can be fixed or maybe recieve other idea to do it.
Thank you so much.
I would avoid all this hardcoding of Ids, use resources instead.
Your call to
text.setText(series)
is passing an int. The only valid setText(int resId) overload expects a resource associated with the int value, i.e. a string resource.
Convert your series value to a string.
Something like:
text.setText(Integer.toString(series));
You should setup series as an integer. And increase/descrease it as you wish. When you want to change the button's text convert the int to String.
Instead of:
text.setText(series);
use:
text.setText(String.valueOf(series));
Variablenames in java can't start with a capital letter. That is reserved for classnames.
Counter -> counter
Done -> done
I tried this and it worked:
//Create onClickListener
OnClickListener pickChoice = new OnClickListener()
{
public void onClick(View v)
{
TextView txt = (TextView) findViewById(4);
int number = Integer.valueOf(txt.getText().toString());
txt.setText(String.valueOf(number -1));
}
};
//Create layout
LinearLayout lnLayout = new LinearLayout(this);
lnLayout.setOrientation(LinearLayout.VERTICAL);
TextView txt = new TextView(this);
txt.setId(4);
txt.setText("0");
lnLayout.addView(txt);
Button Done = new Button(this);
Done.setText("-1");
Done.setId(6);
Done.setOnClickListener(pickChoice);
lnLayout.addView(Done);
setContentView(lnLayout);
Where are you creating your button inside? an activity? the part where you pass the onClickListener to the button doesn't make sense, maybe the button is getting a wrong listener and gets you an error every time you press the button ?
The code should be easy to understand, if there is anything you need me to explain please ask :)

How to get EditText value and display it on screen through TextView?

I want to get the user input for the EditText view and display it on the screen through TextView when the Button is clicked. I also, want to know what modifications can be done on the string.xml file to do this.
I didn't get the second question, maybe you can elaborate...but for your first query.
String content = edtEditText.getText().toString(); //gets you the contents of edit text
tvTextView.setText(content); //displays it in a textview..
I'm just beginner to help you for getting edittext value to textview. Try out this code -
EditText edit = (EditText)findViewById(R.id.editext1);
TextView tview = (TextView)findViewById(R.id.textview1);
String result = edit.getText().toString();
tview.setText(result);
This will get the text which is in EditText Hope this helps you.
EditText ein=(EditText)findViewById(R.id.edittext1);
TextView t=new TextView(this);
t.setText("Your Text is="+ein.getText());
setContentView(t);
bb.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
String s1=tt.getText().toString();
tv.setText(s1);
}
}
);
First get the text from edit text view
edittext.getText().toString()
and Store the obtained text in a string, say value.
value = edittext.getText().toString()
Then set value as the text for textview.
textview.setText(value)
yesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
eiteText=(EditText)findViewById(R.id.nameET);
String result=eiteText.getText().toString();
Log.d("TAG",result);
}
});
Easiest way to get text from the user:
EditText Variable1 = findViewById(R.id.enter_name);
String Variable2 = Variable1.getText().toString();
in "String.xml" you can notice any String or value you want to use, here are two examples:
<string name="app_name">My Calculator App
</string>
<color name="color_menu_home">#ffcccccc</color>
Used for the layout.xml: android:text="#string/app_name"
The advantage: you can use them as often you want, you only need to link them in your Layout-xml, and you can change the String-Content easily in the strings.xml, without searching in your source-code for the right position.
Important for changing language, you only need to replace the strings.xml - file
Use the following code when clicked on the button :
String value = edittext.getText().toString().trim(); //get text from editText
textView.setText(value); //setText in a textview
Hope to be useful to you.
Try this->
EditText text = (EditText) findViewById(R.id.text_input);
Editable name = text.getText();
Editable is the return data type of getText() method it will handle both
string and integer values
First get the value from edit text in a String variable
String value = edttxt.getText().toString();
Then set that value to textView
txtview.setText(value);
Where edttxt refers to edit text field in XML file
and txtview refers to textfield in XML file to show the value

how to take input from user in android

i have a EditText in android in which i want the user to enter the text and checks for the condition "BYE"
Code sample:
EditText text = (EditText)findViewById(R.id.EditText01);
String abc= text .getText().toString();
while( !(abc).equals("bye")){
abc = text.getText().toString();//user should enter the text from keyboard and the while loop should go and chech the condition.but not able to enter the text
//do some operation with abc
}
How can i make user to enter the text??The UI should wait for the text to be entered(something like we have InputStreamReader in java applications).
Very Simple:-
EditText text = (EditText)findViewById(R.id.EditText01);
String str = text.getText().toString();
now in str u will get string which is entered in EditText
I don't think you need a loop to do this. From your comment it looks like you also have an "Enter" button or something that you click to do the checking. Just set an onclicklistener and onclick you can make the edittext invisible (or un-editable), check is the edittext is equal to "BYE" and then do your actions might look something like this:
final EditText ET = (EditText) findViewById(R.id.EnterText);
Button B1 = (Button) findViewById(R.id.EnterButton);
B1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ET.setVisibility(View.INVISIBLE);
if(ET.getText().toString() == "BYE")
{
//do something if it is "BYE"
} else {
Context context = getApplicationContext();
CharSequence text = "Please enter BYE";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
ET.setVisibility(View.VISIBLE);
} });
Instead of running this check in an infinite loop, only run it on every onKeyUp of the EditText. You know, anyway, that the condition will only ever be fulfilled if the user actually enters something.

Categories

Resources