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 :)
Related
I want to add filter button to my onCreateOptionsMenu. When button was clicked i need to show layout with checkboxes and apply button.
Only way that i know how to do this - start new activity from onOptionsItemSelected and do all operations there. But is there more efficient way? Without leaving current activity. Something like pop-up window etc.
You can do it By Using this Material Dialog
https://github.com/drakeet/MaterialDialog Library and Below code, Just
put the gravity of Dialog TOP|RIGHT.
private void showMaterialDialog() {
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice);
for (Card card : cards) {
arrayAdapter.add(card.getName());
}
alert = new MaterialDialog(this);
View mView = getLayoutInflater().inflate(R.layout.custom_view_virtual_card, null);
ListView listView = (ListView) mView.findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
AppCompatButton btnCancel = (AppCompatButton) mView.findViewById(R.id.btnCancel);
AppCompatButton btnOk = (AppCompatButton) mView.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAsyncForRequestVirtualCard(selectedCardTypeId);
alert.dismiss();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedCardTypeId = cards.get(position).getId();
Log.d("request", cards.get(position).getId() + " " + cards.get(position).getName());
}
});
alert.setView(mView);
alert.show();
}
custom_view_virtual.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Select Card Type"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/colorAccent" />
<View
android:layout_width="match_parent"
android:layout_height="0.25dp"
android:background="#color/divider" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="5dp"
android:divider="#color/transparent"
android:theme="#style/ProgressBarStyle"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp">
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnCancel"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="#string/cancel"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnOk"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
Don't create a new activity instead create a new java class in that apply your sorting logic to filter the content and when your button is pressed that activity should return all your sorted content using List class or ArrayList class or anything else then display it.
I'm having trouble with a ListView.
Basically, i have this hidden ListView to be shown on a TextView click.
This works ok.
When I click on a Item of the ListView, i would like to show another Layout(containing a TextView & a FloatingButton).
The problem is that when I show the LinearLayout, I can't click on the ListItem anymore. Any suggestions?
Here's some code:
Layout.xml
<ListView
android:id="#+id/meal_insertion_meals_portions_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/meal_insertion_location"
android:layout_above="#id/meal_insertion_add_container"/>
<LinearLayout
android:id="#id/meal_insertion_add_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="8dp">
<android.support.design.widget.FloatingActionButton
android:id="#+id/meal_insertion_add_meal_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_add"
android:tint="#android:color/white"
app:backgroundTint="#color/colorPrimary"/>
<TextView
android:id="#+id/meal_insertion_add_meal_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center_vertical|center_horizontal"
android:text="#string/diary.meal.insertion.aliment.add"
android:textColor="#color/colorPrimary"/>
</LinearLayout>
Fragment.java
mealSelector.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (selectorList.getVisibility() == View.VISIBLE) {
selectorList.setVisibility(View.GONE);
mealLocation.setVisibility(View.VISIBLE);
} else {
selectorList.setVisibility(View.VISIBLE);
mealLocation.setVisibility(View.GONE);
}
}
});
selectorList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView t1 = (TextView) view.findViewById(android.R.id.text1);
mealSelector.setText(t1.getText());
selectorList.setVisibility(View.GONE);
mealLocation.setVisibility(View.VISIBLE);
addLayoutContainer.setVisibility(View.VISIBLE);
}
}
);
THIS IS NOT THE RIGHT SOLUTION - Just a workaround:
I just hide the ContainerLayout again when I show the ListView.
This let me clicks on Items again.
AGAIN: Still looking for a better solution.
Try below solutions,
change listview, android:layout_height="wrap_content" to android:layout_height="match_parent", in addition try with removing android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false" from Floating button.
Hi I'm implementing setOnClickListener for GridView parent
Code:
Main layout
<LinearLayout
android:id="#+id/profile_ll_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:descendantFocusability="blocksDescendants"
android:background="#drawable/linear_layout_border"
android:orientation="vertical" >
<ImageView
android:id="#+id/profile_img_friends"
android:layout_width="#dimen/profile_img_apf_width"
android:layout_height="#dimen/profile_img_apf_height"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/cont_desc"
android:scaleType="fitXY"
android:src="#drawable/bg_480_800"
android:visibility="gone" />
<GridView
android:id="#+id/friends_photos_gv"
android:layout_width="#dimen/profile_img_apf_width"
android:layout_height="#dimen/profile_img_apf_height" >
</GridView>
<TextView
android:id="#+id/profile_tv_friends"
android:layout_width="#dimen/profile_img_apf_width"
android:layout_height="#dimen/txt_size_height_profile"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:background="#color/txt_common_white"
android:gravity="center"
android:text="#string/txt_Friends"
android:textColor="#color/txt_common_black"
android:textSize="#dimen/txt_size" />
</LinearLayout>
Activity
LinearLayout friendsLayout = (LinearLayout) findViewById(R.id.profile_ll_friends);
friendsLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ProfileActivity.this, FriendsListActivity.class);
startActivity(intent);
}
});
Adapter Layout
<?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" >
<ImageView
android:id="#+id/friends_photos_img"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/cont_desc"
android:scaleType="fitXY"
android:src="#drawable/bg_480_800"
android:visibility="visible" />
</LinearLayout>
When clicking on TextView (profile_tv_friends) area, setOnClickListener working fine but when clicking on GridView (friends_photos_gv) area, setOnClickListener is not working
For this problem i tried
android:focusable="false"
android:focusableInTouchMode="false"
for ImageView (friends_photos_img) in the adapter layout and also tried
android:descendantFocusability="blocksDescendants"
for LinearLayout (profile_ll_friends), but setOnClickListener is not working when clicking on GridView area
You should use:
friendlayout.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// DO something
}
});
A Gridview works like a listview you can try something like this.
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// DO something
}
});
For more information checkout this link. GridView
Another mistake might be that you are referencing the linearlayout here instead of your actual GridView.
LinearLayout friendsLayout = (LinearLayout) findViewById(R.id.profile_ll_friends);
Replace:
friendsLayout.setOnClickListener(new OnClickListener() {
with:
friendsLayout.setOnClickListener(new View.OnClickListener() {
UPDATE
My button is as big as the view of each ListItem - maybe this causes the problem? Is it possible to press once and let both OnItemclick and OnClick respond?
I have a list view with two textviews and a button. I know there are a lot of questions being posted but I have read a lot of them and not one seems to work.
I want the OnItemClick of each view of the list view to work.
I also want the OnClick of the button inside each view of the list view to work.
Only the onClick of the button seems to respond
In my xml i have set android:focusable="false" for the button. For the textviews I have set android:textIsSelectable="false" android:clickable="false" android:focusable="false"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rectangle_order"
android:layout_gravity="center_horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/order_height"
>
<Button
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textStyle="bold"
android:textSize="#dimen/text_size"
android:textColor="#color/white"
android:rotation="90"
android:textIsSelectable="false"
android:clickable="false"
android:focusable="false"
/>
<TextView
android:id="#+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="#dimen/timer_size"
android:rotation="90"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="29dp"
android:textIsSelectable="false"
android:clickable="false"
android:focusable="false"
/>
</RelativeLayout>
</RelativeLayout>
this is the layout for my list view:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="0.5"
android:layout_width="#dimen/order_height"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
>
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:scrollbars="none"
>
</ListView>
</RelativeLayout>
In the GetView of my adapter i have :
Button b = (Button) rowView.findViewById(R.id.img);
b.setBackgroundResource(R.color.pending);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(numberOfTimers < 2){
view.setBackgroundResource(R.color.ongoing);
countDownTimer = new MyCountDownTimer(TIME_PANINI, 1000,timerTextView, view);
countDownTimer.start();
}
}
});
in my main activity i have a onItemClickListener set to the listView:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG_MAIN, "we are in select onitemclicklistener");
Toast.makeText(getApplicationContext(),"BOOOOH", Toast.LENGTH_SHORT).show();
}
});
Try to replace OnClickListener with OnTouchListener.
Like this:
b.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN){
if(numberOfTimers < 2){
view.setBackgroundResource(R.color.ongoing);
countDownTimer = new MyCountDownTimer(TIME_PANINI, 1000,timerTextView, view);
countDownTimer.start();
}
}
return false;
}
};);
When you return false at this listener, you mean "hey i'm not done with that action(press down), please let the action to the next View"
I have a list view with single selection mode and would like to get access the position outside the setOnItemClickListener event
As of now I can access its position as shown below
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
//btnNxt.setEnabled(true);
Intent i = new Intent(getApplicationContext(),
NewActivity.class);
// Pass a single position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
});
Now I would like to know how do I access the selected position outside(ie..on button click event).
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Here I need to get the position and selected item
}
});
Listview.xml
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#3366CC"
android:dividerHeight="2dp"
android:choiceMode="singleChoice"/>
ItemDetails.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.abc.widget.CheckableRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/sampleimg"
android:layout_width="150dip"
android:layout_height="100dip"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="160dp"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants">
<TextView android:id="#+id/itemname"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/itemdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/carname"
android:focusable="false"/>
<TextView android:id="#+id/itemprice"
android:textSize="19sp"
android:textStyle="bold"
android:textColor="#003399"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/cardesc"/>
<com.example.abc.widget.InertCheckBox android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:button="#drawable/checkbox" />
</RelativeLayout>
</com.example.abc.widget.CheckableRelativeLayout>
OrElse how do I check If any listview item is checked or not on button click ?
EDIT:
This is the way I'm trying to access the position from button click
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),
myposition,
Toast.LENGTH_SHORT).show();
}
});
Simply make a member variable (declare it outside of a method like before onCreate() and use that.
public class ...
{
int pos;
// onCreate() and such
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
pos = position; // initialize it
...
}
});
then use pos inside your onClick() or wherever you need to in the Activity.
If I completely missed something in your question then please explain.