Unable to select TextView inside a Fragment - android

I am trying to select text from a TextView which is present inside a fragment.
This is the XML for my TextView.
The textIsSelectable, focusable, enabled and longclickable properties are all set to true according to this post.
<TextView
android:id="#+id/post_text_recycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/post_divider"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autoLink="web"
android:fontFamily="serif"
android:text="#string/stall_user"
android:textColor="#android:color/black"
android:textColorHighlight="#android:color/holo_blue_light"
android:textIsSelectable="true"
android:focusable="true"
android:enabled="true"
android:longClickable="true"
android:textSize="16sp" />
I have also programatically set the following in my Fragment activity:
text.setTextIsSelectable(true);
If it helps, I am fetching data from a Bundle passed through the Activity that holds the fragment, then I am setting the TextView using:
text.setText(Html.fromHtml(data.getString("text")));
text.setTextIsSelectable(true);
I am still unable to select the text. I read in some Stack Overflow post that setting width/height to "wrap_content" allows you to select the text (some old Android bug I guess). This trick has worked for my recyclerview TextView in another activity. Doesn't seem to work here.
Thanks for your help!
Edit:
Here is the entire Fragment layout code as requested:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="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.myapp.www.ViewFragments.OriginalPostFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/hide_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text=""
android:layout_alignParentTop="true"
android:background="#eeeeee"
android:gravity="end"
android:padding="4dp"
android:layout_marginEnd="16dp"
android:textColor="#android:color/black"
android:textSize="16sp"
android:elevation="6dp"
/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/detail_wrapper"
android:background="#ffffff"
android:descendantFocusability="blocksDescendants"
android:paddingBottom="48dp"
cardview:cardCornerRadius="4dp"
cardview:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_image_recycle"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp" />
<TextView
android:id="#+id/post_type_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_toRightOf="#+id/user_image_recycle"
android:textStyle="italic" />
<TextView
android:id="#+id/post_title_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/post_type_recycle"
android:layout_marginStart="16dp"
android:layout_toRightOf="#+id/user_image_recycle"
android:maxLines="1"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/user_name_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/post_title_recycle"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/user_image_recycle"
android:textColor="#0094BD"
android:textSize="16sp" />
<ImageView
android:id="#+id/post_divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#+id/user_image_recycle"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="#android:color/black" />
<TextView
android:id="#+id/post_text_recycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/post_divider"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autoLink="web"
android:fontFamily="serif"
android:text="#string/stall_user"
android:textColor="#android:color/black"
android:textColorHighlight="#android:color/holo_blue_light"
android:textIsSelectable="true"
android:focusable="true"
android:enabled="true"
android:longClickable="true"
android:textSize="16sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
</FrameLayout>

The reason is descendantFocusability. In Your CardView layout, you set descendantFocusability to blocksDescendants.
What it does it disable the focusability of the descendants, so Your TextView never get those focus events.
For more info see here.
To make it work just remove this line from CardView
android:descendantFocusability="blocksDescendants"

Related

EditText & Button Not Showing Up Above ListView

I have this fragment that contains a list view. I now added a button and an edit text to use as a search bar. The application runs fine and shows the list view, but I don't see the button or the edit text anywhere. I'm not sure what I'm doing wrong, here is my code:
<?xml version="1.0" encoding="utf-8"?>
<ListView 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=".BookListFragment"/>
<RelativeLayout
android:id="#+id/container1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<EditText
android:id="#+id/searchBox"
android:layout_width="241dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="24dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="113dp"
android:layout_marginBottom="639dp"
android:autofillHints="#string/search"
android:ems="10"
android:inputType="textPersonName"
android:visibility="visible" />
<Button
android:id="#+id/searchButton"
android:layout_width="98dp"
android:layout_height="39dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="279dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="13dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="646dp"
android:text="#string/search" />
</RelativeLayout>
You've the below issues
You've two root layouts: ListView & RelativeLayout, so I wrapped the
ListView within the RelativeLayout to keep a single top root view.
Your RelativeLayout has width & height of 0dp, so its content won't
display
Fixed ListView under the Button & EditText using
android:layout_alignParentBottom
Removed too big margin at the bottom from the Button & the EditText
Removed
Removed unusable Constraint that are related to ConstraintLayout
try using the below layout
<?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:id="#+id/container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
tools:context=".BookListFragment">
<EditText
android:id="#+id/searchBox"
android:layout_width="241dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="113dp"
android:layout_marginRight="113dp"
android:autofillHints="#string/search"
android:ems="10"
android:inputType="textPersonName"
android:visibility="visible" />
<Button
android:id="#+id/searchButton"
android:layout_width="98dp"
android:layout_height="39dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="279dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="13dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="16dp"
android:text="#string/search" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/searchBox"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Your ListView has a match-parent Height, in this case it will fill all the screen. When you want to put many thins in the screen you should use a Layout manager to organize it. try something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/container1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp">
<EditText
android:id="#+id/searchBox"
android:layout_width="241dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="24dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="113dp"
android:autofillHints="search"
android:ems="10"
android:inputType="textPersonName"
android:visibility="visible" />
<Button
android:id="#+id/searchButton"
android:layout_width="98dp"
android:layout_height="39dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="279dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="13dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="16dp"
android:text="search" />
</RelativeLayout>
<!-- Your List View Come Here -->
<ListView
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"/>
</LinearLayout>
It may works but is not the best way to make this layout. you should see a little more about Layout Xml Official Description

