i want to create a edit text that will vibrate if given input is invalid.
for example edit text for number if number is wrong like it contain 9 digits than edit text will became clear and will vibrate for some time
how to create that?
thanks in advance
Create anim folder in resources and then create file named shake.xml
and paste the below code
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="10" android:duration="1000"
android:interpolator="#anim/cycle_7" />
and another file cycle_7.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />
and then in your java file
if(invalid)//check your input
{
Animation shake = AnimationUtils.loadAnimation(Login.this, R.anim.shake);
editText.startAnimation(shake);
}
If anyone is looking for a method to do what #Priya suggested programatically, then you can try this.
public TranslateAnimation shakeError() {
TranslateAnimation shake = new TranslateAnimation(0, 10, 0, 0);
shake.setDuration(500);
shake.setInterpolator(new CycleInterpolator(7));
return shake;
}
And then:
myEditText.startAnimation(shakeError());
For vibrate use the following code.
Vibrator vibe = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
Then, in the OnTextChanged Listener method use the following code.
vibe.vibrate(50); // 50 is time in ms
And don't forget you need to add the permission to the manifest (after the </application> tag):
<uses-permission android:name="android.permission.VIBRATE" />
Create shake.xml under anim folder
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-10"
android:toXDelta="10"
android:repeatCount="5"
android:repeatMode="reverse"
android:interpolator="#android:anim/linear_interpolator"
android:duration="70" />
</set>
After this add animation for button. I wrote this code in Kotlin for simplicity.
button.setOnClickListener {
button.startAnimation(AnimationUtils.loadAnimation(context, R.anim.shake)
}
You should add this listener to EditText for your desired validation,
editText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Write your logic here
if(condition satisfy)
// call vibrate();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
public void vibrate()
{
Vibrator vibrate= (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE) ;
vibrate.vibrate(50);
}
here is code for Kotlin
private fun vibrator() {
vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator!!.vibrate(
VibrationEffect.createOneShot(
500,
VibrationEffect.DEFAULT_AMPLITUDE
)
)
} else {
//deprecated in API 26
vibrator!!.vibrate(500)
}
}
Related
What i want : I want to create a edit text that will vibrate if given input is empty or invalid.
Example : In login screen the password edit text is empty or invalid, than edit text will vibrate at the same time my android device need to vibrate for some time how to create that it?
thanks in advance
Alright here is what you need..
Vibrate Animation
put these two xml files inside res/anim folder
vibrate.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="10"
android:duration="1000"
android:interpolator="#anim/cycle_5" />
cycle_5.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="5" />
Vibrating Phone
use these method to vibrate your phone for 500 milliseconds
public void shakeItBaby() {
int DURATION = 500; // you can change this according to your need
if (Build.VERSION.SDK_INT >= 26) {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(DURATION, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(DURATION);
}
}
and don't forget to put permission in menifest
<uses-permission android:name="android.permission.VIBRATE" />
Putting all together
now use them both wile validating your EditText
Animation vibrate = AnimationUtils.loadAnimation(this, R.anim.vibrate);
if (paytm_amt.getText().toString().trim().isEmpty()) {
paytm_num.setError("Please Enter PayTM Number");
paytm_num.startAnimation(vibrate);
shakeItBaby();
} else {
// do something
}
Happy Coding..
Components
EditText mPassword = findViewById(R.id.edtPassword);
Button mLogin = findViewById(R.id.btnLogin);
Android Device Vibration
private void AndroidDeviceVibrate() { // Android Device Vibration
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds only
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
v.vibrate(500); // deprecated in API 26
}
}
Edit text vibrate Animation
public TranslateAnimation VibrateError() { // Edit text vibrate Animation
TranslateAnimation vibrate = new TranslateAnimation(0, 10, 0, 0);
vibrate.setDuration(600);
vibrate.setInterpolator(new CycleInterpolator(8));
return vibrate;
}
Finally,
mLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String strPass = mPassword.getText().toString();
if (TextUtils.isEmpty(strPass)) {
AndroidDeviceVibrate(); // Android Device Vibrate
mPassword.startAnimation(VibrateError()); // Edit text vibrate Animation
}
}
});
i have implemented an animation on one of my image views. My problem is that the animation will NOT stop at all. I call everything clearanimation i set it to null set it to cancel and it still wont stop.
public void tiltani(){
ImageView vault = (ImageView)findViewById(R.id.vault2) ;
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
vault.startAnimation(tilt);
}
public void stopani() {
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
vault.clearAnimation();
vault.setAnimation(null);
tilt.cancel();
tilt.reset();
}
here is xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="6"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:repeatCount="infinite"/>
<rotate
android:fromDegrees="6"
android:toDegrees="-2"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:repeatMode="reverse"
android:repeatCount="infinite"
/>
here is where i start it
Intent intent1 = getIntent();
if (intent1.hasExtra("id1")) {
tiltani();
and i try to stop/cancel everything in an onclick method
vault.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
vault.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.chestopen));
stopani();
update
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
if (intent1.hasExtra("id1")) {
vault.startAnimation(tilt);
vault.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
vault.setImageDrawable(ContextCompat.getDrawable(MainActivity.this,
R.drawable.chestopen));
vault.setAnimation(null);
You can just use vault.startAnimation(tilt); to start your animation, and use vault.setAnimation(null); to stop your animation.
You have initialized Animation a second time in stopani() method. Your view initialization and animation initialization should be global.
Your code should be like this.
ImageView vault = (ImageView)findViewById(R.id.vault2); //Global variable
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt); //Global variable
public void tiltani(){
vault.startAnimation(tilt);
}
public void stopani() {
vault.clearAnimation();
vault.setAnimation(null);
tilt.cancel();
tilt.reset();
}
Hope, It will help you.. :)
I have a button which need to fade in. But it works only the first time. It doesn't work the second time.
Here is my code.
final TextView doctorInfoView = (TextView) rowView.findViewById(R.id.doctorInfo);
final TextView specialtyView = (TextView) rowView.findViewById(R.id.specialty);
final ImageButton deleteDoctor = (ImageButton)rowView.findViewById(R.id.deleteDoctor);
final Animation fadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fade_in_animate);
final ImageButton editDoctor = (ImageButton)rowView.findViewById(R.id.editDoctor);
final RelativeLayout mainRowLayout = (RelativeLayout)rowView.findViewById(R.id.doctorListInfoView);
final LinearLayout rowLayout = (LinearLayout)rowView.findViewById(R.id.doctorInfoLayout);
final LinearLayout editButtonLayout = (LinearLayout)rowView.findViewById(R.id.editButtonLayout);
final LinearLayout deleteButtonLayout = (LinearLayout)rowView.findViewById(R.id.deleteButtonLayout);
rowLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isClicked) {
editDoctor.setAnimation(fadeInAnimation);
editDoctor.setVisibility(View.VISIBLE);
deleteDoctor.setAnimation(fadeInAnimation);
deleteDoctor.setVisibility(View.VISIBLE);
mainRowLayout.setBackgroundColor(Color.parseColor("#ffffff"));
doctorInfoView.setTextColor(Color.parseColor("#eeeeee"));
specialtyView.setTextColor(Color.parseColor("#eeeeee"));
editButtonLayout.setBackgroundColor(Color.parseColor("#16aea3"));
deleteButtonLayout.setBackgroundColor(Color.parseColor("#16aea3"));
isClicked = false;
} else {
editDoctor.setVisibility(View.GONE);
deleteDoctor.setVisibility(View.GONE);
mainRowLayout.setBackgroundColor(Color.parseColor("#f2f2f4"));
doctorInfoView.setTextColor(Color.parseColor("#000000"));
specialtyView.setTextColor(Color.parseColor("#0d9e9f"));
editButtonLayout.setBackgroundColor(Color.parseColor("#f2f2f4"));
deleteButtonLayout.setBackgroundColor(Color.parseColor("#f2f2f4"));
isClicked = true;
}
}
});
Here is fade_in_animate.xml
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500"/>
</set>
I'd appreciated about any feedback.
One approach to solve this would be to set the animation to null
editDoctor.setVisibility(View.GONE);
editDoctor.setAnimation(null);
EDIT: You forgot to set it to infinite
animation.setRepeatCount(Animation.INFINITE);
Here is the xml
android:repeatCount="-1"
android:repeatMode="repeat"
Here is the full documentation
EDIT 2: I didn't see that you are setting the alpha. My bad. This should work! You don't need to repeat it. This will work with the method of setting the animation to null.
editDoctor.setVisibility(View.GONE);
editDoctor.setAnimation(null);
editDoctor.setAlpha(.0f);
I'm a beginner to android programming. I'm not clear about the concept of shared preferences.
I need to set a particular animation on the first launch of the app(fragment from fragment activity) and for the consecutive launches of app(minimizing) another animation. So how can I utilize the shared preference to do so?
public class MyActivity extends FragmentActivity {
SharedPreferences prefs = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("key", MODE_PRIVATE);
}
#Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
prefs.edit().putBoolean("firstrun", false).commit();
// here comes your animation for first start
}
// here comes your animation for other starts
}
}
Check this documentation at developer.android.com
Have a look at these links:
http://examples.javacodegeeks.com/android/core/content/android-sharedpreferences-example/
http://www.vogella.com/articles/AndroidFileBasedPersistence/article.html
http://rajeshvijayakumar.blogspot.in/2013/03/shared-preferences-example-in-android.html
http://androiddeveloperspot.blogspot.in/2013/01/sharedpreference-in-android.html
http://alchemiaandroid.altervista.org/sharedPreferencesTutorial.html
http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
http://www.edumobile.org/android/android-development/shared-preferences-example-in-android-programming/
http://viralpatel.net/blogs/android-preferences-activity-example/
For animations use this
overridePendingTransition(R.anim.no_anim, R.anim.slide_to_top);
to create animations create "anim" folder in "res" and create slide_left.xml like following
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="200"
android:fromXDelta="0%"
android:toXDelta="-100%" />
<alpha
android:duration="200"
android:fromAlpha="1"
android:toAlpha="0" />
</set>
I created an animation that simulates an explosion: a "booom" image with this animation:
explosion.xml HyperspaceExplosion on Activity
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:interpolator="#android:anim/bounce_interpolator"
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.5"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:fillBefore="false"
android:duration="3000"
/>
</set>
when a player clicks on the bomb explosion begins.
At the end of explosion I want open a Dialog.
the simple code for bomb behaviour:
getBombImage().setOnClickListener(
new View.OnClickListener() {
MediaPlayer mp = null;
#Override
public void onClick(View v) {
getExplosionImage().setVisibility(View.VISIBLE);
if(!isSoundOff()){
mp = MediaPlayer.create(getApplicationContext(), R.raw.explosion);
mp.start();
}
getExplosionImage().startAnimation(getHyperspaceExplosion());
getExplosionImage().setVisibility(View.INVISIBLE);
showDialog(1);
}
}
);
The problem is that the explosion and the dialog are in conflict in terms of time and the explosion continues after the dialog is open.
I want sincronize two events: before the explosion. At the end of explosion, I want open the dialog.
Anybody ca help me?
Thanks in advice.
Use an AnimationListener and open your dialog inside onAnimationEnd().
For example like this:
Animation a = getHyperspaceExplosion();
a.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
showDialog(1);
}
// ..other listener methods here..
});
getExplosionImage().startAnimation(a);