How do I add animation to BottomSheetDialogFragment - android

I have a BottomSheetDialogFragment which has two buttons in it and when I click on any button dismiss() method is called. Is there a way by which I can animate BottomSheetDialogFragment. I want it to show a slow sliding down animation with a duration of 1000ms.
Sample code
signin.findViewById(R.id.signin_button_using).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callback.onClickSignInEmail();
dismiss();
}
})

In your fragment which is extended with BottomSheetDialogFragment, try overriding this method like this
#Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation;
}
DialogAnimation can be defined in the styles like this
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_up</item>
<item name="android:windowExitAnimation">#anim/slide_down</item>
</style>
Further, slide_up and slide_down would be your implementation of required animation. You can find plenty of example for the same online.

Related

How to switch background of button in RecyclerViewAdapter

I have buttons in my Recyclerview what is want to do is whenever user clicks first button the background of that button should changed and when user clicks next button that time first button should appear as normal button. how to do it? Please help me.
Here is my sample code:
#Override
public void onBindViewHolder(final AreaRecyclerViewAdapter.ViewHolder Viewholder, final int position) {
final GetAreaAdapter getAreaAdapter1 = getAreaAdapter.get(position);
Viewholder.btn_name.setText(getAreaAdapter1.getBtn_name());
setImageIntoButton( Viewholder.btn_name,getAreaAdapter1.isSelected());
Viewholder.btn_name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getAreaAdapter1.setSelected(!getAreaAdapter1.isSelected());
setImageIntoButton(Viewholder.btn_name,getAreaAdapter1.isSelected());
Viewholder.btn_name.setBackgroundResource(R.drawable.ripple_effect);
}
});
}
private void setImageIntoButton(Button buttonView,boolean isSelected){
if(isSelected)
buttonView.setBackgroundResource(R.drawable.ripple_effect);
else
buttonView.setBackgroundResource(R.drawable.button_2);
}
Use State-List drawables
create a xml file in your drawable directory
and add something like
button_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/some_drawable_for_pressed_state"
android:state_pressed="true"/>
<item android:drawable="#drawable/default_drawable"/>
</selector>
and set it as background for the button.
android:background="#drawable/button_bg.xml".

Android open Dialog animation listener

I have Dialog that has EditText. this dialog has enter and exist animation that defined in style.xml
<style name="LocationDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">#style/LocationDialogAnimation</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
<style name="LocationDialogAnimation">
<item name="android:windowEnterAnimation">#anim/dialog_location_enter_anim</item>
<item name="android:windowExitAnimation">#anim/dialog_location_exit_anim</item>
</style>
i want to open the keyboard after enter animation finished cause my current solution
locationDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
overlap the animation and doesn't wait for the animation to finish first.
i also tried
locationDialog.setOnShowListener(...);
What you want can be possible by using Animation in code. You can first set the focus of your EditText to android:focusable="false" in xml or in code and when your enter animation finished you can the requestFocus to true.
Animation enter= AnimationUtils.loadAnimation(context,R.anim.enter_anim);
enter.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
yourEditText.setFocusable(true);
yourEditText.requestFocus();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
I hope it will help you.

Cannot update imageButton from inside adapter

I want to change the image of the imagebutton when I click on it. This imageButton is part of layout out file which I have inflated in my custom adapter which feeds a kind of card view (Image with few buttons) to my main container layout.
I have added the imagebutton listener in my CustomAdapter.java:
cardHolder.mLikeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Sushil", "clicked like button!!!!!");
updateHeartButton(true);
}
});
When I click on the button, my listener is called and from inside I call a method to update the image of button:
private void updateHeartButton(boolean animated) {
cardHolder.mLikeButton.setImageResource(R.drawable.ic_heart_red);
}
But the imagebutton does not get updated with new image. can someone help me.
Thank You
Since cardHolder changes (part of the adapter getView(), the last cardHolder will be called rather the one which was tapped.
Please change your code as follow and try again:
cardHolder.mLikeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("Sushil", "clicked like button!!!!!");
((ImageButton) view).setImageResource(R.drawable.ic_heart_red);
}
});
Try with setBackgroundResource(R.drawable.ic_heart_red);
Or in other way if you need to go back to the previous image when clicking back on it:
You can create a btn.xml file in the drawable folder :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- When selected, use grey -->
<item android:drawable="#drawable/like_selected"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/like_normal"/>
</selector>
In your xml for the button you set this xml to the background:
android:background="#drawable/btn"
In your updateHeartButton() method you can use:
cardHolder.mLikeButton.setSelected(true)
this work for me
cardHolder.mLikeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cardHolder.mLikeButton.setImageResource(R.drawable.ic_heart_red);
}
});

Android ActionBar overflow menu options not working when fragment visible

