Different onClick and onLongClick methods for two buttons - android

I'm trying to create buttons that will execute certain different instructions based on a simple click and a long click, but I'm struck with low understanding on how to put everything together. Performing a defined method for every button is ok, but I think it would be better to use onClickListeners for this, isn't it?
So my code is as follows. As you can see, I'm trying to catch both types of event for each button, but when I press the button 1A I get the toast of the 2A, and when I click the button 2A I get an error and the app crashes.
The second thing to fix is to bind together the onClick and the onLongClick.
activity_scout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.android.scout.ScoutActivity">
<LinearLayout
android:id="#+id/activity_scout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:orientation="vertical" >
<Button
android:id="#+id/but1A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1a"
android:onClick="click1a"
/>
<Button
android:id="#+id/but2A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2a"
android:onClick="click2a" />
</LinearLayout>
</ScrollView>
ScoutActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import static com.example.android.basketscout.R.id.butPlayer1A;
import static com.example.android.basketscout.R.id.butPlayer2A;
import static com.example.android.basketscout.R.id.butPlayer3A;
import static com.example.android.basketscout.R.id.butPlayer4A;
import static com.example.android.basketscout.R.id.butPlayer5A;
import static com.example.android.basketscout.R.id.textView;
public class ScoutActivity extends AppCompatActivity {
Button but1A;
Button but2A;
Button but3A;
Button but4A;
Button but5A;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scout);
but1A = (Button) findViewById(R.id.but1A);
but2A = (Button) findViewById(R.id.but1A);
but1A.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
Toast.makeText(getApplicationContext(), "Button 1A clicked", Toast.LENGTH_SHORT).show();
}
});
but2A.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
Toast.makeText(getApplicationContext(), "Button 2A clicked", Toast.LENGTH_SHORT).show();
}
});
but1A.setOnLongClickListener(new View.OnLongClickListener(){
public void onLongClick (View view) {
Toast.makeText(getApplicationContext(),"Button 1A long clicked", Toast.LENGTH_SHORT).show();
}
});
but2A.setOnLongClickListener(new View.OnLongClickListener(){
public void onLongClick (View view) {
Toast.makeText(getApplicationContext(),"Button 2A long clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
[If you see any error like unclosed parentheris or not completely correct variable names, it's because of some edit from the copy/paste I did]

You are finding the same view twice, you have to change this part of your code:
but1A = (Button) findViewById(R.id.but1A);
but2A = (Button) findViewById(R.id.but1A);
To this:
but1A = (Button) findViewById(R.id.but1A);
but2A = (Button) findViewById(R.id.but2A);
Also, remove the android:onClick attribute from layout, it's redundant and causes conflicts.
<Button
android:id="#+id/but1A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1a"/>
<Button
android:id="#+id/but2A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2a"/>

Remove your onClick input in the xml file, If you declared an onClick method from xml you don't need to call setOnClickListener on the Buttons object instead just create that method with the parameters of yourOnClickMethod(View view)

There are errors in your code. First is already pointed out by #Luiz which is the binding of view to the button object:
but2A = (Button) findViewById(R.id.but2A);
Also, here's a thread that might help you because I see you have declared onClick attribute in your xml for the Button tags: How exactly does the android:onClick XML attribute differ from setOnClickListener?
Basically there are these two ways of implementing click listeners and if your are using setOnClickListener() then onClick attribute in XML is not required and vice versa.

Remove the onClick attribute from
Button layout
And correct this code
but1A = (Button)findViewById(R.id.but1A);
but2A = (Button)findViewById(R.id.but1A);
To
but1A = (Button)findViewById(R.id.but1A);
but2A = (Button)findViewById(R.id.but2A);

problem is you finding the same id twice:
but1A = (Button) findViewById(R.id.but1A);
but2A = (Button) findViewById(R.id.but1A);
To
but1A = (Button) findViewById(R.id.but1A);
but2A = (Button) findViewById(R.id.but2A);
Then,
For long click listener for views see this how to implement a long click listener on a listview

Related

How to add correctly button listner in fragment [duplicate]

This question already has answers here:
How to handle button clicks using the XML onClick within Fragments
(19 answers)
Closed 5 years ago.
I write code and it look ok.
I dont know what is the reson that I get ERROR when I write setOnClickListener(this)
all I want is to create button in fragment.
"Error:(25, 78) error: incompatible types: void cannot be converted to Button"
"Error:Execution failed for task ':android:compileDebugJava'.
Compilation failed; see the compiler error output for details."
Many Thanks for any Help
the source code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.popthejam.game.android.R;
public class Wellcome extends Fragment implements View.OnClickListener {
private Button btnLOGIN ;
public Wellcome() {
// Required empty public constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_wellcome,container,false);
btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);
return view;
}
public void onClick(View view) {
Toast.makeText(getActivity(), "LOGIN BUTTON PRESS", Toast.LENGTH_SHORT).show();
}
}
----
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/popthejam"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fragment.Wellcome">
<TextView
android:gravity="center"
android:textSize="#dimen/profile_entry_text_size"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/welcome_to_app" />
<Button
android:id="#+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#android:color/holo_orange_dark"
android:text="Login"
/>
</FrameLayout>
The error is that you're trying to assign the result of setOnClickListener() (void) to your button variable.
So change this
btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);
to this
btnLOGIN = (Button) view.findViewById(R.id.btnLogin);
btnLOGIN.setOnClickListener(this);
(Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);
Above code return a void function. So :
btnLOGIN = (Button) view.findViewById(R.id.btnLogin).setOnClickListener(this);
not working because u register a button by the function.
Let do like #Headcracker suggess
Use this
btnLOGIN = (Button) view.findViewById(R.id.btnLogin);
btnLOGIN.setOnClickListener(this);
And implement the overridden onClick in your fragment class
#Override
public void onClick(View view) {
switch (v.getId()) {
case R.id.btnLogin:
Toast.makeText(getActivity(), "LOGIN BUTTON PRESS", Toast.LENGTH_SHORT).show();
break;
}
}

Android button IllegalStateException with onClick(MainActivity)

Brand new to android. I have a button that is supposed to trigger an onClick method that is an override of the onClick method used with the onClickListener. The problem is that when I press the button an IllegalStateException is thrown saying that the onClick(MainActivity), as defined in the XML of the button, method cannot be found.
The code below is the code that throws the IllegalStateException. However if I change the XML of the button to android:onClick="onClick" then the button works completely fine. Why is this?
Button XML:
<Button
android:text="Accept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp"
android:id="#+id/button2"
android:onClick="onClick (MainActivity)" />
MainActivity Code:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener{
private TextView textView;
private EditText username;
private EditText password;
private Button accept;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView3);
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
accept = (Button) findViewById(R.id.button2);
}
public void logIn(View view)
{
Log.i("testUsername", username.getText().toString());
Log.i("testPassword", password.getText().toString());
}
#Override
public void onClick(View view)
{
if((Button)view == accept)
{
Log.i("testUsername", username.getText().toString());
Log.i("testPassword", password.getText().toString());
}
}
}
Why is this?
Because android:onClick="onClick" is correct syntax. None of the following are correct syntax:
android:onClick="onClick (MainActivity)"
android:onClick="onClick (View)"
android:onClick="onClick(View)"
android:onClick="onClick and anything else"
Quoting the documentation for android:onClick:
Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).
OK, with the help of #HappyRavi on Twitter, I was able to reproduce the problem. It's an IDE bug that you will encounter if you do things in a certain order:
Add the method that you want to route the click event to
Drag the Button into the GUI editor
Click the onClick drop-down in the Properties pane of the GUI editor
Doing things in a different order will not reproduce the problem.
With luck, they can get this fixed in some patch release for Android Studio 2.3.
It should be implemented like below
<Button
android:text="Accept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp"
android:id="#+id/button2"
android:onClick="buttonClickHandler" />//name of method should be given here
And add the method in Activity file
public void buttonClickHandler(View view) {
//handle button click here
}
This will work only when method is added in Activity class and not with Fragment

