Open top menu


In this Tutorial we will explain how to implement animated Numberpicker dialog with bounce.

first we will create animated xml class ,

res/anim/bounce.xml
..............................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/bounce_interpolator">
    <translate
        android:fromYDelta="-100%p"
        android:toYDelta="0"
        android:duration="2000"/>

</set>

res/anim/abc_slide_out_bottom.xml
..............................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:interpolator="@android:anim/accelerate_interpolator"
           android:fromYDelta="0" android:toYDelta="50%p"

           android:duration="@android:integer/config_mediumAnimTime"/> 

................................................................................................................................................................
res/value/styles
Then both anim class add in style theam,  how explain below
...................................................................................................................................................................

 <style name="DialogSlideAnim" parent="@android:style/Theme.Dialog">
        <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
    </style>
    
    <style name="DialogAnimation">
        <item name="android:windowEnterAnimation">@anim/bounces</item>
        <item name="android:windowExitAnimation">@anim/abc_slide_out_bottom</item>

</style>


Used this method in MainActivity.java
...................................................................................................................................................................

package com.example.androidtestcode;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;

public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener {
Context context;  
TextView tv;
Button button_decrement,button_increment;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   setContentView(R.layout.activity_main);
   tv=(TextView)findViewById(R.id.textView1);
   tv.setText("1");
   button_decrement=(Button)findViewById(R.id.button_decrement);
   button_increment=(Button)findViewById(R.id.button_increment);
   context=this;

   button_decrement.setOnClickListener(new View.OnClickListener()
   {

       @Override
       public void onClick(View v)
       {
        String present_value_string = tv.getText().toString();
            if(present_value_string!=null)
            {
                int present_value_int = Integer.parseInt(present_value_string);
                if(present_value_int>1){
                present_value_int--;
                tv.setText(String.valueOf(present_value_int));
                }
            }          
       }
   });
   
 
}

public void show() {

final Dialog dialogbox = new Dialog(this,R.style.DialogSlideAnim);
dialogbox.setTitle("Set Number Of Tiffin");
dialogbox.setContentView(R.layout.pikerdiaog);
Button b1 = (Button) dialogbox.findViewById(R.id.button1);
Button b2 = (Button) dialogbox.findViewById(R.id.button2);
final NumberPicker nPicker = (NumberPicker) dialogbox
.findViewById(R.id.numberPicker1);
nPicker.setMaxValue(100);
nPicker.setMinValue(1);
nPicker.setWrapSelectorWheel(false);
nPicker.setOnValueChangedListener(this);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText(String.valueOf(nPicker.getValue()));

dialogbox.dismiss();
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialogbox.dismiss();
}
});
dialogbox.show();

}

@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Log.i("value is", "" + newVal);

}
}
...................................................................................................................................................................




0 comments