I have a dynamically visible transparent ActionBar in my app (using v4 compatibility) that appears and disappears when the user long presses on the screen. This is working fine, and I'm able to use the ActionBar to execute my actions via public boolean onOptionsItemSelected(MenuItem item)
However, when I display a DialogFragment on screen, only the visible (those not in overflow) ActionBar actions fire onOptionsItemSelected. The overflow menu appears as before but I cannot click on them. They act like they are disabled.
The DialogFragments are setup so that background motion events trigger
final Window w = getDialog().getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
My ActionBar xml isn't too complicated, but here it is.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:showAsAction="ifRoom" android:id="#+id/settings" android:title="#string/settings" android:icon="#android:drawable/ic_menu_preferences"/>
<item android:showAsAction="ifRoom" android:id="#+id/history" android:title="#string/menu_history" android:icon="#drawable/ic_menu_archive"/>
<item android:showAsAction="ifRoom" android:id="#+id/new_game" android:title="#string/new_game" android:icon="#android:drawable/ic_menu_add"/>
<item android:showAsAction="ifRoom" android:id="#+id/stats" android:title="#string/menu_stats" android:icon="#android:drawable/ic_menu_agenda"/>
<item android:showAsAction="ifRoom" android:id="#+id/help" android:title="#string/help" android:icon="#android:drawable/ic_menu_help"/>
</menu>
Other than that, I'm not doing anything too strange in my app. Any ideas why the overflow menu fails to work when a DialogFragment is displayed?
My DialogFragment class calls setHasOptionsMenu(true) and I can see onCreateOptionsMenu(Menu menu, MenuInflater inflater) being called, but my onOptionsItemSelected doesn't get called for the overflow items.
EDIT
I decided to throw away the ActionBar implementation and use a Navigation Drawer slider for my app. After I implemented everything, I ran into the same problem. With no DialogFragments visible, I can use the Navigation Drawer just fine and make selections. However when my DialogFragment is visible, I can drag the drawer in and out, but I am unable to make any selections on the ListView.
Here's my code for the child dialog.
public class CChildDialog extends DialogFragment {
public interface DialogDismissHandler {
abstract public void onDismiss(CChildDialog dlg);
}
protected Game m_game;
protected DialogDismissHandler m_dismissListener;
public CChildDialog() {
super();
}
public boolean isDialogShowing() {
Dialog d = getDialog();
if (d != null)
return d.isShowing();
else
return false;
}
public void setGame(Game g) {
m_game = g;
}
public void setOnDismissHandler(DialogDismissHandler dismissListener) {
m_dismissListener = dismissListener;
}
public void onDismiss(DialogInterface dlg) {
super.onDismiss(dlg);
if (m_dismissListener != null) {
m_dismissListener.onDismiss(this);
m_dismissListener = null;
}
m_game = null;
}
public void onPreferencesChanged(Game game) {
}
#Override
public void onStart() {
final Window w = getDialog().getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
WindowManager.LayoutParams lp = w.getAttributes();
if (m_game.isAdVersion()) {
// move it up 25 pixels
lp.y = -25;
}
// check for super small screens
if (UIUtil.GetWindowSize(m_game.getWindowManager()).y < 400) {
lp.y -= 30; // move it up a little more
}
super.onStart();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE,R.style.Theme_CustomDialog);
}
And here is my custom theme
<style name="Theme.CustomDialog" parent="#android:style/Theme.Holo.Dialog">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowNoTitle">true</item>
</style>
The answer is to also add the FLAG_NOT_FOCUSABLE LayoutParams flag. Adding this allows full use of the overflow menu when a DialogFragment is visible.

Right justify text in AlertDialog

I have been trying to alter my AlertDialog so that it will show the text right justified (for Hebrew).
With inazaruk's help here: Right justify text in AlertDialog
I managed to get the dialog showing but it only works correctly in the emulator (Eclipse).
When I move it onto my device (Xpersia X10a) the alert box appears at the top of the screen with the background blocking out everything behind it.
The image on the emulator:
The image on my device:
Code:
public class test extends Activity {
/** Called when the activity is first created. */
public class RightJustifyAlertDialog extends AlertDialog {
public RightJustifyAlertDialog(Context ctx) {
super(ctx, R.style.RightJustifyTheme); } }
#Override
public void onCreate(Bundle savedInstanceState) {
final Context con = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog dialog = new RightJustifyAlertDialog(con);
dialog.setButton("button", new OnClickListener(){
public void onClick(DialogInterface arg0, int arg1)
{
}
});
dialog.setTitle("Some Title");
dialog.setMessage("Some message");
dialog.show();
}
});
}
}
Styles:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="RightJustifyTextView" parent="#android:style/Widget.TextView">
<item name="android:gravity">right|center_vertical</item>
<item name="android:layout_centerVertical">true</item>
</style>
<style name="RightJustifyDialogWindowTitle" parent="#android:style/DialogWindowTitle" >
<item name="android:gravity">right|center_vertical</item>
<item name="android:layout_centerVertical">true</item>
</style>
<style name="RightJustifyTheme" parent="#android:style/Theme.Dialog.Alert">
<item name="android:textViewStyle">#style/RightJustifyTextView</item>
<item name="android:windowTitleStyle">#style/RightJustifyDialogWindowTitle</item>
</style>
</resources>
My device is working with Android 2.1-update1 and the emulator is set to same.
One simple thing to rule out... Check you device's other apps to see if all of them have had their AlertDialog background alpha transparencies removed. You can try deleting something in the default mail application, or other applications that also have a long press delete for items. I wouldn't see the need to change this styling, but you never know what Carriers are going to do these days.

Categories

Resources