Reputation: 45
EDIT: Posting full code (except XML as it a bunch of ridiculous table formatting!) Please ignore the code that doesn't pertain to my question! I am just getting functionality right now. I'll clean it up later.
First app and first question. I've researched here a while and usually find my answer but I have a bugger that is probably very obvious. I have an imageButton that doesn't seem to be calling the method assigned.
My XML for my imageButton:
<ImageButton
android:background="@null"
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton1"
android:src="@drawable/stats"
android:layout_gravity="center_vertical">
</ImageButton>
My code:
package com.talismancs;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class sheet extends Activity{
private String selection;
private String pick;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sheet);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
// Get extra from .main and remove spaces
String pick = extras.getString(selection);
pick = pick.replace(" ", "");
//Convert extra from string to int as ID and make image
int imageResource = getResources().getIdentifier(pick, "drawable", getPackageName());
ImageView iv = (ImageView) findViewById(R.id.imageView1);
final Drawable image = getResources().getDrawable(imageResource);
iv.setImageDrawable(image);
// Populate tv's from string
TextView tv1 = (TextView) findViewById(R.id.textView4);
TextView tv2 = (TextView) findViewById(R.id.textView5);
TextView tv3 = (TextView) findViewById(R.id.textView6);
TextView tv4 = (TextView) findViewById(R.id.textView7);
TextView tv5 = (TextView) findViewById(R.id.textView8);
int arrayresource = getResources().getIdentifier(pick, "array", getPackageName());
String[] CharString = getResources().getStringArray(arrayresource);
tv1.setText(CharString[0]);
tv2.setText(CharString[1]);
tv3.setText(CharString[2]);
tv4.setText(CharString[3]);
tv5.setText(CharString[4]);
}
}
public void onClick(View view) {
Intent i = new Intent(sheet.this, stats.class);
i.putExtra(pick, pick);
startActivity(i);
}
}
Seems simple right? When I click the imageButton it does absolutely nothing!
Please help.
EDIT: LOGCAT After selecting a spinner item which gets us to this activity .sheet
> 03-16 06:15:38.977:
> INFO/ActivityManager(563): Displayed
> activity com.talismancs/.sheet: 766 ms
> 03-16 06:15:42.907:
> DEBUG/dalvikvm(1735): GC freed 448
> objects / 39160 bytes in 58ms 03-16
> 06:15:43.847:
> INFO/NotificationService(563):
> enqueueToast pkg=com.talismancs
> callback=android.app.ITransientNotification$Stub$Proxy@43773720
> duration=1 03-16 06:15:43.877:
> INFO/ActivityManager(563): Starting
> activity: Intent {
> comp={com.talismancs/com.talismancs.sheet} (has extras) } 03-16 06:15:43.917:
> WARN/InputManagerService(563): Window
> already focused, ignoring focus gain
> of:
> com.android.internal.view.IInputMethodClient$Stub$Proxy@43718320
> 03-16 06:15:44.527:
> INFO/ActivityManager(563): Displayed
> activity com.talismancs/.sheet: 646 ms
After that is does nothing when I click the imageButton
Upvotes: 3
Views: 22945
Reputation: 16872
Not related to the OP's problem, but:
If you see that buttons do not work in a program that works ok on some device (you have megabytes of source but you are not the author), this may be because the buttons are intentionally blocked by the program. It may be done in at least two ways:
1) the buttons are disabled by the code via setEnabled(),
2) there may be a transparent layer above the buttons,
3) the transparent layer may even be a transparent activity.
(The rest is trivial: some condition was true on the device where the program worked but is always false on the new device... It would be weird if a market application behaved like that, but not so unreasonable if you work in a company that manufactures devices.)
Upvotes: 1
Reputation: 105
Button mybutton=new Button(ViewPagerSample.this);
mybutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Upvotes: 1
Reputation: 103
Vikias is right except that one doesn't need to override onCreate. The only thing wrong with the orginal code lies in the XML-layout file for the IMageButton.
The ImageButton declares android:onClick="click"
but the code in the activity doesn't contain a method click
, instead is has a method onClick
which isn't used in the XML for the ImageButton. That's why nothing happens.
Upvotes: 4
Reputation: 8014
Everything seems ok just try to do following changes..
public class sheet extends Activity implements onClickListener{
then in onCreate use
iv.setOnClickListener(this);
It should work. Else debug your code and see where your control is going.
Upvotes: 0
Reputation: 43359
I am sure first you must override the onCreate
method. Also load that layout resources you want to use in your Activity
.
So your code should be like this:
public class sheet extends Activity {
private String selection;
private String pick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void click(View view) {
Intent i = new Intent(sheet.this, stats.class);
i.putExtra(pick, pick);
startActivity(i);
}
}
Also try to use class name in standard way. Like every class name should start with a capital letter.
Check this for further references.
Upvotes: 3
Reputation: 9479
Your code is wrong. You havent assigned ur button with ClickListener.
Please see this sample code for how to implement it.
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
...
}
You may visit this link for more reference.
Upvotes: 11