How to nest a constraint layout in a frame layout in xml? - android

Hi I'm new to Android Studio and currently in my activity_main xml I have this constraint layout with a few text views.
<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/country_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.monash.fit2081.countryinfo.CountryDetails">
<TextView
android:id="#+id/textView"
android:layout_width="150dp"
android:layout_height="25dp"
//some code here
Now I want to change it to a fragment and make that activity as the bottom fragment.I made my fragment like this but how do nest the layout above into my frame layout?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="edu.monash.fit2081.database_4.MainActivity">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_top"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/fragment_border"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/country_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/fragment_top"
android:background="#drawable/fragment_border"
/>
</RelativeLayout>
Any help will be appreciated!

The FrameLayout is a ViewGroup just like RelativeLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/country_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/fragment_top"
android:background="#drawable/fragment_border"
>
<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/country_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.monash.fit2081.countryinfo.CountryDetails">
<TextView
android:id="#+id/textView"
android:layout_width="150dp"
android:layout_height="25dp"
/>
</FrameLayout>

Related

Android Studio does not show layout preview(no error occurs also)

Android Studio does not display a preview of the layout. No errors or warnings are showing. I already try this solution Invalidate Caches/ Restart.
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"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<RelativeLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main"
tools:context=".MainActivity">
<ListView
android:id="#+id/peripherals_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView android:id="#+id/peripheral_list_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/peripheral_list_empty"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<TextView
android:id="#+id/txt"
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" />
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_below="#+id/txt"
android:layout_height="wrap_content"/>
</RelativeLayout>
If the system UI is on Listview will not show. Look for an icon with eye and unmark show system UI

How to set equal space between CardViews in RecyclerView?

I have some CardViews in my RecyclerView, and when I want to set space between them by adding margin, I get this:
As you can see, distance between cards is double. If I for example set margin just for top, left, and right, problem is solved but last card will touch the bottom of screen, and I don't want it. Like this:
As you can see, all spaces are equal and good, but there is no space in the bottom.
I have a MainActivity.java, activity_main.xml, recyclerview_row.xml, and MyAdapter.java in my project.
This is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:background="#dddddd">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
And this is my recyclerview_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/image_view"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
My question is how can I set equal distance between these Cards. Thank you.
Change your activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:background="#dddddd">
<android.support.v7.widget.RecyclerView
android:paddingBottom="10dp"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
The magic is done by this line: android:clipToPadding="false"
Try This in Main Activity,
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:background="#dddddd">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#dddddd">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:paddingBottom="#dimen/dim_10"
android:background="#dddddd">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

Centering the progressbar

I am trying to center my progressbar within the fragements screen. Here is what I have tried so far and what in my opinion shoul be correct:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="#+id/pgFragment"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/recycler_row" />
</ScrollView>
</LinearLayout>
I tried deleting the wrapper layout, getting rid of the whole ScrollView (but as the visibility is set to gone, it shouldn't matter). The current result is, that the ProgressBar sticks to the top of the screen.
Might the cause be because of the Activity layout implementing a NestedScrollView?
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.mediasaturn.productlistpoc.activities.MainActivity"
tools:showIn="#layout/activity_main">
<fragment android:name="com.productlistpoc.recyclerview.RecyclerViewFragment"
android:id="#+id/fragment_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/fragment_recycler" />
</android.support.v4.widget.NestedScrollView>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="#+id/pgFragment"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Update your code like this
<?xml version="1.0" encoding="utf-8"?>
<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">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/recycler_row" />
</LinearLayout>
<ProgressBar
android:id="#+id/pgFragment"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
Don't use RecyclerView inside ScrollView. You have already used NestedScrollView as container layout so you don't need to add ScrollView.
Update your XML as below:
<?xml version="1.0" encoding="utf-8"?>
<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.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<RelativeLayout
android:id="#+id/layoutProgress"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/pgFragment"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
</RelativeLayout>
#. If you want to show/hide prograssbar, just show or hide the layoutProgress programmatically using layoutProgress.setVisibility(View.VISIBLE) and layoutProgress.setVisibility(View.GONE) method.
Hope this will help~

Enable textView above fragment

How can i be able to enable this textView on top of the fragment. I want it to remain static even when fragments change from one another. Iam having no luck enabling it on top. Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.skwebviewapp.MainActivity">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/mainFragment"
tools:layout="#layout/fragment_main" />
<TextView
android:layout_width="wrap_content"
android:layout_height="16dp"
android:id="#+id/textView"
android:text="#string/app_name"/>
</LinearLayout>
Try below code,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.skwebviewapp.MainActivity”
android:orientation=“vertical"
android:weightSum=“1.0">
<TextView
android:layout_width="wrap_content"
android:layout_height=“0dp”
android:layout_weight=“0.10"
android:id="#+id/textView"
android:text="#string/app_name”/>
<fragment
android:layout_width="match_parent"
android:layout_height=“0dp"
android:layout_weight=“0.90"
android:id="#+id/mainFragment"
tools:layout="#layout/fragment_main" />
</LinearLayout>
You can try this with RelativeLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="16dp"
android:text="#string/app_name" />
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map_view_fargment"
android:name="com.google.android.gms.maps.MapFragment"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
/>
</LinearLayout>

No release momentum inside ScrollView

In my activity, I have two RecyclerViews inside a ScrollView. The problem is that when I swipe/flick the ScrollView, scrolling stops immediately. Is there a way to get the ScrollView to implement momentum or inertia so there's some deceleration before the scrolling stops?
My XML is below:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ProgressBar
android:id="#+id/threadload_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView 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/subforums"
android:name="net.polunom.forum.fragments.ThreadFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.ThreadFragment"
tools:listitem="#layout/fragment_subforum"/>
<android.support.v7.widget.RecyclerView 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/threadlist"
android:name="net.polunom.forum.fragments.ThreadFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.ThreadFragment"
tools:listitem="#layout/fragment_thread"/>
</LinearLayout>
</ScrollView>
I figured it out! All I had to do was set setNestedScrollingEnabled(false); to the two RecyclerViews, and everything started working like a charm.
You can disable nested scrolling directly in your XML with: android:nestedScrollingEnabled="false"
So your XML would look like:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ProgressBar
android:id="#+id/threadload_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView 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/subforums"
android:nestedScrollingEnabled="false"
android:name="net.polunom.forum.fragments.ThreadFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.ThreadFragment"
tools:listitem="#layout/fragment_subforum"/>
<android.support.v7.widget.RecyclerView 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/threadlist"
android:nestedScrollingEnabled="false"
android:name="net.polunom.forum.fragments.ThreadFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.ThreadFragment"
tools:listitem="#layout/fragment_thread"/>
</LinearLayout>
</ScrollView>

Categories

Resources