i'am using a list in my main activity
and i don't want to use android list , i want to create my own list ,already i created my custom Adapter that enables me to use images and text in any row of list but i want to disable the divider of the list and the hover mode
it's mean that i don't want to have something like this pic
it is not my project screenshot
thanks in advance,
related with Avoid ImageView hover state inside Listview if List Item is pressed
you can use :
set empty background
android:cacheColorHint="#android:color/transparent"
or
android:cacheColorHint = "#00000000"
to remove divider between items in the list use :
myListview.setDivider(null);
or XML:
android:divider="#null"
android:dividerHeight="0dp"
in your adapter, override the method isEnabled(int position) and have it return false for this item.
Since you already have your own CustomListAdapter, do the following in your onCreate() method:
ListView list=(ListView)findViewById(R.id.list); //or whatever the id of your listview is
// Getting adapter by passing xml data ArrayList
YourCustomAdapter adapter = new YourCustomAdapter(this, listDataToBeDisplayed);
list.setAdapter(adapter);
Try code below to disable the highlight, you may change the text color as well
ListView.setSelector(new ColorDrawable(Color.TRANSPARENT));
Related
I am developing an android app, and in one fragment, I already have a listview and have a set of items that will be added to this listview via an arrayadapter. However, I was requested to dynamically color some list items based on a particular property in the original database, specifically bold only those elements to highlight them. Is there any possible way to acheve this with the arrayAdapter class, or if not, considering this is a simplistic use case, what can I do to achieve this
It's very easy to handle this problem you just have to put a condition in your adapter and if that condition is true you just have to customize your item as it's asked from you .
if(condition)
{
view.setBackgroundColor(activity.getResources().getColor(R.color.White));
}
And if you want to set a TextView as Bold programmatically in your adapter check your condition and set it like below :
textView.setTypeface(null, Typeface.BOLD);
you can do it by your ArrayAdapter class,
just put you condition in adapter getView Method, and set your text view as a bold
if(conditionPart){
textView.setTypeface(null, Typeface.BOLD); // For bold
textView.setBackgroundColor(context.getResources().getColor(R.color.Black)); // For Black Color Background
}else{
textView.setTypeface(null, Typeface.NORMAL);// For Noral
textView.setBackgroundColor(context.getResources().getColor(R.color.Gray)); // For Gray Color Background
}
Refer to this tutorial:
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
Extend ArrayAdapter class and override getView() method
Apply conditional statement as mentioned above
You can create your custom Adapter where you can style your text as you want here is sample for custom adapter:
Custom Adapter for List View
I have a listview which contains 2 textViews and an ImageView. Whenever is pressed I want to the selected row to change a bit it's layout. For example change the color of the texts change the imageView add another one etc..I have added a custom list selector for the background to change. My question is which is the correct listener in which I should add:
TextView title;
TextView descr;
title = (TextView) arg1.findViewById(R.id.title);
descr = (TextView) arg1.findViewById(R.id.descr);
title.setTextColor(Color.parseColor("#189dd9"));
descr.setTextColor(Color.WHITE);
I added it to OnItemClickListener and it works correct after the item is clicked not while it is selected.
Inside adapter you can keep a track of which item is selected.
int selectedItemIndex;// ArrayList<Integer> selectedItems=new ArrayList<Integer>();
Now in getView just write if
if(position==selectedItemIndex){
//Inflate different layoout
}else{
//normal flow
}
To set selected items you can set onItemClick Listener and set this variable in adapter or you can add an onClick listener inside getView itself and in it set this variable.
In case you want to have same layout just write different code inside if/else
I did using multiple selectors, according to this link I'm reposting the link just in case someone finds this thread first. cheers!
I am trying to dynamically add information to a ListView. The information I am adding consists of a "Device Name" (the main item) and "MAC Address" (the sub item). An example from online is below. Note: I want to replace Item 1 with a device 1's name, sub item 1 with device 1's MAC address, and so on. This MUST be done dynamically because the list is being populated as devices are scanned for.
.
Before this is marked as a repeat, I have looked at the following questions and they have not helped me: Adding ListView Sub Item Text in Android, How to add subitems in a ListView, Adding Items and Subitems to a ListView
The conclusion I have come to through reading these questions is that I need to implement a custom ArrayAdapter and override the getView() method. I have created a custom layout with two text views in it:
cyan_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/main_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/cyan"/>
<TextView
android:id="#+id/sub_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/dark_cyan"/>
</LinearLayout>
I then try to create a custom ArrayAdapter in my Activity class, but I am lost as to what to put in my public View getView(final int position, View convertView, ViewGroup parent) method. Additionally, is creating a custom ArrayAdapter necessary if all I am trying to do is add a textview sub item?
The answer to your question is: NO, you don't need to create a custom ArrayAdapter if you just want to add items. I recommend, however, creating it if your layout is customized, as you'll gain so much control over the items you're displaying. You didn't add your code where you create your ArrayAdapter, but in your case I'd use this constructor. The important part is the third parameter: In your activity, you should store an ArrayList with the initial items you're adding to your ArrayAdapter, then, if you want to add a new item, you simply add it to the ArrayAdapter and call notifyDataSetChanged() on your adapter. Simply doing that, your item will be added to the layout and displayed. If you need to override the GetView method for your own ArrayAdapter, I recommend this link, it helped me understanding the whole thing.
are you searching some listview example in google like those tutorials :
http://www.vogella.com/tutorials/AndroidListView/article.html
http://www.mkyong.com/android/android-listview-example/
I think they explain step by step how to create a list adapter
You need to add getter method into your Adapter
YourAdapter ...{
List<Device> items = new ArrayList<Device>;
public List<Device> getItems(){
return items;
}
}
then change item that you need
...{
//for 1s item
Device item = getItems().get(0);
item.setTitle(macAdress)
}
and call notifyDataSetChanged for your adapter
...
yourListView.getAdapter().notifyDataSetChanged();
}
Thats it. Now you are able to change your list data.
And for your question, i think yes. Is better to create your own adapter in order to have simple possibility to exentd it later. And in your case (if you dont want to change your adapter after each title change) you deffinetly need custom one. Cheers
I have to show list view as like below image
Here I have the view in the xml as like this
<View
android:id="#id/margin"
android:background="#6DAAE9"
android:layout_width="20.0dip"
android:layout_height="fill_parent" />
I Have to change this background (#6DAAE9) dynamically when list is loaded and I have to fix these colors to corresponding item even the list items are increased.
Can any body help me. Thanks in advance
Basiclly you need to edit your getView method.
you can use
convertView.setBackgroundColor(Color.parseColor("#6DAAE9"));
With your appropriate logic, meaning if you want it to be random set random color or hold an array with the order, etc.
See this listview I added list item as per odd or even row and you can do it checking position in getView() method.And you should put imageview for that and set color rather that set color to row.
if(position==0){
//here set color for imageview which position is 0
}else if(position==1){
//here set color for imageview which position is 1
}
....
I have ListView with custom Adapter which supplies View to ListView in this way:
public View getView(int position, View convertView, ViewGroup parent)
{
RelativeLayout.LayoutParams lineParams;
RelativeLayout line=new RelativeLayout(context);
TextView tv=new TextView(context);
tv.setText("Text in postion="+i);
lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
line.addView(tv, lineParams);
lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);
//checkbox
CheckBox checkBox=new CheckBox(context);
lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);
line.addView(checkBox, lineParams);
return line;
}
And somewhere inside ListView there's setOnItemClickListener(), which should intercept item clicking events. My problem that, whenever I try to add checkbox to item - I don't get any responces from my ListView. If I skip CheckBox or any other Button it works.
I am really stuck with this problem, I have tried all kind of Layouts, aligning, wrapping and so on - useless. Looks like CheckBox interferes ListView item click events.
Any ideas how to overcome?
just add this line into the item views instead of listView itself
android:focusable="false"
check more detail about this from Android custom ListView unable to click on items
If you have ImageButtons inside the list item, you need to add:
android:descendantFocusability="blocksDescendants"
to the root list item element [such as the root layout].
Then within each ImageButton in the list item, you need to add:
android:focusableInTouchMode="true"
This worked for me - but I was using ImageButtons, not the standard button.
I have also faced the same issue I have tried to set android:focusable="false" to listview but it don't work then I add this to listview item.. like in my listview item I have uesed Toggle button which was creating problem, I add android:focusable="false" to Toggle button and listview on item click listener start work again
Add following line to your listView
android:choiceMode="singleChoice"
or make sure to set following lines to your layout text fields
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
I had also had the problem of a Button in my ListView. Unfortunately just setting the focus to false for all objects in my Adapter did not work for me.
I now have a workaround.
In your Adapter create an OnClickListener for the button (or other clickable object) if you have not already done that. In that OnClickListener you call the OnItemClickListener yourself.
public void onClick(View v) {
mOnItemClickListener.setOnItemClick(mListView, v, vPos, vId);
}
It does mean that you will need to give your adapter access to both the parent ListView and the OnItemClickListener.
You can consider to write your on OnTouchEvent in your listview item and send the proper touchEvent to you child view , the button .
Well i know none of the above solutions will work.I tried changing xml attributes but those does not work out, But i implemented it in a new fashion.
Here is how:
Create an interface CheckBoxOnCheckListener with method onCheckBoxChecked and pass needed parameters, implement interface CheckBoxOnCheckListener in your activity or fragment containing listView.
Next in your adapter, declare an mListener as CheckBoxOnCheckListener, and pass this as a parameter to Adapter's constructor from fragment/activity and cast it to CheckBoxOnCheckListener and assign to mListener.
Next set mListener as itemView.onClick or CheckBox.onCheckCheckedListener and onCheckChanged method call mListener.onCheckBoxChecked.
That's it. It will definitely work,it worked for me.
For code just pm.
If you are using ListView in Activity, ensure you have setup setOnItemClickListener()
myListView.setOnItemClickListener(this); // if your activity implement OnItemClickListener