Cannot resolve symbol issues in making a stopwatch

(Keep in mind i'm very new to coding) I'm having an issue when coding a stopwatch app, I've scavenged this site and found no solution to my issue, comparing my code to another user's who attempted the same tutorial, i found that they are the same, yet I am getting several unresolved symbols when to my knowledge, everything is correct...
Ok, i updated the file with some corrections, now im not having any more errors with start/stop, but i have new errors with the m(start/stop/etc)Listener code...
(Here's the Main Activity Java File)
package com.jackson.eason.stopwatch.;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import your.package.R;
public class MainActivity extends Activity {
Chronometer mChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button;
mChronometer = (Chronometer) findViewById(R.id.Chronometer);
// Watch for button clicks.
button = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);
button = (Button) findViewById(R.id.stop);
button.setOnClickListener(mStopListener);
button = (Button) findViewById(R.id.reset);
button.setOnClickListener(mResetListener);
button = (Button) findViewById(R.id.set_format);
button.setOnClickListener(mSetFormatListener);
button = (Button) findViewById(R.id.clear_format);
button.setOnClickListener(mClearFormatListener);
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());
}
};
View.OnClickListener mClearFormatListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setFormat(null);
}
};
}
}
(Also, here's my xml file, which Android Developer says is fine)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Chronometer android:id="#+id/chronometer"
android:format="#string/chronometer_initial_format"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0"
android:paddingBottom="30dip"
android:paddingTop="30dip"
/>
<Button android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start">
<requestFocus />
</Button>
<Button android:id="#+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chronometer_stop">
</Button>
<Button android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reset">
</Button>
<Button android:id="#+id/set_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="format">
</Button>
<Button android:id="#+id/clear_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear_format">
</Button>
</LinearLayout>
I appreciate any help anyone can offer!
Your problem is that you are importing the wrong R class. android.R is different from the R file generated for your app and referencing your resources, it is the R file from the framework itself, allowing you to use convenience resources, see this question for an example.
import android.R;
replace that with
import your.package.R;
Also, as Thanos suggested, put your listener declarations inside the onCreate method.
UPDATE :
When I said your.package.R, it was just an example. The package depends on what you chose it to be when creating your project, but judging by the code you provided, you probably need import com.jackson.eason.stopwatch.R
Secondly, you need to declare your listeners first before you use them. If you don't, if you call setOnClickListener(mClearFormatListener) but mClearFormatListener is declared after, the compiler doesn't know about mClearFormatListener yet so it shows a compilation error. The below code should work.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get references to views
mChronometer = (Chronometer) findViewById(R.id.Chronometer);
Button button1 = (Button) findViewById(R.id.start);
Button button2 = (Button) findViewById(R.id.stop);
Button button3 = (Button) findViewById(R.id.reset);
Button button4 = (Button) findViewById(R.id.set_format);
Button button5 = (Button) findViewById(R.id.clear_format);
// Declare the listeners
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());
}
};
// You forgot to declare a listener for set format in your updated code
View.OnClickListener mSetFormatListener = new OnClickListener() {
public void onClick(View v) {
// TODO : set the format
}
};
View.OnClickListener mClearFormatListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setFormat(null);
}
};
// Assign the listeners to your buttons
button1.setOnClickListener(mStartListener);
button2.setOnClickListener(mStopListener);
button3.setOnClickListener(mResetListener);
button4.setOnClickListener(mSetFormatListener);
button.setOnClickListener(mClearFormatListener);
}
One last thing : in the notation mVariable, m stands for "member", meaning a member variable of a class, as opposed to a variable inside a method. Since your listeners are only declared inside a method, we usually don't use mListener but rather listener. Of course, this is just a naming convention and it will not prevent the code to compile and run ;)
I think that all the listeners should be inside a method. Am I wrong? Try placing them inside the onCreate method (at the very end of it).