Can I put a Button to bottom of a ConstraintLayout inside ScrollView?

I have a ScrollView contains a ConstraintLayout. Inside the ConstraintLayout, I put many views with an attribute layout_constraintTop_toBottomOf to make a relation between the view and the view top of it, and I used layout_marginTop to put spaces between the views.
In my design, I have a Button that should be in the bottom of the layout and it cannot happen with the layout_marginTop because it should have a relation with the bottom of the ConstraintLayout.
Here's my code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="120dp"
android:text="Logo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/un_et"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="25dp"
android:layout_marginRight="28dp"
android:gravity="center"
android:hint="User name"
android:textColor="#bebebe"
android:textCursorDrawable="#null"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/logo" />
<EditText
android:id="#+id/pw_et"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="13dp"
android:layout_marginRight="28dp"
android:gravity="center"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#bebebe"
android:textCursorDrawable="#null"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/un_et" />
<RelativeLayout
android:id="#+id/save_pw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="28dp"
android:paddingRight="28dp"
app:layout_constraintTop_toBottomOf="#id/pw_et">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:buttonTint="#bebebe"
android:text="Save account"
android:textColor="#bebebe" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Forget password?"
android:textColor="#a40000" />
</RelativeLayout>
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="17dp"
android:layout_marginRight="28dp"
android:text="Login"
android:textAllCaps="false"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="#id/save_pw" />
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Sign up"
android:textAllCaps="false"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="#id/btn" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
Replace ScrollView with NestedScrollView & also add android:fillViewport="true" like this
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
// rest of code
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Sign up"
android:textAllCaps="false"
android:textColor="#FFFFFF"
app:layout_constraintVertical_bias="1"
app:layout_constraintTop_toBottomOf="#id/btn"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Just set this property to your button:
app:layout_constraintBottom_toBottomOf="parent"
Then add this to your last view this property:
app:layout_constraintBottom_toTopOf="#+id/button"
Also, add to your last view:
android:layout_marginBottom="height_of_button"
However, if you are adding a lot of views it would be better to populate them inside a RecyclerView using an Adapter. Decide what you want to do. Also, tell me if I understood your question correctly, it was a bit confusing.
I found one solution to keep the button at the bottom, doesn't matter is your text is dynamic, read about app:layout_constraintVertical_bias https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout#Bias
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:text="Title"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintTop_toBottomOf="#id/text_gallery"
app:layout_constraintBottom_toBottomOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true">
<androidx.appcompat.widget.AppCompatTextView
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/texto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text=" Text \n Text" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Buton1"
app:layout_constraintVertical_bias="1"
app:layout_constraintTop_toBottomOf="#+id/texto"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

Onclick doesn't get triggered on CardView

