Programmatic Theme Change Not Working - android

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.

Related

Android Splash Screen shifts

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.

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.

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>

how to delete the toolbar in android design

The red frame should not have in my code, but after I updated my Android Studio, it is showed in all of my APPS. please click and see the picture.
How to delete the red frame from my code? And it could not find in its design/text. The blue frame's begin code is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/activity_login_user_email_edt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="35dp"
android:autoText="true"
android:hint="e-mail" />
The Answer Skynet posted is correct,but this is the programatical way
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Full Screen and remove toolbar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
That is the theme your activity is extending from. You change it in the styles.xml. Then you need to update this setting in the Manifest.
You need to add this to styles.xml:
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="#style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Then in your AndroidManifest.xml for this Activity:
<activity
android:name=".YourActivity"
android:label="#string/title_activity_main"
android:screenOrientation="landscape"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
</activity>
The above will work if you are extending from AppCompatActivity which should look like:
public class YourActivity extends AppCompatActivity{}

Android: custom title style

I have a listactivity with iconified text which I'm giving a custom title. Here's my custom_style.xml:
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#222222</item>
</style>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">50dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
</resources>
Here's the layout for the title, window_title.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:paddingLeft="10dip"
android:background="#222222">
<ImageView
android:id="#+id/header"
android:src="#drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I set the new title style in onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// if it wasn't a listactivity, here I'd use
// setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
browseToRoot();
}
My problem is that the styled title bar appears at the correct size and with the correct background colour (taken from custom_style.xml) but eveything in window_title.xml is ignored. The rest of the listadapter still works fine. In a very similar question here: Custom title with image it says setFeatureInt must come before super.onCreate but either way my result is the same.
I've tried replacing the imageview with a nice simple textview, but the entire window_title.xml seems to be ignored. Any ideas where I'm going wrong? Many thanks, Baz.

Categories

Resources