change button image in android - android

I want to change the image of a button in my code. I found this can be done in xml:
please check this link
but the image will not stay on after I released the button. I want to click the button
and the button change to a new image. Can this be done?

in onClick method you need to change the Button's image, this way..
public void onClick(View v) {
if(v==buttonName){
buttonName.setBackgroundResource(R.drawable.imageName);
}
}

Assuming an ImageButton...
You can change the background image in an onClick listener, similar to the following:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//set button image
myButton.setImageBitmap(myBitmapFile)
}
});

Related

Android transform ImageView to Button programmiticaly with two images and onClickListener

I want to create a button animation with two image on an ImageView programmiticaly only and by using onClickListener. I have two image that i can set with setImageResource() fonction but i don't know how to play the button animation before the button action launches.
Please, could you explain a bit more what you are wanting? Do you want to replace the ImageButton resource when you click on it? You can achieve it setting the OnClickListener
new View.OnClickListener() {
public void onClick(View v) {
imageButtonView.setImageResource(idOfTheNewResource);
}
};
Is this what you are wanting?

hide one image and show other

I have two images(above, below) place on each other. I want to hide the part of the above image where user touchs. At the end above image will completely remove and below image completely become visible.
How will I achieve this thing in Android. I am really clueless. Please help
example:
button = (Button) findViewById(R.id.button);
leftButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
image.setVisibility(View.INVISIBLE); //to set the image as invisible
image.setVisibility(View.VISIBLE); // to set it back to visible
}
});
hope this will help you.

Change zoom controls in a MapView

I would like to change the predefined style of the zoom controls in my application to appear like the new zoom controls in the google maps application. See below:
How can I do it? I have been looking around and I haven't found anything.
Thanks in advance.
You should have four image drawables namely:
Zoom in (+) enable/disable
Zoom out (-) enable/disable
Put them in MapView layout an ImageView or Button with background image as the above drawables.
Then you should assign onClickListeners like:
zoomIn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mapView.getController.zoomIn()
checkzoom();
}
});
zoomOut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mapView.getController.zoomOut()
checkzoom();
}
});
public void checkzoom() {
if(mapView.getZoomLevel()==1) // dont know if 0 or 1, just wrote it
{
zoomOut.setEnabled(false);
// or u can change drawable and disable click
}
if(mapView.getZoomLevel()==18) {
zoomIn.setEnabled(false);
// or u can change drawable and disable click
}
}
EDIT:
I solved the problem this way: disabled built in zoom controls (setBuiltInZoomControls(false)), added my own buttons to UI and added handlers for click on button events.

android: Replace the checkbox in CheckedTextView by an button

By default, there is a checkedbox in CheckedTextView, but now I want to replace it by an button.
I have tried to setCheckMarkDrawable which does exactly what I want it look like.
But now the problem is how can I add a listener to this. I want this button to remove current item in the ListView. How can I achieve this?
You could set your button by calling a ressource id like that:
setCheckMarkDrawable (R.id.myListButton);
Then set your listener on that button with:
(Button)findViewById(R.id.myListButton)
.setOnclickListener(new onClickListener()
{
public void onClick(View v)
{
//Do somethting here
}
});

ImageView click problem - Android

Can I make ImageView in android make some action when I click on that ImageView ?
For example, when I click on ImageView imgV that some panel be visible and when I click on other ImageView imgUV that he become unvisible .
imgV.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
int i=tbrSearchNear.VISIBLE;
tbrSearchNear.setVisibility(1-i);
tbrSearchCriterium.setVisibility(i);
}
});
It looks like that doesn't register click at all .
Maybe ImageButton is what you are looking for? http://developer.android.com/reference/android/widget/ImageButton.html
Setting visbility to 1-View.VISIBLE translates to 1-0; if you want to hide it, write something like:
imgV.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
// toggle visibility
int visibility=tbrSearchNear.getVisbility() == View.VISIBLE ? View.GONE : View.VISIBLE;
tbrSearchCriterium.setVisibility(tbrSearchNear.getVisbility());
tbrSearchNear.setVisibility(visbility);
}
});

Categories

Resources