I have an OnClickListener on a CardView. The listener only works when I tap on a region outside the content(TextViews/ImageViews). I also have a linear layout inside my CardView. I wanted it to work when I tap anywhere on the CardView. Is there a better way to implement this compared to how I'm doing it?
Here's my CardView in xml
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:padding="10dp"
android:layout_margin="3dp"
android:elevation="3dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="#+id/post_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/guilt_theme"
android:padding="4dp"
android:text="GUILT"
android:textColor="#color/textColor"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/post_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:layout_marginTop="10dp"
android:focusableInTouchMode="false"
android:clickable="true"
android:text="text content"
android:textColor="#color/secondaryText"
android:textSize="16sp" />
<TextView
android:id="#+id/date_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:inputType="number"
android:text="on 16/5/2018 by user"
android:textColor="#color/weaker_text"
android:textSize="14sp" />
<LinearLayout
android:id="#+id/LinearLayout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/view_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
android:layout_marginTop="4dp"
android:src="#drawable/ic_action_name" />
<TextView
android:id="#+id/total_views"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:inputType="number"
android:layout_marginTop="3dp"
android:textColor="#color/secondaryText"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:layout_marginLeft="20dp"
android:id="#+id/like_image"
android:layout_width="20dp"
android:layout_marginTop="4dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
android:src="#drawable/thumbs_up"
android:layout_height="20dp" />
<TextView
android:id="#+id/total_upvotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_marginTop="3dp"
android:inputType="number"
android:textColor="#color/secondaryText"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/dislike_image"
android:layout_marginLeft="20dp"
android:layout_width="20dp"
android:layout_marginTop="4dp"
android:src="#drawable/thumb_down"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
android:layout_height="20dp" />
<TextView
android:id="#+id/total_downvotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_marginTop="3dp"
android:inputType="number"
android:textColor="#color/secondaryText"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
I then added a listener to it in my Activity.
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), PostReader.class);
TextView posttext = view.findViewById(R.id.post_text);
String post = posttext.getText().toString();
intent.putExtra("postinfo", post);
startActivity(intent);
}
});
I think you are getting that behaviour because some of your TextViews and ImageViews have the attribute android:clickable="true"
Try:
Remove the attributes and add it only to the CardView. You may also have to remove the attribute
android:focusable="true" from the ImageViews so that the cardview can listen to click events.
In my case the issue was setting the inner view of the CardView to catch clicks by setting android:clickable="true" & android:focusable="true" to the inner view. This prevents the CardView from catching clicks.
Removing both allowed the CardView to catch clicks.
I've facing a problem with it today, a card view with the view I created was not performing the click function.
I tried many things and nothing worked. My solution was only to take the content of the card view and insert it into a constraint layout requiring all the view area. After that the click started working normally.
Example
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:backgroundTintMode="add"
android:elevation="5dp"
app:cardBackgroundColor="#android:color/transparent"
app:cardCornerRadius="5dp"
app:strokeColor="#color/yellow"
app:strokeWidth="2dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/textMove"
style="#style/TextAppearance.MaterialComponents.Subtitle2"
android:layout_width="120dp"
android:layout_height="50dp"
android:gravity="center"
android:padding="2dp"
android:text="#string/move"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

Focus on last edittext in Custom Dialog doesn't work

In my App I'm using a Custum Dialog (android.app.Dialog) with three Edit Texts in it. I'm able to set the next focus from the first to the second EditText when I press the next Button on the SoftKeyboard, but it doesen't work to set the focus on the third EditText. I already tried to set nextFocusForward or nextFocusDown and so on but it still doesn't work.
Here is the XML Layout from my Custom Dialog:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/addcard_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/dialog_addcard_info" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/addcard_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="#string/name"
android:inputType="text"
android:lines="1"
android:maxLines="1"
android:scrollHorizontally="true" />
<EditText
android:id="#+id/addcard_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:hint="#string/number"
android:inputType="number"
android:maxLength="16"
android:scrollHorizontally="true"/>
<EditText
android:id="#+id/addcard_pin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:hint="#string/pin"
android:inputType="numberPassword"
android:maxLength="4"
android:textSize="14sp"
android:scrollHorizontally="true"/>
</LinearLayout>
<TextView
android:id="#+id/addcard_errormessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="#string/error"
android:textColor="#ff0000"
android:textStyle="bold"
android:visibility="gone" />
<Button
android:id="#+id/addcard_verify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="#string/verify" />
</LinearLayout>
Does anyone have an idea why i'm not able to set the focus on the third EditText?
Thanks in advance.
try this one in your code :)
<EditText
android:id="#+id/addcard_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:hint="#string/number"
android:inputType="number"
android:maxLength="16"
android:imeOptions="actionNext" // try this one
android:nextFocusDown="#+id/addcard_errormessage" // and add this one
android:scrollHorizontally="true"/>

