Android Studio device preview without device frame has gone - android-studio

I got the design file from GoogleCloudPlatform/appengine-endpoints-helloendpoints-android project and made small changes. I changed the theme to a dark one. I don't know how this happened but I don't see the device controls or border/frame anymore. How can I reset it? Other design files are showing alright.
Finally found the reason. When I clicked on the black outer region of the preview window, a toolbar appears above it. First button on the left is called Toggle Viewport Render Mode. This brought back my device view.

Hhmm.. For me, whenever you design/designing your layout with the parent ScrollView Layout - it will not show the toolbar and the device frame.
To show your Scrollview layout to another layout you must include it. For example :
My ScrollView.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:fadeScrollbars="true"> </ScrollView>
Include it in your activity_main.xml or other .xml file.
<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"
android:fitsSystemWindows="true"
tools:context=".activities.ShareAppActivity">
<!--Include ActionBar/Toolbar-->
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
android:elevation="8dp"
android:minHeight="?attr/actionBarSize"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
<include
android:id="#+id/scrollview_layout"
**layout="#layout/scrollview"**
android:layout_height="match_parent"
android:layout_width="match_parent"/> </RelativeLayout>
Use the <include /> - tag and in the layout="#layout/YOUR_XML_SCROLLVIEW"
in xml layout you wish to display/include your scrollview.
You can now see your scrollview_layout with device frame and toolbar.

Click on preview window and press button 'F' on keyboard.

Related

How to place an imageview on the top in android Studio?

As it's shown in the android studio it looks perfectly fine but whenever i run the app on my cell phone it looks like this any idea ,please ?? the app on the phone and the android studio
You are using Relative Layout.
First thing I noticed in your code is that you used margin buttom in Image view which is top of the screen already.So I suggest to not use Margin Buttom in that because relative layout bydefault keep that image on top .So i give you some idea to fix it.
1.)Image is on top so and its Relative layout.
<ImageView
android:id="#id/image" (give any id here)
android:src=""
android:marginleft="" ("Add ur dimensions")
android:marginright=""
android:adjustviewbound="True"/>
2.)Next is Textview.
<TextView
android:id="#id/text"
android:text="Name:Lovely Hamester"
android:layout_below="#+id/image"
android:margintop=""/>
So the main thing i wanna point of is this "Id".Relative Layout directly cant put views line by line like Linear Layout,We need to give command to the view to where it need to place.So thats why we use "layout_below" which instruct Text view to stay after imageview.There are different commands also.Please check that,like "layout_rightof","layout_leftof".
Just give id to every view and command them in Relative layout.Your other code is perfect.Dont use Margin Bottom , because it kept space from bottom which looks perfect in the preview but every Mobile have different size so thats why its not shows perfect in your screen.
just move code imageview to the top
example
<RelativeLayout
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">
<ImageView
android:layout_width="200dp"
android:layout_height="600dp"
android:src="#drawable/test"/>
<ProgressBar
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/progressbar"
android:layout_centerInParent="true"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_notes"
android:layout_width="250dp"
android:layout_height="match_parent"
tools:listitem="#layout/item_note"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
android:src="#drawable/button"
app:borderWidth="1dp"
app:elevation="9dp"
android:backgroundTint="#FFFFFF"/>
</RelativeLayout>
The problem as i see it that you have a margin bottom override the layout align Parent Top so if you delete that and leave only :
android:layout_alignParentTop="true"
also it is redundant to call align right and left and bottom , align top will be enough.
i'm sure it will work.

ScrollView and text

How can I make my screen scroll?
How to use ScrollView? At my activity, in it there is a picture and the text, it is necessary for me that at movement downwards or upwards the screen scroll.
The ScrollView XML resource is fairly easy to use and powerful. Just wrap whatever content you want to be scrollable in the XML with the ScrollView tags and the rest will be taken care of for you. Simple example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:background="#color/colorPrimary"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/yourID"
android:layout_weight="1" />
...
</LinearLayout></ScrollView>
This example shows a LinearLayout filled with whatever you need (TextView in this case) that will scroll on screen when it has to.

Is there any typing option in android spinner?

As shown in the image, in Microsoft Word/Powerpoint the drop-down spinner of font size enables the users to type his own choice of font size in the spinner box in addition to manually selecting one from the drop-down list.
My question is if it is possible in Android to get the "self-typing" option likewise?
Here it is. I've solved it this way.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_light">
<Spinner
android:layout_width="match_parent"
android:layout_height="match_parent"></Spinner>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="12"
android:inputType="numberDecimal"
android:gravity="center"/>
</FrameLayout>

