I have an ImageButton which on click i show a dialog box where users can either take a photo from the camera or choose from the gallery. On selecting image from either sources i setBitmap for that ImageButton to the image selected like this
SelectedPhoto = BitmapFactory.decodeFile(selectedImagePath);
DisplayPhoto.setImageBitmap(SelectedPhoto);
Now when some one has already selected an image and click the image again i want to show a different dialog which contains a third option "Remove Photo".
What property of the image button should i check and against what ?
ImageButton in XML
<ImageButton
android:id="#+id/DisplayPhoto"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginRight="8dip"
android:background="#drawable/signup_photo_selector" android:scaleType="centerCrop" />
ImageButton Background XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/signup_form_photo_selected" android:state_pressed="true"/>
<item android:drawable="#drawable/signup_form_photo"/>
</selector>
Would imgButton.getDrawable() work, since it returns null if no drawable has been assigned to the imagebutton?
If not, or if you don't want to get the entire drawable just to see if it's there, you can use a tag. imgButton.setTag(object) lets you store any object within the imagebutton... every time you set its background, you can tag a value that identifies whether its background was set. You could even use different values to differentiate whether you set its background using a camera or from the gallery, if that's useful. When you want to see if the imagebutton has a background or not, use imgButton.getTag() to retrieve the object.
Edit. Here is how you would use setTag and getTag. I will use an Integer object as the ImageButton's tag, where a value of 0 indicates no background has been set and a value of 1 indicates a background has been set. You can use an enum or final variables if you want to make the code a bit clearer, but using an Integer will work as an example.
public class MainActivity extends Activity, implements OnClickListener {
private ImageButton imgButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgButton = (ImageButton)findViewById(R.id.imgID);
imgButton.setTag(new Integer(0)); // no background
...
}
public void onClick(View view) {
ImageButton ib = (ImageButton)view;
int hasBackground = ib.getTag().intValue();
if(hasBackground==0) {
// imagebutton does not have a background. do not include remove option
...
} else {
// imagebutton has a background. include remove option
}
}
}
Related
I saw some posts with a similar question but they still differ from my problem here. I am making painting app in Android Studio and I want to indicate the option which user selected (whether it is move tool, pencil etc.) Here is the picture:
So, I want to change the background color of the button when it is selected and revert it back to default color when another button is selected.
I tried doing it with XML selector but later I saw that there is now "selected" attribute for a regular button. These are regular buttons. What is the easiest way to solve this?
Try this code (button_selector.xml, put it in your drawable folder)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/holo_blue_dark" android:state_selected="true"></item>
<item android:drawable="#android:color/holo_blue_dark" android:state_pressed="true"></item>
<item android:drawable="#android:color/darker_gray"></item>
</selector>
XML
<Button
android:background="#drawable/button_selector" />
You could use a class variable for keeping track of the currently selected button, and detect when a new button is selected. You would then perform the action of "selecting" the new button, and "deselecting" the previous one. Example:
private Button mSelectedButton;
private void setOnClickListeners() {
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
Button clickedButton = (Button) view;
//in case no button is selected, this will only "select" the clickedButton
if (mSelectedButton == null) mSelectedButton = clickedButton;
//previous selected button (should return to original state)
mSelectedButton.setBackgroundColor(R.color.original_state);
//your new selected button
clickedButton.setBackgroundColor(R.color.selected_state);
mSelectedButton = clickedButton; //save currently selected button
}
};
yourButton1.setOnClickListener(listener);
yourButton2.setOnClickListener(listener);
yourButton3.setOnClickListener(listener);
...
}
I want to know how I could change a buttons backgroundresource that mimics another button's background resource so that whenever I change that button's backgroundresource it another button mimics the looks of the first button...
for example:
int icon = R.drawable.ic_icon; //more specifically I stored R.drawable.ic_icon in SQL and retrieve and save in int icon when retrieve from that table, so when the table is change the first button dynamically change on create;
btn_01.setBackgroundResource(icon); //when this button is pressed it inflates a layout containing btn_02
btn_02.setBackgrounResource(??????); //this button is on a different layout and is used by different activity and should take the backgroundresource of the button that have been pressed to call that layout.
I could use if else statement but I have different button to be copied by the second button and each button has different backgroundresource possibility.
I couldn't understand your question fully but anyways here is what I got
You can put the resource id in the Intent before starting the activity when Btn_01 is clicked.
Btn_01.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this,Activity2.class);
intent.putExtra("resource_key", R.drawable.ic_heart);
startActivity(intent);
}
});
then in your Activity2 you can just get your resource data and set to whatever button you want
int DEFAULT_RESOURCE = R.drawable.ic_close;
int resourceId = getIntent().getIntExtra("resource_key",DEFAULT_RESOURCE);
Btn_02.setBackgroundResource(resourceId);
I am busy creating an app. I succeeded in creating the button with a custom font and all. Now what I'd want is that when I click the button, it must disapear, the background color of the view must randomely change and text must be loaded from an database.
How does one go about this?
Matthew
Well, the disappearing can be make like this:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click - Disappear in your case...
v.setVisibility(View.INVISIBLE); //can be View.GONE as well...
}
});
}
}
Then for the background I guess you can create an array with all the colors you want (or something that generates a random HEX code) and then do setBackground(X) where X is the HEX code that you just generated... You need to specify more about the database part though.
I have a layout which contains lots of images. What I have to do is when an image is clicked, I have to show its details. But I don't want to have onClickListeners for all the images. How can I achieve this?
You don't have to have different handlers for all the images. Instead use one handler for all the images. This would make your code cleaner, manageable and solve your problem too.
public void onCreate(Bundle bundle) {
//...
OnClickListener mHandler = new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.img1:
//..
break;
case R.id.img2:
//....
break;
}
}
};
ImageButton btn1 = (ImageButton)findViewById(R.id.img1);
ImageButton btn2 = (ImageButton)findViewById(R.id.img2);
//...
btn1.SetOnClickListener(mHandler);
btn2.SetOnClickListener(mHandler);
//...
}
One Listener to rule them all.
Implement onClick() on an object, register it as listener
In onClick(), examine the View object passed as parameter to determine which of the images was clicked. You can do anything from getId() to casting it to (ImageView) and getting the actual image out.
Once you know which image was clicked, do what you will with it.
If you're looking to implement custom behavior for an ImageView (or whatever), and then have multiple instances of that type of view, you should subclass the ImageView and put your listener in there. Then you've got an encapsulated View that implements the custom behavior you want, and if you decide later that you want more or less or them, or to put them in another place, it's easy to move the View and its behavior without ripping apart your Activity.
Some XML attributes of buttons (such as background, textColor, etc) can be defined with color or drawable state List like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/>
<item android:state_focused="true"
android:color="#ff0000ff"/>
<item android:state_enabled="true"
android:color="#ff00ffff"/>
<item android:color="#ff000000"/>
</selector>
When view state changes (pressed/unpressed, for example), corresponding color is changed automatically.
How can I prograqmmatically handle some kind of stateChangedEvent to perform more complicated layout change, than just changing a color (for example, change font size or set another text)?
For focus changes and touch events you can register listeners by setOnFocusChangeListener and setOnTouchListener. And changes about disabled/enabled states you can perform directly after changing your button state.
// use the selector method to pass your button and image
// you can use color also
b1=(Button)findViewById(R.id.button1);
// b2=(Button)findViewById(R.id.button2);
selector(b1, R.drawable.image_1_2, R.drawable.image_1);
// selector(b2, R.drawable.image_2_2, R.drawable.image_2);
}
public void selector(Button b,int pressed_image,int normal_image )
{
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(pressed_image));
states.addState(new int[] { },
getResources().getDrawable(normal_image));
b.setBackgroundDrawable(states);
}
Just override View.setPressed:
#Override
public void setPressed(boolean pressed) {
super.setPressed(pressed);
...
}
Handler onTouch(View v, MotionEvent event) for specific views and perform actions in MotionEvent.DOWN / Up according to your requirement.
You have to take a reference to the View Object (button) via findViewById(<object_id>) and than use the appropriate methods from the API.
For example:
private Button aButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
aButton = (Button) findViewById(R.id.your_button_id);
//example 1
aButton.setVisibility(SomeExpressionEvaluation ? View.GONE : View.VISIBLE);
//example 2
if (SomeExpressionEvaluation) {
aButton.setText("Some Text");
}
and so on, just take a look at the API, especially inherited methods from the View class.
I must say you can use a (touch listner) this how u use a touch listner
image=(ImageView)findViewById(R.id.image);
find ur image first
Add a touch Listner to ur image
image.setOnTouchListener(image_onTouch);
//Add a touch method which is by name image_onTouch
OnTouchListener image_onTouch=new OnTouchListener(){
#Override
public boolean onTouch(View arg0,MotionEvent arg1){
int iAction=arg1.getAction();
if(iAction==0){
image.setImageResource(R.drawable.image1);
}
else{
image.setImageResource(R.drawable.image2);
}
return false;
}
};
// image 1 is ur image which u want 2 click and image 2 is the image when you touch that image you have to make an another image in which background color do u wanna show and use it in the code
I had also this kind of problem in past. I solved by putting this XML file in separate drawable folder in Res instaed of drawable-mdpi or else. And make sure that you have to give this Xml as your button's background.