How to animate an Android View only once on startup? - java

I am currently working on a very simple UI for my Android App. My goal is to animate some (I don't know how many yet) buttons on startup and NEVER AGAIN.
So following the official docs, reading java doc and searching on stackoverflow aswell, I finally got it work. Here's what I do with a single test view.
Set the View and the Animation in the OnCreate() method.
private TextView test_text;
private Animation test_anim;
...
protected void onCreate(Bundle savedInstanceState) {
...
test_text = (TextView) findViewById(R.id.text);
test_anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.test_animation);
}
Start the Animation in the OnWindowFocusChanged() method.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
test_text.startAnimation(test_anim);
}
This procedure works, the animation is executed when the activity starts, the only problem is that the onWindowFocusChanged() method is called everytime the activity changes status. So the text animates when the app is resumed, when the layout rotates and stuff like that.
So, repeating: My goal is to animate the text only ONCE when the app boots up and then stop forever.
If it helps, I already tried to put the Animation start in other methods like onStart() or onResume(), but the issue remains the same.

You can use SharePreferences, to check a boolean value. If is true or not exists means is first launch or you can animate app in onWindowFOcusChange() method. Set it to false to never aniamte again.
////////////////////////////
/// CONSTANTS
////////////////////////////
private static final String PREF_NAME = "pref_name";
public static final String IS_STARTUP = "is_startup";
////////////////////////////
/// FIELDS
////////////////////////////
private SharedPreferences settings;
#Override
public void onWindowFocusChanged(boolean hasFocus) {
settings = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
if (settings.getBoolean(IS_START_UP, true)) {
test_text.startAnimation(test_anim);
settings.edit().putBoolean(IS_START_UP, false).commit();
}
}
In case you want to aniamte again when app starts next time, you can set the pref IS_START_UP to true when exit the application.
if (!settings.getBoolean(IS_START_UP, false)) {
settings.edit().putBoolean(IS_START_UP, true).commit();
}

Use SharedPreference to store a boolean variable & make it to true immediately after first animation & check this each time before animation starts.
if(!isAnimatedAlready){
animate();
setIsAnimated(true);
}else{}

Simply you can add a boolean variable with initial value true and after first time you can change its value to false and inside onfocus you can add another condition
If(boolean){do the animation;
boolean=false;}
this will do want you want but if you want the animation to be once during the application life cycle you can use shared prefs or simply add a static Boolean variable in application class

Related

Using SharedPreference to save an activity in android?

