Relative Layout is not allowed inside Toolbar - android

I'm facing strange warning, trying to customize the Toolbar appearance. I put RelativeLayout inside android.support.v7.widget.Toolbar to reorganize Toolbar. Everything works and appears as expected on both emulator and device. The only problem is the annoying warning that appears in .xml file as seen below:
The question is what is the actual problem, how can it harm my application and how can I solve this warning. As I know Toolbar behaves as ViewGroup, why RelativeLayout is not allowed to be it's child?
Thanks.

Toolbar extends from ViewGroup and RelativeLayout's too.
I've tried that on Android Studio 1.5.1 and i couldn't see that problem.
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:id="#+id/toolBarTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textSize="18dp">
</RelativeLayout>
</android.support.v7.widget.Toolbar>
You should be able to update your Android Studio to the last version or checking if you are using outdated Platform, then you need to just update that Platform to the last version.
And then it won't show up.or perhaps it will fixed in the next update.

Related

AndroidStudio ConstrainLayout not working

I am beginner in android studio and having headache with this if someone could help me am i missing something?.... I have problem with Constraint layout, the problem is i put some element(ImageView, Button it does not mater) in layout and constraint it to parent but when I run it in emulator it just is not showing. also it is not showing me those buttons under elements connect BaseLine and remove Constrains for that element.
"screenShot":
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="343dp"
android:layout_height="118dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_c`enter code here`onstraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#tools:sample/backgrounds/scenic[7]" />
</android.support.constraint.ConstraintLayout>
when i run it in emulator it just is not showing
The problem is here :
tools:srcCompat="#tools:sample/backgrounds/scenic[7]"
You need to understand what tools is, so I recommend to you to read a little bit the documentation.
Just a note of it :
It's a namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
Instead you have to use to show it on the emulator.
app:srcCompat="#drawable/your_drawable"
Note: remember to add : xmlns:app="http://schemas.android.com/apk/res-auto"
I found the solution.it's difrent in new Android Studio 3.4, You have to Right click on element and option Show Base Line is there now.

Drag/Drop components are not visible in preview in android studio

I am a beginner in android development. I have created a android project. But in the preview section, I am not able to see the components. But components are visible when i run the same app on emulator i am able to see the components. I am missing some setting here? XML is given below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I've had have also this problem and solved it by only selecting another "Theme" cause
the default theme appTheme doesn't show added items.
If you i.e. select Transulcent, any item becomes visible ;-)
cheers
Oliwan
Maybe sound a weird answer, but self-tested this.
Change the XML file's (maybe activity_x.xml) layout's root tag android.support.constraint.ConstraintLayout to LinearLayout, then preview will start working again (as happened in my case). [If not, then perform force refresh (R)]
Then switch back to old one, I mean: Change the XML file's (maybe activity_x.xml) layout's root tag LinearLayout to android.support.constraint.ConstraintLayout. And force a refresh.
This does seem to be a bug in Android Studio. When it showed up for me, I fixed it by making a trivial change in the app build.gradle (change version code), do a gradle sync, then change back and sync again.
After that, the design view worked again.

ViewPager inside CoordinatorLayout covers other views on API >= 21

I am using the new CoordinatorLayout from Google's new design support library.
I have the following layout:
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.design.widget.CollapsingToolbarLayout
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<android.support.design.widget.TabLayout />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager" />
</LinearLayout>
<com.getbase.floatingactionbutton.FloatingActionsMenu
app:layout_anchor="#id/viewPager"
app:layout_anchorGravity="bottom|right|end"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
>
<com.getbase.floatingactionbutton.FloatingActionButton />
</com.getbase.floatingactionbutton.FloatingActionsMenu>
</android.support.design.widget.CoordinatorLayout>
The problem is that the ViewPager always overlays on top of the FloatingActionMenu on API >= 21, is there a fix for this? I tried using view.bringToFront() to fix this, but no luck. I also moved the Views around as well, still no luck. Things I tried:
Moving the FABMenu inside AppBarLayout
Moving the FABMenu inside LinearLayout
No luck.
Has anyone experienced anything similar?
This turned out to be a simple elevation issue, I was setting the elevation as android:elevation=".." and this was a lower elevation than another view, thus covering the FloatingActionButtonMenu. This only occurs on API >= 21.
although you seemed to have found the answer yourself, there is an absolutely amazing resource here: https://github.com/chrisbanes/cheesesquare
This is an implementation of viewpager inside a coordinator layout that uses the official google design support library, along with all the other goodies supported by this library (including an extremely easy to use Navigation side panel).
Why am I sharing this? Because the code is clean, uses an official latest library and is backward compatible. I found it very easy to use this as a base to my application and I am already getting good feedback on the overall implementation of the design which closely follows the material guidelines.
The example is written by a person from the official android team so it is also reliable that way!
Hope you find this useful (although not totally a direct answer to your question - but related nonetheless).
I am a new user who can't even post comments due to low rep so awarding the rep will be really helpful!
Thanks :)

Android toolbar Alignment issue

Even though there's no subtitle in my Toolbar, the title is shifted to top. How can I align it with navigation icon?
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:navigationIcon="#drawable/abc_ic_ab_back_mtrl_am_alpha"
android:gravity="top"
app:title="Hello title"
android:background="#drawable/citylook"/>
You should assing a minHeight to your Toolbar.
<android.support.v7.widget.Toolbar
android:minHeight="?attr/actionBarSize">
I faced a similar issue recently and managed to find the solution. I hope that'll fix yours too. I just paste the link here 'cause I'm lazy: Android Toolbar: title and overflow menus aligment issue on 5.0 only
Basically, if you are launching the application on a Lollipop device, there is no need to set an Activity with no ActionBar and replace it with a Toolbar: you have to code different themes and layouts for pre-Lollipop and Lollipop devices.
Let me now if that works for you as well!

Total beginner; need help properly setting up my Android 5.0 Toolbar

I do have a toolbar right now, but I don't really understand a lot of the things that I needed to add and copypaste from tutorials and other questions to get it to work. Also, my toolbar doesn't display an elevation even though I set android:elevation to 9dp.
Similar to the Google I/O app, I've set up a resource in /layout for my toolbar. I copied the name and named it "toolbar_actionbar_with_headerbar". Please excuse that it doesn't make any sense.
This is the content:
toolbar_actionbar_with_headerbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="128dp">
<Toolbar
android:id="#+id/toolbar"
android:elevation="9dp"
android:layout_height="128dp"
android:layout_width="match_parent"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:gravity="bottom" />
</LinearLayout>
Now, as it's apparently supposed to be done, I am applying my toolbar to activities by including them in their /layout XML-file. Works fine.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
[...]
<include layout="#layout/toolbar_actionbar_with_headerbar" />
[...]
</RelativeLayout>
However, this is my first question. When I did that initially, my toolbar was cut off at the sides like all other content of the activity, according to the margins set at the beginning of the xml. Right now, I simply deleted those, and add margins for every text field and other things on the activity. There probably is a better way?
The second question is: Why doesn't my elevation work? I've even tried setting it through code onCreate, but that doesn't work either. And lastly, what would I have to do to properly support devices pre-Level21?
Thanks a lot.
You should read this post: appcompat-v21
Take care when you copy some code text, there are some ” that are wrong, and you should change to ".
About elevation (you can read it in the post too), it only works with lollipop.

Categories

Resources