This question already has answers here:
How we can put Floating action button on top of any layout
(5 answers)
Closed 4 years ago.
I m new to android programing and i have a tricky problem i passed a couple hours on trying to resolving it but without succuess, im looking how to add FloatingActionButton inside RecyclerView my template look like :
<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"
tools:context=".MainFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/messages"
android:layout_width="match_parent"
android:layout_height="451dp"
android:layout_weight="1"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<!-- TODO: fix this by adding the floating button inside the recyclerView -->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/ic_launcher"
android:layout_margin="16dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:gravity="center_vertical">
<EditText
android:id="#+id/message_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/prompt_message"
android:imeActionId="#+id/send"
android:imeActionLabel="#string/action_send"
android:imeOptions="actionSend"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"/>
<ImageButton
android:id="#+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_send"
android:contentDescription="#string/action_send"/>
</LinearLayout>
Here a my target layout that i need :
you need to use Relative layout instead Linear. Look at my code for floatingActionButton, this will help you.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#layout/app_toolbar"
android:layout_weight="0.6"
android:orientation="vertical"
android:stateListAnimator="#null">
<android.support.design.widget.FloatingActionButton
android:id="#+id/btn_asset_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:background="#android:color/transparent"
android:backgroundTint="#color/white"
android:src="#drawable/icon_camera"
app:rippleColor="#color/white" />
<android.support.v7.widget.RecyclerView
android:id="#+id/messages"
android:layout_width="match_parent"
android:layout_height="451dp"
android:layout_weight="1"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<RelativeLayout/>
Related
In My Android project I am using RecyclerView inside one layout. Here is the code given below-
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
tools:context=".ui.reader.pageview.PageViewFragment">
<TextView
android:id="#+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:overScrollMode="never"
android:id="#+id/rv_page_view"
android:layout_below="#id/pb"
android:layout_above="#+id/ll_page_controller_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:background="#color/colorWhite"
android:orientation="vertical"
android:id="#+id/ll_page_controller_bottom"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<org.adw.library.widgets.discreteseekbar.DiscreteSeekBar
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:id="#+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:dsb_indicatorColor="#color/colorOrange"
app:dsb_trackColor="#color/colorOrange"
app:dsb_progressColor="#color/colorOrange"
app:dsb_rippleColor="#color/colorGray"
app:dsb_min="0"
app:dsb_max="100"/>
<TextView
android:layout_marginTop="-10dp"
android:layout_marginBottom="5dp"
android:text="1"
android:id="#+id/tv_page_number"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_below="#id/pb"
android:layout_above="#id/ll_page_controller_bottom"
android:id="#+id/view_touch"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<RelativeLayout
android:visibility="invisible"
android:id="#+id/rl_settings"
android:layout_alignParentEnd="true"
android:layout_below="#id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include layout="#layout/reader_settings"/>
</RelativeLayout>
<TextView
android:layout_margin="10dp"
android:layout_below="#id/pb"
android:id="#+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp"
android:visibility="invisible"
android:layout_above="#id/ll_page_controller_bottom"/>
</RelativeLayout>
Now, for rendering a lot of data when the users scrolls too much fast the screen got hanged. So, I want to slow down the speed of the scrolling.
I have tried the following solution-
How can i control the scrolling speed of recyclerView.smoothScrollToPosition(position)
It works well with horizontal scrolling but when I switch to vertical view then It doesn't work.
Is there any way to solve this?
enter image description here https://docs.google.com/document/d/1_ag-aMsJn408Yef_V0TbVC0GvRIbbuRRPd0uRgsNoM4/edit?usp=sharing
It's in red. I tried to solve it but I still can't resolve the error. :( I already found some similar questions around here but nothing helped me to solve this issue. I hope someone has an idea... ^.^
A valid XML file has only one root node.
Now It included LinearLayout and RelativeLayout.
You must put RelativeLayout and LinearLayout in a reasonable place.
You can do like this,but you need to change the location.
<?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:gravity="center_horizontal"
android:orientation="vertical"
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="sg.edu.tp.uxidcomt.LoginActivity">
<!-- Login progress -->
<ProgressBar
android:id="#+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone"/>
<ScrollView
android:id="#+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_password"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/email_sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/action_sign_in"
android:textStyle="bold"/>
<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.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="#+id/layoutDots"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="200dp"
android:gravity="center"
android:orientation="horizontal"></LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#id/layoutDots"
android:alpha=".5"
android:background="#android:color/white"/>
<Button
android:id="#+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#null"
android:next="NEXT"
android:textColor="#android:color/white"/>
<Button
android:id="#+id/btn_skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#null"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:text="SKIP"
android:textColor="#android:color/white"
tools:context=".LoginActivity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
This question already has answers here:
Add textview to layout?
(4 answers)
Closed 7 years ago.
I have a layout with 4 buttons in the center. I simply want to add a textview next to the four buttons (WITHOUT MOVING THE FOUR BUTTONS FROM THE CENTER), like illustrated here (TextView should be in the place of the red) :
[![enter image description here][1]][1]
Right now, I have the textview in its own linear layout and the four buttons in their own Relative Layout, all in one big relative layout. When ever I try to move the textview to the left of the buttons, the buttons move over to the right. How do I achieve what I illustrated? Here is my 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"
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.ruchir.circleswithmap.MainActivity"
android:id="#+id/layout"
android:gravity="center"
android:background="#010101">
<LinearLayout
android:id="#+id/text_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="20:00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textColor="#fbfafa"
android:id="#+id/timetext" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Blue"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/bluesquare" />
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Green"
android:layout_below="#+id/Blue"
android:layout_alignLeft="#+id/Blue"
android:layout_alignStart="#+id/Blue"
android:background="#drawable/greensquare" />
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Red"
android:layout_alignTop="#+id/Blue"
android:layout_toRightOf="#+id/Blue"
android:layout_toEndOf="#+id/Blue"
android:background="#drawable/redsquare" />
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Purple"
android:layout_alignBottom="#+id/Green"
android:layout_alignLeft="#+id/Red"
android:layout_alignStart="#+id/Red"
android:background="#drawable/purplesquare" />
</RelativeLayout>
</RelativeLayout>
Put the textView in one Linearlayout and the four buttons in another LinearLayout and then trying Horizontal weights to align then in a single line might work out.
Code could be as follows
<?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.ruchir.circleswithmap.MainActivity"
android:id="#+id/layout"
android:gravity="center"
android:background="#010101"
android:weightSum="100">
<LinearLayout
android:id="#+id/text_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_weight = "50"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="20:00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textColor="#fbfafa"
android:id="#+id/timetext" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="50">
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Blue"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Green"
android:layout_below="#+id/Blue"
android:layout_alignLeft="#+id/Blue"
android:layout_alignStart="#+id/Blue"
/>
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Red"
android:layout_alignTop="#+id/Blue"
android:layout_toRightOf="#+id/Blue"
android:layout_toEndOf="#+id/Blue"
/>
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/Purple"
android:layout_alignBottom="#+id/Green"
android:layout_alignLeft="#+id/Red"
android:layout_alignStart="#+id/Red"
/>
</RelativeLayout>
</LinearLayout>
I have this xml:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity$PlaceholderFragment"
android:weightSum="1"
android:padding="0.5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#000000"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="#android:drawable/alert_dark_frame"
android:id="#+id/textView1"
android:gravity="center"
android:background="#FF6600"
android:layout_weight="0.20"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="New Text"
android:textColor="#FFFFFF"
android:id="#+id/textView2"
android:background="#B24700"
android:gravity="center"
android:layout_weight="0.30" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="--"
android:textSize="30dp"
android:id="#+id/textView3"
android:gravity="center"
android:background="#FFFFFF"
android:layout_weight="0.60" />
</LinearLayout>
which i include in here:
how can i inject the "new text" to be different in each item.
As I know, this cannnot be done with <include/> itself, the closest thing is to create your own widget class, in the class you inflate from the xml and read a custom attributes and set the value, e.g. text in TextView programmatically.
Please have a look at the following code
<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: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=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/sales_inqury"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="21dp"
android:layout_toRightOf="#+id/textView1"
android:src="#drawable/add_2" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginTop="16dp"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</RelativeLayout>
This generates the following output
To be honest, this is not what I want. The text should "spread" to match the screen width, until it find the image view. I think improving the space between letters might be the solution.
But I would like to know whether there is any better solution, which we can do in Code or XML.
Hey i did some changes in your code. I use LinerLayout instead of Relative Layout.
<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="horizontal"
android:weightSum="3.0f"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0f"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0f"
android:layout_marginLeft="21dp"
/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0f"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</LinearLayout>
If I get you right, you want the EditText below "Sales Inquiry".
<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"
android:orientation="vertical"
tools:context=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="#drawable/add_2"
android:text="#string/sales_inqury"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="16dp"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</LinearLayout>
Note I replaced the ImageView with drawableRight. It is called compoundDrawable and it could be set to any location near the text of TextView.
There is no need to use RelativeLayout here. Vertical LinearLayout should do the trick.
layout_width="match_parent"
Tells that the View should be spread across the width of the layout.
But if you want EditText in the same line as "Sales Inquiry" then it's done line this.
<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"
android:orientation="horizontal"
tools:context=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:drawableRight="#drawable/add_2"
android:text="#string/sales_inqury"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_marginTop="16dp"
android:hint="Search"
android:ems="8" >
<requestFocus />
</EditText>
</LinearLayout>
Use horizontal LinearLayout, set layout_width of first TextView to wrap_content.
The EditText should use layout_weight="1" (only works in LinearLayout) that means it will take as much space available. When using layout_weight in horizontal LinearLayout, set the layout_width to 0dp. If it's vertical LinearLayout, layout_height should be 0dp.