I'm beginner in android development and often have received the strange results. Below is markup where I expect to see red and green sequential rectangles. But really I see only green area:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="200dp"
android:layout_height="100dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/first_constrain"
android:layout_width="100dp"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#FF0000" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/second_constrain"
android:layout_width="100dp"
android:layout_height="match_parent"
app:layout_constraintStart_toEndOf="#+id/first_constrain"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#00FF00">
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Please explain what is wrong in my code.
That is because of the parent FrameLayout - FrameLayout arranges elements (children) on top of each other.
You need to change the parent layout to other layouts e.g. Linear or ConstraintLayout Layouts - something like this.
<androidx.constraintlayout.widget.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="200dp"
android:layout_height="100dp"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/first_constrain"
android:layout_width="100dp"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#FF0000" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/second_constrain"
android:layout_width="100dp"
android:layout_height="match_parent"
app:layout_constraintStart_toEndOf="#+id/first_constrain"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#00FF00"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Related
I am constructing a UI that has multiple of the same fragment displayed on the screen at a time. 2 fragmentContainerViews are rotated 90 degrees and 1 is normal. The on not rotated fills the container but the 2 that are rotated don't fill the container vertically.
This is what the display looks like when ran:
activity screen
This is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ThreePlayerActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView3"
android:name="com.example.mtgcommanderapp.PlayerFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout2" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="#+id/fragmentContainerView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView4"
android:name="com.example.mtgcommanderapp.PlayerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:rotation="90" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView5"
android:name="com.example.mtgcommanderapp.PlayerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:rotation="-90" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Also if anyone knows a good resource for learning how to format an activity that is more in-depth?
I'm new to Android and I need to use a ScrollView to wrap my whole content, since in some cases it needs to take up more height than is available on the screen. Most of the cases though, the height of the content is smaller than the screen. The ScrollView will almost always have a background color (not white), which needs to fill the whole screen available, not just wrap the content. I've checked a few other topics related to this, but the answers were outdated and none of them seems to solve the issue or even focuses on the question asked.
Extra details: Inside the ScrollView there is a RelativeLayout which encapsulates the content, as there can be only one element inside a ScrollView.
Please limit the answers to Java for Android Studio or XML configuration, if they don't use a programmatic approach, neither Kotlin, nor any other language used for Android programming. Thank you in advance!
<?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"
android:id="#+id/detailed_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/detailed_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/product_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/product_image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/details_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/details_container_background"
android:clipChildren="false"
android:clipToPadding="false"
android:elevation="10dp"
android:translationY="-50dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/product_image">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/product_name" />
</androidx.constraintlayout.widget.ConstraintLayout>
<GridLayout
android:id="#+id/params_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/details_container"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<View
android:id="#+id/param_calories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/param_with_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="true"
app:indicatorColor="#android:color/background_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</ScrollView>
Your problem is really about how you set the widths and heights.
MATCH_PARENT - As big as parent's container.
WRAP_CONTENT - As big as child's container or default width/height.
<!-- As big as specified container or device screen if it has no container -->
<ScrollView
...
android:layout_width="match_parent"
android:layout_height="match_parent"
...
>
<!-- As big as the ScrollView -->
<RelativeLayout
android:id="#+id/detailed_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
Simply put your ScrollView inside a ConstraintLayout, and set ScrollView's android:layout_height="0dp" like following:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/detailed_scroll_view"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:fillViewport="true">
...
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
I have problem, i setted bakground image, it showing good in mobile, but in bluestacks emulator it shows white background.... Why? What is the problem?
Can somebody help me to fix this problem?
The problem with: android:background="#drawable/map_new_9"
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:ignore="ContentDescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/base">
...
...
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/base1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/base2"
android:layout_width="match_parent"
android:layout_height="1920dp"
android:background="#drawable/map_new_9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
I am using ConstraintLayout in a Fragment, but when I run the app the UI is empty, below are the Fragment and an Activity where the fragments are shown.
Fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".FirstFragment">
<!-- TODO: Update blank fragment layout -->
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/firstFragButton"
tools:text="Go"
android:layout_marginBottom="384dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/first_fragment"
android:id="#+id/textView" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/firstFragButton" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main
Contains NavHostFragment
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
tools:context=".MainActivity">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="395dp"
android:layout_height="340dp" app:navGraph="#navigation/nav_graph" app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="#+id/fragment"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Different phones got different screen size, in your layout, you are using fixed size on your view (fixed size is 50dp for example) and the result is that what may look good on one screen (your android studio preview screen) will not look good on another screen (your actual phone).
You may want o do something like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".FirstFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="#+id/firstFragButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Go" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first_fragment"
app:layout_constraintBottom_toTopOf="#+id/firstFragButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
To achieve a responsive look on all devices:
Same goes for your NavHostFragment, either use:
android:layout_width="0dp" and
android:layout_height="0dp" to make your view spread all over your screen or use something like to make your view to be responsive:
app:layout_constraintWidth_percent="0.8"
app:layout_constraintHeight_percent="0.5"
This will tell your view to be 80% of the screen size in width and 50% in height:
You can check my examples because with the white background on the images it may not be so very clear.
Generally, If you are using ConstraintLayout I recommend do use it with guidelines and Chains to support different screen sizes.
The Gmail Android App Floating Button
I want to add a floating button over a scroll view in Android. Such that, even if I scroll the layout, the button should remain constant at the given position (bottom-right) in this case. Just like the Gmail android mobile app, they have a send mail button intact at the bottom-right and the background is scrollable.
If you place the button on top of the layout instead of in the recyclerview it wont move when you scroll.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#android:drawable/ic_input_add" />
</androidx.constraintlayout.widget.ConstraintLayout>
It's not a ScrollView, it's a RecyclerView. You can use CoordinatorLayout for this purpose
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
</com.google.android.material.appbar.AppBarLayout>
<Your layout with Recycler View/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="#android:drawable/ic_menu_edit" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
If you still want to use a ScrollView, just put it as a sibling of your RecyclerView with gravity bottom.
make the Scroll View inside constraint layout don't make the main parent layout the scroll view.
<?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">
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:srcCompat="#tools:sample/avatars[0]" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</android.support.constraint.ConstraintLayout>