Android - Click through a TextView when not long-pressing (selecting) the TextView - java

What I'm trying to achieve is have a TextView inside of a CardView (that's in a RecyclerView) be selectable only when you long-press the TextView. And if you don't long-press the TextView, it is ignored and the input (click) is passed on to the parent (CardView) which has it's own event to do when pressed.
I've tried one solution where I add a "OnLongClickListener" to the TextView and enable "selectable" when triggered. However when you short-press the TextView, it blocks the input from reaching the parent CardView, which prevents the CardView from performing it's action, which is expanding the CardView.
textView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
textView.setTextIsSelectable(true);
return true;
}
});
I'm trying to find a solution to have the TextView be essentially ignored and pass the input to it's parent when it isn't long-pressed. Anyone have any ideas on how to achieve this?
I've looked here already: TextView selectable just on long click

Just write textView.setTextIsSelectable(true);
Remove onLongClick event, When you long press on Textview then it auto select textview text

Related

How to refresh layout/context from list adapter (I think this is what i need)

Im having a bug that i cant understand the reason and how to resolve it. I belive that is a problem of layout/view/context refresh but i dont know.
I have a cell from a listView(I prefer recyclerView but the project has years) and in the corner of the cell i have a button to show more options. Programatically it just make an element View.GONE and another element View.VISIBLE.
I will attach code in a moment
To this button i setted too a listener that when i tap on it it do the opposite of below mentioned. It shows some elements and hide an entire LinearLayout from the cell. The elements are showed BUT the LinearLayout keeps on the screen like bugged. If i tap anywhere it disappears and if i try to tap on it it disappears too. Its like the view of that linear got bugged and keep in there like a ghost view. I will shop some pictures.
The cell normally at the beginning: https://imgur.com/1BjK0KP
The cell after i press the entire view to show the LinearLayout at the bottom of the cell: https://imgur.com/eONSptW
The cell after i press the arrow of the corner to hide the LinearLayout. Here it shows the view bugged https://imgur.com/jrT0qxV
The cell after i tap anywhere else https://imgur.com/XD1jN7U
public void expandView(View view){
final View cellView = view;
final LinearLayout editLinear = (LinearLayout) view.findViewById(R.id.cart_edit);
editLinear.setVisibility(View.VISIBLE);
final TextViewFont countText = (TextViewFont) view.findViewById(R.id.itemCount);
countText.setVisibility(View.GONE);
final TextViewFont total = (TextViewFont) view.findViewById(R.id.itemTotal);
final ImageView imageViewArrow = (ImageView) view.findViewById(R.id.cart_edit_image);
imageViewArrow.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_arrow_up));
//notifyDataSetChanged();
imageViewArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
countText.setVisibility(View.VISIBLE);
editLinear.setVisibility(View.GONE);
imageViewArrow.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_arrow_down));
}
});
First of all, you don't need to store context because in View you have the method view.getContext().
I recommend this article:
https://possiblemobile.com/2013/06/context/
On the other hand, ensure that you don't have a ghost view that is overlapping your image view.

Show delete button inside row of a ListView when clicking a button Android

I need to implement a kind of Slide to Delete inside my ListView but what I want is not to slide the row but when the user clicks a button inside the row the delete button should show up. Is it possible? I've already implemented the Button and I know when the user press it but I pretty much have no idea on how to implement the delete button slide in effect from the right of the row.
Can someone give me an Idea on how to start?
If you don't want Swipe-to-delete, then the implementation is pretty simple.
Basically you are gonna show/hide that button on button click.
Since Android ListView reuses the Views, if you show/hide the button of a View of ListItem in onItemClick(), then when the list is scrolled the state will be lost and will result in incorrect behavior.
So we need to maintain this selection state in the Adapter's modal class. Just add a field like selected in the modal class. For instance, if you are showing list of Contact objects, then your modal class will be all like,
class Contact {
private String name;
private String number;
private boolean selected;
..........
}
from what I've understand; you need to implement lets say an edit button, then when user clicks on it, it will disappear and another button called delete will popup instead of it on the same position.
here is a possible approach to achieve that:
in your list_view_adapter.xml:
1-create a frame layout contain both of the buttons on top of each other.
2-default state of delete button is GONE -> android:visibility="gone"
3-when edit is pressed delete button will be visible and you will programmatically set Edit visibility to GONE
4-set your delete button:
#Override
public View getView(final int pos, View convertView, ViewGroup parent) {
View v = convertView;
// Some other things...
Button delete = (Button) v.findViewById(R.id.delete);
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// After you delete the object from Parse database here,
notifyDataSetChanged();
}
}

How I can implement a ExpandableListView in java (for Android) that make click display items?

I'm trying to implement a ExpandableListView in java that when click deployment space elements inside, not the typical wish list allows choosing an option. What I try to do is basically something like this
One can see that the spinner "wraps" to have items and does not have a list, Would I could help a little?
You can use ExpandableListView .. check this tutorial http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
You can easily create one on your own. Just create a Button and assign a Layout below. When the Button gets clicked set the View of you Layout to Visible and otherwise to Invisible. Additionaly you can set up and down arrows to the right of your Button.
final Button button = (Button) findViewById(R.id.button);
final LinearLayout linearLayout= (LinearLayout) findViewById(R.id.linearLayout);
linearLayout.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (linearLayout.getVisibility() == View.VISIBLE) {
linearLayout.setVisibility(View.GONE);
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.expand, 0);
} else {
linearLayout.setVisibility(View.VISIBLE);
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.collapse, 0);
}
}
});

