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();
}
});
}
}
Related
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 am new to Android programming and I'm trying to write a simple game.
The problem is I came not that far and I'm encountering my first problem I try to search for a solution on the internet but with no success.
Error message:
android.widget.ImageButton cannot be cast to
android.widget.RelativeLayout
The error appears on this line of code:
Btn = (RelativeLayout) findViewById(R.id.imagebutton1);
I'm using Eclipse and also try to clean the project but with no success either.
Java file:
package com.example.tapthevac;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
RelativeLayout Btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btn = (RelativeLayout) findViewById(R.id.imagebutton1);
Btn.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
Btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/re/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/space"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.tapthevac.MainActivity" >
<ImageButton
android:id="#+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/start" />
</RelativeLayout>
Take a look at your xml file - imagebutton1 is an id of an ImageButton not of a RelativeLayout, so change the type of Btn to ImageButton and write the following lines:
ImageButton Btn;
....
Btn = (ImageButton) findViewById(R.id.imagebutton1);
The other parts of the code are okay.
Here is the problem:
Btn = (RelativeLayout) findViewById(R.id.imagebutton1);
Change it to:
Btn = (ImageView) findViewById(R.id.imagebutton1);
You are trying to convert ImageView to RelativeLayout
You should cast as ImageButton as your view is a ImageView button
Btn = (ImageButton) findViewById(R.id.imagebutton1);
Use:
ImageView btn;
btn = (ImageView) rootView.findViewById(R.id.your_image_button);
Make sure you understand how basic casting works in java.
(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).
this my xml file main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#raw/christmas"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/by_kostas"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="83dp"
android:text="#string/by_kostas"
android:textSize="20sp"
android:textColor="#F2F3F4"/>
<ImageView
android:id="#+id/settings"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="nameOfMethod"
android:src="#raw/settings" />
</RelativeLayout>
and my java Main
package com.kostas.mytorch;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//start our layout
super.onCreate(savedInstanceState);
setContentView(R.layout.main);{
final ImageView diskView = (ImageView) findViewById(R.id.settings);
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//my codes
}
});
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// System.out.println("image clicked...");//in my logcat
startActivity(new Intent("com.kostas.standroid.settings"));
}
});
}
}
my problem is when i click in the settings icon, my program crashes instead of what i wanted to create a new layout (settings xml is just black page),can someone be kind enough to help me out
You can attach click listener to any view by two ways:
In xml, write onClick attribute of the view and pass the method
name you have implemented in the activity.
Example: android:onClick="someMethod" and in the activity code, declare a method
public void someMethod(View view)
{
// handle click here
}
In activity, use setOnClickListener() to the view.
For now, try this:
diskView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// System.out.println("image clicked...");//in my logcat
startActivity(new Intent(Main.this, settings.class));
}
});
Also remove this line from xml: android:onClick="nameOfMethod"
Some time there is contextual problem. Try this.
Intent intent = new Intent(CurrentActivity.this, UpcomingActivity.class);
startActivity(intent);
Also don't forget to define class in Manifest file
try to remove
android:onClick="nameOfMethod"
from the xml layout. I believe this is the method that it's called when you click the settings button and it doesn't exist so it crashes.
Edit: I cannot get this to work correctly. Probably because I have no idea what I am doing. Anyways, here's my code. If anyone could help, I'd be very grateful: I needs to get it to display the battery mood image for the corresponding mod...
Themes:
package com.cydeon.plasmamodz;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Themes extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.themes);
ActionBar actionBar = getActionBar();
actionBar.hide();
Button Plus = (Button) findViewById(R.id.button1);
Button Blue = (Button) findViewById(R.id.button2);
Plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(Themes.this, Bmod.class);
i.putExtra("drawableResource", R.drawable.blue);
Themes.this.startActivity(i);
}
});
Blue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent a = new Intent(Themes.this, Bmod.class);
a.putExtra("drawableResource1", R.drawable.plus);
Themes.this.startActivity(a);
}
});
}
}
Bmods:
package com.cydeon.plasmamodz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class Bmod extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battery);
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.blue);
ImageView img = (ImageView) findViewById(R.id.iv1);
img.setImageResource(R.drawable.blue);
Intent a = getIntent();
int drawableResource1 = a.getIntExtra("drawableResource1", R.drawable.plus);
ImageView img1 = (ImageView) findViewById(R.id.iv1);
img1.setImageResource(R.drawable.plus);
}
}
battery(xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/bInstall"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Install" />
<Button
android:id="#+id/bReturn"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Return" />
<ImageView
android:id="#+id/iv1"
android:layout_width="match_parent"
android:layout_height="800dp" />
</RelativeLayout>
Intent i = new Intent(this, BaseClassForMod);
i.putExtra("drawableResource", R.drawable.this_mod_drawable);
startActivity(i);
Then in that Activity's onCreate():
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.default);
//Get a reference to your ImageView...
imageView.setImageResource(drawableResource);
Don't trust this code to compile, but that's the general idea. Use the same Activity for all of them, pass along the proper resource ID in the intent (e.g. for mod1, send the drawable ID for mod1), then in the activity, check for that resource ID and set it programmatically.
You shouldn't need 50 classes and 50 xml layouts if every one of them does the same thing. Make one activity and one layout. When the user selects something, pass an id of some kind as an Intent extra to the second activity so it can load whatever item is appropriate. I don't know how your data is modeled, but there should be a way to uniquely identify each option (and if there isn't, you should implement one).
Your first activity also doesn't need a button for each item. Use a ListView and an Adapter, and then you just need to provide a layout for one row.
make a single activity only. Inside of it get a reference to your image:
ImageView iv = (ImageView)findViewById(R.id.yourImgId);
Then set the picture to whichever one you want like:
iv.setImageResource(R.drawable.battery_img_1);