Android ImageView duplicated content LazyLoad - android

I have problem with lazy loading images to ImageViews. When i scroll down, i see images from top (dupliacated), when new image is downloaded its replaced with new one. How can i show on new images ProgressBar, and replace it with images when its downloaded?
Here is my getView() from LazyAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
ImageView image=(ImageView)vi.findViewById(R.id.image);
imageLoader.DisplayImage(data[position], image);
return vi;
}
and item.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ProgressBar android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="center"/>
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:minHeight="300px"
/>
</FrameLayout>
imageLoader.DisplayImage its asyncTask.

You need to set the image path to tags for ImageView.
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
ImageView image=(ImageView)vi.findViewById(R.id.image);
image.setTag(data[position]);
imageLoader.DisplayImage(data[position], image);
return vi;
}

Related

Toolbar Spinner Dropdown inflating issue

I am passing my custom layout in constructor of ArrayAdapter<String>:
private static class ActionSpinnerAdapter extends ArrayAdapter<String> {
public ActionSpinnerAdapter(final Context context) {
super(context,
R.layout.action_spinner_text, //This is my own layout
//R.id.action_text,
Lists.newArrayList(ACTION_REPLY, ACTION_REPLY_ALL, ACTION_FORWARD));
action_spinner_text.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_text"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="8dip"
android:paddingStart="8dp"
android:paddingTop="10dp"
android:paddingRight="8dp"
android:paddingEnd="8dp"
android:text="#string/reply_all_action"
android:textSize="18sp" />
And these are my getView() and getDropDownView() methods:
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View result = super.getDropDownView(position, convertView, parent);
result.setBackgroundColor(mContext.getResources().getColor(R.color.white));
TextView textView = ((TextView) result.findViewById(R.id.action_text));
textView.setTextColor(mContext.getResources().getColor(R.color.dark_grey));
textView.setText(getDisplayValue(position));
return result;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View result = super.getView(position, convertView, parent);
TextView textView = ((TextView) result.findViewById(R.id.action_text));
textView.setTextColor(mContext.getResources().getColor(R.color.white));
textView.setText(getDisplayValue(position));
return result;
}
But When I click on DropDown from Toolbar it shows text values with black background on right side.
What went wrong?
I had exactly the same problem, and it seems like there's a bug in Pre-Lollipop devices when you specify a size in the dropdown view that is not "match_parent" on "android:layout_width", basically all you need to do is
this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="8dip"
android:paddingStart="8dp"
android:paddingTop="10dp"
android:paddingRight="8dp"
android:paddingEnd="8dp"
android:text="#string/reply_all_action"
android:textSize="18sp" />
Hope it Helps!

Android Spinner Initial Padding

I am creating an Android app that consists mainly of a form. This form needs to have EditText and Spinners side-by-side (for this I chose a LinearLayout). My final goal is to have the text in the Spinner align vertically with the text in the EditText; however, I also want to have padding on the options in the expanded Spinner. This padding messes up the Spinner alignment as it appears in the form.
Here is my code for a custom Spinner list item:
<TextView
android:id="#+id/transportation_spinner_item_textView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#android:color/black"
android:textSize="18dp"/>
Here is my code for the main form:
<LinearLayout
android:id="#+id/linearLayout6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/divider3"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="100"
android:orientation="vertical">
<TextView
android:id="#+id/ip_minutes_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/ip_minutes"
android:layout_marginTop="4dp"
android:textSize="12sp"/>
<Spinner
android:id="#+id/ip_minutes_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:gravity="bottom"/>
</RelativeLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/ip_charge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="100"
android:layout_gravity="bottom">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="100"
android:editable="false"
android:hint="#string/ip_charge"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
And here is an image of how it looks:
Image of alignment with above code
I have been Googling and trying to adjust the padding/margins of different views for hours, so if anyone has any suggestions, that would be great. Thanks!
I'm not sure I fully understand, when you say,
This padding messes up the Spinner alignment as it appears in the
form.
Do you mean the DropDownView? that shows when the Spinner is clicked?
If so, simply create a separate layout for the DropDownView.
In your ArrayAdapter you will have the following methods,
public View getDropDownView(int position, View cnvtView, ViewGroup prnt)
public View getView(int pos, View cnvtView, ViewGroup prnt)
Just have them return two different Views, e.g
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomDropDownView(position, cnvtView, prnt);
}
#Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
and
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
TextView main_text = (TextView) mySpinner.findViewById(R.id.spinner_text);
main_text.setText(spinnerValues[position]);
return mySpinner;
}
public View getCustomDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View mySpinner = inflater.inflate(R.layout.custom_dropdown_spinner, parent, false);
TextView main_text = (TextView) mySpinner.findViewById(R.id.spinner_text);
main_text.setText(spinnerValues[position]);
return mySpinner;
}

I cannot load listview on my Android phone

