Right way with two imageView onClick? - android

The problem is next, when I typed in ImageView mIV_WishItem "heart", caused onClick background ImageView mIV_WishItem
The code this buttons:
holder.mIV_WishItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setFavorites(holder, sectionsItem, true);
}
});
holder.mIV_pictureGoodsItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SectionsItem sectionsItem = sectionsItems.get(position);
sectionItemId = sectionsItem.getId();
getDataForItemDetails(mContext);
}
});
And xml for this buttons
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/_200sdp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="end"
>
<ImageView
android:id="#+id/iV_Wish"
android:layout_width="#dimen/bottom_navigation_icon"
android:layout_height="#dimen/bottom_navigation_icon"
/>
</LinearLayout>
<ImageView
android:id="#+id/iV_pictureGoods_Item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
/>
</RelativeLayout>
Please help me find solution

You need to add attribute in your ImageView(mIV_WishItem) :
android:focusable="true"
android:focusableInTouchMode="true"
Viewholder item itself focusable, so we need to set focus on imageview. After that we can enable click on imageview.

Related

How to dynamically change content inside Modal Bottomsheet android

I have a Bottomsheet xml which contains two elements, Follow and Copy link.. when I click on Follow, it should be changed to Unfollow dynamically. I tried using setText, not worked.
Please guide me how to change text of bottomsheet dialogues.
Here is my Bottomsheet xml file
<LinearLayout app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="#+id/bottom_sheet_follow"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:foreground="?android:attr/selectableItemBackground"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_mode_follow_grey_24dp"
/>
<TextView
android:id="#+id/followTView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="Follow"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_sheet_copy"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:foreground="?android:attr/selectableItemBackground"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_copy_grey_24dp"
/>
<TextView
android:id="#+id/copyLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="Copy link"/>
</LinearLayout>
Bottomsheet Java code
View bottomSheetView = View.inflate(mContext, R.layout.bottom_sheet_dialog_for_sharedposts, null);
mDialog = new BottomSheetDialog(mContext);
mDialog.setContentView(bottomSheetView);
mDialog.show();
followBtnLayout = bottomSheetView.findViewById(R.id.bottom_sheet_follow);
followTView = bottomSheetView.findViewById(R.id.followTView);
followBtnLayout .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
followTView.setText("Unfollow");
}
});
But, the Follow Textview (I mean followTView) is not changing to Unfollow.
Please tell me where I have gone wrong...
Thanks in advance...
You can do it with boolean i have done it with my imageview.
Might help you
//Initialize boolean as true by default
public boolean showingFirst = true;
holder.ivFavorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (showingFirst){
Toast.makeText(mActivity, "Item added to favorite", Toast.LENGTH_SHORT).show();
holder.ivFavorite.setImageResource(R.drawable.filled_heart);
showingFirst = false;
}else {
holder.ivFavorite.setImageResource(R.drawable.ic_favorite);
Toast.makeText(mActivity, "Item removed from favorite", Toast.LENGTH_SHORT).show();
showingFirst = true;
}
}
});
Just a guess. Did you try to re-arrange the code? maybe the the click listener should load first before calling "mDialog.show()".
View bottomSheetView = View.inflate(mContext, R.layout.bottom_sheet_dialog_for_sharedposts, null);
followBtnLayout = bottomSheetView.findViewById(R.id.bottom_sheet_follow);
followTView = bottomSheetView.findViewById(R.id.followTView);
followBtnLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
followTView.setText("Unfollow");
}
});
mDialog = new BottomSheetDialog(mContext);
mDialog.setContentView(bottomSheetView);
mDialog.show();

buttons inside listview don't work properly?

in my list_item I have two buttons inside each item. the problem is that the buttons don't work unless I click the item then click the buttons.
Here for example I have to click on the item then click on the delete icon to delete the item.
ListView with two items
list_item :
<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="wrap_content"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin"
>
<ImageView
android:id="#+id/play_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:padding="5dp"
android:src="#drawable/ic_play_circle_black_24dp"
android:tint="#4CAF50"
android:layout_weight="1"
/>
<ImageView
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:padding="5dp"
android:src="#drawable/ic_delete_black_24dp"
android:tint="#FF0000"
android:layout_weight="1"
/>
</LinearLayout>
Sorry everyone for lateness!
I've got the solution; the problem was that I deleting the ListView item in the Activity instead of doing it in the Adapter ;)
the Code that doesn't work (MainActivity) :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
// delete button
ImageView deleteBtn = view.findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mAdapter.remove(mAdapter.getItem(i));
}
});
}
});
The correct code (Adapter) :
deleteBtn.setTag(position);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer index = (Integer) deleteBtn.getTag();
remove(getItem(index));
}
});
Thank you so much everyone for helping :)

How to make the parent view to handle all the OnClick events?