I have an app where in the mainActivity the user has about 5 options to choose from. Clicking one of them opens a new activity but essentially all 5 opens up identical activities with different headings. In the newly opened activities, users use multiple rating bars to delegate points to some specified attributes. Using SharedPreference, can I save the entire activity so when I back out, click on the same option everything isn't gone? Or do I need to save let's say, the individual rating bar values using the SharedPreference?
Here is some code for one of the activities that opens from a button click. Something is terribly wrong because it is crashing now. Any suggestions?
public class MageSkillScreen extends AppCompatActivity
{
public float skillPoints = 10;
public float strengthRating;
public float intellectRating;
public float wisdomRating;
public float dexterityRating;
public float totalSkill;
public float mageStrength;
public float mageDexterity;
public float mageIntellect;
public float mageWisdom;
public RatingBar strengthBar;
public RatingBar intellectBar;
public RatingBar wisdomBar;
public RatingBar dexterityBar;
Button submit;
//preferences
SharedPreferences magePref;
boolean rememberRatings = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mage_skill_screen);
strengthBar = (RatingBar) findViewById(R.id.mageStregth);
intellectBar = (RatingBar) findViewById(R.id.mageInt);
wisdomBar = (RatingBar) findViewById(R.id.mageWisdom);
dexterityBar = (RatingBar) findViewById(R.id.mageDext);
submit = (Button) findViewById(R.id.submit);
}
#Override
public void onPause()
{
SharedPreferences.Editor edit = magePref.edit();
edit.putFloat("strengthPts", strengthBar.getRating());
edit.putFloat("dexterityPts", dexterityBar.getRating());
edit.putFloat("intellectPts", intellectBar.getRating());
edit.putFloat("wisdomPts", wisdomBar.getRating());
//edit.putString("pointsLeft", ptsRemaing.getText().toString());
edit.commit();
super.onPause();
}
#Override
public void onResume()
{
super.onResume();
rememberRatings = magePref.getBoolean("mageRatings", true);
mageStrength = magePref.getFloat("strengthPts", 0.0f);
mageDexterity = magePref.getFloat("dexterityPts", 0.0f);
mageIntellect = magePref.getFloat("intellectPts", 0.0f);
mageWisdom = magePref.getFloat("wisdmPts", 0.0f);
}
}
update view activity in function onResume
#Override
protected void onResume() {
super.onResume();
//load SharedPreferences again and update view
}
Okay, let's go step by step.
So I have an app where in the mainActivity the user has about 5
options to choose from.
Okay, sounds good, so you have 5 buttons in MainActivity for users.
Clicking one of them opens a new activity but essentially all 5 opens
up identical activities with different headings.
Can, you please be more specific here, click on one button should launch one single activity not all 5. And this is not possible at a time only one activity exists for the user to view or interact with. Others, goes in the backstack.
Please read about activity lifecycle for the same.
https://developer.android.com/guide/components/activities/activity-lifecycle.html
Using SharedPreference, can I save the entire activity so when I back
out, click on the same option everything isn't gone?
No, you can't save the whole activity inside Sharedpreference. SharedPref is used to store key value pairs, like hashMap and only primitive values, you can store activities and you should never do it.
https://developer.android.com/reference/android/content/SharedPreferences.html
Now, coming back to the solution for the same.
It totally depends on the usecase you are trying to implement, if you are launching different activities on each button click and want to persist some data across other activities, store the primitive values in sharedpref
and then access it in other activities.This also holds, true if you want to persist the same data in app re-launch.
If not, then you can have a singleton object and modify it and access it, across other activities, make sure to make it null to avoid memory leak.
I hope it clears your doubt.
Cheers..!!

Changing background in other activities with a button

I have a problem. I have 3 activities (MainActivity, DetailsActivity, SettingsActivity) and in SettingsActivity I have a Togglebutton "Nightmode". What I want is, when the button is changed, change background of all three activities on gray color.
public class SettingsActivity extends AppCompatActivity {
//This is SettingsActivity(not Main one)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
TextView SettingsTitle = (TextView) findViewById(R.id.SettingsTitle);
TextView NightText = (TextView) findViewById(R.id.NightmodeText);
ToggleButton toggleNightMode = (ToggleButton) findViewById(R.id.toggleNightmode);
final RelativeLayout NightBG = (RelativeLayout) findViewById(R.id.NightBG);
final LinearLayout DetailsBG = (LinearLayout) findViewById(R.id.mainBG);
final LinearLayout HomeBG = (LinearLayout) findViewById(R.id.HomeBG);
toggleNightMode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NightBG.setBackgroundColor(Color.parseColor("#545657"));
HomeBG.setBackgroundColor(Color.parseColor("#545657"));
DetailsBG.setBackgroundColor(Color.parseColor("#545657"));
}
});
NightBG is in the same activity as that java file (SettingsActivity). But HomeBG is in MainActivity and DetailsBG is in the DetailsActivity. Everytime I start the app, and press on that button, app craches. If I delete HomeBG and DetailsBG from this file, it works just fine with changing current layout's color to gray. Please help me.
One easy way to store little settings like this across multiple activities that may not be open/active at the time of the button click would be to use SharedPreferences.
It might be a little overkill for such a simple piece of code but you can always give it a try if you don't find anything else.
Your code could look something like this:
toggleNightMode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Set the color of this activity
int color = Color.parseColor("#545657")
View view = SettingsActivity.this.getWindow().getDecorView();
view.setBackgroundColor(color);
// Save color preference
SharedPreferences sharedPref = SettingsActivity.this.getSharedPreferences("bgColorFile",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("color", color);
editor.apply();
}
});
And then when you open your activities you place something like this in the onStart() or onCreate() method of your activity:
// Get the color preference
SharedPreferences sharedPref = getSharedPreferences("bgColorFile",Context.MODE_PRIVATE);
int colorValue = sharedPref.getInt("color", 0);
View view = this.getWindow().getDecorView();
view.setBackgroundColor(colorValue);
So what you're actually doing is storing the background color as persistent data and fetching it once you reopen/open the activity that you want to have the color on. The benefit of this method is that whenever you close your app the preferred background color will be remembered. I hope this helps.
Change background for current activity in the same activity. Since DetailsActivity is not running, you can't do that, it gives you null pointer. Is kind of you are trying to eat 3 apples and you have just one. After current activity is started, change background.
Update:
You can do that in current activity and just in current activity:
findViewById(android.R.id.content).setBackground(getColor(R.color.your_color));
Don't try to call this in other activities that are not running.
setBackground()
or
setBackgroundColor()
If your other activities are open, you should send a message to the other activities by using an Intent.
How to send string from one activity to another?
When you receive the Intent you could then set the background of the activity.
If your other activities are not open yet, you will not be able to send an Intent to them. In this case you could have each Activity reference a static value in your main activity that could contain the current background color. You would want to reference that value on the other activities on create functions.
Here is an example on how to reference a variable from another activity.
How do I get a variable in another activity?
This might not be the most pretty way to handle it but it should work.
as Ay Rue said you have 2 options: use static variable for that button, and then in onResume of each activity, check the value of the static variable (true or false). or you can save a private variable nightMode and then pass this value in the intent when you need to move to the other two activities.
don't set the background color if you already set before and have an updated background color.

