Start Activity with an animation - android

I am trying to start an activity with a custom transition animation. The only way I have found out so far to do this (without using onPendingTransition() in the previous activity) is to use a custom theme on the activity and define either activityOpenEnterAnimation, taskOpenEnterAnimation, windowEnterAnimation or windowAnimationStyle to set the animation. But, none of these attributes are working for me. Some experimentation yielded the following results-
If I set the windowAnimationStyle attribute to some custom style which defines values for activityOpenEnterAnimation, taskOpenEnterAnimation, windowEnterAnimation or windowAnimationStyle I can get rid of the default transition animation occurring at the start of the activity. It doesn't show the transition animation using the actual value specified but at least the default animation is not shown.
According to the reference doc here,
I should be able to define an animation at the start of the activity using activityOpenEnterAnimation. But no success so far.
Any ideas?

I am using this in a current project of mine, it is basically pretty simple. You define a new animation style in your styles.xml, like this:
<!-- just defines top layer "Animation" -->
<style name="Animation" />
<!-- the animations must have been defined in your "anim" folder, of course -->
<style name="Animation.MyAwesomeAnimation" parent="android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">#anim/myawesomeanimation_enter</item>
<item name="android:activityOpenExitAnimation">#anim/hold_long</item>
<item name="android:activityCloseEnterAnimation">#anim/hold_long</item>
<item name="android:activityCloseExitAnimation">#anim/myawesomeanimation_exit</item>
</style>
Then set this style in a theme (themes.xml):
<style name="Theme.MyAwesomeTheme" parent="Theme.Default">
<item name="android:windowAnimationStyle">#style/Animation.MyAwesomeAnimation</item>
</style>
And then you can simply set these themes to every activity you like in your AndroidManifest.xml:
<activity
android:name=".MyAwesomeActivity"
android:theme="#style/Theme.MyAwesomeTheme" />
Now I wish you big fun with activity animations! :-D

Related

Mapbox NavigationView GPS tracking arrow drawable