RecyclerView scrolling as part of parent layout and not as a separate scroll (Similar to YouTube video comments)

I'm building a layout similar to the YouTube video layout, trying to get the RecyclerView holding the video comments to scroll as part of the whole layout and not be a separate scroll. I'm relatively new to Android programming and cannot get this to work.
The layout is as such:
At the top of the layout there is a YouTube player which never
scrolls with the screen;
Bellow it are other views which should scroll;
and at the bottom of the layout is the comment section which is a
RecyclerView and it should scroll as part of the whole layout.
I've read on similar questions regarding headers to put the header inside the RecyclerView, but this seems bigger than just a header.
Additionally, the "header" part has a few views that change (rating, view count).
<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">
<fragment
android:id="#+id/youtube_fragment"
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:layout_width="match_parent"
android:layout_height="200dp" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/youtube_fragment"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:layout_marginTop="40dp"
android:text="Star Wars the Force Awakens"
android:textColor="#color/colorPrimaryText"
android:textSize="#dimen/text_size_large" />
<TextView
android:id="#+id/textview_view_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/textview_title"
android:text="1737\nViews"
android:textColor="#color/colorSecondaryText"
android:textSize="#dimen/text_size_small" />
<ImageButton
android:id="#+id/imagebutton_show_description"
style="?attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#id/textview_view_count"
android:contentDescription="Image of an arrow pointing down, which when pressed shows the item's description"
android:src="#drawable/ic_arrow_drop_down_black_24dp" />
<TextView
android:id="#+id/textview_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imagebutton_show_description"
android:layout_marginTop="12dp"
android:textColor="#color/colorSecondaryText"
android:textSize="#dimen/text_size_small"
android:visibility="gone" />
<View
android:id="#+id/divider1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/textview_description"
android:layout_marginLeft="#dimen/divider_horizontal_margin"
android:layout_marginRight="#dimen/divider_horizontal_margin"
android:layout_marginTop="32dp"
android:background="#color/colorDivider" />
<RatingBar
android:id="#+id/ratingbar"
style="#style/MyRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textview_title"
android:layout_alignStart="#+id/textview_title"
android:layout_below="#+id/divider1"
android:layout_marginTop="16dp"
android:numStars="5" />
<TextView
android:id="#+id/rating_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/ratingbar"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/ratingbar"
android:layout_toRightOf="#+id/ratingbar"
android:text="0"
android:textColor="#color/colorSecondaryText"
android:textSize="#dimen/text_size_large" />
android:layout_below="#+id/divider1"
android:layout_marginTop="16dp"
android:text="FLOAT" />
<ImageView
android:id="#+id/imageview_user"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/ratingbar"
android:layout_marginTop="32dp"
android:src="#drawable/singer" />
<View
android:id="#+id/divider2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/imageview_user"
android:layout_marginLeft="#dimen/divider_horizontal_margin"
android:layout_marginRight="#dimen/divider_horizontal_margin"
android:layout_marginTop="12dp"
android:background="#color/colorDivider" />
<TextView
android:id="#+id/comment_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/divider2"
android:layout_marginTop="24dp"
android:text="Comments"
android:textColor="#color/colorSecondaryText"
android:textSize="#dimen/text_size_small" />
<ImageView
android:id="#+id/imageview_commenting_user"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/textview_comment_header"
android:layout_marginTop="24dp"
android:src="#drawable/singer" />
<EditText
android:id="#+id/edittext_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textview_comment_header"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_toEndOf="#+id/imageview_commenting_user"
android:layout_toRightOf="#+id/imageview_commenting_user"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Add public comment"
android:longClickable="false" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edittext_comment"
android:layout_marginBottom="16dp"
android:layout_marginTop="32dp" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
I later followed Hister's answer from Is there an addHeaderView equivalent for RecyclerView? and added everything but the video as a header, and the comments as the second holder. It seems to work (though I'm still not sure if this is how I was supposed to do this), but every time I added a new comment to the first position (after the header), I get extra white spaces added to the top of the comments section. I looked over the code and I have no idea what could add an empty section each time i add a new comment.

Categories

Resources