Quit showing an Activity when CheckBox is checked

I have got a DialogActivity with a "do not show anymore" CheckBox.
What I need it to do is exactly like the CheckBox says. When the CheckBox is checked the Activity doesn't have to be displayed to the user anymore, no matter if the app is restarted or killed.
public class PopUpInfoActivity extends Activity {
static final String PREFS = "preference_file";
#Override
public void onCreate(Bundle state) {
setContentView(R.layout.popupinfo_layout);
CheckBox chk = (CheckBox) findViewById(R.id.dontshow_checkbox);
chk.setChecked(PreferenceManager.getDefaultSharedPreferences(this).getBoolean("value", false));
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//store isChecked to Preferences
SharedPreferences settings = getSharedPreferences(PREFS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("isChecked", false);
PreferenceManager.getDefaultSharedPreferences(PopUpInfoActivity.this).edit().putBoolean("value", isChecked).apply();
}
});
Intent intent = new Intent(PopUpInfoActivity.this, ChordsListActivity.class);
if (!chk.isChecked()) {
// run activity
super.onCreate(state);
} else {
startActivity(intent);
}
}
}
If I do it like this the App crashes.
If I replace the else with:
else {
onStop()
}
The code doesn't work as expected.
I would really appreciate if you could help me fix this problem!
EDIT:
This is what I have got in my ChordsListActivity, which is the activity that calls the PopUpInfoActivity but I do not get what I should put in my if() statement.
Intent legendaIntent = new Intent(ChordsListActivity.this, PopUpInfoActivity.class);
if(/*what's here?*/)
startActivity(legendaIntent);
In that MainActivity read your preference key (Show_Dialog), if it is true then launch PopUpInfoActivity otherwise launch ChordsListActivity.
The logic should be in the Mother Activity
In your PopUpInfoActivity you should only edit the preference key to false if the user checks out the ChekBox
To do so, please create a preferences.xml file under res/xml, inside this file you have to create a key (Boolean) that you will store inside the status of PopUpInfoActivity.Checkbox, the default will be (True).
here is an example
<CheckBoxPreference
android:key="Show_Dialog"
android:defaultValue="true" />
From your MainActivity you should read this value like this:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean showDlg = sharedPref.getBoolean("Show_Dialog", true);
if the showDlg is true then you popup your dialog, otherwise continue what you want to do.
of course you need to change the value of Show_Dialog if the user checks the checkbox, you can do it like this:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor preferencesEditor = sharedPref.edit();
preferencesEditor.putBoolean("Show_Dialog", false);
preferencesEditor.commit();
in this way, you can be sure that your main activity will read the Show_Dialog as false next time the user launches your application
Good luck
Firstly, super.onCreate() must be the first call in any Activity's onCreate().
Secondly, store the value of the SharedPrefence in a boolean instead of directly setting the CheckBox, and use that before setContentView() to finish your current Activity and launch the next one if true. If not, carry on with the UI stuff and anything else you need.

Run App Twice To Work

