too slow scrolling GridView android when I use resource background on layout - android

Let me explain my problem.
The GridView scrolling is too slow when I use resource background on Relative root layout.
my background file size is 114KB.
I used android:cacheColorHint="#00000000" or android:overScrollMode="never" with no success.
this is my xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/bg"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:layout_width="match_parent"
android:layout_height="125dp"
android:id="#+id/useful_applications"
android:layout_marginTop="35dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:choiceMode="singleChoice"
android:cacheColorHint="#00000000"
android:overScrollMode="never"
android:clickable="true"
android:numColumns="4"
android:gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
</RelativeLayout>
I use ArrayAdapter with holder pattern.
anyone can help me?
Update 1:
this is my adapter getView that I used holder pattern
#Override
public View getView(int position, View convertView, ViewGroup parent) {
UsefulApplicationHolder holder;
if (convertView == null) {
convertView =LayoutInflater.from(mContext).inflate(R.layout.component_application_icon, parent, false);
holder = new UsefulApplicationHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.score = (TextView) convertView.findViewById(R.id.score);
convertView.setTag(holder);
} else {
holder = (UsefulApplicationHolder) convertView.getTag();
}
Apps info = getItem(position);
holder.icon.setImageDrawable(info.getIcon());
String score = info.getScore();
if (Integer.valueOf(score) > 0) {
holder.score.setTextColor(mContext.getResources().getColor(R.color.green));
score = "+" + score;
} else if (Integer.valueOf(score) < 0) {
holder.score.setTextColor(mContext.getResources().getColor(R.color.red));
} else {
holder.score.setTextColor(mContext.getResources().getColor(R.color.light_gray));
}
holder.score.setText(score);
return convertView;
}

Related

Alternate text and background color in listview

I am new to android, I am trying to display alternate text color and alternate background color, but only background color is working. When I try both, I'm getting error.
Here's the error shown in LogCat:
android.widget.RelativeLayout cannot be cast to android.widget.TextView
This is getView() method in my adapter class:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
if(convertView==null)
{
inflater = (LayoutInflater)mcontext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
view = new ViewHolder();
convertView = inflater.inflate(R.layout.awards_layout_circle, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.Text_View);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.profile_image);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
if (position % 2 == 0){
convertView.setBackgroundResource(R.color.colorNav);
((TextView) convertView).setTextColor(Color.WHITE);
} else {
convertView.setBackgroundResource(R.color.colorWhite);
((TextView) convertView).setTextColor(Color.BLACK);
}
view.txtViewTitle.setText(listAward.get(position));
view.imgViewFlag.setImageResource(listFlag.get(position));
return convertView;
}
And awards_layout_circle.xml using to item list:
<RelativeLayout
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">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="#drawable/bunnyarj"
android:layout_gravity="center"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
<TextView
android:id="#+id/Text_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18dp"
android:text="wsdwedwebd dwedbewd w"
android:fontFamily="sans-serif-medium"
android:layout_below="#+id/profile_image"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
you are using the ViewHolder. No reason to cast the convertView to any type of object (expecially to the wrong one). Just use
view.txtViewTitle.setTextColor()
in your if/else
You have change the code like this,
if (position % 2 == 0){
convertView.setBackgroundResource(R.color.colorNav);
((TextView) view.txtViewTitle).setTextColor(Color.WHITE);
} else {
convertView.setBackgroundResource(R.color.colorWhite);
((TextView) view.txtViewTitle).setTextColor(Color.BLACK);
}

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

Textview background repeats in a listview

So the problem that i am facing, is that when i try to set dynamically the background of a textView that belongs to a listView, the background that i set, is set for other textViews too..it repeats itself...i saw that these problems with listView items are common, but couldn't find a solution for me
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.product_item, null);
holder = new ViewHolder();
holder.increment = (Button) convertView.findViewById(R.id.increment);
holder.decrement = (Button) convertView.findViewById(R.id.decrement);
holder.pic_view_quantity = (TextView) convertView.findViewById(R.id.pic_view_quantity);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.pic_view_quantity.setText(products.get(position).getQuantity());
if (products.get(position).getQuantity().matches("[0-9]+")){
holder.pic_view_quantity.setBackgroundResource(R.drawable.transparent_rectangle); //this is where i set the background, to items that have a quantity
}
return convertView;
}
My layout:
<RelativeLayout
android:id="#+id/relativelayout"
android:layout_width="70dp"
android:layout_height="70dp" >
<ImageView android:id="#+id/pic_view"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="5sp"
android:contentDescription="#string/product_picture" >
</ImageView>
<TextView
android:id="#+id/pic_view_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pic_view"
android:layout_alignTop="#+id/pic_view"
android:layout_alignRight="#+id/pic_view"
android:layout_alignBottom="#+id/pic_view"
android:layout_margin="1dp"
android:textSize="36sp"
android:gravity="center"
android:textColor="#FF0000" />
</RelativeLayout>
Add an else to the following statement
if (products.get(position).getQuantity().matches("[0-9]+")){
holder.pic_view_quantity.setBackgroundResource(R.drawable.transparent_rectangle); //this is where i set the background, to items that have a quantity
}
else {
holder.pic_view_quantity.setBackgroundResource(0);
}

