Android Splash Screen shifts - android

I have an Android Splash Screen which renders a drawable. When it is opened via a cold start, I find that my asset simply shifts in an upward direction.
You can find the appropriate code below, all unnecessary code has been omitted.
Here's the slight shift:
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.show(this, R.style.SplashTheme);
super.onCreate(savedInstanceState);
}
res/drawable/background_splash.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<item android:drawable="#color/splash_background_color"/>
<item
android:gravity="center"
>
<bitmap
android:src="#drawable/x150"/>
</item>
</layer-list>
res/layout/launch_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/splash_background_color">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/background_splash"
/>
</FrameLayout>
res/values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
</resources>

I found the solution:
You should remove ImageView because you've already set splash via android:windowBackground.
Also remove android:background="#color/splash_background_color" from FrameLayout to make it transparent
Btw, you could delete res/layout/launch_screen.xml if you are not going to draw some layouts over your splash.
For Activity don't call setContentView()
For Fragment don't override onCreateView()
It's ok, Android doesn't require to set layout for them.

In styles.xml, replace:
<item name="android:windowBackground">#drawable/background_splash</item>
with
<item name="android:background">#drawable/background_splash</item>
Note the windowBackground -> background
That solves the issue for me.

Related

How to implement JavaCameraView on top of screen

So currently i am having troubles on dealing with my problems. What I want to achieve basically is to open a JavaCameraView while still be able to operate the phone normally.
I have managed to follow the guide here and used this in my styles.xml file
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
What this xml has achieved is that it has successfully managed to make the background transparent, but I can't click anything on the background.
If I want to be able to click background items (operate my phone normally), while keeping the JavaCameraView on top, how can i achieve that?
Here is my onCreate() code if needed.
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
mOpenCvCameraView = (JavaCameraView) findViewById(R.id.tutorial1_activity_java_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.setZOrderOnTop(true);
SurfaceHolder CameraViewHolder = mOpenCvCameraView.getHolder();
CameraViewHolder.setFormat(PixelFormat.TRANSPARENT);
}
activity_main.xml
[<!-- language: xml -->]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.os10.testproject.MainActivity">
<org.opencv.android.JavaCameraView
android:id="#+id/tutorial1_activity_java_surface_view"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="top"
android:screenOrientation="portrait"
android:visibility="visible"
opencv:camera_id="front"
opencv:show_fps="true" />
</RelativeLayout>
Many thanks.
So i found out that what i need is a floating window.
I will leave a link here which helps me in solving my problem to help anyone having the same problem as mine.
Cheers.

Programmatic Theme Change Not Working

My app loads in a bit of data to start, it takes a couple of seconds so I would like a Splash Screen to display for this time.
All I can see to make sure I do is use setTheme before the super.onCreate, which I am doing, but as it loads the MainActivity the background is the splash screen and the actionbar has the splash screen image squashed into it.
Here is my styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:background">#drawable/background_portrait</item>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:background">#drawable/splashscreen</item>
</style>
In my AndroidManifest.xml I have android:theme="#style/SplashTheme"
SplashScreen.java runs all the code that takes time, then switches to MainActivity.java where I have -
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
What am I missing? It also seems the splashscreen background is now a background for other things, like some TextViews and the action bar?
Thanks for any help offered.
EDIT Also, the code in the splash screen xml doesn't run. It just loads the background and on completion, loads the Main Activity?
The SplashScreen.java has -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Log.i("Splash Screen", "loading");
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// code that takes time
The activity_splash_screen.xml is -
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splashscreen"
tools:context="com.androidandyuk.bikersbestfriend.SplashScreen">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="32dp"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"></com.google.android.gms.ads.AdView>
</FrameLayout>
Don't use android:background in a theme. Anything that doesn't have a background will inherit this one.
You meant to set android:windowBackground.

Couldn't change an activity into an AlertDialog

I've tried many themes to change the activity into an alert dialog in vain. I remember accomplishing it before but unable to do it right now
here's the App's theme:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/background</item>
</style>
here's the activity, I'm trying to change into an alert dialog:
Real activity contains more than this(buttons, linear layout, text views).
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.famiddle.android.thechanakyanitiplus.NotificationCenter">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/notification_center_title"
android:gravity="center_horizontal"
android:textColor="#color/colorAccent"
android:layout_marginTop="8dp" />
</ScrollView>
It's java file(NotificationCenter.java)
public class NotificationCenter extends AppCompatActivity { #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// To remove status bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_notification_center);}}
the button that will start this activity:
LinearLayout notificationManager = (LinearLayout) findViewById(R.id.notification_manager);
notificationManager.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), NotificationCenter.class));
}
});
Finally, AndroidManifest file:
<activity
android:name=".NotificationCenter"
android:theme="#style/Theme.AppCompat.Dialog"
android:excludeFromRecents="true">
</activity>
I've tried different themes mentioned here Android Activity as a dialog, but whenever I click the button, It kinda crashes the app(restart it).
It's pretty simple if you ask me. Try this theme and notice the parent theme:
Theme.AppCompat.Light.Dialog. That's what is needed to turn an activity into an AlertDialog. Remove "Light" if you need a dark theme. I've also added some more "items" that matches your app's theme. I hope this will help.
<!-- NOTIFICATION MANAGER THEME -->
<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/background</item>
</style>

