How to show an ImageView in a style? - android

Before, when my app first started up, there was a white screen. So I created a style with a black background in styles.xml and referenced it in my manifest:
<activity
android:name=".InitialScreen"
android:theme="#style/Theme.Transparent"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
styles.xml:
<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:background">#000</item>
</style>
The black screen shows successfully, How can I show an ImageView in this style?

In your mipmap/drawables folder of your project add an image of your choice. Inside your style remove the background and add a windowBackground... inside reference your drawable or mipmap that you have added.
E.G:
I want my background to be a certain image, it's name will be background.png. I will go to the directory of my project and add this image to the drawables or mipmaps folder, depending on which you're using. In your style have this:
android:windowBackground="#drawable/background"
Or #mipmap/background depending on which you are using.
Why I have used windowBackground and not background
I have just recently done the same thing and realized that I had made a mistake when using background instead of windowBackground because if you have a layout/view which has no background specified it will use the same background. This will cause the background to repeat.
Hope I helped,
-Daniel

Related

New Splash screen is shown cut in a cyrcle shape

I'm trying to replace my old activity based splash screen in my Android app with the new Splashscreens API
So i've created a svg of my app logo, create the theme, and set in my MainActivity the installSplashScreen but the logo in the Splashscreen looks like this when app is launched:
How could i fix that issue?
Here is what i've done style.xml:
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">#color/colorAccent</item>
<item name="windowSplashScreenAnimatedIcon">#drawable/ic_visual_vector</item>
<item name="postSplashScreenTheme">#style/AppTheme</item>
</style>
Manifest:
<activity
android:name=".MainActivity"
android:theme="#style/Theme.App.Starting"
android:screenOrientation="portrait"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SplashScreen.installSplashScreen(this);
setContentView(R.layout.activity_main);
...
What you can do is to wrap your icon in an inset drawable so that it is drawn inside the circle.
For example, create a drawable/splash_inset.xml resource:
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/ic_visual_vector"
android:insetLeft="72dp"
android:insetRight="72dp"
android:insetTop="72dp"
android:insetBottom="72dp"/>
The actual inset values depend on your image and its aspect ratio, using 72dp here on all edges as an example.
Then apply this drawable as your windowSplashScreenAnimatedIcon.
In Android 12, if your icon is bigger than the required size, it'll be cut off.
App icon without an icon background: This should be 288×288 dp, and fit within a circle of 192 dp in diameter.
For example, if the full size of an image is 300×300 dp, the icon needs to fit within a circle with a diameter of 200 dp. Everything outside the circle will be invisible (masked).
More info: https://developer.android.com/guide/topics/ui/splash-screen#elements
My answer could be late but I had same issue. I only added android:gravity="center" to my drawable/splash_logo.xml file and using the splash_logo.xml in styles.xml file.
<layer-list>
<item android:gravity="center"
android:src="#drawable/splash_logo_icon" />
This happens when I run my app on an emulator 30s but when I run it on a physical device it show well. Looking at your code and comment above, is it really required you implement the image from the styles/Res ?... I recommend you create the imageView in your splash_screen.xml

Android: Creating a splashscreen on app start WITHOUT timer?

Currently I have a very low-res splashscreen I made by setting the applications theme to a drawable image. This would work PERFECTLY if it wasn't for this. Now I'm looking for an alternative.
The one thing I will not do is create a timed splashscreen, which there are plenty of guidelines on how to make one, the one thing I want to know is how do I create a splashscreen that will load first thing, then finish once MainActivity has finished loading?
The one thing I love about my current splashscreen is that it loads instantly when the app starts, but it will cause major delays when clicking buttons if it's high-res (more than 300x300pixels).
Here's my current code for the flawed, laggy splashscreen that loads and stops based on MainActivity's loaded state:
in styles.xml:
<style name="splashscreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowBackground">#drawable/splashscreen</item>
</style>
in manifest:
in AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:fullBackupContent="false"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:theme="#style/splashscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Check for possible issues with overdraw. It seems that the background (the splash image) is still there. see http://www.curious-creature.org/2012/12/01/android-performance-case-study/ if it applies to your case.
Removing the window background: the background defined in your theme is used by the system to create preview windows when launching your application. Never set it to null unless your application is transparent. Instead, set it to the color/image you want or get rid of from onCreate() by calling getWindow().setBackgroundDrawable(null).

