I have an EditText field for name field and a button. When I type a name in the field and press the button. It should show "Hi, " + value of EditText field.
My solution is to getText().toString() on the EditText object and passed it to a string variable. But it returns "" whenever I use it to setText().
I used this instead and it works but i'm confused what's the difference ? nametext.setText("Hi,nametext.getText().toString());
Button btn_show;
EditText nametext;
String input_msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nametext = findViewById(R.id.name_text);
input_msg = nametext.getText().toString();
btn_show = findViewById(R.id.hi);
btn_show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nametext.setText("Hi, " + input_msg); // this returns ""
/* when i use nametext.getText().toString() in place of input_msg
/* it returns the string I typed in the edittext. What's the
/* difference?
/* nametext.setText("Hi,nametext.getText().toString()); // returns the actual string I typed in the edittext field
}
});
}
nametext.setText("Hi,nametext.getText().toString()); // returns the actual string typed in the field
nametext.setText("Hi, " + input_msg); // returns ""
Hello there you are doing it in a right way just a little mistake that you are calling getText() before Click.
btn_show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
input_msg = nametext.getText().toString();
nametext.setText("Hi, " + input_msg);
}
});
The reason there is a difference is that when you populate input_msg, the value is still empty. When you click the button after the string is the view is populated, input_msg hasn't changed.
So what you need to do is to get the value only when the button is clicked:
btn_show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nametext.setText("Hi, " + nametext.getText().toString());
}
});
And if you want to use Java 8, it's even simpler:
btn_show.setOnClickListener((View.OnClickListener) v -> nametext.setText("Hi, " + nametext.getText().toString()));
Related
In my Android activity, I have one EditText, a '+' button, a '-' button, 'Save' button and 'Load' button. When I press '+', the value in EditText increases by 1, similarly on pressing '-' value decreases by 1. I used SharedPreferences to save the data when I click on 'Save'. When I click 'Load', I want to reload this data onto the EditText field.
Now the problem is, when I completely exit the application (even from recently used apps), and click 'Load' on relaunching it, the saved number doesn't appear. I included the onClick() action for the 'Load' method in onRestart() method. It still doesn't work. What am I missing here? I even tried out all other suggestions for the similar questions asked previously here.
Also, is it really onRestart() or onRestoreInstanceState() ?
public class MainActivity extends Activity {
Button btn1;
Button btn2;
Button btn3;
Button btn4;
EditText scoreText;
int counter = 0;
TextView textTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.add);
btn2 = (Button)findViewById(R.id.subtract);
btn3 = (Button)findViewById(R.id.save);
btn4 = (Button)findViewById(R.id.load);
scoreText = (EditText)findViewById(R.id.total);
textTitle = (TextView)findViewById(R.id.title);
btn1.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
#Override
public void onClick(View v) {
counter++;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.GREEN);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
#Override
public void onClick(View v) {
counter=counter-1;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.RED);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
#Override
public void onClick(View v) {
//store data using sharedprefernces
SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
//Edit method allow to write the data in sharedpreferences
editor.putString("count",scoreText.getText().toString());
//For commit changes commit() method is used
editor.commit();
Toast.makeText(getApplicationContext(),"Data Saved",Toast.LENGTH_SHORT).show();
}
});
btn4.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
#Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
String strcount=sharedPreferences.getString("name",scoreText.getText().toString());
// scoreText.setText(strcount);
scoreText.setBackgroundColor(Color.YELLOW);
}
});
}
#Override
protected void onRestart(Bundle savedInstanceState){
super.onRestart(savedInstanceState);
btn4.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
#Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
String strcount=sharedPreferences.getString("name",scoreText.getText().toString());
if (strcount.equals(""))
{
Toast.makeText(getApplicationContext(), "Data Was Not Found", Toast.LENGTH_SHORT).show();
}
else
{
scoreText.setText(strcount);
}
scoreText.setBackgroundColor(Color.YELLOW);
}
});
}
You using using count as key to save the value
editor.putString("count",scoreText.getText().toString());
but using name as key to retrieve the value so you need to use count key while getting the previously stored data so use
sharedPreferences.getString("count",scoreText.getText().toString());
instead of
sharedPreferences.getString("name",scoreText.getText().toString());
You are using different keys to save and retrieve the data from SharedPrefernces.
editor.putString("count",scoreText.getText().toString());
String strcount=sharedPreferences.getString("name",scoreText.getText().toString());
You should be using the same key in both the cases otherwise it would return default value which is the text in TextView and that would be empty at the start of the app, you just need to change the key and that would do the trick for you.
Just change the below line like it is mentioned
String strcount=sharedPreferences.getString("count",scoreText.getText().toString());
Currently, the value is in an editText, and when the button is clicked, the value is published to a server. But how do I get rid of that editText and store that value in the button, so I do not need the editText? This method publishes the value of 32 when the button is pressed from the editText.
private void publishValue() {
try {
final MqttTopic myTopic = mqttClient.getTopic(TOPIC_VALUE);
final int topicNumber = 32;
EditText theTopic = (EditText) findViewById(R.id.editTopicValue);
final String topic = theTopic.getText().toString();
myTopic.publish(new MqttMessage(topic.getBytes()));
System.out.println("Published data. Topic: " + myTopic.getTopic() + " Message: " + topic);
} catch (MqttException e) {
e.printStackTrace();
System.exit(1);
}
}
}
So if all you are trying to do is change the Button text to the EditText, then hide the EditText. The code for you is below.
So in the java code of your view, you probably want to have you UI controls as private variables. Then set an onClickListener for your button.
That "public void OnClick(view V)" is the void that will fire on when that button is clicked.
editText = findViewById(R.id.textEdit);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
topic = editText.getText().toString();
editText.setVisibility(View.GONE);
button.setText(topic);
}
});
I have the following problem:
I want to use the text of a TextView which is changing every 10 seconds. With a Button, I want to use the text of the TextView and show it in another TextView.
I tried it with the following code.
buttonSave.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
buttonSave.setClickable(false);
buttonSave.setVisibility(View.GONE);
if(buttonSave.isEnabled()) {
String copy = textView.getText().toString();
settingsview.append("\n" + copy);
}
buttonSave.setEnabled(false);
}
My problem now is that it works fine until the TextView refreshes and then the TextView is clearing everything without pressing the button.
This is the code which changes my textfild. The toCheck StringBuilder is a input of OCR which is refreshing every 10secs
public void filterit(StringBuilder toCheck){
Pattern patternDate = Pattern.compile("\\d{2}\\.\\d{2}\\.\\d{4}");
Matcher matcher = patternDate.matcher(toCheck.toString());
while (matcher.find()) {
//textView.setText(matcher.group());
settingsview.setText(matcher.group());
}
}
try this way
buttonSave.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if(buttonSave.isEnabled()) {
String copy = textView.getText().toString();
ettingsview.setText(settingsview.getText().toString()+"\n" + copy);
buttonSave.setClickable(false);
buttonSave.setVisibility(View.GONE);
}
}
I am using an Adapter to generate buttons in a GridView.
Using an OnClickListener, whenever I click a button from the GridView, it is possible to get the String of the button in a variable. However, I want to click a button, store its text in a variable, then click a different button, store its text in another variable so later I can compare the two texts. How could I do that?
gI'm using OnClickListener, therefore my previous implementation for one button was the following:
public void onClick(View v) {
Button b = (Button)v;
String text = b.getText.toString();
With this approach, I can only get and store text from first button clicked, unless I store all the values in an array.
Ok so from what I can understand, you want to compare the strings of the two items in a grid view. If this is what you want, heres how I would do it(just a psuedocode):
String mValue1, mValue2;
boolean isOneClicked = false;
onItemClickedListener(item, position){
if(!isOneClicked){
mValue1 = item.stringValue;
isOneClicked = true;
}
else{
mValue2 = item.stringValue;
//do whatever you want here
//Reset when done
mValue1 = "";
mValue2 = "";
isOneClicked = false;
}
}
Please you can share your code to turn the things more comprehensible? Edit your question please... Anyways:
MainActivity.java
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private String butString, butString2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
butString = (String) button1.getText();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
butString2 = (String) button1.getText();
}
});
}
}
After that you can compare the two strings.
I am making this app, and in the design part, I Placed a Plain Text, the Id is TEXT1,
and I want the String A to be equal to what ever the user place in the Plain Text(TEXT1),
but it doesn't work...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
final Text TEXT1=(Text)findViewById(R.id.TEXT1);
final Button TRANSLATE=(Button)findViewById(R.id.TRANSLATE);
TRADUCIR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String A;
A=(Text)findViewById(TEXT1); // ERROR IS IN HERE!
Toast.makeText(getApplicationContext(), A,Toast.LENGTH_LONG).show();
}
});
Instead of Text, you probably meant to use TextView.
And to get its contents, use getText(), i.e.
String text = (TextView)findViewById(TEXT1).getText().toString();
You must replace Text by TextView and use this :
A = TEXT1.getText().toString();
instead of this :
A=(Text)findViewById(TEXT1);