Android add badge to list - android

I have a list,and I want to add badge to each item.I am using this library:https://github.com/jgilfelt/android-viewbadger
And this is my code:
static class ViewHolder {
TextView nameView;
ImageView imageView;
BadgeView badge;
}
public void placeRandomUsers(final String search) {
randomAdapter = new ArrayAdapter<JsonObject>(this, 0) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.random_bars, null);
holder = new ViewHolder();
holder.nameView=(TextView)convertView.findViewById(R.id.tweet);
holder.badge = new BadgeView(getContext(), holder.nameView);
holder.badge.setTextColor(Color.BLACK);
holder.badge.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
holder.imageView=(ImageView)convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position >= getCount() - 3 && search.equals("") == true) {
//loadRandomUsers("");
}
JsonObject user=getItem(position);
String name=user.get("name").getAsString();
String image_url="http://domain/photos/profile/thumb/"+user.get("photo").getAsString();
holder.nameView.setText(name);
Ion.with(holder.imageView)
.placeholder(R.drawable.twitter)
.load(image_url);
holder.badge.setText("1");
holder.badge.show();
return convertView;
}
};
ListView listView = (ListView)findViewById(R.id.list);
listView.setAdapter(randomAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Log.w("Pos","asd"+randomAdapter.getItem(position).get("name").getAsString());
startChat(randomAdapter.getItem(position));
}
});
loadRandomUsers(search);
}
And this is my random_bars.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="4dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/twitter"/>
<LinearLayout
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
style="#android:style/TextAppearance.Medium"
android:id="#+id/handle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/tweet"
style="#android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
When I add badge to each item,nameView is not showing.Here the output:
If I remove the badge codes.Output is:
Here you can see.Image is showing but name is not showing when I use the badge class.What can be cause this ?

I changed my random_bars.xml code to this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="4dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/twitter"/>
<LinearLayout
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
style="#android:style/TextAppearance.Medium"
android:id="#+id/handle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/tweet"
style="#android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Problem fixed.

Related

How to set Visibility of any view inside listview row from button outside the listview

I have edit button on toolbar on click this button i want to show new view that is cross button inside every row of listview. is this possible and if yes how ? Thanks in advance. Below is layout files:
main layout:
<?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:orientation="vertical"
android:background="#color/backgroundColor"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#color/TabColor"
android:weightSum="10"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_weight="1"
android:onClick="back2"
android:layout_gravity="center"
app:srcCompat="#drawable/back_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="7"
android:paddingLeft="#dimen/margin_small"
android:text="List OF Hotels"
android:textSize="#dimen/text_title"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="2"
android:background="#drawable/transparent_bg"
android:layout_marginRight="#dimen/margin_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/edit_jobs"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:text="Edit"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:visibility="invisible"
android:id="#+id/done_editing"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginBottom="#dimen/margin_small"
android:layout_marginTop="#dimen/margin_small"
android:text="Done"
android:textColor="#color/WhiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
<ListView
android:id="#+id/listview1"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
listview row layout where i have to show cross image:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginTop="#dimen/margin_large"
android:layout_marginLeft="#dimen/margin_large"
android:layout_marginRight="#dimen/margin_large"
android:background="#drawable/border"
android:padding="#dimen/margin_large"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:weightSum="10"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text=""
android:layout_gravity="center"
android:layout_weight="9"
android:textColor="#color/TabColor"
android:singleLine="true"
android:textSize="#dimen/text_large"
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:gravity="right"
android:id="#+id/arrow"
android:src="#drawable/rightarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:visibility="invisible"
android:id="#+id/cross"
android:gravity="right"
android:src="#drawable/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text=""
android:layout_weight="5"
android:textColor="#color/blackColor"
android:textSize="#dimen/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="5"
android:text=""
android:gravity="right"
android:textColor="#color/blackColor"
android:textSize="#dimen/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter class:
public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private ArrayList<String> titlelist,datalist;
public MyAdapter(Context context, ArrayList<String> title, ArrayList<String> data) {
super(context, R.layout.listview_row, title);
titlelist= new ArrayList<>();
datalist= new ArrayList<>();
this.context=context;
this.titlelist=title;
this.datalist=data;
}
private static class ViewHolder {
TextView t1,t2;
}
#Override
public int getCount() {
return titlelist.size();
}
#Override
public String getItem(int position) {
return titlelist.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.title);
holder.t2 = (TextView) convertView.findViewById(R.id.data);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.t1.setText(titlelist.get(position));
holder.t2.setText(datalist.get(position));
return convertView;
}
}
you have to add this functionality in your adapter,
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//ur other codes.....
//
//
if(crossButtonPress){
holder.imageView.setImageResource(R.drawable.cross);
}else{
holder.imageView.setImageResource(R.drawable.default_ic);
}
}
In addition, in you actual button's onClick,
crossButtonPress = true;
adapter.notifyDatasetChanged();

