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.
Related
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.
Can anybody how to remove this parent NavDrawer overlay showing whilst designing layout? (Screen 1).
The overlay is removed when running in actual device (Screen 2).
Because of this I can't design child elements right there in the studio.
Any help would be great!
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Always Visible."
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
When you see that gray block with the tag name in it, that means that Android Studio can't resolve the class from the tag in your layout, so it doesn't know what to preview. Often this will happen if you have a typo in your layout file, but then you wouldn't see it working when you actually run the app.
For me, I can make this happen quite easily by using the old support library versions of widgets, since I have AndroidX enabled in my project. For example, just pasting your code in to my IDE gives me the same gray box, but changing your tags to use AndroidX fixes it (android.support.v4.widget.DrawerLayout -> androidx.drawerlayout.widget.DrawerLayout).
Chances are good that your Android Studio just needs a kick in the pants.
I suggest doing a Gradle Sync (File -> Sync Project with Gradle Files) and, if that doesn't work, an Invalidate + Restart (File -> Invalidate Caches / Restart...).
I am quite familiar with RelativeLayout of Android. However, since I have up updated Android Studio(2.3), ConstraintLayout is set as the default layout of all the new projects.
The problem is:
I am not able to delete the ConstraintLayout.
How can I set it (RelativeLayout) as default so that I don't have to tweak around.
I was able to learn about the tweak around from this answer: How to switch from the default ConstraintLayout to RelativeLayout in Android Studio 2.3.3
Also please provide me any helpful information about ConstraintLayout, ie. why use it over relative layout, tutorial/help text regarding ConstraintLayout so that I can learn it.
To change layout go to your xml code and change your root layout to desired layout, if it shows error, post error message here.
This part is from my other answer:
https://stackoverflow.com/a/42779569/3870382
To change defalut layout when creating an Activity, you can modify default template layout file in Android Studio resources, go to your Android Studio folder and go to following folders path to it:
\plugins\android\lib\templates\activities\common\root\res\layout
Edit file simple.xml.ftl and change layout to your choice, notice that some layouts require additional elements (e. g. LinearLayout needs android:orientation), save file and create activity in Android Studio, you should get an Activity with xml layout that you changed.
Mine looks like this (I had 2.2.3 in that time so I have still RelativeLayout)
(You should see your constrainLayout in this file, so change it to relativeLayout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<#if hasAppBar && appBarLayoutName??>
xmlns:app="http://schemas.android.com/apk/res-auto"
</#if>
android:id="#+id/${simpleLayoutName}"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
<#if hasAppBar && appBarLayoutName??>
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/${appBarLayoutName}"
</#if>
tools:context="${relativePackage}.${activityClass}">
<#if isNewProject!false>
<TextView
<#if includeCppSupport!false>
android:id="#+id/sample_text"
</#if>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</#if>
</RelativeLayout>
You may have already tried this, but if not. Go to the top menu of your Android Studio with a project open. "Window" tab, Then pull-down to "Store Current Layout as default" and "Restore to default layout". If the default is not what you like, download a layout you would like as default and set it as your default layout.
I pretty much always use RelativeLayout or LinearLayout as well.. Unfortunately I am not versed on ConstraintLayout.
Hope this helps! :)
I am answering this for android studio 2.3.1.
One of the easiest way to set RelativeLayout as main layout instead of the constraint layout is going to text mode and editing the xml file as followed
Change this line
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
To
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
And do check your ending tag changes to this
</android.widget.RelativeLayout>
Also go ahead and delete this line if its being shown in grey.
xmlns:app="http://schemas.android.com/apk/res-auto"
Hope it solves the issue.
Heyho mates,
currently I am learning the new layout editor of Android Studio with the new ConstraintLayout.
By the way, I hate it.
But I got the issue, that if I want to specify a layout_height with #dimen, it gets replaced with a dp value instead.
Someone else got this issue?
Android Studio version 2.2.2 & 2.2.3 same issue.
Newest gradle version.
Thanks in advance guys!
Edit :
Code example :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="#dimen/imageViewHeight"
app:srcCompat="#drawable/accept"
android:id="#+id/imageView"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
Also happens outside scrollview.
Turns out this is a bug and will be fixed in Android Studio 2.3.
I think the reason behind this is, that ConstraintLayout as a WYSIWYG layout is inevitably meant to be edited/manipulated in the visual editor only.
Therefore keeping references to dimens in your dimens.xml would mean that as soon as you change something, move some elements around, these would not be relevant anymore and are hence replaced with "actual current values".
Also as mentioned in the docs:
All margins offered by the tool are factors of 8dp to help your views align to Material Design's 8dp square grid recommendations.
I have recently started learning the new ConstraintLayout in Android Studio 2.2 and noticed that when I add simplest of the views, the layout editor automatically generates some absolute coordinates. Here is a sample XML:
<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:id="#+id/activity_portfolio"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.Activity"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<TextView
android:text="#string/creator_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="246dp"
tools:layout_editor_absoluteY="479dp"
android:id="#+id/first_textview"
app:layout_constraintRight_toRightOf="#+id/activity"
android:layout_marginEnd="16dp"
tools:layout_constraintRight_creator="0"
app:layout_constraintBottom_toBottomOf="#+id/activity"
android:layout_marginBottom="16dp"
tools:layout_constraintBottom_creator="0" />
</android.support.constraint.ConstraintLayout>
Notice the absolutes like 81dp, 246dp, 479dp... I tried to manually delete these, but when I go back to the "Design" tab and come back to the "Text" tab, these regenerate. Now, I have three questions:
Is there a way to tell Android Studio to not generate these?
Should I manually place them in dimens.xml?
Would these absolutes cause some layout problems in other devices?
You'll note that all of the absolute values are in the tools namespace - this means they are not compiled into your app, nor used in anything but in the tools (and in this case, the visual editor). They are simply to ensure that switching from the Design to Text tab is always consistent, with the underlying files remaining stable.
Is there a way to tell Android Studio to not generate these?
No.
Should I manually place them in dimens.xml?
These are only useful for the tools and therefore should not be added to a separate dimens.xml file that would be included in your final APK.
Would these absolutes cause some layout problems in other devices?
No, they are only used by the tools.
I'm not sure your original question contains your entire layout, as it references a widget with an id of #+id/activity, so the issue might lie elsewhere in your layout.
Ensure that no widget that exists within a ConstraintLayout has a layout_width or layout_height of match_parent.
MATCH_PARENT is not supported for widgets contained in a ConstraintLayout, though similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
Source
If you use match_parent, Android Studio will generate these absolute values, as well as replacing match_parent with an absolute dimension.
Based on the layout you posted, your TextView probably had a layout_width or layout_height of match_parent before Android Studio replaced it.
You should replace android:layout_width="match_parent" with
android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndtOf="parent"
And android:layout_height="match_parent" with
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomtOf="parent"
In your specific layout, you probably want something like this:
<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:id="#+id/activity_portfolio"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.Activity">
<TextView
android:text="#string/creator_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/first_textview"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/activity"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp" />
</android.support.constraint.ConstraintLayout>
As a side notes to given answers, you can use the Magic Wand Icon in the toolbar menu above the design preview. Click on Infer Constraints button, this will automatically add some lines in the text field and the tools one will be converted to constrained.
Please see below picture :