I have created a custom dialog class (extends Dialog) , inside that dialog xml layout
i have expand/ collapse textview with animation, this is working fine,
but when i expand or collapse the view (of the textview) the size of the dialog also change (with out animation), how can make it also re-size with animation?
public class ErrorDialog extends Dialog {
private String err;
private TextView txt_help_gest;
public ErrorDialogOnClickListener errorDialogListener;
private String shortDesc;
private Animation slide_up_Animation;
private Animation slide_down_Animation;
private Animation rotate_arrow_down;
private Animation rotate_arrow_up;
public ErrorDialog(Context context,String shortDesc, String err, ErrorDialogOnClickListener errorDialogListener) {
super(context);
this.err=err;
this.errorDialogListener = errorDialogListener;
this.shortDesc=shortDesc;
slide_up_Animation= AnimationUtils.loadAnimation(context, R.anim.slide_up);
slide_down_Animation= AnimationUtils.loadAnimation(context, R.anim.slide_down);
rotate_arrow_down= AnimationUtils.loadAnimation(context, R.anim.rotate_arrow_down);
rotate_arrow_up= AnimationUtils.loadAnimation(context, R.anim.rotate_arrow_up);
getWindow().getAttributes().windowAnimations = R.style.Animations_SmileWindow;
}
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_error);
setTitle("שגיאה");
setCancelable(false);
// set values for custom dialog components - text, image and button
Button btnClose=(Button)findViewById(R.id.btnClose);
TextView text = (TextView) findViewById(R.id.textDialog);
txt_help_gest = (TextView) findViewById(R.id.txt_help_gest);
//help_title_gest = (TextView) findViewById(R.id.help_title_gest);
final ImageView imgArrow = (ImageView) findViewById(R.id.imgArrow);
LinearLayout ll_help_title = (LinearLayout) findViewById(R.id.ll_help_title);
// ll = (View) findViewById(R.id.ll);
text.setText(shortDesc+"\n התוכנית תסגר.");
txt_help_gest.setText(err);
txt_help_gest.setVisibility(View.GONE);
ll_help_title.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View arg0) {
//txt_help_gest.clearAnimation();
if(txt_help_gest.isShown()){
//collapse();
//slide_up_Animation.reset();
txt_help_gest.startAnimation(slide_up_Animation);
txt_help_gest.setVisibility(View.GONE);
imgArrow.startAnimation(rotate_arrow_up);
}
else{
// expand();
// slide_down_Animation.reset();
txt_help_gest.setVisibility(View.VISIBLE);
txt_help_gest.startAnimation(slide_down_Animation);
imgArrow.startAnimation(rotate_arrow_down);
}
}
});
btnClose.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View arg0) {
errorDialogListener.onButtonClick();
dismiss();
}
});
}
public interface ErrorDialogOnClickListener {
void onButtonClick();
}
}
Related
sry for my english..
In my app from Home_Activity, i click on an Button and a custom AlertDialog shows up. Within this AlertDialog(DialogAdd), i click on an ImageView say iv1 (or ImageButton - both didn't work) and another custom AlertDialog opens(Choose) - so far so good. Now i have my 2nd AlertDialog opened and see, say 4 little pictures. When i click on one of these pictures, the second AlertDialog should close and the ImageView (iv1) should change to that choosen icon from AlertDialog 2. I got everything running but not to change this ImageView/ImageButton...
I seperated this behaviour into a little app..
Home_Activity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Home_Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btn_showDialog);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogAdd addEntrieActivity = new DialogAdd(Home_Activity.this);
addEntrieActivity.createDialogAdd(Home_Activity.this);
}
});
}
}
DialogAdd
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
public class DialogAdd implements View.OnClickListener, Choose.Choose_Interface {
private LayoutInflater inflater;
private View view;
private Context context;
private AlertDialog dialog;
private String selectetSmile = null;
private ImageView ib_dialog_smile;
public DialogAdd(final Context context) {
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.dialog_line, null);
ib_dialog_smile = (ImageView) view.findViewById(R.id.ib_dialog_smile);
ib_dialog_smile.setOnClickListener(this);
}
public void createDialogAdd(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(view);
dialog = builder.create();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.ib_dialog_smile){
Choose choose = new Choose(context);
choose.createDialogChoose();
}
}
#Override
public void selectedSmile(String smile) {
selectetSmile = smile;
if(selectetSmile.equals("oh")){
ib_dialog_smile.setImageResource(R.drawable.oh);
}else if(selectetSmile.equals("oh_nooo")){
ib_dialog_smile.setImageResource(R.drawable.oh_nooo);
}else if(selectetSmile.equals("oh_what")){
ib_dialog_smile.setImageResource(R.drawable.oh_what);
}else if(selectetSmile.equals("oh_yes")){
ib_dialog_smile.setImageResource(R.drawable.oh_yes);
}
}
}
Choose
public class Choose {
public interface Choose_Interface {
void selectedSmile(String flag);
}
public Choose_Interface mCallback;
private View view;
private Context context;
private LayoutInflater inflater;
private AlertDialog dialog;
private ImageView oh, oh_nooo, oh_what, oh_yes;
public Choose(final Context context) {
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.choose, null);
mCallback = new DialogAdd(context);
oh = (ImageView) view.findViewById(R.id.oh);
oh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.selectedSmile("oh");
Toast.makeText(context, "oh klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
oh_nooo = (ImageView) view.findViewById(R.id.oh_nooo);
oh_nooo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.selectedSmile("oh_nooo");
Toast.makeText(context, "oh_nooo klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
oh_what = (ImageView) view.findViewById(R.id.oh_what);
oh_what.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.selectedSmile("oh_what");
Toast.makeText(context, "oh_what klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
oh_yes = (ImageView) view.findViewById(R.id.oh_yes);
oh_yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.selectedSmile("oh_yes");
Toast.makeText(context, "oh_yes klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
public void createDialogChoose() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(view);
dialog = builder.create();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();
}
}
i've tried it with invalidate ib_dialog_smile, i used an ImageButton and also ImageView, AppCompatImageView, setImageDrawable, setImageResource.. but the image didnt get updated.
Can someone please help me out to change ib_dialog_smile image?
Thank you!
I think your code is a little complicated. You don't need seperate classes to create two custom AlertDialogs. Just create two XML layouts each for every dialog and style them the way you want (in the first XML layout put your "chosen" ImageView and the button and in the second the four images).
Let's call them layout_1 (chosen icon) and layout_2 (four options)
then in your home_activity inflate those in a view var and choose them as alertDialog views like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating the first dialog.
AlertDialog.Builder dialogOneBuilder = new AlertDialog.Builder(this);
View layout1 = getLayoutInflater().inflate(R.id.layout_1, null);
dialogOneBuilder.setView(layout1)
AlertDialog dialogOne = dialogOneBuilder.show();
//Finding the chosen icon and button image from dialog 1
final ImageView chosenIcon = (ImageView)layout1.findViewById(R.id.chosen_icon);
Button chooseButton = (Button)layout1.findViewById(R.id.choose_button);
//On button click
chooseButton.setOnClickListener(new View.OnClickListener(){
//Creating the second dialog.
AlertDialog.Builder dialogTwoBuilder = new AlertDialog.Builder(this);
View layout2 = getLayoutInflater().inflate(R.id.layout_2, null);
dialogTwoBuilder.setView(layout2)
AlertDialog dialogTwo = dialogTwoBuilder.show();
//finding the ImageViews of the four choices
ImageView img1 = (ImageView)layout2.findViewById(R.id.img1);
ImageView img2 = (ImageView)layout2.findViewById(R.id.img2);
ImageView img3 = (ImageView)layout2.findViewById(R.id.img3);
ImageView img4 = (ImageView)layout2.findViewById(R.id.img4);
//Add them in a LinkedList helps to shrink your code
LinkedList<ImageView> choices = new LinkedList<>();
choices.add(img1);
choices.add(img2);
choices.add(img3);
choices.add(img4);
//Now set ONE ClickListener for all choices
for(int i = 0; i < choices.size(); i++){
choices.get(i).setOnClickListener(new View.OnClickListener(){
//change your icon to the chosen image
chosenIcon.setImageDrawable(choices.get(i).getDrawable());
//And close the second dialog.
dialogTwo.dismiss();
});
}
});
}
Worked perfectly for my voting & commenting dialog. Hope this helped you and the code was readable.
I'm trying to have one TextView switch from one sentence to another alongside my ImageSwitcher. Here is a sample code for my activity
private Integer images[]={R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4....};
private int currImage=0;
private Integer text[]={R.string.text0,R.string.text1,R.string.text2,R.string.text3,R.string.text4....};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeImageSwitcher();
setInitialImage();
setImageRotateListener();
setInitialText();
}
private void initializeImageSwitcher() {
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
return imageView;
}
});
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
}
private void setImageRotateListener() {
final ImageButton rightarrow = (ImageButton) findViewById(R.id.next);
rightarrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
currImage++;
if (currImage == 29) {
currImage = 0;
}
setCurrentImage();
setCurrentText();
}
});
}
private void setInitialImage() {
setCurrentImage();
}
private void setInitialText(){
setCurrentText();
}
private void setCurrentImage() {
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setImageResource(images[currImage]);
}
private void setCurrentText() {
final TextView textSwitcher = (TextView) findViewById(R.id.textView);
textSwitcher.setText(Integer.toString(text[currImage]));
}
The it compiles and run just fine. Issue lies in the text output. Instead of the sentences defined in the string, the TextView displays a series of numbers.
For example:
<string name="text1">Sentence one</string>
<string name="text2">Sentence two</string>
<string name="text3">Sentence three</string>
<string name="text4">Sentence four</string>
The output for "text1" would be 8513856, "text2" would be 841363, "text3" would be 18413587, and so on.
If anyone one has an idea of how to solve this issue, your help would be greatly appreciated.
You are displaying the resource id's of the strings, not the strings themselves.
Change to:
private void setCurrentText() {
final TextView textSwitcher = (TextView) findViewById(R.id.textView);
textSwitcher.setText(getResources().getString(text[currImage]));
}
I have a class where I include another layout on a button click. This included layout has some buttons and a code which executes on clicking these buttons. I have used a counter which indicates the number of times the button is clicked. First time clicking on the button includes the layout and the second time clicking removes the views and so on. Here's the code
public class Home extends Fragment implements OnClickListener {
int c = 0;
Button bmain, bnew, bolder;
RelativeLayout r1;
View rootView;
Animation slidedown, slideup;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
bmain = (Button) rootView.findViewById(R.id.btn2);
bmain.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View arg0) {
ViewGroup con = null;
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout flContainer = (FrameLayout)rootView.findViewById(R.id.flContainer);
//Loading animation
slidedown = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down);
slideup = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
//The counter indicates the number of clicks.
//Needs to be replaced for a better solution.
//If it's even add view
if(c%2==0)
{
//Adding layout here
flContainer.addView(layoutInflater.inflate(R.layout.test1,con,false ));
//Starting Animation
flContainer.startAnimation(slidedown);
//After adding layout we can find the Id of the included layout and proceed from there
bnew = (Button) rootView.findViewById(R.id.btntest);
bnew.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity(), "You Clicked New", Toast.LENGTH_LONG).show();
}
});
bolder = (Button) rootView.findViewById(R.id.btntest1);
bolder.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
Intent form = new Intent(getActivity(),FeedbackForm.class);
startActivity(form);
}
});
c++;
} //If ends here
//If it's odd remove view
else
{
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
//flContainer.removeView(flContainer);
//flContainer.removeView(layoutInflater.inflate(R.layout.test1, con, false));
c++;
}
}
}
The code at the end
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
removes the view but fails to process the animation. I have tried using removeView but in that case the buttonclicks in the if statement fail to execute the second time. What am I missing here? How can I achieve it?
The answer is pretty simple. You have to remove the view after the animation is finished. This can be achieved pretty simple, first you have to set an animation listener for your animation and in the onAnimationEnd callback - which is called when the animation is finished - you remove the views.
EDIT:
Replace this:
flContainer.removeAllViews();
flContainer.startAnimation(slideup);
With this:
slideup.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
flContainer.removeAllViews();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
flContainer.startAnimation(slideup);
If there are any further problems let me know.
I created a custom dialog, to create one standard dialog that i could create in just one line later one and it would be standard. With the parameters I change the text. The dialog is very simplistic and has only one button.
Now I am rethinking of this was a good idea. I actually want my app to stop until my dialog is displaying. How could i manage that? Give the dialog a return type? Or is there a better way?
my dialog:
/**
* custom dialog
*
* #param mcontext use activityname.this
* #param title
* #param text
* #param button
*/
public void showDialog(Context mcontext, String title,String text, String button) {
// fonts
Typeface tf_hn = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneue.ttf");
Typeface tf_hn_bold = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneuebd.ttf");
Resources res = mcontext.getResources();
// custom dialog
final Dialog dialog = new Dialog(mcontext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //not the normal dialog title
dialog.setContentView(R.layout.view_dialog);
TextView tv_dialog_title = (TextView) dialog.findViewById(R.id.tv_dialog_title);
tv_dialog_title.setText(title);
tv_dialog_title.setTypeface(tf_hn_bold);
tv_dialog_title.setTextColor(res.getColor(R.color.white));
TextView tv_dialog_text = (TextView) dialog.findViewById(R.id.tv_dialog_text);
tv_dialog_text.setText(text);
tv_dialog_text.setTypeface(tf_hn);
tv_dialog_text.setTextColor(res.getColor(R.color.white));
Button dialogButton = (Button) dialog.findViewById(R.id.bt_dialog_button);
dialogButton.setTypeface(tf_hn_bold);
dialogButton.setText(button);
dialogButton.setTextColor(res.getColor(R.color.white));
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
and then i can use it like this:
dialogH.showDialog(LoginActivity.this, res.getString(R.string.txt_dialog_fout), res.getString(R.string.txt_dialog_not_connected),res.getString(R.string.txt_dialog_button));
It worked all fine, until i wanted to show a dialog with "you are logged in" (or so) and then start a intent after the display was clicked away. Anyone an idea?
Create a custom dialog class with a built in listener like so.
public class MyDialog extends Dialog {
String title;
String text;
String button;
DialogListener listener;
interface DialogListener {
void onCompleted();
void onCanceled();
}
public MyDialog(Context context, String title, String text, String button) {
super(context);
this.title = title;
this.text = text;
this.button = button;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Typeface tf_hn = Typeface.createFromAsset(getContext().getAssets(), "helveticaneue.ttf");
Typeface tf_hn_bold = Typeface.createFromAsset(getContext()..getAssets(), "helveticaneuebd.ttf");
Resources res = getContext().getResources();
requestWindowFeature(Window.FEATURE_NO_TITLE); // not the normal dialog title
setContentView(R.layout.view_dialog);
TextView tv_dialog_title = (TextView) findViewById(R.id.tv_dialog_title);
tv_dialog_title.setText(title);
tv_dialog_title.setTypeface(tf_hn_bold);
tv_dialog_title.setTextColor(res.getColor(R.color.white));
TextView tv_dialog_text = (TextView) findViewById(R.id.tv_dialog_text);
tv_dialog_text.setText(text);
tv_dialog_text.setTypeface(tf_hn);
tv_dialog_text.setTextColor(res.getColor(R.color.white));
Button dialogButton = (Button) findViewById(R.id.bt_dialog_button);
dialogButton.setTypeface(tf_hn_bold);
dialogButton.setText(button);
dialogButton.setTextColor(res.getColor(R.color.white));
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(listener != null)
listener.onCompleted();
MyDialog.this.dismiss();
}
});
setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
if(listener != null)
listener.onCanceled();
}
});
} public void setDialogListener(DialogListener listener) {
this.listener = listener;
}
}
And to implement the dialog:
MyDialog dialog = new MyDialog(getContext(), title, text, button);
dialog.setDialogListener(new MyDialog.DialogListener() {
#Override
public void onCompleted() {
// do stuff when dialog is completed
}
#Override
public void onCanceled() {
// do stuff when dialog is cancelled
}
});
dialog.show();
There are a set of buttons, I want to get the result:
When I click one of them, first I divide them into two parts: the clicked one and the others. I'm trying to set different color or alpha value to different them.
Now I use setAlpha, but when I change the value from 0 to 255, it works, but when I change the value from 255 to 0 , it doesnot work. I don't know why.
Maybe after I invoke the methodButton.setAlpha(), I need invoke another method?
my code:
public class MainActivity extends Activity {
// button alpha value: minimize value
public static int BUTTON_ALPHA_MIN = 0;
// button alpha value: maximize value
public static int BUTTON_ALPHA_MAX = 255;
private LinearLayout centerRegion;
private LinearLayout bottomRegion;
private Button btnCheckIn;
private Button btnReview;
private Button btnMyCircles;
private Button btnSettings;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get all the widgets
getAllWidgets();
// set buttons click response function
btnCheckIn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.RED);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX);
}
});
btnReview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.BLUE);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX);
}
});
btnMyCircles.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.YELLOW);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MAX);
v.getBackground().setAlpha(BUTTON_ALPHA_MIN);
}
});
btnSettings.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.MAGENTA);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MAX);
v.getBackground().setAlpha(BUTTON_ALPHA_MIN);
}
});
}
/**
* get all the widgets
*/
public void getAllWidgets() {
this.centerRegion = (LinearLayout) this.findViewById(R.id.center_region);
this.bottomRegion = (LinearLayout) this.findViewById(R.id.bottom_region);
this.btnCheckIn = (Button) this.findViewById(R.id.button_check_in);
this.btnReview = (Button) this.findViewById(R.id.button_review);
this.btnMyCircles = (Button) this.findViewById(R.id.button_my_circles);
this.btnSettings = (Button) this.findViewById(R.id.button_setting);
}
}
Using AlphaAnimation should work; verified on my device.
public class Test extends Activity implements OnClickListener {
private AlphaAnimation alphaDown;
private AlphaAnimation alphaUp;
private Button b1;
private Button b2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll = (LinearLayout) findViewById(R.id.linear_layout);
b1 = new Button(this);
b1.setText("Button 1");
b1.setOnClickListener(this);
ll.addView(b1);
b2 = new Button(this);
b2.setText("Button 2");
b2.setOnClickListener(this);
ll.addView(b2);
alphaDown = new AlphaAnimation(1.0f, 0.3f);
alphaUp = new AlphaAnimation(0.3f, 1.0f);
alphaDown.setDuration(1000);
alphaUp.setDuration(1000);
alphaDown.setFillAfter(true);
alphaUp.setFillAfter(true);
}
public void onClick(View v) {
if (v == b1) {
b1.startAnimation(alphaUp);
b2.startAnimation(alphaDown);
} else {
b1.startAnimation(alphaDown);
b2.startAnimation(alphaUp);
}
}
}
The key is calling setFillAfter(true) so that the alpha change persists.
Thanks for the question and the answer. This really helped me out.
For my solution, I needed to set the alpha of a button without seeing any animation effect, but the button.setAlpha(x) was failing sporadically. Using animations instead did the trick, but I had to set the duration to zero to get the automatic effect.
alphaDown = new AlphaAnimation(1.0f, 0.3f);
alphaUp = new AlphaAnimation(0.3f, 1.0f);
alphaDown.setDuration(0);
alphaUp.setDuration(0);
alphaDown.setFillAfter(true);
alphaUp.setFillAfter(true);
I use this for player controls in a media application, so I had something like this:
boolean bInitPrevEnabled = m_btPrev.isEnabled();
boolean bInitNextEnabled = m_btNext.isEnabled();
boolean bInitPlayEnabled = m_btPlay.isEnabled();
m_btPrev.setEnabled(true);
m_btNext.setEnabled(true);
m_btPlay.setEnabled(true);
// Process enabling of the specific buttons depending on the state
if (bInitPrevEnabled != m_btPrev.isEnabled())
m_btPrev.startAnimation((m_btPrev.isEnabled()) ? alphaUp : alphaDown);
if (bInitNextEnabled != m_btNext.isEnabled())
m_btNext.startAnimation((m_btNext.isEnabled()) ? alphaUp : alphaDown);
if (bInitPlayEnabled != m_btPlay.isEnabled())
m_btPlay.startAnimation((m_btPlay.isEnabled()) ? alphaUp : alphaDown);
Button btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.main_btn);
Drawable d = getResources().getDrawable(R.drawable.imagen);
d.setAlpha(60);
btn.setBackgroundDrawable(d);
}
This works for me :)
public void setAlpha (int alpha) - deprecated
public void setAlpha (float alpha) (0f < alpha < 1f)
Added in API level 11