Android GridView add header from adapter?

I managed to update the header View of adapters. listview, headerView (from listview), gridview within a single screen. the problem is due to listview and headerView display the same item, I want to hide the list view so as not to appear. how can i do that?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
final Context gridContext = rootView.getContext();
gridView = (GridViewWithHeaderAndFooter) rootView.findViewById(R.id.grid);
this.inflaterGeneral = inflater;
headerView = inflaterGeneral.inflate(R.layout.cell_fragment_home_header_list, null);
listViewHeader = (ListView) headerView.findViewById(R.id.listViewHeader);
rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return rootView;
}
My Imageadapter
class ImageAdapterHeader extends BaseAdapter {
private Context context;
private final String[] mobileValues;
private TextView t;
public ImageAdapterHeader(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View v = convertView;
ViewHolder holder;
final Item i = itemVideo.get(position);
if (i != null) {
final VideoDetail ei = (VideoDetail) i;
//begin
if (convertView == null) {
convertView = inflater.inflate(R.layout.cell_fragment_home_header, null);
holder = new ViewHolder();
holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.imgThumbnail);
holder.txtDuration = (Button) convertView.findViewById(R.id.txtDuration);
holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
holder.txtView = (TextView) convertView.findViewById(R.id.txtView);
holder.txtComment = (TextView) convertView.findViewById(R.id.txtComment);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtDuration.setText(ei.getDuration());
holder.txtTitle.setText(ei.getTitle());
holder.txtView.setText(ei.getViewCount());
holder.txtComment.setText(ei.getLikeCount());
Picasso.with(this.context)
.load(ei.getDetailPreview().replace(" ", "%20"))
.error(R.drawable.ic_launcher).into(holder.imgThumbnail);
holder.imgThumbnail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().runOnUiThread(new Runnable() {
public void run() {
Intent intent = new Intent(getContext(), VideoPlayerActivity.class);
intent.putExtra("videoId", ei.getVideoId());
intent.putExtra("title", ei.getTitle());
intent.putExtra("artistName", ei.getArtistName());
intent.putExtra("view", ei.getViewCount());
intent.putExtra("like", ei.getLikeCount());
startActivity(intent);
}
});
}
});
} else {
System.out.println("Item Null");
}
//end
return convertView;
}
Fragment home
<?xml version="1.0" encoding="utf-8"?>
<in.srain.cube.views.GridViewWithHeaderAndFooter
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:scrollbarStyle="outsideOverlay"
android:verticalScrollbarPosition="right"
android:scrollbars="vertical"
android:descendantFocusability="beforeDescendants"
android:background="#d9d8d8" >
</in.srain.cube.views.GridViewWithHeaderAndFooter>
cell Fragment home header list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listViewHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#d9d8d8"
android:scrollingCache="false" />
</LinearLayout>
Cell Fragment Home header
<?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="wrap_content"
android:orientation="vertical"
android:padding="4dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1" >
<ImageView
android:id="#+id/imgThumbnail"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/jen4d" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgPlay"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="200dp"
android:background="#drawable/button_play"
android:scaleType="fitXY"
android:src="#drawable/button_play" />
</RelativeLayout>
<Button
android:id="#+id/txtDuration"
android:layout_width="50dp"
android:layout_height="20dp"
android:layout_alignBottom="#+id/xtName"
android:layout_alignLeft="#+id/imgThumbnail"
android:layout_alignRight="#+id/imgThumbnail"
android:layout_alignTop="#+id/play"
android:layout_gravity="bottom|right"
android:layout_marginBottom="65dp"
android:layout_marginRight="20dp"
android:background="#drawable/round"
android:gravity="center_horizontal|center_vertical"
android:text="3:20"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#drawable/transparan"
android:orientation="vertical"
android:layout_alignBottom="#+id/xtName"
android:layout_alignLeft="#+id/imgThumbnail"
android:layout_alignRight="#+id/imgThumbnail"
android:layout_alignTop="#+id/txtDuration"
android:layout_gravity="right|bottom">
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="Klepek - Klepek Versi Dance &apos;Dangduters&apos;"
android:textColor="#ff8a65" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.3"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="Janeta Janet"
android:textColor="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.5"
android:orientation="horizontal"
android:paddingBottom="25dp" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_remove_red_eye_white_24dp"
android:paddingLeft="10dp"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="10000"
android:textColor="#FFFFFF" />
<ImageView
android:layout_width="19dp"
android:layout_height="19dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30dp"
android:src="#drawable/ic_chat_white_24dp" />
<TextView
android:id="#+id/txtComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="10000"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Try to use [HeaderGridView][1], it subclass of GridView. I hope it helps.

