I want to add badge notification to my arraylist.But I couldn't find a tutorial for this.This is my list code:
public void chatList() {
conversationsAdapter=new ArrayAdapter<JsonObject>(this,0) {
#Override
public View getView(int c_position,View c_convertView,ViewGroup c_parent) {
if (c_convertView == null) {
c_convertView=getLayoutInflater().inflate(R.layout.random_bars,null);
}
JsonObject user=getItem(c_position);
String name=user.get("name").getAsString();
String image_url="http://domain.com/profile/thumb/"+user.get("photo").getAsString();
TextView nameView=(TextView)c_convertView.findViewById(R.id.tweet);
nameView.setText(name);
ImageView imageView=(ImageView)c_convertView.findViewById(R.id.image);
Ion.with(imageView)
.placeholder(R.drawable.twitter)
.load(image_url);
return c_convertView;
}
};
ListView conversationsListView = (ListView)findViewById(R.id.conversationList);
conversationsListView.setAdapter(conversationsAdapter);
conversationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
startChat(conversationsAdapter.getItem(position));
}
});
}
My list:
I want to add badge to this list.How can I do this ?
Ps:I found a library https://github.com/jgilfelt/android-viewbadger but I don't want to use it.
Inflate the layout in custom listview which extends base adapter instead of array adapter. Add following layout into the custom list item layout.
drawer_list_item.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="48dp"
android:background="#drawable/list_selector">
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/desc_list_item_icon"
android:src="#drawable/ic_home"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/list_item_title"
android:gravity="center_vertical"
android:paddingRight="40dp"/>
<TextView android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/counter_bg"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:textColor="#color/counter_text_color"/>
</RelativeLayout>
Use below drawable for background textview counter and set notification count value in textview
counter_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- view background color -->
<solid android:color="#color/counter_text_bg" >
</solid>
<!-- If you want to add some padding -->
<padding
android:right="3dp"
android:left="3dp" >
</padding>
<!-- Here is the corner radius -->
<corners android:radius="2dp" >
</corners>
</shape>
Related
how to customise autocompletetextview drop-down background like this image
currently i'm using recyclerViewand custom adapter with filterable but i need to visible and hide recyclerview and it manage to hard that's why i finding solution for customise autocompletetextview dropdown background , any help can appreciative,Thank you in advance
Here is the relevant code that I used.
Custom Typeface
The trick to this one is that I had to set my font to both mAutoCompleteTextView and tvAutocompleteListItem in my Activity.
Remove Shadow
I set the background of mAutoCompleteTextView to be R.drawable.autocomplete_dropdown. In that drawable the important line is
<stroke
android:width="0dip"
android:color="#color/cp_green" />
Radius
The radius was set in R.drawable.autocomplete_dropdown like this:
<corners
android:radius="20dip"/>
MainActivity.java
private void setAutoCompleteListener() {
mAutoCompleteTextView.setDropDownBackgroundDrawable(
mContext.getResources().getDrawable(R.drawable.autocomplete_dropdown));
mAutoCompleteTextView.setAdapter(
new AutoCompleteAdapter(mContext, R.layout.autocomplete_list_item, mLatLng));
mAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String autoCompleteText = (String) adapterView.getItemAtPosition(i);
mAutoCompleteTextView.setText(autoCompleteText);
initiateSearch();
hideKeyboard();
}
});
mAutoCompleteTextView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void afterTextChanged(Editable editable) {
if (editable.length() > 0) {
mClearTextIcon.setVisibility(View.VISIBLE);
} else {
mClearTextIcon.setVisibility(View.INVISIBLE);
}
}
});
}
public void applyFonts() {
Log.d(TAG, "Applying Fonts.");
FontHelper.applyFont(findViewById(R.id.rlMap), mContext);
font = Font.getInstance(getApplicationContext());
mAutoCompleteTextView.setTypeface(font.mAvenirLTStandardLight);
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.autocomplete_list_item, null);
TextView tvAutocompleteListItem = (TextView) view.findViewById(R.id.tvAutocompleteListItem);
tvAutocompleteListItem.setTypeface(font.mAvenirLTStandardLight);
}
autocomplete_dropdown.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#color/white" />
<stroke
android:width="0dip"
android:color="#color/cp_green" />
<corners
android:radius="20dip"/>
<padding
android:left="25dip"
android:top="10dip"
android:right="25dip"
android:bottom="10dip" />
</shape>
autocomplete_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:textColor="#color/gray_text"
android:textSize="14sp"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:id="#+id/tvAutocompleteListItem"/>
activity_main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/search"
android:id="#+id/search"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="10dp"
android:paddingEnd="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#drawable/search_bar"
android:id="#+id/search_bar">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/logo_image"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="0dp"
android:id="#+id/ibLogoImage"
android:contentDescription="#string/logo"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/search_icon"
android:layout_centerVertical="true"
android:layout_marginStart="0dp"
android:layout_marginEnd="15dp"
android:layout_alignParentEnd="true"
android:id="#+id/ibSearch"
android:contentDescription="#string/search_hint"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ibClearText"
android:layout_toStartOf="#id/ibSearch"
android:background="#drawable/clear_text"
android:visibility="invisible"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="20dp"
android:contentDescription="#string/clear" />
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/actvSearch"
android:hint="#string/search_or_enter_address"
android:background="#color/transparent_white"
android:textSize="14sp"
android:textColor="#color/black"
android:completionThreshold="3"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_centerVertical="true"
android:layout_toEndOf="#id/ibLogoImage"
android:layout_toStartOf="#id/ibClearText"
android:dropDownAnchor="#id/search_bar"
android:dropDownVerticalOffset="10dp" />
</RelativeLayout>
</RelativeLayout>
You can use the following function to Change the background of dropdown
setDropDownBackgroundResource();
I am trying to change text color of a TextView inside a listview item.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setSelected(true);
ViewGroup selectedGroup = (ViewGroup)view;
((TextView)selectedGroup.getChildAt(4)).setTextColor(Color.parseColor("#336699"));
String mID =String.valueOf(((TextView) selectedGroup.getChildAt(4)).getText());
}
});
But nothing happens, I can get text value of the textview, though. What might be wrong in this code?
This is, by the way, my listView layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="4dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#336699"
android:id="#+id/questionTitle"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#444444"
android:id="#+id/questionDate"
android:textSize="12sp"/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="#+id/questionContent" android:value=""/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="#+id/questionSenderContact" android:value=""/>
<TextView android:layout_height="0dp" android:layout_width="0dp" android:visibility="invisible"
android:id="#+id/messageID" android:value=""/>
</LinearLayout>
EDIT: I was getting wrong textview. Index of it was not 4, but 0. Sorry for a question caused by lack of attention.
try this code:
public View row;
your_list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v,
int position, long id) {
if (row != null) {
row.setBackgroundResource(R.color.orange);
}
row = v;
v.setBackgroundResource(R.color.transparent_green);
)};
Create a new StateListDrawable like you did before but with black as default color and white when pressed.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/black" />
<item android:state_focused="true" android:color="#color/black" />
<item android:state_pressed="true" android:color="#color/black" />
<item android:color="#color/white" />
</selector>
Now for in the TextView change the text color to the new drawable:
android:textColor="#color/list_item_text"
More on StateListDrawables: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
you will have to set textview as tag to the clickable button as:
...
view.setOnClickListener(this);
view.setTag(textView);
...
public void onclick(View v){
...
TextView textView = (TextView)v.getTag();
textView.setTextColor(getResources().getColor(R.color.YOURCOLOR));
...
}
I have an app which uses a ListFragment. I want to style my own ListItem layout but don't know how to get desired effect. I want to it to look something like this:
I want that big imageView, little shaded stripe with TextView and Like button. Any ideas how to solve this ? especially that shading part.
You will need to set an adapter to your ListFragment using setListAdapter(adapter).
You can create your own adapter implementing BaseAdapter.
Then you override getView like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
CompleteListViewHolder viewHolder;
if (convertView == null)
{
LayoutInflater li = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.listitem, null);
}
ImageView image = (ImageView)v.findViewById(R.id.image);
TextView text = (TextView)v.findViewById(R.id.text);
... add the like button etc
return v; }
listitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/FrameLayout1"
android:layout_width="200dp"
android:layout_height="200dp" >
<ImageView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/someImage" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:orientation="vertical"
android:background="#drawable/gradient" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text" />
<Button
android:id="#+id/likeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Like" />
</LinearLayout>
</FrameLayout>
gradient.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#00000000"
android:startColor="#FF000000"
android:type="linear" />
<corners
android:radius="0dp"/>
I have a custom listview with a imageview and an textview. I want when user select a item the textview color should change and all the other textview should remain default.
here are my xmls
ListView.xml
<?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" >
<ListView
android:id="#+id/listViewBell"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
ListViewItems.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="#+id/txt_bell_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/rihanna_love_the_way_lie"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<ImageView
android:id="#+id/list_bell_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/bronze_bell" />
</RelativeLayout>
Create following button_text.xml in drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
Change your text view's textColor to:
<TextView
android:id="#+id/txt_bell_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/rihanna_love_the_way_lie"
android:textColor="#color/button_text" //point to that xml
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
You can read about color state list here
Like others have said, just change the color in your onItemClick method. Additionally:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
setItemNormal();
View rowView = view;
setItemSelected(rowView);
}
public void setItemSelected(View view){
View rowView = view;
TextView tv = (TextView)rowView.findViewById(android.R.id.text1);
tv.setTextColor(Color.BLUE);
}
public void setItemNormal()
{
for (int i=0; i< mDrawerList.getChildCount(); i++)
{
View v = mDrawerList.getChildAt(i);
//TextView txtview = ((TextView) v.findViewById(R.id.menurow_title));
TextView txtview = ((TextView)v.findViewById(android.R.id.text1));
txtview.setTextColor(Color.BLACK);
}
}
by doing this we set all items to normal before we change it on each item press.
For that you've to set OnClickListener for that particular ImageView and then change TextView's color on click of ImageView in Adapter Class.
Alternatively,
mList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
TextView tv= view.findViewById(resID);
tv.setTextColor(color)
}
});
Implement setOnItemClickListener for the listView
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long arg3) {
RelativeLayout relativeLayout = (RelativeLayout) view.getParent();
TextView textView = (TextView) relativeLayout.getChildAt(0);
ImageView imaegView = (ImageView) relativeLayout.getChildAt(1);
textView.setTextColor(Color.RED);
// Perform whatever action for your textView and imageView
}
});
after many attempts set color in navigation view, I found out the simplest way to set text color in listview, maybe it will help to someone
res/color/listview_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This is used when the navigation item is selected -->
<item android:state_enabled="true"
android:state_activated="true" android:color="#color/colorAccent" />
<!-- This is the default text color -->
<item
android:color="#android:color/black" />
</selector>
and set in TextView as android:textColor:
<TextView
android:id="#+id/tvItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_vertical_extra_margin"
android:textColor="#color/listview_selector"
android:textSize="#dimen/text_size_normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="Row item text" />
the trouble was in the right state, use android:state_activated in navigation view
I have implemented expandablelistview in android app and also implemented divider in it.
I have one problem in not getting the divider between the last item of the child and next group header.
Following image is how I want :
But following is what I'm getting:
Here if you compare both images the divider between the CID and About Set is not coming how to implement that divider ?
Also the groupIndicator is not changing inspite of providing the xml in the groupindicator containing the item tag with 2 different images in android:state_expanded and android:state_empty.
But the property of android:state_expanded and android:state_empty doesn't appear.
It's very easy to Implement.
1- We will add divider in groub_item_layout and child_item_layout:
group_item_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="wrap_content"
android:background="#color/white"
android:minHeight="50dp"
android:orientation="vertical">
<View
android:id="#+id/adapter_divider_top"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#B6B6B6" />
<TextView
android:id="#+id/tv_adapter_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingBottom="10dp"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:text="Home"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/primary_text" />
</LinearLayout>
child_item_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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tv_adapter_desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:text="description"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#212121" />
<View
android:id="#+id/adapter_divider_bottom"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider" />
</LinearLayout>
2- In your Adapter getChildView() and getGroupView() Methods:
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup viewGroup) {
if (view == null) {
view = mInflater.inflate(R.layout.group_item_layout, null);
mGroupViewHolder = new GroupViewHolder();
mGroupViewHolder.tvTitle = (TextView) view.findViewById(R.id.tv_adapter_title);
mGroupViewHolder.dividerTop = view.findViewById(R.id.adapter_divider_top);
view.setTag(mGroupViewHolder);
} else {
mGroupViewHolder = (GroupViewHolder) view.getTag();
}
// That if you want to show divider in expanded group only.
// If you want to show divider in all group items remove this block.
if (isExpanded) {
mGroupViewHolder.dividerTop.setVisibility(View.VISIBLE);
} else {
mGroupViewHolder.dividerTop.setVisibility(View.INVISIBLE);
}
// That if you want to show divider in expanded group only.
String myTitle = getGroup(groupPosition);
if(myTitle != null) {
mGroupViewHolder.tvTitle.setText(myTitle);
}
return view;
}
#Override
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
if (view == null) {
view = mInflater.inflate(R.layout.child_item_layout, null);
mChildViewHolder = new ChildViewHolder();
mChildViewHolder.tvDesc = (TextView) view.findViewById(R.id.tv_adapter_desc);
mChildViewHolder.dividerBottom = view.findViewById(R.id.adapter_divider_bottom);
view.setTag(mChildViewHolder);
} else {
mChildViewHolder = (ChildViewHolder) view.getTag();
}
if(isLastChild) {
mChildViewHolder.dividerBottom.setVisibility(View.VISIBLE);
} else {
mChildViewHolder.dividerBottom.setVisibility(View.INVISIBLE);
}
Timesheet timesheet = getChild(groupPosition, childPosition);
if(timesheet != null) {
mChildViewHolder.tvDesc.setText(timesheet.getDescription());
}
return view;
}
Hope this helps anyone.
Instead of using ExpandableListView's divider, you can make your own. Set the background of the lists's parent elements to
android:background="#drawable/top_divider"
Where drawable/top_divider.xml contains
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="-1dp" android:right="-1dp" android:bottom="-1dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#android:color/black" />
</shape>
</item>
</layer-list>
Firstly make custom expandable listview...
then add different divider to header and its child
Refer the code(header of expandable listview xml):-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#000000" >
<ImageView
android:id="#+id/divider_top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/divider_vertical" />
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/divider_top"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textColor="#c5e26d"
android:textSize="17dp" />
<ImageView
android:id="#+id/divider_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/lblListHeader"
android:layout_centerHorizontal="true"
android:background="#drawable/divider" />
</RelativeLayout>