Event handling not working with button. Why?

+1 vinoth vino · November 15, 2015
Hello friends.... I'm new to Android programming and now i just simply create one button with listener and one multiline text(I searched a lot whether the edittext is available in the right pane but there were no edittext there. so i'm using multiline text in this activity and i've change the layout resource ot text then in that edittext tag is present). if i click this button then i want to display the content which I've set up in the edittext variable 

package com.example.vinothvino.sample;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity {
 
EditText editText;
//EditText editText3;
Button button;
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
requestWindowFeature(Window.FEATURE_NO_TITLE); //it hides only the title
 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
 
editText = (EditText) findViewById(R.id.editText);
//editText3 = (EditText) findViewById(R.id.editText2);
button = (Button) findViewById(R.id.button);
 
setContentView(R.layout.activity_main);
 
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
 
editText.setText("Welcome to Android");
}
});
}
}



and below snippet also not working


package com.example.vinothvino.sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

//EditText editText;
//EditText editText3;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE); //it hides only the title

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

// editText = (EditText) findViewById(R.id.editText);
//editText3 = (EditText) findViewById(R.id.editText2);
button = (Button) findViewById(R.id.button);

setContentView(R.layout.activity_main);

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

TextView myText = (TextView)findViewById(R.id.textView2);
myText.setText("Welcome to Android World");
}
});

}
}

please anyone help me with this issue... You people want to clarify doubts I've posted this question in some other forums but no one replied there.... So kindly anyone help me i'm newbie to android programming




Post a Reply

Replies

Oldest  Newest  Rating
+1 Vedh Koutha · November 16, 2015
Hello,


So your trying to create an on click listener that changes the text, correct?

It looks like a few things are missing from the code.
When you did the button = (Button) part, use this instead.

Button button = (Button) findViewById(R.id.button)

The first button lets android know that its a button being identified. 
The second button is a variable that can be named as anything you want.
If you change the variable, you have to edit it in the R.id.(variable) part however.

Rewritten, this is how I would of done the code-


Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(
            new Button.onClickListener(){
                   public void OnClick(View v){
                           TextView myText = (TextView) findViewById(R.id.myText);
                                    myText.setText("Welcome to Android World");
}

}

)


Also, I'd try putting the Button button = (Button) 
part below the 
SetContentView
Don't know if that would change anything, but its worth a shot!
Hope this helped!
0 vinoth vino · November 19, 2015
Thank you so much..... i figured out this one day before yesterday anyways thanks a lot. Please help me out if i'm having any doubts regarding with android programming because I've posted this question in someother forums but none of them replied for that question. You only helped me....
  • 1

Java / Android Development

107,026 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator