How to disable complete list view - android

I want to disable entire list view at the first instance. So how to achieve that?
I have put a button( All buttons are click able from the item in listview) on the customized list view so on click of that button i am doing some processing.After finishing that i want to enable only that item in listview.

I think you have a editext and a button as row items..
In your getview method of your adapter..
edittext.setClickable(false);
edittext.setFocusableInTouchMode(false);
and in onclick of the button put
private OnClickListener btnOnClickListener = new OnClickListener() {
public void onClick(View v) {
edittext.setClickable(true);
edittext.setFocusableInTouchMode(true);
}
};
button.setonClickListener(btnOnClickListener);

Related

clickable listview for activity switching

I am using listviews instead of button in my application. I want to set OnClickListener() on listview instead of setOnItemClickListener().
here is my code:
listview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), BillingActivity1.class));
}
});
can someone suggest a method to do OnClickListener()?
thank you
Ok, I understood your problem.
Lets analyze cases:
Listview with items
If your ListView has some items, the good approach is to set anyway an onItemClickListener and, based on which item has been clicked, do something. You can also do the same thing for each item without considering which item has been pressed, but this is still the best approach.
ListView with no items
From Docs:
the list view will be hidden when there is no data to display.
ListView (usually) has the height set to wrap_content, so even setting the onClickListener on an empty list won't work since the list will result having height of 0, being not able to be clicked (you can't click a view with no height since it is not visible).
Perform actions on an empty ListView
If, as it looks like, you need to do some stuffs on your ListView even if it's empty, just add a Button or a FloatingActionButton to your Activity and then use those: you can both keep the button in any case (like an "Add item" Button) or you can make it visible only if the ListView is empty. something like:
xml
<Button
android:id="#+id/buttonEmptyListStuffs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List is empty, click me!"
android:visibility="gone"/>
activity
//init the button and do other stuffs
...
buttonEmptyListStuffs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//go to activity 2
}
});
...
List<MyListViewItem> myListViewItems = //init your list of items for the listView
buttonEmptyListStuffs.setVisibility(myListViewItems.size() > 0 ? View.GONE : View.VISIBLE);
...
Note: I wrote this code by hand without compiler so it might not be perfect, just take the concept behind it
As Pskink said from comments
I forgot to mention that ListView has setEmptyView(View) which allows you to set a custom layout for the Listview if it is empty. Refer to his link for a good tutorial
USe this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
startActivity(new Intent(getApplicationContext(), BillingActivity1.class));
}
});
If you really want to handle click on any Point of your listView. You can put the list inside a FrameLayout or LinearLayout and then add your onClickListener on the layout.
findViewById(R.id.my_parent_list_layout).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO your stuff
}
});
If your list item count is empty the dynamaically change the visibility of the list view with a button(switching the visibility with list view and button) and give the click action to the button.

Where should a click listener be in a recycler view?

item of my recycler view has some different views which has to have a click listener if i add a click listener to recycler view with view and position parameters it always take the layout behind the views on click. Therefore, i am setting my click listeners in onBindViewHolder like this:
holder.myTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// stuff
}
});
holder.myImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// stuff
}
});
I wonder whether it is a correct method?
Where should a click listener be in a recycler view?
There are many ways for achieve this, but ;
Both is correct as i see on your codes.Means, if you set those items with myTextView, the users will be able to click on TextView (f.g) for showing that RecyclerView item content or whatever and other stuffs like ImageView won't work for listener.
And this is correct for myImageView too, if you set those items with it for listener, the users will be able to click only in this myImageView.
By the way, i think it is better to use implements View.OnClickListener for whole of the Holder.
Something like this:
https://stackoverflow.com/a/24471410/4409113
or Jacob's answer

How to manipulate data in list view that is populated by custom cursor adapter

I have populated a list view using the custom cursor adapter, That listview consists of a checkbox and button in the below manner.
checkbox1 button1
checkbox2 button2
.
.
.
Now I want to navigate to another activity when click on the button in list view now my issue here is when I click on the button1 then I want to see the data in checkbox1 and similarly for other rows as well.
Now I am confused on how to maintain a sync between the checkbox and button and how to achieve the click functionality. How can I achieve this?
You can do the following on the Button's onClickListener if the Button and CheckBox share the same parent
boldButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
View parentView = (View) view.getParent();
CheckBox checkBox = (CheckBox) parentView.findViewById(R.id.check);
}
});
This will give you the CheckBox corresponding to the clicked Button
You will need to find your corresponding view in the list view implementing the onclickListener
For eg
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Button b = (Button)v.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(this,SecondActivity.Class)
startActivity(i);
}
});
//Similarly for the checkbox
}
});
Use a ViewHolder class in adapter class and use that in getView(), Then within getView
viewHolder.yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(viewHolder.checkbox.isChecked())
// toggle the check and do your work here
}
});
You can learn ViewHolder pattern here its very simple
Another example link
This Selectable ListView Tutorial link should help in your solution.
Check this post might help.
You have to preserve the state of row in listview so row can be populated with its state.
Android how to identify item in listview with checkbox

Disable onClick on part of ListItem

I have a ListView in which each ListItem row contains some TextView and an ImageView.
When the image is clicked some XYZ operation is performed.
But I want to disable onClickListener on rest of the ListItem row.
How can this be done?
Thank You
You need to "play" with ListView settings:
// Enable elements inside listview
myListView.setItemsCanFocus(true);
//disable the focus on the whole list items
myListView.setFocusable(false);
myListView.setFocusableInTouchMode(false);
myListView.setClickable(false);
Next you need to add the attribute onClick in your imageview
<ImageView
android:id="#+id/img"
android:onClick="function"
/>
Function
public void function(View v)
{
...
}
just disable the clickable for the Listview add the android:onclick="call" attribute to the imageview
and declare call method on the corresponding activity of the Listview.
public void call(View view){
//do Something
}

List Items postion click on each row issue in android

I have a list of items in Listview.
In each row, I have Text and image. Currently, the click effect is there for the entire row. I want to add a specific click effect to text view and image.
How to do that?
I guess you have a custom adapter for your listview... so if you override the getView method, there you can set the onclick listener for each of your views.
image.setOnClickListener(new OnClickListener()
{
#Override
public void onClick()
{
// Do something
}
});
and
text.setOnClickListener(new OnClickListener()
{
#Override
public void onClick()
{
// Do something
}
});
Now, Doyou want the same listener defined in onItemClickListener in each of your views? easy: you can define your performance in another method, and only call it where you want.
I hope this help

Categories

Resources