How to avoid 'blank activity' flick during navigation - android

I have Android app with custom headers for every activity view.
I'm using Activities without title:
RequestWindowFeature(WindowFeatures.NoTitle);
When I'm navigating from one Activity to another, what I see is first a blank Activity with title, then my desired View without title.
What should I do in order not to see that blank Activity?

Sounds to me that a lot of stuff is going on in your OnCreate's. Try to minimize that.
Additionally you can set a Theme for your Activity which looks more like what it is going to view. My opinion on using RequestWindowFeature to hide the title or ActionBar is that it should be avoided if possible and use a theme instead.
What I would do is to create a Resources/Values/style.xml file like this:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme.Default" parent="#android:style/Theme"></style>
<style name="MyTheme.NoTitle" parent="#android:style/Theme.NoTitleBar"></style>
</resources>
To support API 11 create a Resources/Values-v11/style.xml with:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme.Default" parent="#android:style/Theme.Holo"></style>
<style name="MyTheme.NoTitle" parent="#android:style/Theme.Holo.NoActionBar"></style>
</resources>
And again for API 14 a Resources/Values-v14/style.xml with:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme.Default" parent="#android:style/Theme.Holo.Light"></style>
<style name="MyTheme.NoTitle" parent="#android:style/Theme.Holo.Light.NoActionBar"></style>
</resources>
Then in all your Activities use the Theme property in the Activity flag like so:
[Activity(Label="My Activity", Theme="#style/MyTheme.NoTitle")]
public class MyActivity : Activity
{
...
}

Related

How to switch themes (night mode) without restarting the activity?

I have made a few apps that support multiple themes, but I always had to restart the app when user switches theme, because setTheme() needs to be called before setContentView().
I was okay with it, until I discovered this app. It can seamlessly switch between two themes, and with transitions/animations too!
Please give me some hints on how this was implemented (and animations too). Thanks!
#Alexander Hanssen's answer basically has answered this...
Don't know why it was not accepted... Maybe because of the finish()/startActivity().
I voted for it and I tried to comment but cannot...
Anyway, I would do exactly what he described in terms of styles.
<style name="AppThemeLight" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
<style name="AppThemeDark" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
<!-- This will set the fade in animation on all your activities by default -->
<style name="WindowAnimationTransition">
<item name="android:windowEnterAnimation">#android:anim/fade_in</item>
<item name="android:windowExitAnimation">#android:anim/fade_out</item>
</style>
But instead of finish/start with new intent:
Intent intent = new Intent(this, <yourclass>.class);
startActivity(intent);
finish();
I would do:
#Override
protected void onCreate(Bundle savedInstanceState) {
// MUST do this before super call or setContentView(...)
// pick which theme DAY or NIGHT from settings
setTheme(someSettings.get(PREFFERED_THEME) ? R.style.AppThemeLight : R.style.AppThemeDark);
super.onCreate(savedInstanceState);
}
// Somewhere in your activity where the button switches the theme
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// decide which theme to use DAY or NIGHT and save it
someSettings.save(PREFFERED_THEME, isDay());
Activity.this.recreate();
}
});
The effect is as shown in the video...
The transition/animation makes the theme change seamless when you restart the activity, and this can be done by adding the items "android:windowanimationStyle" to your themes, and then referencing a style where you specifiy how the Activity should animate when it enters and exits.
Note that this makes the animation apply on all activities with that theme.
<style name="AppThemeLight" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
<style name="AppThemeDark" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
<!-- This will set the fade in animation on all your activities by default -->
<style name="WindowAnimationTransition">
<item name="android:windowEnterAnimation">#android:anim/fade_in</item>
<item name="android:windowExitAnimation">#android:anim/fade_out</item>
</style>
Then, when you want to change theme you could do this when clicking a button:
AppSettings settings = AppSettings.getInstance(this);
settings.set(AppSettings.Key.USE_DARK_THEME,
!settings.getBoolean(AppSettings.Key.USE_DARK_THEME));
Intent intent = new Intent(this, <yourclass>.class);
startActivity(intent);
finish();
Then in your onCreate method, use the setTheme() to apply the theme that is currently set in AppSettings like this:
AppSettings settings = AppSettings.getInstance(this);
setTheme(settings.getBoolean(AppSettings.Key.USE_DARK_THEME) ? R.style.AppThemeDark : R.style.AppThemeLight);
super.onCreate(savedInstanceState);
setContentView(<yourlayouthere>);
Check out this gist for reference: https://gist.github.com/alphamu/f2469c28e17b24114fe5
for those who are trying to find solution for android version 10 or updated.
to set dark/light mode use this:
AppCompatDelegate.setDefaultNightMode(state) //state can be AppCompatDelegate.MODE_NIGHT_YES or AppCompatDelegate.MODE_NIGHT_NO
it will change the display of your app but with a flicker
to avoid the activity recreation flicker (for smooth transition), in your activity add the below method
#Override
public void recreate() {
finish();
overridePendingTransition(R.anim.anime_fade_in,
R.anim.anime_fade_out);
startActivity(getIntent());
overridePendingTransition(R.anim.anime_fade_in,
R.anim.anime_fade_out);
}
setTheme() before super.onCreate(savedInstanceState) in GKA answer is perfect approach and work well, thanks to GKA.
but it creates new instances for all resources again, including activities, fragments, and recycler views. I think it may be heavy work and cause to loss of some saved data like local variables.
accourding to google document: https://developer.android.com/reference/android/app/Activity#recreate()
Cause this Activity to be recreated with a new instance. This results
in essentially the same flow as when the Activity is created due to a
configuration change -- the current instance will go through its
lifecycle to onDestroy() and a new instance then created after it.
there is another approach that you can change the theme programmatically with code (Java or Kotlin), in this approach you don't need to recreate all resources, and also you can use custom animation like ripple.
check my GitHub library:
https://github.com/imandolatkia/Android-Animated-Theme-Manager
in this library, you can create your custom themes and change them dynamically with ripple animation without recreating any resources.
Simply efficient one liner in fragment:
requireActivity().recreate();
For activity:
recreate();
There isn't anything preventing you from calling setTheme() and then setContentView() again. You'll just need to restructure your app a bit so that, if you change the theme, you need to reinitialize any member variables you might have that are holding references to View objects.

