How to publish a value when a button is clicked? - java

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);
}
});

Related

Setting text to TextView from EditText element shows blank

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()));

Set a textView in a different layout to the value of an EditText

I'm making an app and I am trying to make a settings panel where all of your data from signing up is stored. I have a change button next to a TextView and when you click on that it takes you to a page called email where you can change your email you signed up with. There is an EditText and in the code I have a string that stores the data of what is in the EditText. It is supposed to set the email text to the EditText and go back to the settings page. But it goes back to the settings page and the email stays the same.
Code:
private TextView SetEmail;
private Button ChangeEmail;
private EditText newEmail;
private Button Save;
ChangeEmail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.email);
newEmail = (EditText) findViewById(R.id.EmailChange);
Save = (Button) findViewById(R.id.Save);
emailerror = (TextView) findViewById(R.id.emailerror);
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String newEmails = newEmail.getText().toString();
String Text = SetEmail.getText().toString();
if (newEmails.contains("#") && newEmails.contains(".com") || newEmails.contains(".net") || newEmails.contains(".global")) {
SetEmail.setText(newEmails);
setContentView(R.layout.settings);
} else {
emailerror.setVisibility(View.VISIBLE);
}
Thank you for helping me out
p.s. I am working in one class.
I suppose your "page called email" is a different Activity, so you open that Activity using "startActivityForResult()" and when you want to close that "email Activity" you have to execute "setResult(resultCode, Intent)".
Link: https://developer.android.com/training/basics/intents/result

Update Button text after Button click

I want to update the text in a clickable button after I click it.
I have a button for liking a product, its text shows how many likes the product has.
<Button
android:id="#+id/like"
android:text= "#string/like"
android:drawableTop="#drawable/like"/>
<string name="like">Likes: </string>
So in my onClick() method for this button I use like.setText("Likes: " + product.getLikes()); in order to update the text:
Button like = (Button) v.findViewById(R.id.like);
like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
like(product);
like.setText("Likes: " + product.getLikes());
}
});
The like() method does work - I checked that. The method product.getLikes() returns the correct value.
However the button stays the same. I canĀ“t figure out what the issue is.
UPDATE:
I noticed the text updates itself once i scroll down to other items and up again....
Try this.
final Button btn = (Button)findViewById(R.id.btn);
final Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
btn.setText("new text");
}
};
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handler.sendEmptyMessage(0);
}
});
Try this:
String mlikes= product.getLikes().toString();
like.setText("Likes: " + mlikes);`

Event get String from 2 buttons

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.

Get data from TimePicker which is inside an Alert, which is inside a Fragment

I'm trying to set an OnTimeChangedListener to set the value of a TextView which is located in my Fragment (I've got an action bar with three tabs), i setted the OnTimeChangedListener in my MainActivity like this :
private OnTimeChangedListener OnTimeChangedListener = new OnTimeChangedListener() {
#Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
time.setText(getSelectedTime(view));
}
};
Here is the method getSelectedTime();
public String getSelectedTime(TimePicker t) {
String time = t.getCurrentHour() + " : " + t.getCurrentMinute();
return time;
}
Like i said, the TimePicker is located in an Alert, the alert pops when a button is pressed in the same fragment than the TextView, here is the code :
public void showTimePicker(Button but) {
final Dialog dialog = new Dialog(this);
final TimePicker t;
dialog.setContentView(R.layout.alert_time);
dialog.setTitle("Heure");
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
t = (TimePicker) findViewById(R.id.time_cri);
/* findViewById() return NULL here !! */
t.setOnTimeChangedListener(OnTimeChangedListener);
dialog.show();
}
I have got a nullPointerException at this point (findViewById() in the above method). I tried different ways to get this working, but i found nothing ! If someone knew a solution to this, this would make my day !!!
Thank you for reading this !

Categories