I've just started with android and I'm trying to make simple program to take string from user and display the result as Toast
EditText e = (EditText) findViewById(R.id.editText);
final String result = e.getText().toString();
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
Now If I type anything in editText it's supposd to print that value but instead it's printing the default text value and even if I edit that it prints the same value
As you can see in the picture on pressing the button it shows "Name" on toast
and even upon changing it shows the same thing that is "Name". I want it too show the value that I typed later.
What should I do?
You store the text when you setup the views. Thats why you get the default text.
move
final String result=e.getText().toString();
into the onClick
#Override
public void onClick(View v) {
final String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
and it should get the text when the button is pressed.
Reason is your variable String result is not taking latest value when button is clicked.
Try this:
final EditText e = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String result = e.getText().toString();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
your requirement is show EditText Text in Toast
first make EditText object as globle
class ActivityClassFileName extend AppCompactActivity
{
EditText ToastText;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylayout);
//initialize EditText object
ToastText = (EditText) findViewById(R.id.editText);
Button btx = (Button) findViewById(R.id.button2);
btx.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//showing Toast
Toast.makeText(getApplicationContext(), ToastText.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
}
Sometimes you have to display toast outside of onclick,by creating new method.
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = findViewById(R.id.button);
editText=findViewById(R.id.editTextView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String testString=editText.getText().toString();
showToast(testString);
}
});
}
private void showToast(String str) {
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
}
Related
This is the simple code I applied.Please explain why such behavior is taking place.Oncreate is not updating then why on click of button it can be called again and again
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gaggan();
}
public void gaggan(){
Button bt =(Button) findViewById(R.id.button4);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText e2= (EditText) findViewById(R.id.editText);
EditText e3= (EditText) findViewById(R.id.editText3);
int num1=Integer.parseInt(e2.getText().toString());
int num2=Integer.parseInt(e3.getText().toString());
int sum=num1+num2;
Toast.makeText(MainActivity.this,Integer.toString(sum), Toast.LENGTH_SHORT).show();
}
bt.setOnClickListener will register your click-listener (new View.OnClickListener() {..} on the button and thus, whenever the button is clicked, the onClick() method of your click-listener is executed.
The Android docs have a more detailed explanation
I'm using the following class and ive been trying for the last 24 hours i cant figure out why its not working. when i press the button it does nothing.
I'm creating this so i can verify the use login information
My class
public class login extends AppCompatActivity {
Button button11;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText) findViewById(R.id.user);
passwordField = (EditText) findViewById(R.id.password);
Button clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"it
clicked",Toast.LENGTH_LONG);
TextView mt=(TextView) findViewById(R.id.messy);
mt.setText("Please Wait");
}
});
}}
I Added my XML file on https://codeshare.io/5XWxOE
You are creating Toast but do not call it to show its content try code blew :
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"it
clicked",Toast.LENGTH_LONG).show();
TextView mt=(TextView) findViewById(R.id.messy);
mt.setText("Please Wait");
}
you forgot to use show()
Please try this solution
Toast.makeText(login.this,"it
clicked",Toast.LENGTH_LONG).show;
Instead of the getApplicationContext() you have.
Try this ...
TextView mt;
Button clickButton;
mt=(TextView) findViewById(R.id.messy);
clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "Button click toast", Toast.LENGTH_SHORT).show();
mt.setText("Your text here");
}
});
When something is going wierd.. you can check basic log. try this one~
public class login extends AppCompatActivity {
Button button11;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameField = (EditText) findViewById(R.id.user);
passwordField = (EditText) findViewById(R.id.password);
Button clickButton = (Button) findViewById(R.id.loginButton);
clickButton.setText("Button Found");
clickButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
clickButton.setText("Button Clicked");
}
});
}}
There is another way to write code for button clicking
public class login extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button firstButton = (Button) findViewById(R.id.loginButton);
firstButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginButton: {
Toast.makeText(login.this, "Button click toast", Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.messy);
textView.setText("Your text here");
break;
}
default: {
Toast.makeText(login.this, "Something happens", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
I hope this will resolve your issue.
The app stops. Where am I wrong? The button when clicked change the text to the number of times the item was clicked.
private Button button;
int countClicks = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
countClicks++;
button.setText(countClicks);
}
});
That's a classic mistake
You are using View.setText() on an int, and it needs to be on a String.
Try
button.setText(countClicks + "");
By concatenating the integer with an empty String, it will auto-cast it to a String
I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.
I need to set text of a button by taking value from edit text ,when button is clicked it has to change its text to the text specified in Edittext.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
TextView txt=(TextView)findViewById(R.id.editText);
btn.setText((CharSequence) txt);
}
}
);
}
you just need define EditText variable instead of TextView and call getText method of editText and then toString method like this:
final Button btn=(Button)findViewById(R.id.button);
assert btn != null;
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
EditText txt = (EditText)findViewById(R.id.editText);
btn.setText(txt.getText().toString());
}
}
);
Declare your views globally
Button btn;
EditText txt;
set your id in onCreate method
btn=(Button)findViewById(R.id.button);
txt=(EditText)findViewById(R.id.editText);
keep your onclicklistener as follow
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v)
{
final String newText=txt.getText().toString().trim();
if(newText.length()>0){
btn.setText(newText);
}
}
}
);
final Button btn = (Button)findViewById(R.id.button);
if (btn != null) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText et = (EditText)findViewById(R.id.editText);
if (et != null){
Button btn = (Button)view;
btn.setText(et.getText());
}
}
});
}