Create the Layout with CardView and Floating Action Button Android - android

I want to Create the Layout like the Following Image with Material CardView and Custom Floating Action Button:
And I was developed upto this Layout xml file as follows :
<?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"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/mainview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="2dp"
app:cardElevation="2dp"
android:layout_marginBottom="#dimen/cardMarginVertical"
android:layout_marginLeft="#dimen/cardMarginHorizontal"
android:layout_marginRight="#dimen/cardMarginHorizontal"
android:layout_marginTop="#dimen/cardMarginVertical"
app:cardPreventCornerOverlap="false"
app:contentPadding="0dp">
<com.github.florent37.materialviewpager.sample.ExpandableTextView
android:id="#+id/expandingTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:text="#string/longText" />
<RelativeLayout
android:id="#+id/buttonlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/expandingTextView"
android:layout_gravity="bottom"
android:background="#android:color/transparent">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="#+id/action_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="#fff"
android:layout_alignParentRight="true"
fab:fab_colorPressed="#fff"
android:visibility="visible" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</FrameLayout>

To implement UI like your picture, some code like below can help.
Parameters layout_anchor and layout_anchorGravity in FloatingActionButton are the point.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level. This design means that your applications can use the libraries' features and still be compatible with devices running Android 1.6 (API level 4) and up." />
</android.support.v7.widget.CardView>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="#id/cv"
app:layout_anchorGravity="right|end|bottom"
android:layout_margin="16dp"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
Edit:
Early answer had some bug in Android 5.0.1 and 5.0.2, FAB would show below CardView.
Add NestedScrollView with layout_behavior outside CardView to work in Android 5.0.1 and 5.0.2

Related

Floating Action Button in android webview

I've created a simple android app with webview to showcase my website, so now am trying to add FAB so users can share articles with that button. Wherever I place the code, either it crashes or just not being useful when I place inside webview because it gets scrolls, am sharing my code below, can you please help with the alignment, I want FAB to be a sticky button at the right bottom of the app.
<?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">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_behavior="#string/appbar_scrolling_view_behavior">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
style="?android:attr/progressBarStyle"
/>
</RelativeLayout>
Can you please guide me with the correct way of doing it, after this I've to change JAVA file.
Try 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:elevation="6dp"
android:layout_alignParentBottom="true">
<YOUR_FAB_HERE
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="25dp"
android:visibility="visible"
app:srcCompat="#drawable/ic_file_upload_white_24dp"
tools:ignore="VectorDrawableCompat" />
</RelativeLayout>
<--- your additional layout here ----->
</RelativeLayout>

Included layout missing on Android API 21

I have an app that has been running on my deivce perfectly fine for a long time. Today I decided to test it on an older device. I have an old Glaxy S4 Mini, so I loaded it up, and an entire part of my layout was missing. The missing part is a reusable layout I made, and included it in this activity. The space where it would be is not taken up by anything, it is just left empty
This is the activity layout file. Everything that is inside of include is completely missing when the app is ran.
<?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="com.example.eddie.songsPrimirenya.SearchActivityReg">
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/search_bar_include"
layout="#layout/search_bar"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/RecyclerViewReg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="#+id/search_bar_include"
android:layout_margin="3dp"/>
</RelativeLayout>
and this is the layout that I was including earlier. I think it's not showing because of some compatibility issue, but I do not know what it is.
<?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.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardSearchReg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
card_view:cardCornerRadius="5dp"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent"
card_view:layout_constraintVertical_bias="0.0">
<RelativeLayout
android:id="#+id/LayoutAll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/LayoutSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/txtInReg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:hint="#string/search"
android:singleLine="true"
android:textColorHint="#color/textHint" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/LayoutSearchModeSelect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/LayoutSearch"
android:layout_marginStart="3dp">
<Switch
android:id="#+id/swtSearchType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="false"
android:text="#string/search_by_name"
android:textColorHighlight="#color/colorAccent"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Here is an image of how it is supposed to look like
and this is what it ends up looking like
make your include layout match parent,it should work fine
<?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="com.example.eddie.songsPrimirenya.SearchActivityReg">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/search_bar_include"
layout="#layout/search_bar"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/RecyclerViewReg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="#+id/search_bar_include"
android:layout_margin="3dp"/>
</RelativeLayout>