How to hide the actionbar before the Activity is loaded?

My goal is to show a splash screen on my applications startup. Right now what it will do is briefly show the actionbar with an otherwise blank page, then jump to the splash screen. I'm trying to figure out how to not show the beginning screen and just start with the splash screen.
I'm trying to use these links for information on how to solve this.
ActionBar Lag in hiding title
In this one I'm assuming I can use the same type of method for hiding the actionbar by changing the theme, but I don't know what I would actually use as my style to do so.
How to hide action bar before activity is created, and then show it again?
and here it talks about adding a line to the manifest that would do it. Where in the manifest? Anywhere I put it did not do anything.
try this in manifest file
<activity
android:name="yourActivityName"
android:label="your label"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
</activity>
Check this link Android: hide action bar while view load
Code snippets from the link, incase the link breaks down, courtesy #kleopatra:
Setting the properties windowNoTitle to true on your theme will
hide the ActionBar. use two different themes both extending parent="Theme.AppCompat.Light" in order to prevent NPE when using getSupportActionBar
set the styles as
<style name="AppThemeNoBar" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
</style>
<style name="AppThemeBar" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">false</item>
</style>
Due to some odd behaviour on versions < 11, you need to add
if (Build.VERSION.SDK_INT < 11){
getSupportActionBar().hide(); }
inside activities that do not need the actionbar
Delete the "android:label" entries in Manifest file, from application and the first activity which is loaded. In your case, the Splash activity.
Sample...
<application
android:allowBackup="true"
android:icon="#drawable/starticon"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo">
<activity
android:name=".ActivitySplash"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Just add this code in your Activity in the onCreate function.
val actionBar = supportActionBar?.apply{hide()}

no orientation notification when translucent set

Using phones that have android 2.1 & 2.2 installed, using the simplest case of a hello world app and add android:theme="#android:style/Theme.Translucent" to the activity in the android manifest to have the app be transparent, the app sticks as portrait only and won't rotate to landscape when the phone is rotated.
Take the line out and the app rotates ok. This is verified by adding the override of onConfigurationChanged and putting a breakpoint in that routine. Brk hits when translucent isn't applied, doesn't when you add translucency.
However, using a samsung galaxy tab using andr 2.2, rotation works ok even with translucent applied. Anyone have any ideas on this?
I had a same problem. Just add android:screenOrientation="sensor" in the manifest file after you specify theme:
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="sensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
So far I tested it on android 2.2 and 4.1 - works as expected.
I have a same problem... but in my case I used Translucent because I solve redrawn warning (this warning appear when set color on android:background)
I solved the warning creating a Theme with parent Theme.Lignt and rewrite two attributes
Something like this
<style name="MyTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/my_background</item>
<item name="android:colorBackground">#color/my_background</item>
</style>
If you need use Translucent in ApiDemos has a sample when an activity have a translucent theme and orientation service works well

How to set different label for launcher rather than activity title?

