Basic toolbar crashes app - java

I created a basic tool bar off the android website and it is crashing with a code it gives to put in the onCreate Section. the only widget in the program so far is that and it crashed with the .java code
home.java
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar my_toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(my_toolbar);
}
}
xml
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>

Make sure the theme of your activity Home does not have an ActionBar already. Check the manifest file to find out which theme is being used.
Also check the file styles.xml and make sure the the theme used by your activity does not have an ActionBar. Use something like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

In your styles.xml put below code..
<!-- Base application theme. -->
<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>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and in your AndroidManifest.xml write the theme as below
<activity android:name=".Home"
android:theme="#style/AppTheme.NoActionBar"></activity>

Related

Make the image background in whole background [duplicate]

This question already has answers here:
Android Completely transparent Status Bar?
(38 answers)
Closed 3 years ago.
I want to make my login activity with background off whole activity including clock of the phone itself Like this
This is my code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bgd2"
tools:context=".Signin">
But my output is this
I need to remove the orange bar and make it the same image like the first image.
How can I make it according to this style:
<!-- 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="colorControlNormal">#FFF</item>
<item name="colorControlActivated">#FFF</item>
</style>
you need to remove the ActionBar color using NoActionBar style.
there are two options.
put the windowNoTitle style in style.xml
example :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="windowNoTitle">true</item>
</style>
Put this code on the Activity.
requestWindowFeature(Window.FEATURE_NO_TITLE);
getSupportActionBar().hide();
addition :
if you also hide the navigation buttons then use FullScreen option in the style XML.
<item name="android:windowFullscreen">true</item>

open activity from another activity after some seconds

I try to open a new activity from another one after some seconds, I used this code,
but it's not working (first activity runs but after some seconds I have an error)
public class WelcomeActivity extends AppCompatActivity {
private static int TIME_OUT = 4000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
//finish();
}
}, TIME_OUT);
}
}
This is a stacktrace:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{________}: java.lang.IllegalStateException: This
Activity already has an action bar supplied by the window decor. Do
not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar
to false in your theme to use a Toolbar instead
The problem is not with this code. According to your exception the problem is in the MainActivity class. I think your are trying to create custom action bar. But the error is occurring due to your current theme already have an action bar. So you need customize your theme for that class.
In styles.xml create a new theme.
<style name="Theme.FullScreen" parent="AppTheme.NoActionBar">
<item name="android:windowNoTitle">false</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowActionBar">false</item>
</style>
In the AndroidManifest.xml change the theme of your view to your customized theme.
<activity
android:name=".MainActivity "
android:theme="#style/Theme.FullScreen" />
This link will help you.
make
<item name="windowActionBar">false</item> in your style.xml
Example:
<!-- 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:windowAnimationStyle">#style/AnimationActivity</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

Splash screen still exist in next activity android

I created a splash screen due to this tutorial: Splash Screen right way
But I have a problem that the splash screen always on display when I create new activity
It just cover the whole activity layout, is there anyway to remove it? I'm new to Android. I dont know how to search this problem on Google. Please help.
IMO, this is the best tutorial for splash screens
create a drawable background_splash
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/gray"/>
<item>
<bitmap
android:gravity="center"
android:src="#mipmap/ic_launcher"/>
</item>
add theme
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
add to you manifiest
<activity
android:name=".SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
add splash activity that forwards to your main activity
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
I think you should replace your main application with splash theme. So Please separate your main application theme and your splash 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>
</style>
// Splash Theme
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">#drawable/splash</item>
</style>
Change Theme in XML

Illegal state exception. Activity unable to start because of toolbar

I am trying to add an actionbar similar to soundclouds. I am following the instructions on how to customize the action bar on the android developer website. The error seems to be that the action bar is already being supplied but I am trying to create another one.
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
Here is onCreate():
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
addBirthdayFragment();
Here is the layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".view.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</RelativeLayout>
I have tried to just remove the xml code and java code for the toolbar, in the layout and onCreate respectively, however this is not working; the same error still persists. It must be something to do with the theme I am using, but I do not know what exactly. It confuses me because the theme is the default one so I did not expect this.
Here is my theme:
<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>
</style>
Side question:
I am trying to achieve the look of the soundcloud action bar, which appears to be tabbed. Is this the way I should go if I want this result, or is there some other widget that is used for tabbed action bars?
Thank you for the help.
Android is complaining because you never told it that you will use a Toolbar instead of the Android-provided ActionBar. To let Android know about this, you need to add these to your style:
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
If you'd like to use the Android ActionBar sometimes and a Toolbar of yours some other time, you can add the above two lines to a new style extending your AppTheme, and using that on AndroidManifest.xml:
<!-- Use this one in the Manifest when you want to use the native ActionBar -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- your styling here -->
</style>
<!-- Use this one when you want to implement your own Toolbar -->
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Your code is:
<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>
</style>
The line parent="Theme.AppCompat.Light.DarkActionBar" says dark action bar. you need to change it to NoActionBar.
<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>
</style>

Android Lollipop theme compatibility All API Version [ 7- 21]

Hi I'm using Lollipop theme for all other API version, but i don't use Lollipop toolbar and style actions. I have created a ActionBar like custom ActionBar layout like left and right corner Buttons and title is in center.
Here the my the theme but it doesnot work. can you update this issue. also tell me the style for "Theme.AppCompat.Light"
// Style code
res/values/theme.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimary</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
values-v21/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
</resources>
// Activity code like.
public class CreateAccountActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// To set the custom title with Button
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.create_account_screen_1);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_layout_with_two_button);
((TextView) findViewById(R.id.myTitle)).setText(R.string.create_acc);
// ((Button) findViewById(R.id.back_button)).setText(R.string.login);
((Button) findViewById(R.id.right_button))
.setVisibility(View.INVISIBLE);
}

Categories