Apply the configuration of the app for each activity - java

How can I apply the configuration chosen by the user, in each activty of the app, without to rewrite the code in each of them?
For example, if I can use the application in fullscreen mode, now I forced to add this code in each activity
if(useFullscreen)
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
else
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Is there a way to do this?

I would suggest you to make a master activity for your app and add that code to each activity then save the configuration of user. Now you can easily apply the configuration to each page without more LOC.
http://mfarhan133.wordpress.com/2010/09/19/maser-layout-tutorial-reusing-layouts/
look above link to make master page

Related

Is there any way to track what fragment/s are visible right now?

I work on a very large project. It has a lot of modules and views - activities and fragments. I need to understand what fragment and what activity are running at the moment. Then I need to move to this class of activity and fragment in the project. Is there any way to find the class name? maybe by logs or smth else?
You can use Android Profiler for this. You will also get a lot of details from this tool. Just hover the mouse over the graph, it will show the fragment currently showing.
You can try Layout Inspector, a feature of android studio. When the app is running on a device that is connected to Android Studio, you can click on any item to see the view id, you can use these ids to find out which activity/fragment they belong to (through xml file)
Use UserVisibleHint for that:
boolean isVisible;
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
isVisible = isVisibleToUser;
}
When you want to check the visibility of the fragment, simply check it.
if (isVisible) {
//Fragment is visible
} else {
//Fragment is not visible
}

Prevent Android from showing sensible information on multi tasking view

I need to hide the content of my application when it goes to the background so sensitive information are not showing up on the android multitasking view.
It's been suggested to use the following line to hide the screen
getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
It works fine.
However, this prevents the user from taking screenshot as well which is not an expected behavior for me. I want to let the user take screenshot of the app if they need to. What I don't want is Android to display the latest screen on the multitasking view.
Would it be possible to set the FLAG_SECURE only when the app goes in the background?
We've ended up with this solution which worked the best for us:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!hasFocus) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
}
You can use Activity Lifecycle Callbacks. Just call setVisiblity(View.INVISIBLE) on the views that you want to hide in the onPause() Callback and setVisiblity(View.VISIBLE) in onResume() Callback.

set custom labels for activity name in Google Analytics

i use Google Analytics for monitor my users in my Android app. Now i want to set label's for each activity in google analytics. Just for better understanding i don't want see Activity Class name, instead see my custom label's. how i can do this?
in onCreate:
// Get tracker.
Tracker t = ((MyApplicationClass)
getActivity().getApplication()).getTracker(MyApplicationClass.TrackerName.APP_TRACKER);
// Set screen name. Where path is a String representing the screen name.
t.setScreenName("YOUR_CUSTOM_LABLE_FOR_THIS_SCREEN");
// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());
Initialize variable:
private Tracker tracker;
onCreate()
tracker = ((GlobalClass) getApplication()).getTracker(GlobalClass.TrackerName.APP_TRACKER);
tracker.setScreenName("Add_your_own_lable");
tracker.send(new HitBuilders.AppViewBuilder().build());
Hope this will help you.
You can also configure the activity names with XML tracker configuration file.
https://developers.google.com/analytics/devguides/collection/android/v4/#analytics-xml
For example if you like to name "com.example.app.MainActivity" activity as "Home Screen" you can add:
<screenName name="com.example.app.MainActivity">
Home Screen
</screenName>
to the configuration file. When creating the tracker, make sure you are using the XML configuration instead of hardcoded trackerId.

Remove Recent Apps Button From Navigation Bar

I am currently developing an app and I would like to have a little more control over it then usual without having to root the device.
I would like to remove the capability of the recent apps button in the navigation bar, or at least make it do something else from the default actions. Is there a way to do this? I'm sure there is since SureLock does the same thing.
Thanks
I have found a workaround for this on this website:http://www.juliencavandoli.com/how-to-disable-recent-apps-dialog-on-long-press-home-button/
you need to add this permission: android.permission.REORDER_TASKS
And add this code:
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if( !hasFocus)
{
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
am.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_WITH_HOME );
sendBroadcast( new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS) );
}
}
It is not possible to override the recent apps button.
There is no KeyEvent like there is for the Back Button, and as such this feature is not available.
See documentation here: http://developer.android.com/reference/android/view/KeyEvent.html
You may not be able to disable a button, but you can disable the app that is associated with it. I don't know how it is done, but I have seen kiosk app (for non-rooted devices) that disallow other apps from loading.

TabHost to launch external browser when clicked

I have some tabs in my app and I want the last tab to launch google in the default system browser. I thought this would work:
Uri uri = Uri.parse("http://www.google.com/");
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Google", res.getDrawable(R.drawable.google)).setContent(new Intent(Intent.ACTION_VIEW, uri)));
But it results in a force close error. Any tips on getting this working?
EDIT
I solved this. Basically what I do is add an onClick event handler to capture when the tab is clicked in the first place (only this tab in question) and then from within that I prevent the default action by returning true (for handled) after launching a new Intent in the regular fashion.
You can start an Activity from Tab Host(that you have mention as last Tab Host).Then from that activity you can launch external Browser.As i think its not possible to launch default activity from TabHost.
Edited
I have checked it.It give ActivityNotFound Exception.Conclusion is that TabHost look for the activity that is registered in Android manifest.If you want to achieve it then go with my first suggestion

Categories

Resources