I have a Layout that has a button that shows another view, but only for 1 time, I mean, you click the button, it dissapear and the other view is shown, the second time you should click the parent view of that button and the other view (the one that was shown) should dissapear and the button should appear again. I am trying with clickable:false and focusable:false but it is not working. How can I achieve that?
Relevant Code
XML
<LinearLayout
android:id="#+id/item_tournament_header"
android:background="#drawable/bg_card_tournament"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="15">
<RelativeLayout
android:clickable="false"
android:focusable="false"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent">
<com.github.siyamed.shapeimageview.CircularImageView
android:id="#+id/item_friend_img_profile_pic"
android:layout_height="48dp"
android:layout_width="48dp"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_profile"
app:siBorderColor="#color/white"/>
</RelativeLayout>
<LinearLayout
android:clickable="false"
android:focusable="false"
android:layout_weight="10"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="#+id/tournament_name"
android:textSize="#dimen/text_h3"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tournament_client"
android:textSize="#dimen/text_p"
android:textColor="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<RelativeLayout
android:clickable="false"
android:focusable="false"
android:layout_weight="2"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:id="#+id/btn_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_plus_tournaments"/>
</RelativeLayout>
</LinearLayout>
Java
btn_plus = (ImageView) findViewById(R.id.btn_plus);
TournamentContent =(LinearLayout)findViewById(R.id.item_tournament_content);
TournamentHeadaer =(LinearLayout)findViewById(R.id.item_tournament_header);
btn_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TournamentContent.setVisibility(View.VISIBLE);
btn_plus.setVisibility(View.GONE);
}
});
TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(btn_plus.getVisibility()==View.VISIBLE)
{
// It's not entering here!!!
TournamentContent.setVisibility(View.GONE);
btn_plus.setVisibility(View.VISIBLE);
}
}
});
btn_plus = (ImageView) findViewById(R.id.btn_plus);
TournamentContent =(LinearLayout)findViewById(R.id.item_tournament_content);
TournamentHeadaer =(LinearLayout)findViewById(R.id.item_tournament_header);
btn_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TournamentContent.setVisibility(View.VISIBLE);
btn_plus.setVisibility(View.GONE);
}
});
TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(btn_plus.getVisibility()==View.GONE)
{
// It's not entering here!!!
TournamentContent.setVisibility(View.GONE);
btn_plus.setVisibility(View.VISIBLE);
}
}
});
Maybe i miss understood the problem, but i think that your problem is the logic of if statement : if(btn_plus.getVisibility()==View.VISIBLE)
that should be :
if(TournamentContent.getVisibility()==View.VISIBLE) or if(btn_plus.getVisibility()==View.GONE)
Try to modify your code like this:
btn_plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TournamentContent.setVisibility(View.VISIBLE);
btn_plus.setVisibility(View.INVISIBLE);
}
});
TournamentHeadaer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(btn_plus.getVisibility()==View.INVISIBLE)
{
TournamentContent.setVisibility(View.GONE);
btn_plus.setVisibility(View.VISIBLE);
}
}
});

Android RelativeLayout and OnClickListener

I found many many threads and answers on how to make a Relative layout respond to click event. I've implemented my layout according to them. But still OnClickListener is not called. I have also given selector for it and it works well.
my_list_item xml:
<?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/air.com.jingit.mobile"
android:layout_width="match_parent" android:layout_height="#dimen/actionBarHeight"
android:clickable="true"
android:background="#drawable/my_list_selector">
<com.loopj.android.image.SmartImageView
android:layout_width="#dimen/actionBarHeight"
android:layout_height="#dimen/actionBarHeight"
android:id="#+id/image"
android:scaleType="fitCenter"
android:layout_marginLeft="20dp"
android:clickable="false"
android:focusable="false"/>
<com.uma.mobile.view.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Loading..."
android:id="#+id/userName"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Regular"
android:textSize="20sp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/image"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#000"
android:clickable="false"
android:focusable="false"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#aaa"
android:layout_alignBottom="#+id/image"></FrameLayout>
and this is where I inflate the view:
public MyListItem(final Context context, final Integer retailerId) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.my_list_item, this);
image = (SmartImageView) view.findViewById(R.id.image);
userName = (CustomTextView) view.findViewById(R.id.userName);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("INSIDE LIST","CLICK");
}
});
}
Edit 1:
This Relative layout is added to a linear layout which is inside a scrollview, so that it works like a list view. Does this has something to do???
list_view.xml
<?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="wrap_content"
android:background="#eee">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listContainer" />
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_centerHorizontal="true"
android:src="#drawable/up_indicator"
android:layout_alignBottom="#+id/scrollView" />
Once i had a RelativeLayout not firing onClick event and it was because i had a Button in the middle of it, and the Button's onClick event was being fired, instead of the RelativeLayout one. Solved it implementing a listener for both of them:
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
//Handle onClick
}
};
relativeLayout.setOnClickListener(listener);
button.setOnClickListener(listener);
Hope it helps!
Add OnClickListener in your RelativeLayout java Class

How can I enable jumping to linked page if user clicks TextView?

XML Layout
<LinearLayout style="#style/behindMenuScrollContent"
android:paddingTop="25dp" >
<TextView
style="#style/behindMenuItemTitle"
android:text="People" />
<LinearLayout
android:id="#+id/iconViewLink4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<jp.fureco.IconView
android:id="#+id/iconViewItem4"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textSize="20dp"/>
<TextView
android:layout_marginLeft="10dp"
style="#style/behindMenuItemLabel"
android:text="Visitor" />
</LinearLayout>
</LinearLayout>
Then I have myWebView just like this
WebView myWebView = (WebView)findViewById(R.id.webView1);
When a user clicks on iconViewLink4, I want it jump to the url "http://example.com/messsage"
How can I code that?
Yes you can. Just use android:onClick="methodName" in <LinearLayout> or add it programatically by using setOnClickListener like
LinearLayout linLay = (LinearLayout)findViewById(R.id.iconViewLink4);
linLay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// the method or code that will take control to your URL
}
}
Make it clickable and give it an OnClickListener (you can do this on any View)
<LinearLayout
android:id="#+id/iconViewLink4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
findViewById(R.id.iconViewLink4).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something here
}
});

Categories

Resources