I'm making an android app that test if certain security features on your phone are enabled. For example, if you have password log in enabled or if your data is encrypted on your phone.
For some reason, the app has to be run twice to test and see if these security features are enabled on the phone or not, and this is the problem I'm trying to solve. I'd like it to test and see if the security features are enabled when the app is created and the first time the app is run, not the second time it is run.
I test if these features are enabled in the onStart() function in my MainActivity file. I included the functions code below:
#Override
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#SuppressLint("NewApi")
public void onStart()
{
super.onStart();
//determine if phone uses lock pattern
//It returns 1 if pattern lock enabled and 0 if pin/password password enabled
ContentResolver cr = getBaseContext().getContentResolver();
lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED, 0);//Settings.System
//returns 1 if pin/password protected. 0 if not
KeyguardManager keyguardManager = (KeyguardManager) getBaseContext().getSystemService(Context.KEYGUARD_SERVICE);
if( keyguardManager.isKeyguardSecure())
{
//it is pin or password protected
pinPasswordEnable=1;
}
else
{
//it is not pin or password protected
pinPasswordEnable=0;
}//http://stackoverflow.com/questions/6588969/device-password-in-android-is-existing-or-not/18716253#18716253
//determine if adb is enabled. works
adb=Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0);
//determine if bluetooth is enabled.works
bluetooth=Settings.Global.getInt(cr, Settings.Global.BLUETOOTH_ON, 0);
//Settings.System BLUETOOTH_DISCOVERABILITY
//determine if wifi is enabled. works
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled())
{
//wifi is enabled
wifiInt=1;
}
else
wifiInt=0;
//determine if data is encrypted
getDeviceEncryptionencryption();
//determine if gps enabled
}//end of onStart() function
If any more code needs to be posted to answer this question, just let me know, and thanks for your help. Maybe the issue has something to do with the super.onStart();
Does anyone think that a splash loading screen might help solve the issue?
Here is good explanation how app lifecycle flows.
onStart() can be executed many times. You can keep counter how many times you had entered this method and act differently on each time:
static int counter=0;
public void onStart()
{
counter++;
Log.i("MyApp", "onStart() run "+counter);
switch (counter){
case 1: break; // first run
case 2: break; // second run
default: break;// other runs
}
}
To be more clear about life cycle and why your onStart() method is called twice I suggest to have counter and Log.i() in each important state of the cycle - at least in onCreate() and onRestart().
Keep in mind that app stays in memory when you click Home button. When you click app icon again it restarts already running app (calls onRestart() and then onStart() methods and no onCreate() ). When you really kill you app for real then sequence would be onCreate and onStart without onRestart. Having logcat records really helps you to understand app lifecycle flow and why your onStart() is called twice or more times.
Using a static variable to check how many times onStart has been called isn't a good idea, because an app can be killed if Android needs more memory for other apps while still allowing the user to navigate back to the app. That would be the path through the red box in the picture below (http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle):
A static variable would be 0 again after that and your app would run the security check again.
What you need to do is use an instance variable that you persist in onSaveInstanceState and restore in onCreate. In case the app is killed, onSaveInstanceState is called and you save your Activity's state. If the user goes back to the app, onCreate is called and the state would be restored. This works for all other cases too when the app isn't killed but the user just navigates away from the app and later re-opens it. Here's a simple example of an app saving and restoring:
public class MainActivity extends Activity {
private boolean mSecurityCheckDone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mSecurityCheckDone = savedInstanceState.getBoolean("mSecurityCheckDone");
}
}
#Override
protected void onStart() {
super.onStart();
if (! mSecurityCheckDone) {
// run the security check
mSecurityCheckDone = true;
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("mSecurityCheckDone", mSecurityCheckDone);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
mSecurityCheckDone = savedInstanceState.getBoolean("mSecurityCheckDone");
}
}
}
how about using flag ?
singleTop
If an instance of the activity already exists at the top of the target
task, the system routes the intent to that instance through a call to
its onNewIntent() method, rather than creating a new instance of the
activity.
singleTask
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.
singleInstance
Same as "singleTask", except that the system doesn't launch any other
activities into the task holding the instance. The activity is always
the single and only member of its task.
http://developer.android.com/guide/topics/manifest/activity-element.html
I'm not sure why you are using onStart(), if you want it to be run the first time the activity is created I would probably use onCreate() instead.
There is no API in Android that will tell you if the app has been run at least once so you will need to use some type of persistent storage for that, e.g. SharedPreferences could be used to persist a flag that would be set the first time your app is run and thereafter you can check it as shown here.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences("Prefs", 0);
if (settings.getBoolean("first_time", true)) {
//the app is being launched for first time, do something
Log.d("Comments", "First time");
// first time task
// record the fact that the app has been started at least once
settings.edit().putBoolean("first_time", false).commit();
}
}

