ImageView in ListView gives extra image - java

I'm getting an extra icon while adding ImageView in a ListView as shown below. What is this and how can I remove it?
In my content it also shows up and when selected it highlights the ImageView shown here
The xml code is here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="anaxin.newshellas.Feed"
tools:showIn="#layout/activity_feed">
<ListView
android:choiceMode="singleChoice"
android:listSelector="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/feedView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:clickable="false" />
<TextView
android:id="#+id/textTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/textBot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_below="#+id/textTop"
/>
<ImageView
android:layout_below="#+id/textTop"
android:layout_alignParentRight="true"
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
/>
</RelativeLayout>
I use a SimpleAdapter with two rows (textTop, TextBot) to populate my listView like this:
final SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.content_feed,
new String[]{"First Line", "Second Line"},
new int[]{R.id.textTop, R.id.textBot}) {
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
return view;
}
};
And in method to add items this part
Map<String, String> datum = new HashMap<>(2);
datum.put("First Line", article.getTop());
datum.put("Second Line", article.getBot());
data.add(datum);

First of all Don't mix up ListView and other elements like TextView and ImageView which should be in the single item layout. Because of it you are seeing multiple images in single list.
And another thing to be noticed is your android:layout_below="#+id/textTop" attribute is conflicted in ImageView and TextView as well, so I suggest you to fix that as well.
Rest of the codes are fine enough !!

You have an extra ImageView and 2 extra TextViews in your layout file. These should be in the layout file of your list adapter and not in the layout containing listview itself. Try removing these extra views, that might solve your problem

remove all content in your list view like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="anaxin.newshellas.Feed"
tools:showIn="#layout/activity_feed">
<ListView
android:choiceMode="singleChoice"
android:listSelector="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/feedView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:clickable="false" />
you don't need them

Related

Android ListView background color masks onClick ripple effect

I have a custom ListView where each element has an icon and 3 TextViews. I am able to change the background to white but now I don't see the dynamic effect when I click on the text. I mean the gray color on touch (ripple effect).
this is the MainActivity
ListView mListView = findViewById(R.id.list_layout);
names = new ArrayList<>();
macs = new ArrayList<>();
adapter= new CompleteListAdapter(this, names, macs);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Method in my CompleteListAdapter which extends BaseAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = LayoutInflater.from(mContext).inflate(R.layout.two_line_icon, parent, false);
TextView text1 = (TextView) rowView.findViewById(R.id.text1);
TextView text2 = (TextView) rowView.findViewById(R.id.text2);
ImageView icon = (ImageView) rowView.findViewById(R.id.icon);
text1.setText(names.get(position));
text2.setText(macs.get(position));
return rowView;
}
This is part of the main_activity.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
tools:context="com.example.myapp.MainActivity">
<ListView
android:id="#+id/list_layout"
android:layout_width="match_parent"
android:layout_height="400dp"></ListView>
</LinearLayout>
And this is the single entry of the ListView: two_line_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#android:color/white"
android:layout_height="match_parent">
<ImageView android:id="#+id/icon"
android:foregroundTint="#android:color/transparent"
android:foregroundTintMode="add"
android:scaleType="fitStart"
android:src="#drawable/img_logo"
android:tintMode="add"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Texto 1"/>
<TextView android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Tet2"/>
<TextView android:id="#+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="text3"/>
</LinearLayout>
</LinearLayout>

CardView in the ListView

I'm trying to load views with image and text by using CardView in the list. However, there is a divider between the cards.
This is my output:
I'm trying to do like in this video, no divider lines.
Here's my XML code.
activity_cat_list.xml
<?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">
<ListView
android:id="#+id/lvCat"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
item_cat
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FEFEFE"
android:layout_margin="8dp"
app:cardCornerRadius="4dp"
card_view:cardCornerRadius="4dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
card_view:cardElevation="8dp">
<!-- Main Content View -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ivCat"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/tvCatName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ivCat"
android:text="Name"
android:maxLines="3"
android:padding="8dp"
android:textColor="#222"
android:textStyle="bold"
android:textSize="22dp" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvCatName"
android:text="Description"
android:maxLines="3"
android:padding="8dp"
android:textColor="#666"
android:textSize="14dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
edit 1: add CatAdapter.java
public class CatAdapter extends ArrayAdapter<Cat> {
public CatAdapter(Context context, ArrayList<Cat> cats) {
super(context,0,cats);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//get data item from this position
Cat cats = getItem(position);
//check if existing view is being reused, otherwise inflate the views
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_cat, parent, false);
//lookup view for data population
TextView tvName = (TextView)convertView.findViewById(R.id.tvCatName);
TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);
ImageView ivCat = (ImageView)convertView.findViewById(R.id.ivCat);
//populate into template view from data source
tvName.setText(cats.getName());
tvDescription.setText(cats.getDescription());
Glide.with(getContext()).load(cats.getImages()).into(ivCat);
return convertView;
}
}
You can remove divider with xml :
<ListView
android:id="#+id/lvCat"
android:dividerHeight="0dp"
android:divider="#null"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>