Filling ListView out

I'm working on project and main layout contains ListView. I want to dynamically add subItems to ListViewItem View. My main layout is as follows :
main_layout.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="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView"/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
</ListView>
</RelativeLayout>
list_item.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="50dip"
android:background="#android:color/transparent" >
</LinearLayout>
And I want to add TextView to returned view in getView with following properties :
Height : fill_parent
Width : wrap_content
Text : Text View Item
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.M_context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
return convertView;
}
Please note that, I want to dynamically add subItems to ListViewItem. You can suppose that, I have array of TextView and want to fill my listView using this Items. How can I do this scenario ? I can not find any result with searching on other threads and aslo Google. So, Could any one please help me to solve my problem ?
Thanks in Advance :)
This was my getView() method.
main.xml
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#+id/button1"
android:cacheColorHint="#android:color/transparent"
android:divider="#00ff00"
android:fastScrollEnabled="true"
android:focusable="false"
android:listSelector="#000000"
android:paddingTop="5dp"
android:saveEnabled="true" >
</ListView>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Adapter
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
/*convertView = mInflater.inflate(R.layout.item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
holder = (ViewHolder) convertView.getTag();
holder.getAppName().setText(str.get(position));*/
if(position==0)
{
ImageView iv = new ImageView(getApplicationContext());
iv.setBackgroundResource(R.drawable.ic_launcher);
convertView = iv;
}
else if(position==1)
{
TextView tv = new TextView(getApplicationContext());
tv.setText("text view");
convertView = tv;
}
else
{
EditText et = new EditText(getApplicationContext());
et.setText("ET go home");
convertView = et;
}
return convertView;
}
private class ViewHolder {
private View pathRow;
private TextView appNameView = null;
public ViewHolder(View row) {
pathRow = row;
}
public TextView getAppName() {
if (null == appNameView) {
appNameView = (TextView) pathRow.findViewById(R.id.tv);
}
return appNameView;
}
}
}
The commented out portion is the one used to be. The code below it is the one which I used to dynamically generate the view and return them. my listitem.xml still has a textView in it. But the adapter sets the view which we are returning from getView(). This works for me.
I really don;t see the point of the LinearLayout in your item.xml. You can do that in your own code.

Android, changing background of list in array adapter?

In my application i have list which i inflate it with array adapter. every thing is OK and i have my result in the list.
I like to change background color of list (even rows red for example and odd rows green).
This is xml of my rows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:id="#+id/rllist"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
>
<TextView
android:id="#+id/outstanding_contractdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="#+id/outstanding_contractno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/outstanding_contractdate"
android:paddingLeft="20dip"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="#+id/outstanding_contractamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="15sp" />
</RelativeLayout>
</LinearLayout>
I have written following codes:
static class ViewHolder {
RelativeLayout rlList;
TextView tvContDate;
TextView tvContNo;
TextView tvContAmount;
}
and in Array Adapter I have this one:
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
View row = convertView;
if(row == null){
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.list_outstanding, parent, false);
//convertView = mInflater.inflate(R.layout.list_outstanding, parent, false);
holder = new ViewHolder();
holder.rlList = (RelativeLayout) convertView.findViewById(R.id.rllist);
holder.tvContDate = (TextView) row.findViewById(R.id.outstanding_contractdate);
holder.tvContNo = (TextView) row.findViewById(R.id.outstanding_contractno);
holder.tvContAmount = (TextView) row.findViewById(R.id.outstanding_contractamount);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.tvContDate.setText(llData.get(position).split(",")[0].trim());
holder.tvContNo.setText(llData.get(position).split(",")[1].trim());
holder.tvContAmount.setText(llData.get(position).split(",")[2].trim());
if(position%2 != 0)
holder.rlList.setBackgroundColor(0x00FF00);
else
holder.rlList.setBackgroundColor(0XFF0000);
return(row);
}
after running application when i reach to this code, an error occurs. according to LogCat it is "Null Pointer Exception" and points to this line in getView():
holder.rlList = (RelativeLayout) convertView.findViewById(R.id.rllist);
it seems everything is OK, but I don't know where my problem is?!!!
===========
Update
I have changed above line to:
holder.rlList = (RelativeLayout) row.findViewById(R.id.rllist);
and now i have my result but background did not applied?!!!
Try using
if(position%2 != 0)
holder.rlList.setBackgroundColor(Color.parseColor("#00ff00"));
else
holder.rlList.setBackgroundColor(Color.parseColor("#ff0000"));

Categories

Resources