How to create fragment without action bar in android?

Hi all I have to create fragment with out action bar. Like this
In this picture I created Lienarlayout and placed that image in center. Below of that I want to create 4 fragments like projects,calender,filter and search. I searched a lot and tried by many codes but all the fragments has created with action bar. But i need to create fragments without action bar. How can I create that? Can anybody tell me? Thanks in advance.
ActionBars are actually components of your Activity objects and not your Fragments. (Take a look at the ActionBar documentation here). If your app is API level >= 11, your Activity objects will have ActionBars by default. These are set by the SDK-provided Holo theme. You can remove the ActionBar from the Activity to which your fragments are attached.
In order to do so, modify the Holo theme. Create a styles resource like so:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="NoActionBar" parent="">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
Then in your manifest you should associate the modified theme with the Activity that you want to be ActionBar-less:
<activity
android:name="com.yourproject.activity.NoActionBarActivity"
android:theme="#style/NoActionBar" >
</activity>

Change Text for loading screen in ActionBar-PullToRefresh library

I have successfully implemented ActionBar-PullToRefresh in my code. Now whenever I refresh the list it shows "Loading ..." text in ActionBar.
So how to change that text in ActionBar. Do I directly change the string in the library or is there any other way to do that...
Approved approach from the samples
Source: https://github.com/chrisbanes/ActionBar-PullToRefresh/tree/master/samples
Create a theme with text overrides (e.g. ptrPullText),
that is, res/values/styles.xml:
<resources>
<style name="Theme.Holo.CustomPtrHeader" parent="android:Theme.Holo">
<item name="ptrHeaderStyle">#style/Widget.Custom.PtrHeader</item>
</style>
<style name="Widget.Custom.PtrHeader" parent="android:Widget">
<item name="ptrRefreshingText">Pulling down the internet</item>
</style>
</resources>
Apply the custom theme to your activity in AndroidManifest.xml
<activity
...
android:theme="#style/Theme.Holo.CustomPtrHeader" />
or Register you own HeaderTransformer
For the example on how to do this, please see the GridView sample.
or A little hackier way
Please note that setPullText is not on the HeaderTransformer interface, it's an instance method of DefaultHeaderTransformer:
attacher = PullToRefreshAttacher.get(this);
attacher.addRefreshableView(listView, this);
transformer = ((DefaultHeaderTransformer)attacher.getHeaderTransformer());
transformer.setRefreshingText("Pulling down the internet");

Can I set ListView and ListFragment selected color in an application-wide theme?

I have an application with two Activities. The first Activity is a ListActivity. The second Activity is a FragmentActivity, with two Fragments in it. The first Fragment is a ListFragment. I'd like to format the application using a custom Theme that extends Theme.Light. I've successfully changed the windowBackground, but when I try to change listSelector nothing happens. Is it possible to do this through a Theme, or do I have to do it separately on each list? Here's my code:
My Custom Theme:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="StitchTheme" parent="android:Theme.Light">
<item name="android:listSelector">#ffffff</item>
</style>
</resources>
Then, in my Manifest, I put:
<application
android:theme="#style/StitchTheme" >
I've also tried doing this by setting up a selector.xml and setting android:listSelector in my Theme, but it has no effect. Thoughts?

How to set the background screen as Wallpaper all the time?

Currently, I'm using this to show my application background as phone wallpaper.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER,
WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
But for some reason when I start my application by pressing the icon. It just shows the activity screen with the icons on the home screen. I didn't use dialog but it looks like a dialog because layout is just set that way. So I just want to show the wallpaper whenever this activity is running. But it only shows the wallpaper only after the next event occurs such as switching to different activity. I already put that code on onCreate() and whenever I do setContentView()..... Is there way to do such thing or there is just no way?
For users of AppCompat, just use the following in your styles.xml, no need for code:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowShowWallpaper">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
After long search and trial and error. I've found the solution to what I wanted. It was just creating separate themes.xml file and just tweak the Theme.Dialog which is already defined in default android themes.xml. All I did was change the Animation part. Originally in android themes.xml the line looks like this.
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
but since modifying in android themes.xml doesn't take the effect. I just created my own themes.xml as I said above and just set parent as android:Theme.Dialog. And added a line like this.
<item name="android:windowAnimationStyle">#android:style/Animation</item>
Thanks for the help and I hope this solution helps others.
Use following code -
rl = (RelativeLayout)findViewById(R.id.someid);
//relative layout is my root node in main.xml (yours may be linearlayout)
WallpaperManager wm = WallpaperManager.getInstance(this);
Drawable d = wm.peekDrawable();
rl.setBackgroundDrawable(d);// You can also use rl.setBackgroundDrawable(getWallpaper);

Categories

Resources