how to center an image horizontal and vertical inside an edit text in android xml

Something like this please.
I'm new to android, and I am trying to center the image and hint both vertical and horizontal. If anyone can help, I will really appreciate it.
You can't put ImageView inside of an EditText.
You can, instead, put EditView and TextView inside RelativeLayout and center them, like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="This is some edit text" />
<ImageView
android:id="#+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/com_facebook_button_icon_blue" />
</RelativeLayout>
Since we put EditText width and height to match RelativeLayout, it would appear as if the ImageView is inside of an EditText.
If you want to put text under image just add this line to TextView: android:layout_below="#+id/my_image_view".

This view is not constrained vertically. At runtime it will jump to the left unless you add a vertical constraint

New Layout editor in Android Studio 2.2 keeps showing this error on views like EditText and Buttons. kindly help.Also, any links that help in onboarding with the new constraint layout would be appreciated.
code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="#+id/activity_main"
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="com.set.email.MainActivity"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<TextView
android:text="To:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="4dp"
android:id="#+id/textTo"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="24dp"
android:id="#+id/editTo"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"/>
<EditText
android:layout_width="384dp"
android:layout_height="42dp"
android:inputType="textPersonName"
android:ems="10"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="94dp"
android:id="#+id/editSubject"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"/>
<EditText
android:layout_width="384dp"
android:layout_height="273dp"
android:inputType="textPersonName"
android:ems="10"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="179dp"
android:id="#+id/editMessage"
app:layout_constraintLeft_toLeftOf="#+id/activity_main"
tools:layout_constraintLeft_creator="50"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"/>
<Button
android:text="Send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="454dp"
android:id="#+id/btnSend"
app:layout_constraintLeft_toLeftOf="#+id/editMessage"
tools:layout_constraintLeft_creator="0"
app:layout_constraintRight_toRightOf="#+id/activity_main"
android:layout_marginEnd="16dp"
tools:layout_constraintRight_creator="0"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Medium"/>
</android.support.constraint.ConstraintLayout>
From Android Studio v3 and up, Infer Constraint was removed from the dropdown.
Use the magic wand icon in the toolbar menu above the design preview; there is the "Infer Constraints" button. Click on this button, this will automatically add some lines in the text field and the red line will be removed.
Constraint layout aims at reducing layout hierarchies and improves performance of layouts(technically, you don't have to make changes for different screen sizes,No overlapping, works like charm on a mobile as well as a tab with the same constraints).Here's how you get rid of the above error when you're using the new layout editor.
Click on the small circle and drag it to the left until the circle turns green,to add a left constraint(give a number, say x dp. Repeat it with the other sides and leave the bottom constraint blank if you have another view below it.
Edit: According to the developers site, Instead of adding constraints to every view as you place them in the layout, you can move each view into the positions you desire, and then click Infer Constraints to automatically create constraints. more here
Just copy this code to all components that you will drag.
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
example:
<TextView
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="To:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="4dp"
android:id="#+id/textTo"/>
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
This help me a lot
Go to the Design, right click on your Widget,
Constraint Layout >> Infer Constraints. You can observe that some code has been automatically added to your Text.
Follow these steps:
Right click on designing part > Constraint Layout > Infer Constraints
If Inferring the Constraints still gives you the error, just use this code:
app:layout_constraintBottom_toBottomOf="parent"
You need to drag the EditText from the edge of the layout and not just the other widget. You can also add constraints by just dragging the constraint point that surrounds the widget to the edge of the screen to add constraints as specified.
The modified code will look something similar to this:
app:layout_constraintLeft_toLeftOf="#+id/router_text"
app:layout_constraintTop_toTopOf="#+id/activity_main"
android:layout_marginTop="320dp"
app:layout_constraintBottom_toBottomOf="#+id/activity_main"
android:layout_marginBottom="16dp"
app:layout_constraintVertical_bias="0.29"
Go to the XML layout Text where the widget (button or other View) indicates error, focus the cursor there and press alt+enter and select missing constraints attributes.
for example I have this
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
use RelativeLayout layout like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
Simply switch to the Design View, and locate the concerned widget. Grab the one of the dots on the boundary of the widget and join it to an appropriate edge of the screen (or to a dot on some other widget).
For instance, if the widget is a toolbar, just grab the top-most dot and join it to the top-most edge of the phone screen as shown in the image.
You have to change androidx.constraintlayout.widget.ConstraintLayout to RelativeLayout.

Resources