I cannot load listview on my phone. It displays the error in the logcat:
03-24 05:27:14.537: D/AbsListView(22125): unregisterIRListener() is called
My code is as follow
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
rootView = inflater.inflate(R.layout.fragment_event, container, false);
listView = (ListView) rootView.findViewById(R.id.eventListView);
listView.setVisibility(View.VISIBLE);
listView.setClickable(true);
EventArrayAdapter eArrayAdapter = new EventArrayAdapter(getActivity(), R.layout.rowlayout, events);
listView.setAdapter(eArrayAdapter);
eArrayAdapter.notifyDataSetChanged();
return rootView;
}
rowlayout.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="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/eventDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#1d766f"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/eventMonth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#e2fffd"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#0083af" />
<TextView
android:id="#+id/eventTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#e2fffd"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#1d766f" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/eventName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0083af"/>
<TextView
android:id="#+id/eventLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
mainlayout.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="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnAllEvent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All Events"
/>
<ListView
android:id="#+id/eventListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
</LinearLayout>
Unfortunately, it doesnot show anything, even the listview itself. Please help me. Thank you
Update: I use the view Holder pattern
You have to return the view simply.
Also For a better scrolling you need to implement and understand the ViewHolder pattern.
Reason why you should use it is because:
layout inflations are expensive operations.
If you do something like this:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = mContext.getLayoutInflater();
View item = mInflater.inflate(R.layout.list_item, null);
MyObj data = getItem(position);
((TextView) item.findViewById(R.id.text1)).setText(data.text1);
((TextView) item.findViewById(R.id.text2)).setText(data.text2);
((TextView) item.findViewById(R.id.longtext)).setText(data.longText);
return item;
}
Please don't do it, because the adapter will create a new view for every position and if you have a very long list it will cause lag scroll. Imagine you have 500 item in the list, the adapter have to create 500 views!!!
So the ViewHolder pattern fix this problem by changing that code from above like this:
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "position=" + position);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
holder.longtext = (TextView) convertView.findViewById(R.id.longtext);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MyObj data = getItem(position);
holder.text1.setText(data.text1);
holder.text2.setText(data.text2);
holder.longtext.setText(data.longText);
return convertView;
}
The convertView from the getView method will have a non-null value when ListView is asking you recycle the row layout. So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
What this snippet does goes like this:
If the ConvertView is null it will inflate the layout and save each view under the ViewHolder class, what ViewHolder does is to save the views, then you save the ViewHolder in the ConverView with setTag(Object object), If convertView isn't Null then it get the ViewHolder object like this: holder = (ViewHolder) convertView.getTag();
And then it get the current object from the list with the position variable from the getView method and reference everyView through the ViewHolder instance.
Also don't use android:layout_height="wrap_content" because the listView has to call getView() lot of time to know how big it should be, so use android:layout_height="fill_parent" instead.

Indenting ListView items Android

I am trying to indent listView row by certain factor. For this, I used view.layout() and view.setX() method but they don't work or result in forceclose. Is there a way to achieve this result. Here is my code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View newRowView = convertView;
ViewHolder viewHolder;
if(newRowView == null)
{
LayoutInflater inflator = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newRowView = inflator.inflate(R.layout.list_view_images, parent,false);
//newRowView.layout(100*(position/list.size()), 0, 0, 0); // not working
newRowView.setX(100*(position/list.size())); // causing force close
viewHolder = new ViewHolder();
viewHolder.appIcon = (ImageView) newRowView.findViewById(R.id.imageView1);
viewHolder.appName = (TextView) newRowView.findViewById(R.id.textView1);
viewHolder.appName.setText(names.get(position));
newRowView.setTag(viewHolder);
}
list_view_images.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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
I am trying to indent the list in semi circular fashion.
What can I do achieve required results?
Regards
Update your getView() method as follows:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View newRowView = convertView;
ViewHolder viewHolder;
if (newRowView == null) {
LayoutInflater inflator = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newRowView = inflator.inflate(R.layout.list_view_images, parent, false);
viewHolder = new ViewHolder();
viewHolder.innerContainer = (LinearLayout) newRowView.findViewById(R.id.innerContainer);
viewHolder.appIcon = (ImageView) newRowView.findViewById(R.id.imageView1);
viewHolder.appName = (TextView) newRowView.findViewById(R.id.textView1);
viewHolder.appName.setText(list.get(position));
newRowView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) newRowView.getTag();
}
float shiftRight = 100 * ((position * 1.0f) / list.size());
viewHolder.innerContainer.setPadding((int) shiftRight, 0, 0, 0);
//...
//...
return newRowView;
}
Update your XML layout as follows:
<?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" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/innerContainer"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
Update your ViewHolder to contain another field as following:
public LinearLayout innerContainer;
Explanation: Basically, we put your original XML layout inside a LinearLayout that acts as an outer container. The we assign padding to the innerContainer (original layout) with respect to the outer layout

Android GridView with ImageView and TextView donĀ“t show well

Hi I have a problem displaying my Gridview. I searched many sites and here on StackOverflow but took several days without finding the solution.
I leave here my xml (listado.xml) code where is gridview:
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<GridView
android:id="#+id/grid_release"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clipChildren="true"
android:stretchMode="columnWidth"
android:numColumns="2"
>
<!-- android:stretchMode="columnWidth"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="1dip"-->
</GridView>
The xml file with textview and imageview (grid_release.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:background="#DC0000"
android:orientation="vertical"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/rls_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/rls_txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titulo" />
Also leave the code of ImageAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
if(convertView==null)
{
grid = new View(context);
grid = layoutInflater.inflate(R.layout.grid_release, null);
}else{
grid = (View)convertView; //imageView=(ImageView)convertView;
}
ImageView rlsimg = (ImageView)grid.findViewById(R.id.rls_img);
rlsimg.setImageDrawable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
TextView rsltxt = (TextView)grid.findViewById(R.id.rls_txtTitle);
rsltxt.setText("Adpt00"+position);
return grid;
Thanks for help!!!
This solve my problem:
Metodo GetView of ImageAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView;
if(convertView==null)
{
LayoutInflater li = ((Activity)context).getLayoutInflater();
myView = li.inflate(R.layout.grid_prueba1, null);
myView.setLayoutParams(new GridView.LayoutParams(100,100));
myView.setPadding(5,5,5,5);
}else{
myView = (View)convertView;
}
ImageView img = (ImageView)myView.findViewById(R.id.img2);
img.setImageDrawable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
TextView txt = (TextView)myView.findViewById(R.id.txt2);
txt.setText("Adept0000"+position);
return myView;
}
But now I have another problem, textview displayed within the image. I want this text below the image. I'll have to open another query.

Categories

Resources