Android Studio designer preview different to phone - android

I'm new to android studio and developing on the android platform. I'm having a problem where the Android Studio designer preview doesn't show exactly what is on my phone. It shows an extra Toolbar widget at the top of the screen. I've had a look in the XML file for the activity and cannot find where it is coming from. It would be much appreciated if one of you could help.
Designer: http://imgur.com/A4TZFWb
Phone: http://imgur.com/kxP4CFn
This is a snippet from the XML file:
<android.support.design.widget.CoordinatorLayout 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="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.tomnulty.helloworld.SecondActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
And this is a snippet from the AndroidManifest file:
<activity
android:name=".SecondActivity"
android:label="#string/hello_world"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.SECOND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

As Pztar said, "Go into your styles.xml and under the AppTheme style set the parent as Theme.AppCompat.Light.NoActionBar"
Although my theme was set when I declared the activity in AndroidManifest.xml, this did not change the preview. This had to be done by changing the theme in the UI of Android Studio or the styles.xml file.
Styles.xml:
<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>

Using Android Studio 3.0 stable, you can change to your custom theme in the preview panel. To remind, the design time is not the same to the runtime if your activity in the AndroidManifest.xml does not implement the same one.

Related

Spacing on AppBarLayout

Spacing on AppBarLayout and incomplete app bar title
on geny motion virtual mobile is ok
on all my mobiles problem
problem screenshot:
on virtual mobile ok screenshot:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
app:tabMode="fixed"
android:layoutDirection="rtl"
app:tabGravity="fill"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</LinearLayout>
Try changing the style of the main activity (the one causing the issues) in the manifest, that should do it. You can check the default values when you create a new project using the "Basic Activity" template.
In this case since your layout contains a toolbar you should use the default theme (or a custom one to the same effect): android:theme="#style/AppTheme.NoActionBar". Notice that I´m using the default values of the template. The style you need should be something like this:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
This would be the manifest entry:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
BTW, just in case you havent done it, consider testing with the same API level in all devices (virtual and physical) to get consistent test results.
Hope it helps!

Duplicating items with android:

Trying to setup my app to use Material design:
<style name="AppTheme" parent="android:Theme.Material.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:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="android:colorAccent">#color/colorAccent</item>
If items with android: are removed - the colors are not getting applied.
If items without android: are removed - app crashes:
Unable to start activity ComponentInfo{com.foo.bar/com.foo.bar.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.support.design.widget.TabLayout
Questions:
Why do I have to duplicate those?
Should I duplicate all styles like that?
EDIT:
The Manifest file is:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foo.bar">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
EDIT2
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/tl_periods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMinWidth="100dp"
app:tabMode="scrollable"/>
The theme of your application is not a descendant of AppCompat theme, whereas your TabLayout is from support packages.
You either have to stick with AppCompat theme and views, or entirely be support packages independent, otherwise such nasty issues will rise.

Android remove action bar

I'm trying to disable the action bar for some of my activities, but no matter what I try I can't seem to remove it.
In my styles.xml I have:
<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>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
And in my manifest.xml I have:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".NewActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
</application>
My activity layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_new"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
My app_bar_new layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="net.binarysea.sensorload.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_new" />
</android.support.design.widget.CoordinatorLayout>
I want the action bar to display on activities apart from one (NewActivity). I use intents to call up NewActivity, at which point I thought it would load up the AppTheme.NoActionBar style, but the bar still appears on the screen
I'm using the navigation drawer activity in android studio if that makes a difference
EDIT: I've tried all the suggesting adding Adding Theme.AppCompat.Light.NoActionBar, but that only makes my action bar white and doesn't actually remove it. I tested this in a new android studio project, created a navigation drawer activity, and add the theme setting to my styles.xml. I still get the same problem
Try this...
Just add parent tag for your style.
Styles.xml
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
AndroidManifest.xml
.......
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".NewActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
</application>
........
Try and drop the following into your manifest
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
Follow this Link- https://developer.android.com/training/system-ui/status
Hide the Status Bar on Android 4.1 and Higher-Kotlin
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
supportActionBar?.hide()
Better use it inside onResume().
Another Solution
https://stackoverflow.com/a/28125711/14366835
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
setContentView(R.layout.activity_main);
}}
Use like this in styles.xml
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/drawer_bg</item>
</style>
Create seperate layout for toolbar
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.v7.widget.Toolbar>
In any activity, access this toolbar by using below code.
Toolbar toolbar; //declaration
In onCreate() after setContentView() use this
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
You can try this in your onCreate() of NewActivity to hide the action bar for particular Activity
ActionBar actionBar = getSupportActionBar();
actionBar.hide();