Android Studio is not showing Design Preview But Application is running

I have updated my android studio. In my layout preview my design is not showing it says
NOTE: One or more layouts are missing the layout_width and layout_height attributes, required in most layouts.
OR: Automatically add all missing attributes. There is no error in application. It's running well. I have uninstalled my android studio but same issue occurred. In all my projects occurs the same issue.
I have updated SDK as well.
Here is layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinate_layout_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_series"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<ProgressBar
android:id="#+id/pb_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible" />
<TextView
android:id="#+id/tv_no_record"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="No record Found"
android:textSize="18sp"
android:visibility="visible" />
</RelativeLayout>
<!-- <android.support.v4.widget.NestedScrollView
android:id="#+id/bottom_sheet_registration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EEEEEE"
android:clipToPadding="true"
android:visibility="gone"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:orientation="vertical">
<include layout="#layout/bottomsheet" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>-->
<View
android:layout_width="match_parent"
android:layout_height="60dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/btn_add_tocart"
style="#style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:backgroundTint="#863029"
android:text="Add To Cart"
android:textAllCaps="false" />
</RelativeLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
This happens to me sometimes. What I do to fix this is to check styles.xml from res folder and check that the style AppTheme is correctly defined.
For example, if I want my app style to be without action bar, I use this as AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
and add required items such as colorPrimary etc.
Then after rebuilding the gradle I usually can see my layout design again
One of the possibility is that your Theme changes. Try changing it to Material -> Dark in the Preview view.
Link to the similar topic:
android studio preview not displaying? version 1.5.1

how to shadow the recycler view and fab button

I am searching in SO and try to add shadow for RecyclerView and fab button.I got the command android:elevation and android:line provider for shadowing the recycler view but nothing happens by using the command
What I am trying
<android.support.design.widget.CoordinatorLayout
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:background="#color/colorgrey"
android:orientation="vertical"
tools:context="com.example.karthi.nestaway.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:outlineProvider="bounds"
android:background="#color/colorwhite
android:clipToPadding="false"
android:elevation="2dp"
android:layout_marginBottom="10dp" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:elevation="6dp"
app:srcCompat="#drawable/ic_add_black_24dp"
tools:ignore="VectorDrawableCompat" />
</android.support.design.widget.CoordinatorLayout>
Attribute elevation is only used in API level 21 and higher check your API level.
In your Manifest file inside application tag add
android:hardwareAccelerated="true"
Increase the value of android:elevation to see shadow effects.
Hope this helps.

Floating action button doesn't appear in kitkat

I have created a single page app where the floating action button is used to share content from the page. It works flawlessly in android lollipop but when i ran the app on kitkat, it doesn't show. Since the only interactive component is the FAB, the user is left with nothing to do but stare. I ran some search and found that the FAB should work perfectly on kitkat and that there are some issues only when it comes to lollipop(like padding). But, it works just fine in lollipop. Do I have to change it to a button for versions older than lollipop? My first question here. Any help is appreciated. Thanks.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.ataraxianstudios.decksharer.MainActivity"
>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/share1"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#016cb4">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/llMain"
>
<Spinner
android:id="#+id/osversions"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
/>
<ImageView
android:layout_width="60dp"
android:layout_height="80dp"
android:id="#+id/selectedImage"
/>
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
I also have the same problem may be it is due to material design as material design introduced after kitkat so there be some problem on some tabs or layouts but try this it works for me maybe works for you..
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="com.ataraxianstudios.decksharer.MainActivity"
>
//Replace your fab from here
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#016cb4">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/llMain"
>
<Spinner
android:id="#+id/osversions"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
/>
<ImageView
android:layout_width="60dp"
android:layout_height="80dp"
android:id="#+id/selectedImage"
/>
</LinearLayout>
</ScrollView>
//to here:
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/share1"/>
</android.support.design.widget.CoordinatorLayout>
Are you using design support library?
This is an example:
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add" />
And in your build.gradle:
dependencies {
.....
compile 'com.android.support:design:xx.yy.zz'
}
Finally, in your Activity you can reference the FAB like:
FloatingActionButton btn = (FloatingActionButton) findViewById(R.id.fab);
Hope it helps you!!
Use bringToFront for you FloatingActionButton
fab = view.findViewById(R.id.fab_hours);
fab.bringToFront();

Categories

Resources