This question has been asked before - but with no satisfying answer at all! So I'm trying it again.
I want to give my application launcher icon (the one that is displayed on the startscreen!) a different, shorter caption. It seems the launcher takes its label from the mainfest section about the main activity's label, as here:
<activity android:name="MainActivity" android:label="#string/app_short_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
I already changed the original reference to my app's name #string/app_name to a different, shorter string resource here.
BUT - big BUT: this also of course changes this activity's default title! And I did not want that to happen, there's enough space for a long application name! Setting the long title again in onCreate using the setTitle(int) method does no good either, because the short name will be visible to the user for a short time, but long enough to notice!
And - please don't answer my question by refering to a custom titlebar... I do not want to go that long way, just because of a stupid string title! It's a pain to draw a custom title bar for so little effect!
Is there no easy way to just give the launcher a different string to display?
Thanks for your answers!
Edit: One more reason why having a custom titlebar is a pain is that it will not look like the default titlebar, I would have to explicitly do things to make it look alike on each device! And that can't be a solution if, after all, I don't want a different appearance!
Apparently <intent-filter> can have a label attribute. If it's absent the label is inherited from the parent component (either Activity or Application). So using this, you can set a label for the launcher icon, while still having the Activity with its own title.
Note that, while this works on emulators, it might not work on real devices, because it depends on the launcher implementation that is used.
http://developer.android.com/guide/topics/manifest/intent-filter-element.html
<activity
android:name=".ui.HomeActivity"
android:label="#string/title_home_activity"
android:icon="#drawable/icon">
<intent-filter android:label="#string/app_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Side Note: <intent-filter> can also have an icon attribute, but
inexplicably it does not override the icon specified in the
Activity. This may be important to you if you plan to use the native
ActionBar in SDK 11+, which uses Icon and Logo specified on the
Activity.
Added Info: The label is being inherited from Activity and not the Application.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".StartActivity"
android:label="#string/app_long_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In this case, app_long_name will be displayed with launcher icon, if we do not put label inside as mentioned above.
I was looking for the same thing and here's what worked for me.
<activity android:name="MainActivity" android:label="#string/app_short_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This will give a short name to your application launcher icon.
To add a larger name to the application bar you just have to add:
this.setTitle(getResources().getString(R.string.app_name));
to your main activity java file.
Solution of Mark Renouf fails to me (using Nexus 4 and Android 4.4). It fails when using shortcuts, shortcuts use the main activity label instead of the app name. I saw some apps like GMail and Google Keep that works fine. But when you open them, I notice its like a moment between the title is blank and the title appears (which seems better than the app name flashing before setting the title using setTitle()).
So here is the best solution I found:
Create a style where the ActionBar does not show the title/label:
<style name="NoActionBarTitle" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/NoActionBarTitle.ActionBar</item>
</style>
<style name="NoActionBarTitle.ActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">showHome|useLogo</item>
</style>
I'm using a navigation drawer and using a logo (because I use a logo and an icon for my app). You can use whatever but don't use showTitle. Then in the AndroidManifest.xml, set the theme for the MainActivity:
<activity
android:name="com.xx.xxx.MainActivity"
android:logo="#drawable/ic_icon_padding"
android:theme="#style/NoActionBarTitle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Then, in the onCreate() method of the MainActivity, set the title of your Action Bar:
getActionBar().setTitle(R.string.your_title);
After it, you can call:
getActionBar().setDisplayShowTitleEnabled(true);
Tricky but worth.
This probably won't satisfy what you want to do, but have you thought about creating a splash screen that displays very briefly (with the default title) and then launches your new actual "main" activity with the title of your choosing using the setTitle(int) method? I have not tried this to see if it works but that might create a pleasant work around that does not show of the less than seamless nature of what you are trying to achieve.
For anyone using Support / Appcompat Toolbar via the setSupportActionBar() method, the Activity title can be set in Toolbar XML:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
app:title="#string/activity_title"
...
/>
This will override application and activity labels set in the manifest.
android.support.v7.widget.Toolbar
I found a workaround for this problem
In manifest.xml
Write your app's name in the android:label of the launcher(main) activity.
This will make the label of your main activity same as that of the app label.
Then, in the onCreate() function of your Launcher(main) activity write this statement
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("main activity label");
}
Here write the label that you want to give to your Launcher(main) activity.
You can do something like this:
public class FooBar extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// change title
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
}
}
You'll have to create a custom layout to hold the title. It could be as simple as (called my_title.xml in this case):
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="foo bar"/>
In your AndroidManifest.xml file you just have to set the title for the app, which is what is going to be shown in the launcher icon. For your activity you don't need to set a title there.
The launcher actually shows android:label and android:icon for activity(ies) that declare
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
so application label is of no use.

Categories

Resources