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());
}
Related
Sorry taking your time, am asked a question in a very wrong way.
So what I doing now, a small notepad program where the title and content saved of the note to the SQLite database.
This part working as should, but I don't have any input check and the app saving the note with empty title and content.
there is my current code for this :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dba = new DatabaseHandler(MainActivity.this);
title = (EditText) findViewById(R.id.titleEditText);
content = (EditText) findViewById(R.id.wishEditText);
saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
saveToDB();
}
});
}
private void saveToDB() {
MyNote wish = new MyNote();
wish.setTitle(title.getText().toString().trim());
wish.setContent(content.getText().toString().trim());
dba.addWishes(wish);
dba.close();
//clear
title.setText("");
content.setText("");
Intent i = new Intent(MainActivity.this, DisplayNotesActivity.class);
startActivity(i);
How can I implement a basic input checking and avoid the saving of empty notes?
my first idea was to check the emptiness of the input and drop a toast message, tried several solutions from not, but not worked me.
many thanks
C
You have to be sure you're using the same EditText variable which in this case I think is title.
EditText title = (EditText) findViewById(R.id.titleEditText);
if(title.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Input Text Is Empty.. Please Enter Some Text", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, title.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
U need to specyfi your question.
But from what i undestood u don't know how check editText is empty and don't know why can't write into text box.
First easy method is just check length of string if it bigger than 0 that's meen it's not empty
EditText title = (EditText) findViewById(R.id.IDEDITTEXT);
if(String.valueOf(title.getText()).length()>0){
//do something
}else{
//do something
}
And about second (i thing question) is check your xml file (layout) does your editText is enabled and not is textViev.
Btw your code is hard to read like your question:)
EditText title = (EditText) findViewById(R.id.titleEditText);
if(title.getText().toString().length()<=0) {
Toast.makeText(MainActivity.this, "Input Text Is Empty.. Please Enter Some Text", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, title.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
try this code...
I passed the data from the first actvity (like int VNIMANI and int OBRATNOST). This data set my TextView after I came on second activity. now I need a button. And when I click on the button I need increment +1. But the code doesn't work.
TextView tvPaklic = (TextView) findViewById(R.id.paklic);
String paklic = String.valueOf( + 10+ (VNIMANI+OBRATNOST));
tvPaklic.setText(paklic);
}
int newPaklic = 0;
public void plusPaklic (View v){
newPaklic = newPaklic + 1;
displayPaklicDve (newPaklic);
}
private void displayPaklicDve(int newPaklic) {
TextView tvpaklicdve = (TextView) findViewById(R.id.paklic);
String paklicdve = String.valueOf(newPaklic);
tvpaklicdve.setText(paklicdve);
}
I would really like to suggest you refer this one Read this, will solve your problem
You were used 2 Textview objects with referring one id check that.
TextView tvPaklic = (TextView) findViewById(R.id.paklic);
TextView tvpaklicdve = (TextView) findViewById(R.id.paklic);
I'm trying to build basic calculator, but when I put a number at first edittext and hit add button, it gets crashed.
It is fine when I add two number in both editTexts. There was no problem in that. But the problem happened when I put only one number and hit add.
It is throwing NumberFormatException: Invalid int: "".
here is my basic code.
public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add,sub,multi,div;
TextView MyResults;
String Number1 ;//;
String Number2 ;//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText)findViewById(R.id.num1);
num2 = (EditText)findViewById(R.id.num2);
add = (Button)findViewById(R.id.Add);
sub = (Button)findViewById(R.id.Sub);
multi = (Button)findViewById(R.id.Multiple);
div = (Button)findViewById(R.id.Divide);
MyResults = (TextView)findViewById(R.id.results);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Number1 =num1.getText().toString();
Number2 =num2.getText().toString();
int mAdd1 = Integer.valueOf(Number1);
int mAdd2 = Integer.valueOf(Number2);
int myAdd = mAdd1+mAdd2;
MyResults.setText(String.valueOf(myAdd));
Toast.makeText(getApplicationContext(), "Added: "+ myAdd, Toast.LENGTH_LONG).show();
}
});
}//end OnCreate.
When you leave an EditText empty
int mAdd2 = Integer.valueOf(Number2);
Will crash because a empty String is not a valid Integer (hence the NumberFormatException).
You could put it in a try/catch like this:
try {
Number1 =num1.getText().toString();
Number2 =num2.getText().toString();
int mAdd1 = Integer.valueOf(Number1);
int mAdd2 = Integer.valueOf(Number2);
MyResults.setText(String.valueOf(myAdd));
Toast.makeText(getApplicationContext(), "Added: "+ myAdd, Toast.LENGTH_LONG).show();
catch (NumberFormatException e) {
//Give a toast saying the user did not enter two correct values
}
Or alternatively, check the value beforehand and check if nothing was entered:
number1 =num1.getText().toString();
number2 =num2.getText().toString();
if (number1.equals("") || number2.equals("")) {
//Show error or set numbers to a different value
}
However, for this to work you must assume the user did not enter anything that isn't a valid Integer like a letter, for example by setting the EditText's input type to numbers.
android:inputType="number"
You should do validation checking for a blank edit text and not do the calculation in this case.
Instead you might take advantage of EditText's setError(CharSequence) method where you can show a message to the user why the input is not valid.
I have a button that begins life "Unticked" with text going to a Label that says "NO". When you push the button it changes the image to "Ticked" and displays text in a Label as "YES". This all works perfectly. What I can't do or find is how to change it back to "Unticked" and "NO" if I then push it again?
Here is the code for the button:
View.OnClickListener imgButtonHandler9 = new View.OnClickListener() {
public void onClick(View v) {
button9.setBackgroundResource(R.drawable.androidnearmisson);
TextView text = (TextView) findViewById(R.id.textView2);
text.setText("YES");
}
};
Any help will be greatly appreciated.
Many Thanks
You can get TextView's current text and make a comparison. If its YES, change to NO, else vice verse:
View.OnClickListener imgButtonHandler9 = new View.OnClickListener() {
public void onClick(View v) {
TextView text = (TextView) findViewById(R.id.textView2);
if(text.getText().toString().equals("NO")){
button9.setBackgroundResource(R.drawable.androidnearmisson);
text.setText("YES");
}
else {
button9.setBackgroundResource(R.drawable.otherimage); //Replace otherimage with proper drawable id
text.setText("NO");
}
}
};
Hope this helps.
First move this code to your onCreate :
TextView text = (TextView) findViewById(R.id.textView2);
Inside your onClick :
if(text.getText().toString().equals("NO"))
{
button9.setBackgroundResource(R.drawable.androidnearmisson);
text.setText("YES");
}
else
{
//change what you want
}
I want to grab Text when user click on the TextView
For Example :
TextView string = "this is a test for android and textView"
When user click on textview in android position grab android
Anyone have a solution for this ?
You can assign an onClick listener to the textview, make it final and then get its text.
final TextView txt = (TextView) findViewById(R.id.txt);
txt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String getTxt = txt.getText().toString();
}
});
TextView text = (TextView) findViewById(R.id.yourTextViewId);
text.onClickListner(this);
#Override
public void onClick() {
String textOnTextView = text.getText().toString();
}
If you want to split lines and display them in different color then refer to following links.
Split text from string
Apply color to specific text
A button has onClick , I dont think that a TextView has onClick so that a user clicks it.
Correct me if i am wrong
If you want to select part of text, try to use EditText
This is not a solution for your need. But only one step to solution.
Use setTextIsSelectable(boolean) or the TextView_textIsSelectable XML attribute to make the TextView selectable (text is not selectable by default).
Using following code, I managed to get selected text as String. You need to first select string by dragging over it.
NB: you need minimum API 11 to use setTextIsSelectable(boolean)
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView) findViewById(R.id.textView1);
t1.setTextIsSelectable(true);// IMPORTANT
t1.setText("This is Android program");
t1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_UP:
int start=t1.getSelectionStart();
int end=t1.getSelectionEnd();
String sub=t1.getText().subSequence(start, end).toString();
Toast.makeText(getBaseContext(), sub, 1).show();
}
return true;
}
});
}