In my android application list view there is a image. The imageview is on another layout. i want to change that image to another one through code . when i run the program with the code its getting error..'
Can anyone help me to find the error thank youu ./
I have tried..
Bitmap bImag = BitmapFactory.decodeResource(this.getResources(), R.drawable.tick);
down.setImageBitmap(bImag);
and this too
myImgView.setBackgroundResource(R.drawable.monkey);
My xml code .. (I used Onclick on the imageview)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="center"
android:text="asdas"
android:layout_weight="0.9"
android:paddingTop="5dp"/>
<ImageView
android:id="#+id/icon"
android:onClick="download"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="right"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/download" />
</LinearLayout>
Java code ..
public void download(View view) {
String value = getIntent().getExtras().getString("id");
if (value.equals("Pretham (2016)")) {
Intent i=new Intent(Song_List.this, MyService.class);
startService(i);
Toast.makeText(Song_List.this, "Downloading..........", Toast.LENGTH_SHORT).show();
Bitmap bImag = BitmapFactory.decodeResource(this.getResources(), R.drawable.tick);
down.setImageBitmap(bImag);
}else{
Toast.makeText(Song_List.this, "Downloaded Song.....", Toast.LENGTH_SHORT).show();
}
my logcat is..
Process: com.example.jithin.myapplication, PID: 1539
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
The imageView has:
android:onClick="download"
Only works if the layout is from Activity setContentView() not from listview or Fragment
You need to add on the adapter:
ImageView img = findViewById(R.id.download);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//CODE on click
}
});
and remove onclick from xml
Related
I'm creating a Soundboard app which uses buttons currently to make different sounds. However I want to change the buttons to image buttons, having tried this I get the following error:
Unexpected Cast to ImageButton: Layout tag was imageView.
I've tried looking around the internet but can't find a fix. The code in the java class looks like this.
public class SecondActivity extends AppCompatActivity {
Button b,b2;
MediaPlayer nice,burp,fancy;
ImageButton IMG;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
b = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button3);
nice = MediaPlayer.create(this, R.raw.nice);
burp = MediaPlayer.create(this, R.raw.burp);
fancy = MediaPlayer.create(this, R.raw.fancy);
configureImageButton();
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
nice.start();
}
}
);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
fancy.start();
}
}
);
}
private void configureImageButton() {
ImageButton IMG = (ImageButton) findViewById(R.id.IMG);
IMG.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
burp.start();
}
}
);
}
The XML code looks like this:
<?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: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.oliver.olliessoundofmusic.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nice"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fancy"
android:id="#+id/button3"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/IMG"
android:background="#drawable/robert"
android:layout_toEndOf="#+id/button3" />
</RelativeLayout>
I just want imageButtons to work with action listeners the same way a normal button would, to make the app more visually appealing.
Thanks in advance.
In your xml change ImageView tag to ImageButton
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nice"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fancy"
android:id="#+id/button3"
android:layout_alignTop="#+id/button"
android:layout_toEndOf="#+id/button" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/IMG"
android:background="#android:color/transparent"
android:src="#drawable/robert"
android:layout_toEndOf="#+id/button3" />
Please notice that I made the background to a transparent color, and made the drawable to be in src property
Cast this to an ImageView instead, then since that is what you have in the XML
ImageButton IMG = (ImageButton) findViewById(R.id.IMG);
The OnClickListener will still work as expected.
Also, beware of variable shadowing. You'd want line this instead.
IMG = (ImageButton) findViewById(R.id.IMG);
Declare IMG as ImageView in you activity or use imagebutton widget in the layout file.
I am developing Restaurant menu app. I have 9 image button in activity_main.xml. For example: If when i clicked soup image button, the app will soup price and soup image in other layout. But i dont want create 9 layouts file for this. All of prices appear one layout. I didnt write method for this. I am waiting your help. Sorry for my bad english.
This is my menu.
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton2"
android:layout_gravity="left|top"
android:background="#drawable/corbalar"
android:contentDescription="#string/corbalar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton3"
android:layout_gravity="center_horizontal|top"
android:background="#drawable/zeytinyaglilar"
android:contentDescription="#string/zeytinyaglilar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton4"
android:layout_gravity="right|top"
android:background="#drawable/spesiyaller"
android:contentDescription="#string/spesiyaller"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton5"
android:layout_gravity="left|center_vertical"
android:background="#drawable/pideler"
android:contentDescription="#string/pideler"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton6"
android:layout_gravity="center"
android:background="#drawable/salatalar"
android:contentDescription="#string/salatalar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton7"
android:layout_gravity="right|center_vertical"
android:background="#drawable/kebaplar"
android:contentDescription="#string/kebaplar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton8"
android:layout_gravity="left|bottom"
android:background="#drawable/tatlilar"
android:contentDescription="#string/tatlilar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton9"
android:layout_gravity="center_horizontal|bottom"
android:background="#drawable/mesrubatlar"
android:contentDescription="#string/mesrubatlar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton10"
android:layout_gravity="right|bottom"
android:background="#drawable/sicakicecekler"
android:contentDescription="#string/sicakicecekler"
android:clickable="true"
android:onClick="degistirActivity"/>
This is the place I'll show information.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="tr.edu.fatih.nad0.com.kilislimenu.fiyatGoster">
//my items image
<ImageButton
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageButton11"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/gosterilecekyemek" />
//my items price
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:id="#+id/textView"
android:layout_below="#+id/imageButton11"
android:layout_centerHorizontal="true" />
What you'll want to do is use an intent with extras to pass information of the clicked item to the activity. There's a couple of ways to do this:
1) Manually create an OnClickListener for each item (ideally, you'd generate your items from a SQLite database rather than having a static list).
Using an OnClickListener would look something like this:
ImageButton myButton = (ImageButton) findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent priceIntent = new Intent(getApplicationContext(), priceActivity.class);
priceIntent.putExtra("price",6.99);
priceIntent.putExtra("name","Hamburger");
startActivity(priceIntent);
}
});
Then in your onCreate method for priceActivity.class, you'd read the values like this:
Bundle extras = getIntent().getExtras();
double price = extras.getDouble("price");
String name = extras.getString("name");
2) Use the onClick attribute in your XML to process the information through an identifying tag or some such.
In your XML you can specify android:onClick="myMethod" as well as an identifying tag, like android:tag="hamburger". Then you'd make your myMethod:
public void myMethod(View v) {
String tagId = v.getTag().toString();
// Create intent with extras like above
}
Again, ideally you'd be loading all of this information from a database so that you can easily access the information with fewer methods rather than manually entering the hard-coded data straight into the app.
you can follow below link to workout with your problem.
http://developer.android.com/training/basics/firstapp/starting-activity.html
Alternatively,
you can implement method like this on your button.
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
On another Activity, you can receive Intent and Display your price.
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
I have Following layout Scenario
Want to implement the Click even of TextView ..but the TextView Click not working
Here is code Sample
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:clickable="false">
<customview
android:id="#+id/custom"
android:layout_width="200dp"
android:layout_height="200dp"
android:clickable="false"/>
<LinearLayout
android:id="#+id/abc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/zero_done"
android:padding="5dp"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
My TextView ClickEvent not working ...please help me
Class Code
TextView view= (TextView) findViewById(R.id.btn);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Press",
Toast.LENGTH_SHORT).show();
}
});
You didn't call show method of Toast,so you have no idea whether TextView click event did happen or not.
Try this:
Toast.makeText(getApplicationContext(), "Press",
Toast.LENGTH_SHORT).show(); // don't miss show method.
Hi I am getting this message android.widget.imagebutton cannot be cast to android.widget.button (See attachment pic for logcat error) which I don't understand why? because my code seems to be right.
Code:
ImageButton Calendar = (ImageButton) findViewById (R.id.Calendar);
Calendar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent OpenCalendarbtn = new Intent (Menu.this, DisplayCalendar.class);
startActivity (OpenCalendarbtn);
}
});
Complete Layout xml file:
`
<ImageButton
android:id="#+id/Read"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/Write"
android:layout_marginRight="31dp"
android:src="#drawable/read" />
<ImageView
android:id="#+id/AppLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/logo" />
<ImageButton
android:id="#+id/Write"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/AppLogo"
android:layout_marginLeft="35dp"
android:layout_marginTop="14dp"
android:src="#drawable/write" />
<ImageButton
android:id="#+id/Calendar"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignLeft="#+id/Write"
android:layout_below="#+id/Write"
android:layout_marginTop="20dp"
android:src="#drawable/calendar/>
`
Make sure you have used the <ImageButton> tag instead of <Button> tag in your layout resource file.
That could be the cause of the error.
Change your
ImageButton Calendar = (ImageButton) findViewById (R.id.Calendar);
To:
Button Calendar = (Button) findViewById (R.id.Calendar);
Its an Eclipse problem not in your code.
Solution for this is just copy your code in xml by Ctrl+c and save the xml then again paste it again then save it and run.....this works with me.
I have this xml-Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical" android:background="#color/white" android:layout_width="fill_parent" android:layout_height="200px">
<TextView
android:layout_x="0dp"
android:layout_y="10dp"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#color/white"
android:text="Name: " />
<EditText
android:layout_x = "20px"
android:layout_y = "10px"
android:layout_gravity="left"
android:textSize="15sp"
android:id="#+id/et_username" android:textColor="#color/black"
android:layout_width="150px"
android:layout_height="50px" />
<Button
android:layout_x = "200px"
android:layout_y = "10px"
android:layout_gravity="left"
android:textSize="16sp"
android:layout_width="96px"
android:layout_height="50px"
android:background ="#drawable/login"
android:id="#+id/btn_login"
android:textColor="#color/white"
android:text="next"
android:onClick="onLoginClicked" />
</AbsoluteLayout>
</LinearLayout>
java File :
public class ButtonAdapter extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
return LayoutInflater.from(mContext).inflate(R.layout.custom_keyboard, null);
}
public void onLoginClicked(View v) {
Button button = (Button) v;
String key = button.getText().toString();
anotherMethod(key, false);
}
...
}
and I use the adapter here:
GridView gridview = new GridView(context);
gridview2.setAdapter(new KeyboardAdapter(1, context));
can anybody tell me, why do I get the following error when I click the button?
java.lang.IllegalStateException: Could not find a method onLoginClicked(View) in the activity class MainActivity for onClick handler on view class android.widget.Button
This happening because you have the following in your xml:
<Button
android:layout_x = "200px"
android:layout_y = "10px"
android:layout_gravity="left"
android:textSize="16sp"
android:layout_width="96px"
android:layout_height="50px"
android:background ="#drawable/login"
android:id="#+id/btn_login"
android:textColor="#color/white"
android:text="next"
android:onClick="onLoginClicked" />
The last line means that when this button is clicked, a method will be invoked. This method is named "onLoginClicked", it should be public and have a parameter of type View and be defined in the Activity class.
So, go to your activity and write something like:
public void onLoginClicked(View v) {
//toast, log, open activity, etc
}
Why are you trying to make the code more complex.
Just try to do this :
Button b=(Button)findViewId(R.id.btn_login);
b.setOnClickListener(new OnClickListener(){
//perform your action here
});
I would remove the onClick parameter from your layout XML and handle the click with a listener. Add this code to your onCreate() method in your activity:
Button button = (Button)findViewById(R.id.btn_login);
button.setOnClickListener(new OnClickListener(){
String key = button.getText().toString();
anotherMethod(key, false);
});