Not sure why getting Duplicate method onClick(View)

I've got one activity
here is what I am trying do. When app starts an image is displayed. When the image is clicked the next image is displayed.
I've got it all setup with main.xml and imageview.
The app builds and runs and displays the first image no problem.
Now trying to setup onclick for the image so the next image will be displayed. I've added android:onClick="onClick" to the image vew in main.xml.
I am using (this) for setOnClickListner and I implemented View.onClickListener for the class but the switch case I setup I get duplicate method for onClick(View v) and I'm not sure why.
Also trying to figure out the findViewById I think the way I have it image1 will always be displayed.
getting error on line public void onClick(View v) that it is a duplicate method.
Here is the code I have. Thanks for any assistance on this.
main.xml is a linear layout not sure that matters.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/imageView1"
android:src="#drawable/bear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="onClick"
android:contentDescription="#string/desc"/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/bear_n"
android:onClick="onClick"
android:contentDescription="#string/desc" />
This is the .java file.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class VTFCActivity extends Activity implements View.OnClickListener{
ImageView image;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.imageView1);
ImageView image1 = (ImageView) findViewById(R.id.imageView1);
ImageView image2 = (ImageView) findViewById(R.id.imageView2);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.imageView1:
image.setImageResource(R.drawable.bear);
break;
case R.id.imageView2:
image.setImageResource(R.drawable.bear_n);
break;
}
}
when you use
image1.setOnClickListener(this);
image2.setOnClickListener(this);
you have to override the default onClick() Method in th Android API:
#Override
public void onClick(View v)
also setting in xml android:onClick="onClick"
will let you handle clik event from your custom onClick Method
and that's way an exception is throwed to you: telling that you have a duplicate method!!
so you have two choices:
remove android:onClick="onClick": and that's what i would do
remove image1.setOnClickListener(this); image2.setOnClickListener(this); and handle clicks on your way on the custom onClick() method
Now trying to setup onclick for the image so the next image will be
displayed. I've added android:onClick="onClick" to the image vew in
main.xml.
If you have added android:onClick="onClick" to the both image views in main.xml
You should remove image1.setOnClickListener(this); and image2.setOnClickListener(this);
from code since you have already specified it in main xml.
Here is the code...
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
final Dialog dl = new Dialog(MainActivity.this);
dl.setContentView(R.layout.cdialog);
dl.setTitle("Title");
TextView txt = (TextView) dl.findViewById(R.id.textView1);
txt.setText("Hi \n How are you \n" +
"Where were you \n Good to see you");
Button btn = (Button) dl.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dl.dismiss();
}
});
dl.show();
}
});
}
}