I need to change the default arrow with circle in the navigation route view. I tried the following and it is not working.
<style name="NavigationLayerStyle">
<item name="mapbox_gpsDrawable">#drawable/ic_directions_black_24dp</item>
</style>
<com.mapbox.services.android.navigation.ui.v5.NavigationView
android:id="#+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navigationViewMapStyle="#style/CustomNavigationViewDark"
app:navigationDarkTheme="#style/CustomNavigationViewDark"
app:navigationLightTheme="#style/CustomNavigationViewDark"
app:navigationViewPrimary="#color/colorPrimary"
app:navigationViewLocationLayerStyle="#style/NavigationLayerStyle"/>
Is there any other way to achieve this?
I had this problem to and i figured out in few hours
First, you need to declare the custom style with your custom drawable.
In order to do this, you need to declare the "NavigationLocationLayerStyle" style extension (which contains the drawable) and give to him the attribute "mapbox_gpsDrawable"
<style name="customGPS" parent="NavigationLocationLayerStyle">
<item name="mapbox_gpsDrawable">#drawable/bike_icon_map</item>
</style>
Then, declare the "main theme" with your custom location layer style
<style name="customdark" parent="NavigationViewDark">
<item name="navigationViewLocationLayerStyle">#style/customGPS</item>
</style>
Once done, you simply have to inject your theme to your navigation view
To achieve this, you can't use any method of navigationView, because they all don't work (or at least i didn't find the correct one). So i wrote just this command (in kotlin):
(this as Context).setTheme(R.style.customdark)
This roughly works, hope is not too late.

Circular reveal on activity with fragment

I have problem with circular reveal on my fragment. Whenever I call my reveal, it starts a new Activity and then inside onCreateView in fragment, it calls my reveal.
It works, but there is one glitch – as I start a new Activity, everything turns white. The Activity's background is white and covers data under it. Is it possible to show this data and not overlay it with this white background? Or should I make some kind of mask, animate it, and at the end start Activity?
I had the same issue and resolved it by using a transparent activity. To make your Activity transparent, use this code.
Theme Definition in styles.xml
<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
Activity Definition in AndroidManifest.xml
<activity
android:name=".ui.CircularRevealActivity"
android:theme="#style/Theme.Transparent"
android:launchMode="singleTask"
/>
For a full answer on how to use a circular reveal animation on an activity, you can check this link.

Themes in android

A create a simple Theme as
<style name='one'>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
However on viewing in the emulator the screen goes black.when i do not apply theme the screen has a white background .
what really happens here.i am just starting with android.
In addition ,if a apply a theme to my activity then the attributes of the theme applies to all components of my activity say button,textfields and edittexts .
why would i then write
android:textSize=?android:textSize
to reference value from the theme for any button in my layout when the same value would already be applying.
is the syntax above the correct way to reference an attribute from my theme to assign to attribute for any view in my layout.
thanks
tejinder
Yeah, so you need to do a little more reading.
Let's start with the basics,
You need to understand the differente betweent an Attribute, a Style, and a Theme.
An Attribute is something that can be styled. For instance: android:textSize is an attribute that can have any value.
A Style is a set of specific attributes that will be applied to a Widget. They are defined
in your /values/styles.xml
For instance:
<style name="normalTextThin" parent="android:Widget.Holo.Light.TextView">
<item name="android:gravity">left|center_vertical</item>
<item name="android:padding">8dp</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">16sp</item>
</style>
The styles can be applied either as part of a theme or directly as theme-independent.
Theme-indepentent styling of a widget is like this:
<TextView
android:id="#+id/text"
style="#style/normalTextThin"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
You are then theming only that one TextView.
A Theme is a collection of Styles that can be applied to a part of your UI, such a a whole Activity, or your whole Application.
For instance:
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:editTextStyle">#style/EditTextAppTheme</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
</style>
Here, we are declaring that all EditText in your application will use the style named EditTextAppTheme, and so forth and on. When done like this, in order to actually have the theme be active, you declare it in the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
That means that you are not required to declare the style on each widget you create.
<EditText
android:id="#+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_search">
<requestFocus />
</EditText>
That widget right there would already be styled using EditTextAppTheme without the need of you explicitely declaring so.
I recommend you try to read on what attributes can be styled, how to style them, and so forth and on.
If you don't want to though, it's fine, you can still get a lot done with the following tools for styling:
ActionBarStyleGenerator to help you create styles for the ActionBar.
Android Holo Colors to help you style standard widgets.
Hope that helps.
Additional Info
Let me clarify on the whole ?attr/attributeName
The ? means that the system will choose the specific attributeName value for the current Configuration (not specific to different themes). This should be used only when you want the value to be different on different configurations. For example:
?android:attr/actionBarSize
This line is a dimension, and it will be different not based on the current theme, but on the current device screen size and orientation (values, values-land, values-sw600dp).
It's important to know that specifying ?android: means you are accessing preset Android values, not yours. If you have or want to create and use your own attribute values for specific configurations, you must do the following:
Create a file named attrs.xml on your /values/ folder.
Declare the desired custom attribute:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<attr name="my_custom_attr" format="reference" />
</resources>
Declare a value for the custom attribute, let's say on your own theme.
<style name="AppTheme" parent="android:Theme.Light">
<item name="my_custom_attr">#resource_type/resource_name</item>
<item name="android:editTextStyle">#style/EditTextAppTheme</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
</style>
And then you can use it on the Widget you'd like:
Hope that clears things out.
EDIT 2
For a better answer to your question, please update your question. And like I said, read more on how to properly create styles.
The Theme named 'one', what do you want to apply it to? An activity, a Widget, the whole Application?
How are you applying the theme? Show the lines of code where you specify the usage of theme 'one'.
Your theme as you specified is simply not a properly constructed theme/style.
<style name='one'>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
This says absolutely nothing, and it is definitely not suitable for an Activity-level theme. The reason you specify a parent is so your theme can inherit all of the attributes from the parent, and then you specifiy which ones to change.
For instance, if you want to use your theme and have a light background, do this:
<style name='one' parent="android:Theme.Holo.Light>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
But even here, despite the fact that it will apply, you don't want to have the same text color and size for the whole application do you? That'd be nonsense, different text color and sizes account for a big part of the user experience, so rather than setting those values from what we can refer to as the main style, we can create substyles and apply them to certain widgets.
I can't really go any more detailed that what I already have, the above explains how to accomplish Widget-specific styling, and activity/application level theming.
For a complete start-up guide, read the Android Developer Site, try the test styles declared there, see how they work, and until then try to create your own, don't try to create something out of nowhere if no reading has been made.

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

Activity should be transparent, but has black background

My use case is writing an overlay controller activity for a landscape camera preview. I followed the instructions from a couple of tutorials for writing a transparent theme.
So my res/values/style.xml looks like this:
<resources>
<style name="Theme" parent="android:Theme" />
<style name="Theme.Transparent">
<item name="android:windowBackground">#drawable/transparent_background</item>
</style>
<drawable name="transparent_background">#00000000</drawable>
</resources>
The activity snippet:
<activity android:name=".CameraPreview"
android:label="Camera"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Controlls"
android:label="Controlls"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Translucent">
</activity>
When I start this activity from my root activity, the layout gets drawn correctly, but the background stays black. I tried to use #android:style/Theme.Translucent instead, but this Theme inherits the orientation from the calling activity (landscape) and thats not what I want.
Edit:
The application holding the camera preview is set to landscape view as it does not display the preview correctly in portrait orientation. (see old google bug report)
What I wanted to do was to put an independent activity for user interaction interface in front of the camera surface holder (this activity should be set to 'portrait', or even better to 'sensor')
Here's a somewhat related answer for anyone else that has a similar problem.
tl;dr
Adding a new style where the name is a suffix of an existing style can cause problems (like making transparent activities have a black screen).
Problem:
Our transparent activity background was black after doing a large refactor. (The activity was already using a transparent theme.)
After several hours of going through commits I found what seemed to be the cause of the problem. In our app we use styles like CSS. There was an existing style like this that we applied to a TextView.
<style name="HeadLine.SM.White.MyFontBold">
<item name="android:fontFamily">#font/some_bold_font</item>
</style>
The non-bold variant of the style was added as a style before the bold variant as below.
//This style was added
<style name="HeadLine.SM.White.MyFont">
<item name="android:fontFamily">#font/some_font</item>
</style>
<style name="HeadLine.SM.White.MyFontBold">
<item name="android:fontFamily">#font/some_bold_font</item>
</style>
For some reason, after the non-bold style variant was added all transparent activities had a black screen. When we changed the non-bold variant style name to something else it fixed the problem for us.
Now our styles look like this (I know there are better ways to handle font styles - these styles are a few years old).
<style name="HeadLine.SM.White.MyFontRegular">
<item name="android:fontFamily">#font/some_font</item>
</style>
<style name="HeadLine.SM.White.MyFontBold">
<item name="android:fontFamily">#font/some_bold_font</item>
</style>
Conclusion
It seemed that adding a new style where the name is a suffix of an existing style caused problems. If you're adding a new style make sure the name is not a suffix of an existing style.
We did try cleaning the build, rebuilding, and invalidating Android Studio caches. None of these things solved our problem.
Try the built-in #android:style/Theme.Translucent instead of your custom one, according to this blog post. I haven't tried to do this myself, so I don't know if the technique written about there works or not.
I found out, that another important child element for the style description above is <item name="android:windowIsTranslucent">true</item> was lacking.
Problem:
That child element also causes synchronizing the activity's orientation with the calling one. (same effect as #android:style/Theme.Translucent)
I bumped into the same problem as you describe (regarding translucent background and screen orientation), but in the end I reconciled with the fact that this is just how it works. In fact it actually makes sense that it works this way. I can't think of any screen based system that supports mixing portrait and landscape views, so why should Android?
I guess the general rule is that all visible activities must have the same orientation, regardless of the attributes in the manifest-file. If all are set to "sensor" then they will all change, if one is fixed to portrait or landscape, then the others must follow (and whoever sets it last "wins").
I guess this was just so obvious to the developers that it didn't occur to them to document it :)
Remove and all its done
#Override
public void onAttachedToWindow() {}
Style name is having some effect on the transparent background to be black. I used below one and works fine.
<style name="Theme.AppCompat.Translucent">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
<!-- AndroidManifest.xml -->
<activity android:name=".TranslucentThemeActivity"
android:theme="#style/Theme.AppCompat.Translucent"/>

Categories

Resources