RecyclerView with dynamic sized elements

I am trying to make the switch from listview to recyclerview but can't seem to figure out how best to recycle views. What I would like is a set of cardviews in a cardview. The data set resembles a bunch of students in different classes. Every class has different sizes and they are to be represented in the same card.
What I have at the moment is the following xml where 'tv1' will contain the title of the class and the student names are dynamically added in 'card_ll'. The problem I am having is that I inflate the view for each student name manually and add / clear the linearlayout depending on how many students there are in the class. Note, the number of students is non-zero but cannot be determined beforehand.
Is there a way to reuse the views of each student card?
Thanks
xml:
<?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"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view1"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/tv1"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view2"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:id="#+id/card_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v7.widget.CardView>
Adapter onBindViewHolder implementation
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final ClassType ec = mDataset.get(position);
final String name = ec.classtitle;
holder.tv1.setText(ec.header1);
if( ec.studentlist!=null && ec.studentlist.size()>0) {
holder.cardll.removeAllViews();
for (Student s: ec.studentlist) {
TextView tv = new TextView(mContext);
tv.setText( s.getName());
tv.setTextColor(Color.BLACK);
holder.cardll.addView(tv);
}
}
}

Dynamically adding views missing scrollbar

I am adding views dynamically like this:
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.dynamicabout, null);
TextView textOut = (TextView)addView.findViewById(R.id.aboutcontent);
textOut.setText(strings.get(index));
((ViewGroup) v).addView(addView);
However, once we get to the bottom of the screen and beyond, there's no scrollbar.
I tried using the ScrollView, but that doesn't do anything.
dynamicabout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/aboutcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
about.xml
<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="wrap_content"
android:paddingLeft="#dimen/stdpadding"
android:paddingRight="#dimen/stdpadding"
android:paddingBottom="#dimen/stdpadding"
android:paddingTop="#dimen/stdpadding"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ScrollView>
</LinearLayout>
What you need to do is you need to have Parent and Child view. Add child to the Parent. Parent is LinearLayout in ScrollView in dynamicabout.xml. Child is about XML. Inflate about XML on the runtime and Add it into Parent View (LinearLayout in ScrollView).
P.S. > Set an ID for the Parent Layout in order to be able to add views inside it, as far as I see it doesn't have ID.
<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="wrap_content"
android:paddingLeft="#dimen/stdpadding"
android:paddingRight="#dimen/stdpadding"
android:paddingBottom="#dimen/stdpadding"
android:paddingTop="#dimen/stdpadding"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ScrollView>
</LinearLayout>
////////////////////////////////////////////////
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/aboutcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"/>
</LinearLayout>
</RelativeLayout>
//////////////////////////////////////////////////////////////
LinearLayout linear=(LinearLayout)findviewbyId(R.id.linear);
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.dynamicabout, null);
TextView textOut = (TextView)addView.findViewById(R.id.aboutcontent);
textOut.setText(strings.get(index));
linearview.addView(addView);
please chk this code:

Android how to add a Linearlayout to another LinearLayout from code

I have a LinearLayout in main.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="fill_parent"
android:orientation="vertical"
android:id="#+id/mainLayout" >
</LinearLayout>
I have made another XML file, called item_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/item_bg"
android:gravity="right" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:text="#string/item1"
android:textSize="30dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:gravity="right"
android:text="#string/number1"
android:textColor="#color/number_bg"
android:textSize="30dp" />
</LinearLayout>
Basically, what I want to do from the code (programmatically), is add a couple item_box.xml into the main.xml. How can I accomplish this?
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);
mainLayout.addView(itemBox);
try this linearlayout inside another linearlayout add id to linearlayout layout1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
in oncreate() call
LinearLayout firstlayout=(LinearLayout)findviewbyId(R.id.firstlayout);
LinearLayout secondlayoout=(LinearLayout)this.getLayoutInflater().inflate(R.layout.layout2,null); //inflating view from xml
TextView btn1=(TextView)secondlayoout.findviewbyId(R.id.button1);
btn1.settext("TEST");
firstlayout.addview(secondlayoout);
You are getting a Nullpointer because linearLayout1 is not in mainLayout. You need to inflate your view first and then add it. You should read this question I think it will help you.
Use LayoutInflater:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);

Categories

Resources