Following is my xml layout.
<android.support.v7.widget.CardView
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
app:cardBackgroundColor="#color/colorPrimary"
app:cardElevation="8dp"
app:layout_anchor="#id/contentMain"
app:layout_anchorGravity="right|bottom|end"
card_view:cardCornerRadius="4dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:selectableItemBackground">
<FrameLayout
android:id="#+id/bottomMargin"
android:layout_width="match_parent"
android:layout_height="70dp">
<ProgressBar
android:id="#+id/customProgress"
android:layout_width="match_parent"
android:layout_height="5dp"
android:visibility="visible"/>
<android.support.v7.widget.Toolbar
android:id="#+id/smallPlayer_home"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_marginBottom="0dp"
android:visibility="visible">
<CircleImageView
android:id="#+id/selected_track_image_sp_home"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/myvisualizerview"
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:src="#drawable/album1"
android:visibility="visible"
app:civ_border_color="#FF000000"
app:civ_border_width="0dp"/>
<TextView
android:id="#+id/selected_track_title_sp_home"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_above="#+id/player_control"
android:layout_alignParentStart="true"
android:layout_marginLeft="35dp"
android:layout_marginTop="2dp"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Title"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="13dp"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="right|center_vertical"
android:layout_marginBottom="5dp"
android:layout_marginRight="17dp"
android:layout_marginTop="2dp"
android:background="?attr/selectableItemBackgroundBorderless">
<ImageView
android:id="#+id/player_control_sp_home"
android:layout_width="24dp"
android:layout_height="24dp"
android:clickable="true"
android:src="#drawable/ic_play_animatable"
android:visibility="visible"/>
<View
android:id="#+id/bufferingIndicatorMain"
android:layout_width="24dp"
android:layout_height="24dp"
android:clickable="true"
android:visibility="invisible"
app:indicator="BallSpinFadeLoader"
app:indicator_color="#FFFFFF"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</FrameLayout>
</android.support.v7.widget.CardView>
Im using the following code to set On click listener for the cardview,
bottomCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showFragment("streaming");
}
});
The problem is that the cardview click event is not getting fired.
I have also tried setting onFocusChangedListener upon cardview but still the onClick event is not getting fired.
How can I be able to sort this out?
You should set click listener for layout inside your cardview that is frame layout.You can try putting clickable property too like this
android:clickable = "true"
Also can refer this answer and this answer
Follow Shalini's answer or,
set clickable and focusable to false for inner layout (in your case bottomMargin FrameLayout), and set clickable and focusable to true for the CardView, as you already did.
Related
I have cardview and material textview used inside LinearLayout layout like below
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:weightSum="2">
<com.google.android.material.card.MaterialCardView
android:id="#+id/card_date"
android:layout_width="0dp"
android:clickable="true"
android:layout_weight="1"
android:layout_marginEnd="2dp"
android:layout_height="match_parent"
android:clipToPadding="true"
app:cardCornerRadius="5dp"
android:focusable="true">
<com.google.android.material.textview.MaterialTextView
android:layout_weight="1"
android:id="#+id/editText_schedule_date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:background="#color/transparent"
android:text="#string/date_post"
android:focusableInTouchMode="false"
android:singleLine="true"
android:textColor="#color/textView_hint_upload"
android:theme="#style/editText_style"
android:textSize="16sp" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_marginStart="2dp"
android:layout_height="match_parent"
android:clipToPadding="true"
app:cardCornerRadius="5dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editText_schedule_time"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/transparent"
android:hint="#string/time_post"
android:gravity="center"
android:inputType="time"
android:singleLine="true"
android:textColor="#color/textView_upload"
android:textColorHint="#color/textView_hint_upload"
android:theme="#style/editText_style"
android:textSize="16sp" />
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
I am trying to use it in my activity like below
private MaterialCardView card_date;
then on create like below
card_date = findViewById(R.id.card_date);
card_date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("called","this card");
}
});
But its not working. I have tried textview onclick listener as well and that also not working. Let me know if anyone can help me for solve the puzzle. Thanks! Code outside this LinearLayout is working fine for same. Thanks!
Please remove android:clickable="true"
<com.google.android.material.card.MaterialCardView
android:id="#+id/card_date"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginEnd="2dp"
android:layout_height="match_parent"
android:clipToPadding="true"
app:cardCornerRadius="5dp"/>
You must set android:clickable="false"
<com.google.android.material.card.MaterialCardView
android:id="#+id/card_date"
android:layout_width="0dp"
android:clickable="false"/>
I was have used
android:layout_weight="1"
in textview which was causing issue. I have removed it and it has been solved.
Thanks!
I have 3 HorizontalScrollview, for each of them i have customView contain editText that i add dynamically depend from web service response. The problem is, when i want to change value in editText and keyboard appear, the HorizontalScrollView scroll automatically to the end, so i can't properly input value to my edit text. My Question is how to prevent autoscroll in my HorizontalScrollView so i can input value on my editText? and the HorizontalScrollView only scroll when user scroll it.
I've search and my problem similar with this thread but the sugestion didn't work. here is workaround that i already try :
Add android:windowSoftInputMode="stateHidden|adjustNothing" or
android:windowSoftInputMode="stateHidden|adjustResize" already try
adjustPan and other combination but it's impact nothing.
Add android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true" in LinearLayout inside my HorizontalScrollView and it's also impact Nothing.
Add android:descendantFocusability="blocksDescendants the
HorizontalScrollView didn't scroll automatically but i can't change value on my edit text, it similar when i add android:focusable = "false" in my editText
Add code bellow but impact nothing.
scrollLayout.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS
scrollLayout.isFocusable = true
scrollLayout.isFocusableInTouchMode = true
scrollLayout.setOnTouchListener { v, event ->
v.requestFocusFromTouch()
false}
Here is My Code
private fun setUpList(rootLayout: LinearLayout, scrollLayout: HorizontalScrollView, items: List<ProductItem>) {
//config horizontal scroll view
scrollLayout.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS
scrollLayout.isFocusable = true
scrollLayout.isFocusableInTouchMode = true
scrollLayout.setOnTouchListener { v, event ->
v.requestFocusFromTouch()
false
}
// add customView to linear layout
rootLayout.removeAllViews()
for (item in items) {
val view = EditableThumbnailProduct(context)
view.setProductInfo(item)
view.setOnProductChangeListener(onChangeListener())
rootLayout.setBackgroundColor(Color.TRANSPARENT)
rootLayout.addView(view)
}
}
Here is my XML
<HorizontalScrollView
android:id="#+id/scrollMed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:id="#+id/contentMed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#android:color/transparent"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal" />
</HorizontalScrollView>
Here is my custom view
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#android:color/transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical">
<ImageView
android:id="#+id/img_thumbnail"
android:layout_width="150dp"
android:layout_height="120dp"
android:background="#color/gray_border"
android:scaleType="fitXY" />
<TextView
android:id="#+id/txt_product_name"
style="#style/DefaultTextTitle"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="5dp"
android:ellipsize="end"
android:gravity="center"
android:maxLines="2"
android:text="#string/product_name_label" />
<TextView
android:id="#+id/txt_product_stock"
style="#style/DefaultTextSubTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="#string/available_stock_label" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/gray_border" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<Button
android:id="#+id/btn_add_to_cart"
style="#style/WhiteTitle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="#drawable/round_button_orange"
android:gravity="center"
android:text="#string/buy_button_label"
android:visibility="invisible" />
<RelativeLayout
android:id="#+id/layout_plus_minus"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/btn_minus"
style="#style/WhiteTitle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:background="#drawable/round_button_blue"
android:paddingLeft="5dp"
android:paddingTop="11dp"
android:paddingRight="5dp"
android:paddingBottom="11dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/ic_minus" />
<ImageView
android:id="#+id/btn_delete"
style="#style/WhiteTitle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:background="#drawable/round_button_blue"
android:padding="3dp"
android:scaleType="fitXY"
android:visibility="invisible"
app:srcCompat="#drawable/ic_delete" />
<EditText
android:id="#+id/txt_cart_amount"
style="#style/LightTextTitle"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:layout_marginRight="10dp"
android:backgroundTint="#color/blue_text_color"
android:inputType="number"
android:maxLength="3"
android:paddingBottom="5dp" />
<ImageView
android:id="#+id/btn_plus"
style="#style/WhiteTitle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:background="#drawable/round_button_blue"
android:gravity="center"
android:padding="5dp"
android:scaleType="fitXY"
android:textSize="16sp"
app:srcCompat="#drawable/ic_plus" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
All sugestion will be appreciate. Thanks in advance.
I am developing an Android application in which I would like to display the GridView using RecyclerView.
I could able to create separate layout for an item and display it in GridView dynamically.But I face an issue in showing tick mark on top right of each item.
The tick mark is hidden by CardView
Here is my item_grid.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="110dp"
android:layout_height="110dp">
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:elevation="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/image_hobby_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:src="#drawable/cycling" />
<TextView
android:id="#+id/text_hobby_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorAccent"
android:gravity="center"
android:padding="3dp"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text="CYCLING"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
<ImageView
android:id="#+id/image_hobby_selected_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="top|right"
android:adjustViewBounds="true"
android:src="#drawable/all_checked_round" />
</FrameLayout>
I have tried bringToFront() method but it is not working.
viewHolder.checkMark.bringToFront();
((View) viewHolder.checkMark.getParent()).invalidate();
(viewHolder.checkMark.getParent()).requestLayout();
Please help me find the solution.
Please use elevation.
If your card view has elevation as 10dp, then set elevation in imageView as 11dp.
<ImageView
android:id="#+id/image_hobby_selected_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="top|right"
android:adjustViewBounds="true"
android:src="#drawable/ic_camera"
android:elevation="13dp"
/>
I am using a RecyclerView and I am not able to see any feedback when I touch on the item of the RecyclerView. How do I achieve it?
I am trying to show a feedback to the user when they are touching the row of RecyclerView. Something like a ripple effect.
I want to know how to achieve it in a specific row of the RecyclerView.
This is the recycler view's custom row layout.
<?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="50dp"
android:id="#+id/drawer_row"
android:background="?android:attr/selectableItemBackground"
android:clickable="true">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_toEndOf="#+id/navigation_image"
android:layout_toRightOf="#+id/navigation_image"
android:textSize="17sp" />
<ImageView
android:id="#+id/navigation_image"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp" />
<TextView
android:id="#+id/horizontal_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#F1F1F1" />
</RelativeLayout>
This is the layout of the Activity having the RecyclerView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/windowBackground">
<RelativeLayout
android:id="#+id/nav_header_container"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:background="#drawable/ic_drawer_header">
<ImageView
android:id="#+id/circle_imageview"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/profile_pic"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="John Doe"
android:textSize="20sp"
android:textColor="#ffffff"
android:layout_below="#+id/circle_imageview"
android:layout_alignLeft="#+id/circle_imageview"
android:layout_alignStart="#+id/circle_imageview"
android:layout_marginTop="5dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Transaction Technologies"
android:layout_below="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:textColor="#ffffff"
android:layout_marginTop="5dp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nav_header_container" />
</RelativeLayout>
I just need to highlight/give feedback of the row which is being touched by the user. How do I achieve it?
Thanks in advance.
You need to add android:focusable="true" in your custom row item for RecyclerView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/drawer_row"
android:background="?android:selectableItemBackground"
android:focusable="true"
android:clickable="true" >
</RelativeLayout>
You can add a Ripple effect by using THIS LIBRARY.Include it in your Gradle files using.
compile 'com.thomsonreuters:rippledecoratorview:+'
Simply put your row layout in a RippleDecoratorView.For eg:
<com.thomsonreuters.rippledecoratorview.RippleDecoratorView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
rdv:rdv_rippleColor="#android:color/holo_blue_dark"
rdv:rdv_rippleAnimationFrames="60"
rdv:rdv_rippleAnimationPeakFrame="15"
rdv:rdv_rippleMaxAlpha="0.8"
rdv:rdv_rippleAnimationDuration="600"
rdv:rdv_rippleRadius="50dp">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/drawer_row"
android:background="?android:attr/selectableItemBackground"
android:clickable="true">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_toEndOf="#+id/navigation_image"
android:layout_toRightOf="#+id/navigation_image"
android:textSize="17sp" />
<ImageView
android:id="#+id/navigation_image"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp" />
<TextView
android:id="#+id/horizontal_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#F1F1F1" />
</RelativeLayout>
</com.thomsonreuters.rippledecoratorview.RippleDecoratorView>
Or you can use any of these libs acc to your requirement:
1>https://github.com/balysv/material-ripple
2>https://github.com/siriscac/RippleView
3>https://github.com/traex/RippleEffect
I can help you but I will need some feedback from you as well. In the onCreateVIewHolder inside your recycler View adapter you must have defined textViews and assigned it to the elements inside the sample recycler view. Add this. Make sure you define the layout outside the function so its accessible throughout the class
RelativeLayout layout;
layout=(RelativeLayout)view.findViewById.(R.layout.drawer_row)
Then go to you recycler view adapter and in the onBindViewHolder define
holder.layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle onClick event
}
});
In the onClick event you can add any kind of feedback you want to the user to experience such as starting a new activity, toasts, messages or in this case a ripple effect. You can use a custom library for ripples such as these
Ripple effect for lollipop views
https://android-arsenal.com/details/1/980
https://github.com/balysv/material-ripple
Ripple effects are not supported in pre-Lollipop devices.
Still use this material-ripple
compile 'com.balysv:material-ripple:1.0.2'
Apply ripple on holder.drawView
MaterialRippleLayout.on(holder.drawerView)
.rippleColor(Color.RED)
.create();
Or you can apply from xml
<com.balysv.materialripple.MaterialRippleLayout
android:id="#+id/ripple"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button inside a ripple"/>
</com.balysv.materialripple.MaterialRippleLayout>
I am trying to set an onClickListener for a RelativeLayout with multiple elements. I have placed the listener on the RelativeLayout but it will not register if you click on top of any of the children elements, only if you click around them. I have tried a few different ways setting the children to clickable="true" or all focusable="false" but cant seem to get the entire layout to act like one button. Is this possible or do I need clickListeners for each of the children essentially doing the same thing?
<RelativeLayout
android:id="#+id/nearbyLikeGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1">
<ImageView
android:id="#+id/nearbyLikeIconImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_centerInParent="true"
android:focusable="false"
android:contentDescription="#string/content_description"
android:src="#drawable/like_icon"/>
<Button
android:id="#+id/nearbyFeedsLikeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_toRightOf="#+id/nearbyLikeIconImageView"
android:focusable="false"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text="#string/like"
android:textColor="#color/white"
android:textSize="12sp" />
</RelativeLayout>
Adapter Class :
RelativeLayout likeGroup = (RelativeLayout)v.findViewById(R.id.nearbyLikeGroup);
likeGroup.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.d("test", "layout group clicked");
}
});
To prevent the child views from consuming the click event you need to set android:clickable="false" on them. I assume only the Button is making problems since ImageViews are not clickable by default.
Try this layout:
<RelativeLayout
android:id="#+id/nearbyLikeIconGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1">
<ImageView
android:id="#+id/nearbyLikeIconImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_centerInParent="true"
android:focusable="false"
android:contentDescription="#string/content_description"
android:src="#drawable/like_icon"/>
<Button
android:id="#+id/nearbyFeedsLikeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_toRightOf="#+id/nearbyLikeIconImageView"
android:focusable="false"
android:clickable="false"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text="#string/like"
android:textColor="#color/white"
android:textSize="12sp" />
</RelativeLayout>