How to set shared Preferences to show custom animation on first launch? - android

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>

Related

Animation will NOT stop running

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.. :)

Image rotation Android

I am working on android. I want to create a rotating image and select parts on it. For example if the image is a human head then while its rotating, the ears can be selected. Now I did the image part selection, but I don't know how to rotate it? Can somebody show me an easy way?
You have 2 ways to rotate a image, i put the simple with xml.
First inside directory res create a directory with the name anim
inside the directory anim create a file xml with this code:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
>
</rotate>
this code is the instruction to make a image rotate.
finally your main activity must look like that:
public class MainActivity extends Activity implements OnClickListener{
ImageView imagen; //declare the image will use
Button boton; // this button will activie de rotation
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagen = (ImageView) findViewById(R.id.iv);
boton = (Button)findViewById(R.id.bt);
boton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt:
Animation rotacion; // declare an animation
rotacion = AnimationUtils.loadAnimation(this,R.anim.rotar);
rotacion.reset();
imagen.startAnimation(rotacion);
break;
default:
break;
}
}
You should use http://developer.android.com/reference/android/view/animation/RotateAnimation.html class to make animation and animate your view

Implementing slide transition after a option item selected android

I build a action bar.Which contain a item say setting (image).After clicking on the image the another activity is being started.
But I want the starting activity to show sliding transition effect.current activity slide left and hide and starting activity should slide from right to left.
here is code
code for selecting the item
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.setting:
goToSetting();
}
return super.onOptionsItemSelected(item);
}
gotoSetting function :
public void goToSetting() {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageSetting.class);
startActivity(intent);
}
which is calling activity named "DisplayMessageSetting"
please help
Edit the onCreate method in your DisplayMessageSetting activity like this :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//specify the animation
overridePendingTransition(R.anim.slide_left_in, 0);
//...
}
Create a slide_left_in.xml file in your res/anim folder (create the folder if it doesn't exist) and paste the following :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="400"/>
</set>
This way you just specified an animation set with one translate animation in it.
You have a lot more options to tweak that animation, you can learn a bit more about that here.

vibration of Edittext in android

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)
}
}

How to animate individual views on Activity transition?

How can I trigger animations on individual views when switching activities?
I.e. if the user clicks a button to go to the next page, I'd like some of my views to fly off screen, and have the background crossfade into the next screen, instead of having the whole screen be animated as one piece.
Is this possible? And if so, how should it be done? (I'm using the most recent API, 4.1, and it doesn't have to be backwards compatible)
EDIT: Currently, doing the transition-in animation is working fine by calling it in onResume(), but when I press back, the activity switches faster than any animations started in onPause() so that makes me think there's a better way/place to do this.
Overriding onResume() works fine, but onPause/onStop don't wait for
the animation to complete before moving to the next screen.
What ever starts the event ex. button click would need to start the animations before start activity is called.
button.setOnClickListener(new ViewOnClickListener() {
#Override
void onClick(... {
// start animations
// wait till they are finished
// start activity
}
});
Since every event that starts a new activity is going to have animation code I would also recommend moving it into some sort of helper class to avoid having duplicate code all over the place. ex.
button1.setOnClickListener(new ViewOnClickListener() {
#Override
void onClick(... {
helper.AnimateViews(/* probably pass activity or context */);
// start activity
}
});
button2.setOnClickListener(new ViewOnClickListener() {
#Override
void onClick(... {
helper.animateViews(/* probably pass activity or context */);
// start activity
}
});
public class ViewAnimiationHelper {
public void animateViews(Activity activity) {
// find all views if not found then don't animate them
View view1 = activity.findViewById(R.id.view1);
if(view1 != null) {
// animate view
}
View view2 = activity.findViewById(R.id.view1);
if(view2 != null) {
// animate view
}
}
}
This is all sudo java code but hopefully enough for you to get the idea. Good luck!
You can set up animations (like slide) when you switch between activities like this :
In the res folder, create an anim folder
For example, put two xml files for slide.
slide_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="200"/>
</set>
slie_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="200" />
</set>
Then on your java code just write this :
Intent i = new Intent(YourActivity.this, OtherActivity.class);
this.startActivity(i);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
If you are testing that on a real device, don't forget to allow it to play animations (Settings -> Display -> Animations -> All Animation)
Hope it helps !:)

Categories

Resources