onClick method not called on Android

I am doing the simplest of onClick models and cannot get the onClick method to fire. I know it is something simple, and I am new to Android. Any help is appreciated.
package com.bordeloniphone.timeentry;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class TimeEntryActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button okButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.btnOK);
okButton.setText(":)");
okButton.setOnClickListener(this);
//setContentView(okButton);
}
public void onClick(View v) {
Log.d("TEST", "TEST");
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
Here is the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnOK"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
After much angst and gnashing of teeth, I figured it out. I had to delete the emulator device and add an new one and now it works like a champ. I appreciate everyone trying to help.
Instead of setting the onClicklistener to this, try this approach:
okButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("TEST", "TEST");
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
});
You should try Cleaning your project or try to restart your Eclipse or any other Editor you are using as it is a valid code and should work fine.
UPDATE:
Also, you should check your Logcat, are you getting the output of Log.d("TEST", "TEST"); because your Toast seems to be implemented in a wrong manner.
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show(); // wrong
Toast.makeText(Activity_name.this, "TEST", Toast.LENGTH_SHORT).show(); // correct
Using this in Toast inside the Listener means you are Referencing the Listener, which indeed should not be the case. You have to reference to the Activity itself so better use Activity_name.this.
Button iv_StyleInspiration_Back = (Button) findViewById(R.id.iv_StyleInspiration_Back);
iv_StyleInspiration_Back.setOnClickListener(this);
Try this Whenever you implement onclick in your activity you need to set as above to make it work for all controls and onclick should look something as below
#Override
public void onClick(View pView) {
if (null != pView) {
switch (pView.getId()) {
case R.id.iv_StyleInspiration_Back:
//do what you want
break;
default:
break;
}
}
}
Check for Three Steps:
find the button by id correctly
link the button to a listener. (adding the actionListener)
you specify a condition for this button in case of implementing an actionListener class.
try making the button clickable
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnOK"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="OK"
android:clickable="true" />

Categories

Resources