Changing the status bar color for Android 5.0

I've read a bunch of different posts regarding this issue but have not found anything that works. I want my app to be compatible with Android 5.0 and older versions.
For Android 5.0 I am trying to implement the status bar color change. I know this won't work for older versions though. But when I run my app on the GenyMotion VM supporting 5.0, the status bar color doesn't change to the colorPrimaryDark. It also doesn't change color in the xml preview section of Android Studio.
Here are the relevant files:
res\values\styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
</style>
</resources>
res\values-v21\styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:colorPrimary">#color/primaryColor</item>
<item name="android:colorPrimaryDark">#color/primaryColorDark</item>
<item name="android:colorAccent">#color/accentColor</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:statusBarColor">#color/primaryColorDark</item>
</style>
</resources>
res\layout\toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryColor"
>
</android.support.v7.widget.Toolbar>
res\layout\activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/tBar"
layout="#layout/toolbar"></include>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity
{
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.tBar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Login");
}
...
...
}
Possible issues:
I think I may have added some unnecessary items in res\values-v21\styles.xml.
Plus I thought I was supposed to make the parent = "#android:style/Theme.Material.Light.DarkActionBar" but other tutorials have it as Base.AppTheme. Also possible that I need to add more code in MainActivity.java
Can someone please help? This whole topic has me very confused.
Thanks
You could also try changing the status bar color programmatically: https://developer.android.com/reference/android/view/Window.html#setStatusBarColor(int)
Very simple program. you can do it programatically
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.BLUE);
}
maybe you can find very stupid but in my code i had that experience and your v-21 style.xml should be like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
</style>
</resources>
also you have to didn't define status bar color in lollipop automatically its created from Dark Primary Color. You can try this code
It'll not change status bar color if you have
<item name="android:windowTranslucentStatus">true</item>
removing this line from your app theme is the only i have found yet to change status bar color t runtime using setStausbarColor().
And still if you want to change color with android:windowTranslucentStatus then this tutorial i found useful so far link.

Applying Checkbox Styles in Android PreferenceActivity

I am writing an application that SDK 10+. I would like all of my check boxes in my Activities as well as my PreferenceActivity to look the same. I created a Theme that has a Theme.Black parent. I created a child style of android:Widget.CompoundButton.CheckBox and set the android:checkboxStyle to that child theme. I have applied my theme across the entire app in the application manifest element with:
android:theme="#style/AppTheme"
When I look at the main Activity, the check box style has been applied as expected, but the checkbox in the PreferenceActivity does not have the style. My questions are:
Is it possible to style minor parts of preferences without having to specify an entire new layout for each one?
More generally, I would like to do this for other controls that show up in the preferences, like toggles or switches. Is that possible? (My guess is that the solution to one will pretty much be able to be applied to those particular issues as well)
I know there is an issue for nested preferences, but this question is applicable to Preferences at the top level. Any help is greatly appreciated. I have been banging my head against a wall on this one for a couple hours now...
Here is what I have so far:
prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="checkbox1"
android:summary="This is a themed checkbox preference"
android:title="Checkbox 1" />
</PreferenceScreen>
ThemedPreferenceActivity.java
public class ThemedPreferencesActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:text="CheckBox"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" style="#style/AppTheme.Preference.Checkbox" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
style.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="android:Theme.Black">
<item name="android:checkboxStyle">#style/AppTheme.Preference.Checkbox</item>
</style>
<style name="AppTheme.Preference.Checkbox" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">#drawable/pref_checkbox</item>
<item name="android:background">#FF0000</item>
</style>
</resources>
pref_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="#android:drawable/ic_menu_call" />
<item android:state_checked="false" android:drawable="#android:drawable/ic_menu_camera" />
</selector>
Looks like the issue was with the line:
<style name="AppTheme.Preference.Checkbox" parent="android:Widget.CompoundButton.CheckBox">
<item name="android:button">#drawable/pref_checkbox</item>
<item name="android:background">#FF0000</item> <---- RIGHT HERE
</style>
Looks like specifying that was causing the check box to misbehave. Turns out that I didn't need that anyway (and in fact, realized I would do it in the "ListView" style anyway if I wanted it) so, problem solved.

Categories

Resources