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
Your code is wrong. You havent assigned ur button with ClickListener.
Please see this sample code for how to implement it.
You may visit this link for more reference.