Reputation: 515
I just want to set a button's image to change when clicked. Example, if I want to click button A it will change. If I click button B, its image will change and the image of button A will revert back. How can I do this? I badly need help on this one.
package com.example.mobile_nurse;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
public class Posture_1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posture_1);
Back();
_1minute();
_15minute();
}
private void _15minute() {
ImageButton Button1 = (ImageButton) findViewById(R.id.imageButton3);
ImageButton Button4 = (ImageButton) findViewById(R.id.imageButton4);
View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button1.setBackgroundResource(R.drawable.posms1_button1_null);
Button4.setBackgroundResource(R.drawable.posms1_button_a);
}
};
Button1.setOnClickListener(myListener);
}
private void _1minute() {
ImageButton Button1 = (ImageButton) findViewById(R.id.imageButton3);
ImageButton Button4 = (ImageButton) findViewById(R.id.imageButton4);
View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button1.setBackgroundResource(R.drawable.posms1_button1);
Button4.setBackgroundResource(R.drawable.posms1_button_a_null);
}
};
Button1.setOnClickListener(myListener);
}
private void Back() {
ImageButton Button = (ImageButton) findViewById(R.id.imageButton1);
View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
};
Button.setOnClickListener(myListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.posture_1, menu);
return true;
}
}
Upvotes: 0
Views: 736
Reputation: 64
try this code:
Button1.setOnClickListener(new OnClickListener(){
public void onClick(View view){
//enter your code
//remove background
Button4.setBackground(null); //or
Button4.setBackgroundColor(Color.WHITE);
Button1.setBackgroundResource(R.drawable.posms1_button1);
}
});
Upvotes: 0
Reputation: 1870
I think you should use ImageView
as button instead of the selector
specified in Harish's answer.
With ImageView
you can set the src drawable to a level-list
, then you can control the level
(drawable) by calling setImageLevel(int)
save the following in res/drawable/level.xml
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:maxLevel="0">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="@android:color/black" />
<solid android:color="#333" />
<padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
<corners android:radius="5dp" />
</shape>
</item>
<item
android:maxLevel="1">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp" android:color="@android:color/black" />
<solid android:color="#FF6699" />
<padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
<corners android:radius="5dp" />
</shape>
</item>
</level-list>
then your activity_main.xml
<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">
<ImageView
android:id="@+id/level_image"
android:layout_width="80dp"
android:layout_height="40dp"
android:src="@drawable/level" />
</RelativeLayout>
then your onCreate()
method
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView img = (ImageView) findViewById(R.id.level_image);
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (count%2==0) img.setImageLevel(1);
else img.setImageLevel(0);
count++;
}
});
}
In your case, your can create multiple ImageViews using level-list.
Upvotes: 0
Reputation: 24720
see StateListDrawable and its representation in the xml: <selector>
tag
Upvotes: 0
Reputation: 7131
Try like this it's just sample code
`
final Button b1 = (Button) findViewById(R.id.button1);
final Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
b1.setBackgroundResource(R.drawable.imagesa);
b2.setBackgroundColor(Color.TRANSPARENT);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
b2.setBackgroundResource(R.drawable.imagesa);
b1.setBackgroundColor(Color.TRANSPARENT);
}
});`
Upvotes: 0
Reputation: 14199
setBackgroundResource updates background not the Image
so
use setImageDrawable(getResources().getDrawable(R.drawable.your_Image))
inside _15minute() change to
Button1.setImageDrawable(getResources().getDrawable(R.drawable.posms1_button1_null)
Button4.setImageDrawable(getResources().getDrawable(R.drawable.posms1_button_a)
and inside _1minute() change to
Button1.setImageDrawable(getResources().getDrawable(R.drawable.posms1_button1)
Button4.setImageDrawable(getResources().getDrawable(R.drawable.posms1_button_a_null)
Upvotes: 0
Reputation: 2386
Try selector for this :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/iconSelector">
<!-- pressed -->
<item android:state_pressed="true" android:drawable="@drawable/btn_icon_hl" />
<!-- focused -->
<item android:state_focused="true" android:drawable="@drawable/btn_icon_hl" />
<!-- default -->
<item android:drawable="@drawable/btn_icon" />
</selector>
you can change state according to your need
Upvotes: 2