ListView Items changed automatically in Fragment android

when I scroll the listview the items changed automatically, in other word an item override other item's view like : when I click on favorite button other items has been changed like this
example:
the item that I clicked
I didn't click on this item
here is my code :
CustomListAdapter.java :
public class CustomListAdapter extends BaseAdapter {
private Context context;
LayoutInflater inflater;
boolean isVoter;
public CustomListAdapter(Context c ) {
this.context = c;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.date_view,null);
viewHolder = new ViewHolder();
viewHolder.vote = (ImageButton) convertView.findViewById(R.id.vote);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
isVoter = newsItemArray.get(position).isVoter();
viewHolder.vote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!Utility.isNetworkAvailable(context))
Toast.makeText(context, R.string.no_network, Toast.LENGTH_LONG).show();
else
voteBtn( position);
}});
return convertView;
}
public void voteBtn(int position){
if ( !isVoter ) {
viewHolder.vote.setImageResource(R.drawable.button_pressed);
}
else{
viewHolder.vote.setImageResource(R.drawable.button_normal);
}
isVoter = !isVoter;
}
}
private class ViewHolder{
ImageButton vote ;
}
#Override
public int getCount() {
return newsItemArray.size();
}
#Override
public Object getItem(int position) {
return newsItemArray.get(position).getTitle();
}
#Override
public long getItemId(int position) {
return position;
}
}
date_view.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="wrap_content"
android:orientation="vertical"
android:background="#f2f2f2"
android:gravity="center"
android:elevation="8dp"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:id="#+id/newsCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/newsimage1"
android:layout_width="45px"
android:layout_height="45px"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/newsname"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fox News."
android:textColor="#000000"
android:textSize="20dp"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/time"
android:layout_weight="0.6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2 day ago"
android:textColor="#color/listsub1"
android:textSize="14dp"
android:layout_gravity="center"
/>
<ImageView
android:id="#+id/more"
android:visibility="invisible"
android:layout_width="15dp"
android:layout_height="22dp"
android:src="#drawable/more"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/sourceNews"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="5dp"
>
<TextView
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous. Trump’s Plan for AmericanMade iPhonew Wold Be Disastrous"
android:textSize="20dp"
android:textColor="#color/listtext"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/newssub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Why even a President Trump couldn’t make Apple manufacture iPhone in the state."
android:layout_marginTop="5dp"
android:textSize="13dp"
android:textColor="#color/listsub1"
android:lineSpacingExtra="3dp"
/>
<TextView
android:id="#+id/votes_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10dp"
android:textStyle="bold"
android:textColor="#f40000"
android:maxLines="1"
android:layout_marginTop="5dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="#+id/vote"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/star"
android:background="#00ffffff"
android:paddingLeft="5dp" />
<!--
<ImageButton
android:id="#+id/vote"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#drawable/star"
android:paddingLeft="5dp" />
<Button
android:id="#+id/share"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Share"
android:textColor="#color/background_material_light"/>
<Button
android:id="#+id/comment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Comment"
android:textColor="#color/background_material_light"/>
-->
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
isVoter and viewHolder should not be global variables within the Adapter. Meaning, you are not actually changing whatever member is returned by newsItemArray.get(position).isVoter()
Try adding the position to the ViewHolder object. Then pass the ViewHolder object into the constructor of the viewHolder.vote.setOnClickListener. Within the OnClickListener you can then call
isVoter = newsItemArray.get(viewHolder.position).isVoter();
newsItemArray.get(viewHolder.position).setVoter(!isVoter);

