click listener is not working - android

Hi I am new in android learning and I'm practice it. I Want to implement click listener on my button but it is giving me error. even i imported android.view.View.OnClickListener; and other all libraries.
please have look at my code check where is my mistake
Cannot Resolve symbol onClickelistner.
package com.example.nexus.myapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
}
button.OnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}

Try this and put it in onCreate method
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});

Combine R2R and Toan Tran answers will give you the best solution:
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}

This should work.
Button button = (Button) findViewById(R.id.testButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
}
});

Try button click inside Oncreate()
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
}

Related

Android TextView and Buttons not working in my app

I am learning Android and trying one simple Android app development, I got one demo code from my lecture and the teacher simply do the following:
There are 2 buttons, 1 textview. When touching button A, it will show "text A" in the textview, while touching button B, it will present "text B" in textview.
I followed the code and rewrote it, but i can't get the correct result when I ran with emulator.
When I touch either button, there's no content in the TextView. But my teacher's reference code works:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
How is report(View v) called? I can't understand how this class is called. Could someone please help me out?
You need to let your button know that, when pressed, report() should be called. This may be done through the android:onClick attribute of your button on your layout's XML:
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_text"
android:onClick="report" />
Or by code, attaching an OnClickListener to the button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
report(v);
}
});
}
Hope it helps.
You should implements the View.OnClickListener, and override that OnClick method. You also need to setOnClickListener on your buttons. So your code should be like:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity implements View.OnClickListener{
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 =(Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
button2 =(Button)this.findViewById(R.id.button2):
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}

Android/Eclipse - saving button error

I have some problems with the "setOnClickListener(onSave)” and “View.OnClickListener”
Below is my code:
package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LunchList extends Activity {
Restaurant r = new Restaurant ();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//save button
//be notified when the button is clicked
Button save =(Button)findViewById(R.id.save);
save.setOnClickListener(onSave){
}
private View.OnClickListener onSave=new View.OnClickListener();
public void onClick(View v) {
EditText name=(EditText)findViewById(R.id.name);
EditText address=(EditText)findViewById(R.id.addr);
r.setName(name.getText().toString());
r.setAdress(address.getText().toString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
for save.setOnClickListener(onSave) I get the error save.setOnClickListener(onSave) and for View.OnClickListener I get the error Cannot instantiate the type View.OnClickListener.
I looked on this website and googled for this problem put I didn’t find a solution. I hope you guys can help me out.
Greets.
Remove semicolon after
private View.OnClickListener onSave=new View.OnClickListener();
and write onClick(View v) inside anonomous constructor of onClickListener
or else change your code with below code.
private View.OnClickListener onSave=new View.OnClickListener(
public void onClick(View v) {
EditText name=(EditText)findViewById(R.id.name);
EditText address=(EditText)findViewById(R.id.addr);
r.setName(name.getText().toString());
r.setAdress(address.getText().toString());
});
Button save =(Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
try the following code:
start.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
mainactivity();
}
});

Variable in setOnClickListener cannot be resolved

I've looked everywhere, but I can't find what I'm looking for.
I'm trying to make a stopwatch app for android using a Chronometer inside of a Fragment, but I'm getting errors on the setOnClickListener for some buttons.
It's the variables inside of parentheses "()" fx. (mStartListener), I get the error mStartListener cannot be resolved to a variable.
please help me, I really wanna get into android developing. Thank you
Here's the code
b = (Button) getView().findViewById(R.id.start);
b.setOnClickListener(mStartListener); //--- error
b = (Button) getView().findViewById(R.id.stop);
b.setOnClickListener(mStopListener); //--- error
b = (Button) getView().findViewById(R.id.reset);
b.setOnClickListener(mResetListener); //--- error
Full Code:
import android.app.Fragment;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class StopWatchFragment extends Fragment {
Chronometer mChronometer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.stopwatch, container, false);
Button b;
mChronometer = (Chronometer) getView().findViewById(R.id.chronometer);
// Watch for button clicks.
b = (Button) getView().findViewById(R.id.start);
b.setOnClickListener(mStartListener); //--- error
b = (Button) getView().findViewById(R.id.stop);
b.setOnClickListener(mStopListener); //--- error
b = (Button) getView().findViewById(R.id.reset);
b.setOnClickListener(mResetListener); //--- error
return view;
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
}
}
Move where you declare each Listener to be class variables:
public class StopWatchFragment extends Fragment {
Chronometer mChronometer;
View.OnClickListener mStartListener;
...
Or just declare your listeners before you try to use them:
Button b;
mChronometer = (Chronometer) getView().findViewById(R.id.chronometer);
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
...
// Watch for button clicks.
b = (Button) getView().findViewById(R.id.start);
b.setOnClickListener(mStartListener); //--- no more error
You are defining and initializing the mStopListener inside the onCreateView method, AFTER you try to set them as the OnClickListener.
You should either move them to the start of your method:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.stopwatch, container, false);
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
Button b;
mChronometer = (Chronometer) getView().findViewById(R.id.chronometer);
// Watch for button clicks.
b = (Button) getView().findViewById(R.id.start);
b.setOnClickListener(mStartListener); //--- error
b = (Button) getView().findViewById(R.id.stop);
b.setOnClickListener(mStopListener); //--- error
b = (Button) getView().findViewById(R.id.reset);
b.setOnClickListener(mResetListener); //--- error
return view;
}
Or, put them outside your method as global variables.

Android SDK Change text when Button clicked

package com.purelymean.earnings;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity{
/**Called when activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button);
TextView tx = (TextView) findViewById(R.id.textView1);
}
}
What do I need to put so that when the button is clicked it will change the textview to whatever i put?
You should add an OnClickListener to your button and override onClick function to set your desire text in your TextView
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView1);
tx.setText("yourtext");
}
});
Add an OnClickListener to your button and thats all you need.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tx.setText("It Works!");
}
});

how to call which button to work in android

I have an activity class with many buttons. If i click one button then it will go to next page then go back to main class.If I click another button in main class, it will go to next page together with data. Do anyone know how to write the function in activity class?
Can I write like this in a class? But when i run it only one button is working , when i clicked other i get error. I am new to android ,so please give me suggestion.
public class MyClass extends Activity {
private Button button,button1,button2;
public void onCreate(){.... initControl();}
public void initControl() { button=(Button)findViewById(R.id.button); .....
button.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
button1.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
button2.SetonClickListener(new View.onClickListener(){ public void onClick(View view)})
}
thanks for help.
First initialize button1, button2 before overriding onCreate(). Then assign the values in oncreateMethod call initializemthod
Have a look on the following code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class WebViewTest extends Activity {
Button button1 = null;
Button button2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.firstbutton);
button2 = (Button) findViewById(R.id.secondbutton);
initControl();
}
public void initControl() {
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
}
Thanks
Deepak
You have to add on click method

Categories

Resources