Rendering Issue in Android Studio

I am new to Android Studio.And I came accross this problem under activity_main.xml tab . So, How to fix this.I have installed the android studio 1.4.1 bundle offline. Also I can't drag and drop widgets to phone in activity_main.xml.But I can drag and drop in content_main.xml
Rendering Problems
The following classes could not be instantiated:
- android.support.design.widget.CoordinatorLayout (Open Class, Show Exception, Clear Cache)
- android.support.design.widget.AppBarLayout (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE
Exception Details
java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:178)
at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:172)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
Copy stack to clipboard
Here is my activity_main.xml in text
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Since your MainActivity extends AppCompatActivity, its theme most be from AppCompat. To fix this issue, correct the following:
Open the file app/res/values/styles.xml
Change the following lines in the styles.xml file to look like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
and
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />.
They all need to inherit from AppCompat for your chosen layout. See the default working styles.xml file below. You can copy and paste it into your styles.xml file.
<resources>
<!-- 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>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
If you are still having issues, go to the app/manifests/AndroidManifest.xml file and make sure that the value for the theme looks like this android:theme="#style/AppTheme.NoActionBar" or this android:theme="#style/AppTheme" this way they are inheriting from the AppCompat themes as listed above.
See here as well: You need to use a Theme.AppCompat theme (or descendant) with this activity
The parent of AppTheme need is child or Theme.AppCompat . Pls check style: #style/AppTheme in your code
Add android:theme="#style/Theme.AppCompat.Light" inside the CoordinatorLayout
I had the same issues and tried many things but what worked for me was simply updating the Android Studio version from 3.1.1 to 3.2 and it solved the exact same issues. Hope this helps.

Material Drawer In Eclipse Isse

i want to add material drawer in android app ...so we need we have to include a toolbar ..
but when we include toolbar i got error
my Code for activitymain_xml is
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<include layout="#layout/toolbar"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#004869"
android:textSize="20sp"
android:dividerHeight="1dp"
android:background="#fff"
android:textColor="#f9f5f5"
android:paddingLeft="15sp"
android:paddingRight="15sp"
android:paddingTop="10dp"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
code for toolbar is
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#drawable/toolbar_background"/>
code for include toolbar is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
My Style.xml is
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
My Mainifiest file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lifegoal.eshop"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.lifegoal.eshop.Splash_Screen_Activity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</manifest>
But I got error like
04-28 12:53:05.632: E/AndroidRuntime(3181): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lifegoal.eshop/com.lifegoal.eshop.Home_Screen_Activity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
04-28 12:53:05.632: E/AndroidRuntime(3181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
04-28 12:53:05.632: E/AndroidRuntime(3181): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
Guys I Need ur help ..thx in advance
Try changing your style.xml like below It works for me:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
According to developers guide we have to use NoActionBar theme to use toolbar as a action bar.
Also extends your activity with ActionBarActivity.
Edit
Use AppCompatActivity instead of ActionBarActivity, It is deprecated.
Try to do following :-
Remove this line from your toolbar.xml:-
android:id="#+id/toolbar"
And add this line in your activitymain_xml file:-
android:id="#+id/toolbar"
And extend parent theme Theme.AppCompat.Light.NoActionBar
AppCompat does not support the current theme features
Seems like, your style of Theme is wrong.
Your style.xml should contain these lines:
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
P.S. : Your Theme should extend Theme.AppCompat.Light.NoActionBar instead of Theme.AppCompat.Light

Categories

Resources