how to make entire view in dialog clickable?

i have dialog and text inside eache view that i can click on .
i want all the view to be clickable insted only the text .
the code :
final Dialog dialog = new Dialog(List_Lists.this);
dialog.setContentView(R.layout.dialog_edit_tables);
dialog.setTitle("Action for " + table);
TextView delete = (TextView) dialog
.findViewById(R.id.tvDeleteTable);
TextView cancel = (TextView) dialog.findViewById(R.id.tvCancel);
OnClickListener l = new OnClickListener() {
#Override
public void onClick(View v) {
dbAdapter = new DBmethods(getApplicationContext());
switch (v.getId()) {
case R.id.tvDeleteTable:
viewListsAdapter.listsV.remove(pos);
dbAdapter.deleteTable(table);
break;
case R.id.tvCancel:
dialog.dismiss();
break;
case R.id.bTableRenameName:
if(dbAdapter.checkTableNameOK(List_Lists.this ,newName.getText().toString())){
viewListsAdapter.listsV.remove(pos);
viewListsAdapter.listsV.add(pos, newName.getText().toString().trim());
dbAdapter.renameTable(table , newName.getText().toString().trim());
renamedialog.dismiss();
};
break;
default:
break;
}
// if button is clicked, close the custom dialog
dialog.dismiss();
lv.invalidateViews();
}
};
delete.setOnClickListener(l);
cancel.setOnClickListener(l);
dialog.show();
}
});
}
as i said this code work fine if user press on text inside the view , but the empty view obviously wont respond . thanks !
if user press on red spot it activate the enter , if green than duplicate and so on...the current status is u have to press on the word , if u press on the colored spots it will do nothing
If im not wrong you need this to make layout clickable
try something like this
You can add a OnClickListener on it :
//onCreate
LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout01);
layout.setOnClickListener(yourOnClickListener);
Should be working ;)
create an id of your dialog_edit_tables
let say android:id=#+id\testing in your xml.
now write this
LinearLayout yourlayout = (LinearLayout) findViewById(R.id.testing);
yourlayout.setOnClickListener(l);
hope this will help.
You haven't shown the XML layout but setting the width of the clickable text view to fill_parent should work, also make sure your parent layout also has the width set to fill_parent.
The onClickListener is not based on the text it is based on the actual component, therefore it looks as if the layout_width attribute is set to wrap_content, so the component is only then length of the text, therefore only where the text is, is clickable. Hope this makes sense.
If I am not wrong than you are using listview or tableview to display the text.
You can do one thing. Make the textview layout_width as fill_parent That way you will able to get the touch on the place that you marked.
Hope this will help.

Android: Cannot get a clickable ListView header/footer

I'm trying to set a header and footer in my list view that are clickable buttons. The problem is that the OnClickListener isn't responding to anything and I can't figure out what I'm doing wrong.
$ View header = getLayoutInflater().inflate(R.layout.header_layout, null, true);
getListView().addHeaderView(header);
myAdapter = new myAdapter(this);
header.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do what I want when i click it
}
});
Update
The best solution I ultimately came up with was adding a separate button to the header layout, and then doing it like this:
View header = getLayoutInflater().inflate(R.layout.header_layout, null);
Button headerButton = (Button)header.findViewById(R.id.header_button);
getListView().addHeaderView(header);
headerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// My Click Stuff
}
});
You need to handle the click in the ListView's onItemClick. Simply check if that's the first or last item in the adapter and handle the clicks that way. You need to treat it as an item in the ListView.
I see a few issues:
when inflating the header, use getListView() as the second parameter (root, where you have null now)l
should the header be a View or a ViewGroup? I've ended up using ViewGroup in these situations.
finally -- perhaps you should be setting the click listener on the button in the header instead of the header itself?
There is a way more easier solution:
Just set a "OnClickListener" to the applied View:
View view = inflater.inflate(R.layout.xxx, null);
view.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//do something
}
});
Very easy thing which solved it!
Example with a footer:
mYourListView.addFooterView(footer, null, true);
Then in the OnItemClickListener you can check:
#Override
public void onItemClick(AdapterView<?> parent,
View view, final int position, final long id) {
if (id != -1) {
// do whatever you do with list items
} else {
// do what you need after the footer been clicked
}
(If you need to handle and the header and the footer click, check position - 0 for the header and [adapter.getCount() - 1] for the footer)
This approach will provide the same visual effect while footer click as if the list item been clicked. (But if you do not need that effect just add OnClickListener to the footer and it will intercept all footer clicks)
i think the listview and buttons are fighting for focus and your listview is winning.
you'll have to use a onTouchListener for the buttons.
There is one more alternative and its to set the following on the footer. There is another method overload created but it did not come up in the docs, i had to check online documentation:
mylistView.addFooterView(footerView, null, false);
where false tells the footer its not selectable. I tested this myself and the buttons inside the footer respond to touches now. I hope this an acceptable answer.
What worked for me:
When inflating the header view, before adding it to the listview, I then used that view to get the subview and add a click listener on it
myHeaderView.findViewById(R.id.myButton).setOnClickListener(new OnClickListener() { ... } );
also, when I added it to the listView, I used the overloaded constructor with the third variable set to true
mListView.addHeaderView(myHeaderView, null, true);

Categories

Resources