Android: TextView.setLayoutParams sometimes work, sometimes doesn't

I have an android app that let's the user modify some layout parameters. One of my functions let's the user decide if a TextView will be aligned against the top or the bottom of a picture.
This is the function:
private void setAlign(String align) {
/* Get Preferences */
SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("align", align);
editor.commit();
Log.d("ALIGN", align);
paramAlign = align;
FrameLayout.LayoutParams floLP = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
(align == "TOP") ? Gravity.TOP : Gravity.BOTTOM);
txtGoal.setLayoutParams(floLP);
int res = paramAlign == "TOP" ? R.drawable.btn_toolbar_align_top_up : R.drawable.btn_toolbar_align_bottom_up ;
btnAlign.setImageResource(res);
}
Now once the activity is started, this function works fine. However, when I initialize the activity, I call the setAlign() function in the onGlobalLayout method after retrieving the alignment preference.
This is the relevant code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personalize);
/* Get Preferences */
SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_WORLD_READABLE);
paramAlign = prefs.getString("align", "BOTTOM");
Log.d("ALIGN", paramAlign);
// Get screen dimensions and initialize preview
ViewTreeObserver vto = rootView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
setAlign(paramAlign);
ViewTreeObserver obs = rootView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
}
Now if you notice the logging functions, they both return "TOP" when I start the activity. And the setAlign() function is obviously getting called. Yet, the TextView is aligned at the bottom. This is the XML for the TextView:
<TextView
android:id="#+id/txtGoal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:textColor="#color/color_white"
android:textSize="8sp"
android:shadowColor="#color/color_black"
android:shadowDx="1.2"
android:shadowDy="1.2"
android:shadowRadius="1.2"
/>
Any idea why the setLayoutParams is not happening when the activity is created? The function is getting fired when the layout is done being drawn so it shouldn't be the issue here. And the XML has no gravity specified to start with.
What am I missing here?
First of all, I see that the variable txtGoal is not initialized (nor even declared) so I am assuming you did that somewhere else (that is not posted in the question).
The behavior you are encountering is pretty much normal : the function is working only at start-up, and that's because once you change the layout of your text view, you must indicate that so it will be redrawn, by adding the following :
txtGoal.invalidate ();
after this :
txtGoal.setLayoutParams(floLP);
EDIT:
You can also try changing the gravity in a different way:
txtGoal.setGravity ( Gravity.TOP );
EDIT:
My apologies, what I suggest (the second solution) is wrong, because it changes the gravity of the text inside the text view (and not the gravity of the text view inside the root view).
Please try the following:
Do not try to modify the gravity of your text view using a listener, you can directly apply the gravity you want after setting the content view of your activity (because the text view is thus created). I advise the following:
Apply the new layout to your text view directly (not via the listener) after retrieving the shared preferences.
Your onCreate method should look something similar to this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personalize);
/* Get Preferences */
SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_WORLD_READABLE);
paramAlign = prefs.getString("align", "BOTTOM");
setAlign(paramAlign);
}
You should compare two string using the equals method:
Replace that :
(align == "TOP")
By that
align.equals ( "TOP" )
And this :
paramAlign == "TOP"
By this :
paramAlign.equals ( "TOP" )
So, here is what I found out and what I did to fix my problem.
Apparently the condition
align == "TOP"
Was not testing true when the activity was started altho the Log dump would tell me that it was in fact true at the time. Now why it did that I have no clue. This seems like a weird bug. That condition tested true once the activity was running.
Since this parameter could only have 2 values, I switched it to a Boolean variable where false is now the equivalent of "BOTTOM" and true the equivalent of "TOP" and it is working perfectly.
This is something that might actually need to be looked into as the condition should of tested true at startup.
** EDIT **
You cannot compare 2 strings in java using the "==" operator. You have to use .equals() instead.

Categories