Hello every thing for change Button change image I found, was change image when you clicking on button like this
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ImageButton aButton = (ImageButton)v;
aButton.setImageResource(R.drawable.image1);
}
});
//I wanna change button image here
How can I change image source out of "on click" function?
Your question is confusing but to do what you want your code should be the following:
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ImageButton aButton = (ImageButton)v;
aButton.setImageResource(R.drawable.image1);
}
});
imageButton.setImageResource(R.drawable.image2);
This will set your buttons image resource to image2 - but change to image1 when it is clicked.
i think you want selector means when you press button then its background changed and when we relieve then it will get previous image.
for this you will selector:
http://www.mkyong.com/android/android-imagebutton-selector-example/
i hope, it will help.
I assume that imageButton is global variable inside parent class
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
imageButton.setImageResource(R.drawable.image1);
}
});
imageButton = (ImageButton) findViewById(R.id.imageButton);
// This will change the Image Button to use image2
imageButton.setImageResource(R.drawable.image2);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
imageButton.setImageResource(R.drawable.image1);
}
});
However i must ask why you need to change imageButton to image2 in the code at the place that you asked? You can simply achieve this in xml.
Related
How can I prevent the user to change the value selected in spinner after some event like button press?
It is like after selecting the desired value from spinner the user will press a button after which they will not be allowed to see the values in the spinner except the top value i.e. the selected one so that no change be made in the selected value.
UPDATE:- And yes the spinners are also created dynamically on the press of that same button so it should lock only the previously created spinners not the new one.
Code for dynamic creation of Layout which contains the Spinner along with an EditText & a Button:-
View.OnClickListener addListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final RelativeLayout newView = (RelativeLayout) getLayoutInflater().inflate(R.layout.product_row_detail, null);
newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
container.removeView(newView);
}
});
container.addView(newView);
Simple just disable the spinner on button click
spinner.setEnabled(false);
In your onClick method, you can just set the visibility to GONE to remove the view as shown below:
View.OnClickListener addListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
final RelativeLayout newView = (RelativeLayout) getLayoutInflater().inflate(R.layout.product_row_detail, null);
newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
newView.setVisibility(View.GONE); // remove (hide) your view
}
});
container.addView(newView);
And to enable it again, you can set its visibility to VISIBLE as shown below:
newView.setVIsibility(View.VISIBLE);
Or you could just show/hide your spinner as shown below:
your_spinner.setVisibility(View.GONE); // to remove (hide)
your_spinner.setVisibility(View.VISIBLE); // to make it visible
If you want to only disable the spinner:
your_spinner.setEnabled(false);
Hope it helps.
You need to create a variable (may be boolean flag = true)
On button click change to flag = false
And inside Listener to Spinner check if flag is true of false
Example
//inside listener of spinner
if(flag){
//do task
}else{
//restrict the task or don't do anything or display message
}
For Updated question
Then you should use disable() method. spinner1.disable() or spinner2.disable() or so on..
I am making a contact app and I have added an image button. When user clicked on the image button, he/she would be directed to the calling application. When I run the code on the emulator, the image button does not work.
Below is my code:
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:12125551212"));
startActivity(intent);
}
});
}
The LogCat shows that eglSurfaceAttrib not implemented. Where did I get wrong?
Any help will be appreciated. Thanking you.
Is it possible to set an onClickListener for a bitmap while programming an Android app in java? I tried using the same code as a button but eclipse displayed an error that a bitmap was not valid. Please provide example code with your answer if it is possible.
Thanks!
You can create an ImageButton and set it the bitmap with setImageBitmap. Then just set a listener to the ImageButton object.
ImageButton imgButton = (ImageButton) findViewById(R.id.imgButton);
imgButton.setImageBitmap(myBitmapObject);
imgButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(yourActivity.this, "ImageButton Pressed",
Toast.LENGTH_LONG).show();
}
});
I create button like this:
Button button = new Button(this);
button.setText("2012");
button.setBackgroundColor(Color.TRANSPARENT);
button.setOnClickListener(mCorkyListener);
layout.addView(dateButton);
On click listiner i have this method. Here i want to change button text color. Bu View don't have this method
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
//v.setBackgroundColor(Color.RED);
//so how to change pressed button text color ?
//v.setTextColor(colors);
}
};
There wouldn't be just one button. There would be many of those and i need to chnage text color when button pressed.
I know you asked about changing text color, and everyone else has pretty well covered that, but you could also change the button color itself (which I find much more visible than a text color change :p)...
import android.graphics.PorterDuff;
To set it to green (assuming you start with a standard gray button):
aButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Button aButton = (Button) view.findViewById(R.id.abutton);
aButton.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
}
}
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
Button button = (Button)v;
button.setTextColor(Color.RED);
}
};
button.setTextColor(Color.WHITE);
This will change the textcolor of the button.Do u want to give a press effet to button when click on it?
the best way to do this is not programmatically (using selector), but if you want to do it programmatically you can cast it to Button and then change the color.
public void onClick(View v) {
Button b = (Button) findViewById(v.getId());
b.setBackgroundColor(Color.RED)l
}
If you are interested to use the View of onClick(View v) then just cast it to Button
public void onClick(View v) {
if(v instanceof Button){
((Button)v).setTextColor(Color.WHITE);
}
}
I'm creating a dialog box and using the (this) isnt working. Up until now its just been a button calling a dialogbox but now the button within the called dialogbox needs to call another dialog. The Dialog dialogdelcon is the one with problem.
Here is the code:
case R.id.delappt:
//rmvall();
final Dialog dialogdelsel = new Dialog(this);
dialogdelsel.setContentView(R.layout.delsel);
dialogdelsel.setTitle("What would you like to do?");
dialogdelsel.setCancelable(true);
Button btndelsel = (Button) dialogdelsel.findViewById(R.id.btndelsel);
btndelsel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete selected code here.
}
});
Button btndelall = (Button) dialogdelsel.findViewById(R.id.btndelall);
btndelall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete all code here.
final Dialog dialogdelcon = new Dialog();
dialogdelcon.setContentView(R.layout.delcon);
dialogdelcon.setTitle("Deletion Confirmation");
dialogdelcon.setCancelable(true);
Button buttoncnclok = (Button) dialogdelcon.findViewById(R.id.btndelcon);
buttoncnclok.setOnClickListener(new OnClickListener() {
// on click for cancel button
#Override
public void onClick(View v) {
dialogdelcon.dismiss();
}
});
dialogdelcon.show();
}
});
dialogdelsel.show();
break;
getApplicationContext() or use YourActictyName.this Because this refers the button click listner ,not your class Object
If this code is in the onCreate() method, or similiar, add getApplicationContext() instead of this and you should be fine. That's because this in a Button-context will refer to the button environment.
To improve the isolation between the two dialogs, it would be best to call showDialog(R.id.delapptcon) from the onClick handler. Then load the new dialog in the onCreateDialog of your activity. In this way, you can create more reusable dialogs and avoid the scoping issue you have now.