how to add full scrollview in layout?

In the below code how to scroll not just the ListView, but all of the Views with it? If I am adding full page scrollview then listview not showing full page.
But top menu should be constant. Inside search button I want to scroll full list.
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:background="#027fdb"
android:id="#+id/linearlayout1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="Contact -"
android:id="#+id/textView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_mylead_listcount"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_alignRight="#+id/textView"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/lin_symbols"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="right"
android:layout_marginRight="20dp">
<LinearLayout
android:id="#+id/lin_addmechines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right">
<ImageView
android:id="#+id/img_addmachines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_machine"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lin_addmechines"
android:layout_gravity="center_vertical"
android:gravity="right"
android:layout_marginLeft="10dp">
<ImageView
android:id="#+id/img_addfilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/filter"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/search"
android:hint=" Search"
android:paddingLeft="5dp"
android:imeOptions="actionSearch"
android:inputType="text"
android:id="#+id/textSearch"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:id="#+id/lin_contactPersons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginBottom="15dp">
<ImageView
android:id="#+id/img_contactprs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/contactprs"
/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000" />
<ListView
android:id="#+id/users"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Use this function to full view list on scroll view.
ListView listViewObject;
// after binding list view.
getListViewSize(listViewObject);// pass your listview object here.
public void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
Hope! it is helpful to you...
You should copy the layouts, you want to scroll with the listview in a different layout XML and put it to the top of the listview as a different first item. You can do it with a custom ListAdapter:
//WARNING: I did not try this code, handle it like pseudocode
private class MyCustomAdapter extends BaseAdapter {
private ArrayList<Item> mData = new ArrayList<>();
private LayoutInflater mInflater;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final Item item) {
mData.add(item);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mData.size() + 1;
}
#Override
public Item getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(position==0){
//initialize your custom view
}else{
//initialize a normal list item
}
return convertView;
}
}
Check this ListAdapter example, if you still have questions!

Gravity center text not working for first child element of Gridview

I'v created adapter with textview which set text to child elements in gridview. Below is my Adapter Code-
class CustomAdapter1 extends BaseAdapter
{
private Context context;
private List<String> list;
LayoutInflater inflater;
public CustomAdapter1(Context context,List<String> list)
{
this.context=context;
this.list=list;
inflater= (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {return null;
}
#Override
public long getItemId(int position) {return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if(convertView==null)
{
convertView=inflater.inflate(R.layout.singlie_textview, null);
}
else
{
textView=(TextView)convertView;
}
textView=(TextView)convertView.findViewById(R.id.text1);
textView.setText(list.get(position));
return textView;
}
}
I set gravity of textview to center but my problem is that first child element of gridview not centered. I've tried using internal list layout of android device it works perfectly. Then what will be the problem? thanks in advance.
Below is my Xml layout-
<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="match_parent"
android:orientation="vertical"
tools:context="com.urva.educationapp.BarakhadiActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:padding="3dp"
android:layout_weight="4">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="#+id/Maintext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="2dp"
android:textSize="60dp" />
</RelativeLayout>
<GridView
android:id="#+id/gridView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:numColumns="3"
android:layout_gravity="right"
android:layout_weight="2"
>
</GridView>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="3">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:layout_gravity="right"
android:layout_margin="2dp"/>
<com.urva.educationapp.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#fff" />
</LinearLayout>
</LinearLayout>
Here is my textview xml file -
<?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="match_parent" >
<TextView
android:id="#+id/text1"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="1dp"/>
</RelativeLayout>

Categories

Resources