(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).
Related
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;
}
}
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
I'm trying to use the findViewById() method to to assign an action to a button I created, however it is giving me the error:
findViewById(int) in android.support.v7.app.AppCompatActivity cannot be applied to (android.widget.Button)
at the lines:
buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);
My code is as follows:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class UserTypeSelection extends AppCompatActivity implements View.OnClickListener{
private Button buttonStudentAccess;
private Button buttonGuestAccess;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_type_selection);
buttonStudentAccess = (Button) findViewById(buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(buttonGuestAccess);
buttonStudentAccess.setOnClickListener(this);
buttonGuestAccess.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (view == buttonStudentAccess) {
}
if (view == buttonGuestAccess) {
//startActivity(new Intent(this, MainActivity.class));
}
}
The corresponding xml file contains the buttons
<Button
android:text="Guest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonStudentAccess"
android:layout_alignLeft="#+id/buttonStudentAccess"
android:layout_alignStart="#+id/buttonStudentAccess"
android:layout_marginTop="30dp"
android:id="#+id/buttonGuestAccess"
android:layout_alignRight="#+id/buttonStudentAccess"
android:layout_alignEnd="#+id/buttonStudentAccess" />
<Button
android:text="Student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/buttonStudentAccess"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true" />
I used the same syntax to create and assign actions to buttons in my login and registration classes and it didn't create this error. Any help is appreciated, thank you
You need to specify where you're getting the id "buttonStudentAccess" from. so here's how you can find the view:
buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
To specify that the button or any other view is located in the "resources" package you refer to it as R and .id specifies that you want to grab the view using its id.
buttonStudentAccess = (Button) findViewById(R.id.buttonStudentAccess);
buttonGuestAccess = (Button) findViewById(R.id.buttonGuestAccess);
I'm new to Android app.. I was trying to change the button color on first click and to change back to default on the next click and this will work no matter how many times user clicks...
Can anyone help me to understand this
Create global variable clickStatus
boolean clickStatus=true;
final Button running_app=(Button)findViewById(R.id.running_app);
running_app.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clickStatus){
clickStatus=false;
running_app.setBackgroundColor(Color.parseColor("#3F51B5"));
}else {
clickStatus=true;
running_app.setBackgroundResource(android.R.drawable.btn_default);
}
}
});
Simple, Just give two different tags to button and keep toggling it on button click. That would help you to change background colour alternatively. Here is the complete code for it. Hope this helps.
Here is main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Click Me" />
</LinearLayout>
Here is MainActivity.java
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(this);
btnClick.setTag("1");
btnClick.setBackgroundColor(Color.BLUE);
}
#Override
public void onClick(View view) {
if (view.getTag().equals("1")) {
view.setTag("0");
btnClick.setBackgroundColor(Color.RED);
} else {
view.setTag("1");
btnClick.setBackgroundColor(Color.BLUE);
}
}
}
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();
}
});
}
}