Android open Dialog animation listener - android

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.

Related

How do I add animation to BottomSheetDialogFragment

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.

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".

Change button background color as if pressed, but without the user pressing it

On Android, a Button changes its background color when pressed.
How can we tell a button that it is pressed (without firing the onClick-action), so that it changes color, without the user pressing it? (for example triggered by a swipe action)
It should change color briefly, and then change back.
There a quite a few questions concerning keeping the pressed state. This question asks, how to set the button_pressed state briefly, as if clicked, but without a real click.
Button.setPressed(true) has not given a color change, neither has Button.performClick().
First, create the effect when button is hovered, clicked etc in XML. Put this style in your drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed button -->
<item android:drawable="#color/dark_green"
android:state_focused="true"
android:state_pressed="false"
/>
<item android:drawable="#color/dark_green"
android:state_focused="true"
android:state_pressed="true"
/>
<item android:drawable="#color/dark_green"
android:state_focused="false"
android:state_pressed="true"/>
<!-- Normal button -->
<item android:drawable="#color/green"
android:state_focused="false"
android:state_pressed="false"/>
</selector>
Then in your XML, initiates the style by using:
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/the_style_in_drawable"
android:text="click"/>
By putting the style in your XML, you don't have to initiate the style when button on click. Android will detect the button state and do the work for you. Just remember to put the state in selector.
To change a button state without anything else is done via
btn1.getBackground().setState(new int[]{android.R.attr.state_pressed});
To reset to ordinary, you use
btn1.getBackground().setState(new int[]{android.R.attr.state_enabled});
A Button's states can be found out via
btn1.getBackground().getState();
which resturns an int[]. You can compare its values to android.R.attr to find out which states are set.
Example Code
private void simulateClick(final ImageButton button,
final long clickDuration) {
button.getBackground().setState(new int[]{android.R.attr.state_pressed});
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(clickDuration);
} catch ( InterruptedException e ) {
// not bad if interrupted: sleeps a bit faster (can happen?)
}
Count.this.runOnUiThread(new Runnable() {
public void run() {
button.getBackground().setState(new int[]{android.R.attr.state_enabled});
}
});
}}).start();
}
Explanation
Each View has a Drawable as background image. A Drawable can be of different subtypes, here it is a StateListDrawable, as defined per XML. (See #Lynx's answer as an example of a XML defined drawable).
This Drawable can be told which state it is to assume (via setState) and does the layout itself.
AsyncTask for button color change illusion:
private class ChangeButtonColorMomentarily extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
btn1.setBackgroundDrawable(new ColorDrawable(Color.rgb(50, 50, 50)));//pressed state
}
#Override
protected String doInBackground(String... params) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
return "";
}
#Override
protected void onPostExecute(String result) {
btn1.setBackgroundDrawable(new ColorDrawable(Color.rgb(200, 200, 200)));//normal state
}
}
Also take note that if your API 16 above use setBackground() instead.
For changing the color of button at that time, you can use setOnTouchListener as:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Button Pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//finger was lifted
}
return false;
}
});

How to create android game loading screen

When you start android app Main activity starts with white background and black header with your app name in left corner. Like this
How to wholly remove this (when app is started not to show this) and add custom loading progress bar or some logo to the screen?
How about creating a splash dialog with custom layout containing progress bar?
In your main activity do something like this
private SplashDialog mSplashDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showSplashScreen();
setContentView(R.layout.yourmainlayout);
}
protected void removeSplashScreen() {
if (mSplashDialog != null) {
mSplashDialog.dismiss();
mSplashDialog = null;
}
}
protected void showSplashScreen() {
mSplashDialog = new SplashDialog(this, R.style.splash_dialog);
mSplashDialog.setCancelable(false);
mSplashDialog.show();
}
Create custom dialog
public class SplashDialog extends Dialog {
private ProgressBar mProgressBar;
public SplashDialog(Context context, int theme) {
super(context, theme);
setContentView(R.layout.splash_dialog);
mProgressBar = (ProgressBar) findViewById(R.id.splash_progress);
}
public void setProgress(int progress) {
mProgressBar.setProgress(progress);
}
}
And add style to that will let dialog fill all screen.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="splash_dialog">
<item name="android:padding">0dp</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowFrame">#null</item>
</style>
</resources>
To change dialog's progress value call mSplashDialog.setProgress(int progress).
When your data is loaded call removeSplashScreen().
I think what you're looking for can be found here:
how to change the splash screen
What worked for me (since I don't have much load up on start actually I didn't need splash screen at all) is changing res/values/styles.xml :
<resources>
<color name="custom_theme_color">#000000</color>
<style name="AppTheme" parent="android:Theme.Light" >
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#color/custom_theme_color</item>
</style>
</resources>
It just start with black screen witch goes to full screen (after 1-2 sec) when it is all loaded.
It looks very "profesional".

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