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
}
}
});
Related
I just installed the Android R (API 30) image in my emulator to try my app, and it crashed when trying to set the Background color of a Toast.
Toast toast = Toast.makeText(ctxt, msg, duration);
View view = toast.getView();
view.setBackgroundColor(0xFF303030);
TextView tview = view.findViewById(android.R.id.message);
tview.setTextColor(Color.WHITE);
toast.show();
This is really strange as in Android Q (API 29) works perfectly.
My build.gradle updated for Android R (API 30)
compileSdkVersion 30
buildToolsVersion "30.0.1"
Is there a new way to do it??
Since Android 11, custom toasts/ toast modifications are deprecated, according to Google to "protect users". Hence why your app in Android 30 is not able to display custom toasts.
From Android Developers documentation:
Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int)
The only way I have found of showing custom toasts from API 30 onwards is by creating them ad hoc.
XML LAYOUT
Customize as needed
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main_activity">
<!--Ad hoc toast Textview-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_margin="18dp"
android:background="#drawable/ad_hoc_toast_background"
android:textColor="#1e1e1e"
android:gravity="center"
android:visibility="gone"
android:layout_alignParentBottom="true"
android:id="#+id/ad_hoc_toast_textview"
tools:text="Temporary message bla bla bla ..."/>
</RelativeLayout>
TOAST BACKGROUND (ad_hoc_toast_background.xml)
Customize as needed
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="220dp"
android:height="100dp"/>
<corners
android:radius="25dp"
/>
<solid
android:color="#e6ffffff"
/>
</shape>
</item>
</selector>
Define the show_ad_hoc_toast() method
private void show_ad_hoc_toast(final TextView ad_hoc_toast_textview, String text){
//Set the text
ad_hoc_toast_textview.setText(text);
//Create alpha animation
AlphaAnimation animation1 = new AlphaAnimation(0f, 1f);
//Set duration
animation1.setDuration(300);
//Set that the animation changes persist once the animation finishes
animation1.setFillAfter(true);
//Set on AnimationEnd Listner
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override public void onAnimationStart(Animation animation){}
#Override public void onAnimationRepeat(Animation animation){}
#Override public void onAnimationEnd(Animation animation){
//After 2250 millis -> hide the toast
new CountDownTimer(2250, 1) {
public void onTick(long millisUntilFinished){}
public void onFinish() {hide_ad_hoc_toast(ad_hoc_toast_textview);}
}.start();
}
});
//Make the view visible
ad_hoc_toast_textview.setVisibility(View.VISIBLE);
//Start animation
ad_hoc_toast_textview.startAnimation(animation1);
}
Define the hide_ad_hoc_toast() method
private void hide_ad_hoc_toast(final TextView ad_hoc_toast_textview){
//Create alpha animation
AlphaAnimation animation1 = new AlphaAnimation(1f, 0f);
//Set duration
animation1.setDuration(300);
//Set that the animation changes persist once the animation finishes
animation1.setFillAfter(true);
//Set on AnimationEnd Listner
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override public void onAnimationStart(Animation animation) { }
#Override public void onAnimationRepeat(Animation animation) { }
#Override public void onAnimationEnd(Animation animation) {
//Make the view gone
ad_hoc_toast_textview.setVisibility(View.GONE);
}
});
//Start animation
ad_hoc_toast_textview.startAnimation(animation1);
}
Call the method from your code when needed
//Find ad_hoc_toast textview
TextView ad_hoc_toast_textview = findViewById(R.id.ad_hoc_toast_textview);
//Define the text to be shown
String text = "This is the custom toast message"
//Show the ad_hoc toast
show_ad_hoc_toast(ad_hoc_toast_textview, text);
RESULT
You can check before to custumized toast
Toast toast = Toast.makeText(ctxt, msg, duration);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
View view = toast.getView();
view.setBackgroundColor(0xFF303030);
TextView tview = view.findViewById(android.R.id.message);
tview.setTextColor(Color.WHITE);
}
toast.show();
#pvalle & #Aayush Panda, It works for me in Android 11. Please check below code
public static void showCenterToastMessage(Context context, String msg) {
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.custom_toast,null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(msg);
text.setPadding(20,0,20,0);
text.setTextSize(18);
text.setTextColor(Color.WHITE);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
layout.setBackgroundColor(Color.DKGRAY);
toast.setView(layout);
toast.show();
}
layout is as follows
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/text"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
The solution with setting a custom view on Toast is deprecated for API level 30
Documentation says
This method was deprecated in API level 30. Custom toast views are
deprecated. Apps can create a standard text toast with the
makeText(android.content.Context, java.lang.CharSequence, int) method,
or use a Snackbar when in the foreground. Starting from Android
Build.VERSION_CODES#R, apps targeting API level Build.VERSION_CODES#R
or higher that are in the background will not have custom toast views
displayed.
There is a walkaround though which still works and is not deprecated
Toast.makeText(applicationContext,
HtmlCompat.fromHtml("<font color='red'>custom toast message</font>", HtmlCompat.FROM_HTML_MODE_LEGACY),
Toast.LENGTH_LONG).show()
Html color tag can also be <font color='#ff6347'>
WindowManager interface can be an alternative to the toast after the Android 11 limitations.
https://developer.android.com/reference/android/view/WindowManager
But you just need user permission to display custom messages over the apps.
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 am developing android application and i am trying to apply some roll effect from top of screen for popup window but don't know how to achieve but currently i am adding some other animation effect
this is my popup window function code
private void loadingPopup() {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View layout = inflater.inflate(R.layout.profile_popup, null);
final PopupWindow windows = new PopupWindow(layout , 450,650,true);
windows.setFocusable(false);
windows.setTouchable(true);
windows.setOutsideTouchable(true);
windows.setAnimationStyle(R.style.AnimationPopup);
layout.post(new Runnable() {
public void run() {
windows.showAtLocation(layout, Gravity.TOP, 0, 0);
}
});
name = (TextView)layout.findViewById(R.id.name);
profilepicture =(ImageView)layout.findViewById(R.id.profileimage);
String sname = profilelistdb.get(0).get("pname");
name.setText(sname);
String imagename = profilelistdb.get(0).get("pimage");
String totalurl = imageurl+imagename;
imageLoader1.DisplayImage(totalurl, profilepicture);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
windows.dismiss();
}
});
}
This is Style.xml
<style name="AnimationPopup">
<item name="#android:windowEnterAnimation">#anim/appear</item>
</style>
appear.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="50%p" android:toYDelta="0%p"
android:duration="#android:integer/config_longAnimTime"/>
</set>
My Requirement:-
My Popup window should come from top with roll kind of thing and whenever user click close button it should hide popup with reverse roll effect could you please help me how to achieve this.
If you don't understand about what i am trying to say about roll
just check this link and see 30 seconds
https://www.youtube.com/watch?v=KSHVSswMUng this is what exactly i need.
You could use this library. It's for View Animations in android, and works very well. And then use this:
YoYo.with(Techniques.RollIn)
.duration(700)
.playOn(findViewById(R.id.edit_area));
YoYo.with(Techniques.RollOut)
.duration(700)
.playOn(findViewById(R.id.edit_area));
Or experiment with it yourself, to see what looks the best.
Edit: Not sure how to achieve that particular roll-effect that you have in mind now that I see a video of it, but if you can't find it, I'm sure you'll find something in this library that will look good. Good luck.
I am just trying to make my application so the user can only click on one checkbox at a time. So pretty much when the user clicks on one checkbox the other 2 become false. I have tried a few things, but nothing really seems to be working, and I can't find anything about it online. Thanks-
Here is my code...
public void buttonClick() {
imgView = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.button);
blade = (ImageView)findViewById(R.id.imageView4);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
standardSound = MediaPlayer.create(this, R.raw.file.mp3);
alternateSound = MediaPlayer.create(this, R.raw.file.mp3);
whiteSound = MediaPlayer.create(this, R.raw.file.mp3);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean alternate = getPrefs.getBoolean("alternate", false);
boolean white = getPrefs.getBoolean("white", false);
boolean standard = getPrefs.getBoolean("standard",false);
if (blade.getAnimation() == null) {
// no animation, start it
if (alternate == true){
alternateSound.start();
blade.startAnimation(animRotate);
} else if (white == true){
whiteSound.start();
blade.startAnimation(animRotate);
} else if (standard == true) {
standardSound.start();
blade.startAnimation(animRotate);
}
} else {
//animation is showing, stop it
blade.clearAnimation();
standardSound.stop();
standardSound.prepareAsync();
whiteound.stop();
whiteSound.prepareAsync();
alternateSound.stop();
alternateSound.prepareAsync();
}
current_image_index++;
current_image_index = current_image_index % images.length;
imgView.setImageResource(images[current_image_index]);
imgView.invalidate();
}
}
);
}
And my xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Sound Settings">
<CheckBoxPreference
android:key="standard"
android:title="Standard Fan"
/>
<CheckBoxPreference
android:key="alternate"
android:title="Alternate Fan"
/>
<CheckBoxPreference
android:key="white"
android:title="White Noise"
/>
</PreferenceCategory>
</PreferenceScreen>
There are a windget called RadioButton which it does this function.
Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side.
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)
}
}