Hello everyone I have a problem.
I want to do a GridView with a Custom Loyout so i used the layoutInflater and i did this:
private ImageView prima;
private ImageView seconda;
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
prima = (ImageView) v.findViewById(R.id.imageView1);
prima.getLayoutParams().height = width / 3;
prima.getLayoutParams().width = width / 3;
seconda = (ImageView) v.findViewById(R.id.imageView2);
seconda.getLayoutParams().height = width / 3;
seconda.getLayoutParams().width = width / 3;
v.setLayoutParams(new GridView.LayoutParams(width / 3, width / 3));
v.setPadding(0, 0, 0, 0);
} else {
v = convertView;
}
prima.setImageResource(mThumbIds[position]); //mThumbIds[] is an array with R.drawable.vip_0_mini, R.drawable.a_1, R.drawable.b_2, R.drawable.c_3 .....
return v;
};
When i run my application the image are arranged random and there are many black space with no images.
What i did wrong ?
Your View and convertView are not linked.
Try with ViewHolder concept like this:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
convertView = li.inflate(R.layout.icon, null);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
v = new ViewHolder();
v.prima = (ImageView) convertView .findViewById(R.id.imageView1);
v.prima.getLayoutParams().height = width / 3;
v.prima.getLayoutParams().width = width / 3;
v.seconda = (ImageView) convertView .findViewById(R.id.imageView2);
v.seconda.getLayoutParams().height = width / 3;
v.seconda.getLayoutParams().width = width / 3;
convertView .setLayoutParams(new GridView.LayoutParams(width / 3, width / 3));
convertView .setPadding(0, 0, 0, 0);
convertView.setTag(v);
} else {
v = (ViewHolder)convertView.getTag();
}
v.prima.setImageResource(mThumbIds[position]); //mThumbIds[] is an array with R.drawable.vip_0_mini, R.drawable.a_1, R.drawable.b_2, R.drawable.c_3 .....
//v.seconda
return convertView;
};
class ViewHolder{
ImageView prima;
ImageView seconda;
}
You forgot to apply images to
seconda = (ImageView) v.findViewById(R.id.imageView2);
prima.setImageResource(....);
Related
I'm trying to resize ListView to show all items.
final ListAdapter adapter = timeline.getAdapter();
int totalHeight = 0;
final int desiredWidth = View.MeasureSpec.makeMeasureSpec(timeline.getWidth(), View.MeasureSpec.AT_MOST);
for (int i = 0; i < adapter.getCount(); i++) {
final View listItem = adapter.getView(i, null, timeline);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
Log.v(TAG, "Item size = " + listItem.getMeasuredHeight()); // Always 38
totalHeight += listItem.getMeasuredHeight();
}
final ViewGroup.LayoutParams params = timeline.getLayoutParams();
params.height = totalHeight;
timeline.setLayoutParams(params);
timeline.requestLayout();
Why is listItem.getMeasuredHeight() always 38, disregard android:layout_height specified in item's XML layout.
The getView() method is pretty standard:
#Override
public View getView(final int position, View view, final ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
final LayoutInflater li = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.timeline_item, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
final ActivityLogItem log = (ActivityLogItem) getItem(position);
holder.time.setText(format.print(log.getRecordedTime().withZone(DateTimeZone.getDefault())));
holder.time.setTextColor(log.isImportant() ? view.getResources().getColor(R.color.orange) : Color.WHITE);
holder.event.setText(log.getLocalizedType(view.getResources()));
holder.event.setTextColor(log.isImportant() ? view.getResources().getColor(R.color.orange) : Color.BLACK);
return view;
}
i try to make my image view position dynamic base on image height. but i stuck to get the image height from drawable.. help me to solve it thanks..
this is my code :
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_wall, null, true);
TextView txtProfileUser = (TextView) rowView.findViewById(R.id.txtWallProfile);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtWallContentTitle);
TextView txtContent = (TextView) rowView.findViewById(R.id.txtWallContent);
ImageView imgWallProfile = (ImageView) rowView.findViewById(R.id.ivWallProfile);
ImageView imgWallContent = (ImageView) rowView.findViewById(R.id.ivWallContent);
txtTitle.setText(web2[position]);
txtContent.setText(web3[position]);
txtProfileUser.setText(web[position]);
imgWallProfile.setImageResource(imageId[position]);
imgWallContent.setImageResource(imageId2[position]);
GetSize gz = new GetSize();
Integer h = gz.getImageSize(imageId[position]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(20,h, 0, 0);
imgWallProfile.setLayoutParams(lp);
return rowView;
}
and this my getImageSize method :
public Integer getImageSize(Integer d){
Drawable bd = getResources().getDrawable(d);
int height=bd.getIntrinsicHeight();
return height;}
If I understand correctly you want to get the width and height of your image view instead of the bitmaps width and height. Layout parameters allows you to access the imageView and view and modify it's parameters.
public int getImageSize(View myView) {
RelativeLayout.LayoutParams myParams = (RelativeLayout.LayoutParams) myView.getLayoutParams();
return myParams.height;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolderList holder;
if (convertView == null) {
holder = new ViewHolderList();
convertView = LayoutInflater.from(context)
.inflate(R.layout.children_row_list, null);
position=position+2;
Log.d("list position::", ""+position);
holder.img_children_view=(ImageView)convertView.findViewById(R.id.image_children);
holder.text_child_name=(TextView)convertView.findViewById(R.id.text_children_name);
holder.text_child_month=(TextView)convertView.findViewById(R.id.text_children_month);
holder.text_group_name=(TextView)convertView.findViewById(R.id.text_children_group);
holder.img_children_view1=(ImageView)convertView.findViewById(R.id.image_children1);
holder.text_child_name1=(TextView)convertView.findViewById(R.id.text_children_name1);
holder.text_child_month1=(TextView)convertView.findViewById(R.id.text_children_month1);
holder.text_group_name1=(TextView)convertView.findViewById(R.id.text_children_group1);
Resources r = getResources();
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, r.getDisplayMetrics());
int pixel_height = (int) pixels;
holder.img_children_view.setLayoutParams(new LinearLayout.LayoutParams(80 , 80));
holder.img_children_view1.setLayoutParams(new LinearLayout.LayoutParams(80 , 80));
convertView.setTag(holder);
}
else{
holder = (ViewHolderList) convertView.getTag();
}
if(position %2 == 0){
// System.out.println(position + " is even number.");
holder.text_child_name.setText(children_list.get(position).getFirst_name());
holder.text_child_name.setText(children_list.get(position).getFirst_name());
holder.text_child_month.setText(display_month);
holder.text_group_name.setText(children_list.get(position).getGroup_name());
Bitmap bitmap = decodeFile(new File( Environment.getExternalStorageDirectory()+"/com.x/y/"+children_list.get(position).getPhoto()), ConfigurationData.staffImageSize,ConfigurationData.staffImageSize);
//holder.img_children_view.setImageBitmap(bitmap);
}
else{
// System.out.println(position+ " is odd number.");
holder.text_child_name1.setText(children_list.get(position).getFirst_name());
holder.text_child_name1.setText(children_list.get(position).getFirst_name());
holder.text_child_month1.setText(display_month);
holder.text_group_name1.setText(children_list.get(position).getGroup_name());
Bitmap bitmap1 = decodeFile(new File( Environment.getExternalStorageDirectory()+"/com.x/y/"+children_list.get(position).getPhoto()), ConfigurationData.staffImageSize,ConfigurationData.staffImageSize);
//holder.img_children_view1.setImageBitmap(bitmap1);
}
return convertView;
} //closing getview
}
I suggest using android's GridView with parameter numColumns equals 2
GridView # developer.android.com
Create Gridview with number of columns 2 android:numColumns="2".
In xml:
<GridView
android:id="#+id/sdcard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:numColumns="2">
</GridView>
And in java code :
public View getView(int position, View convertView,
ViewGroup parent) {
final ViewHolderList holder;
if (convertView == null) {
holder = new ViewHolderList();
convertView = LayoutInflater.from(context)
.inflate(R.layout.children_row_list, null);
position=position+2;
Log.d("list position::", ""+position);
holder.img_children_view=(ImageView)convertView.findViewById(R.id.image_children);
holder.text_child_name=(TextView)convertView.findViewById(R.id.text_children_name);
holder.text_child_month=(TextView)convertView.findViewById(R.id.text_children_month);
holder.text_group_name=(TextView)convertView.findViewById(R.id.text_children_group);
holder.img_children_view1=(ImageView)convertView.findViewById(R.id.image_children1);
holder.text_child_name1=(TextView)convertView.findViewById(R.id.text_children_name1);
holder.text_child_month1=(TextView)convertView.findViewById(R.id.text_children_month1);
holder.text_group_name1=(TextView)convertView.findViewById(R.id.text_children_group1);
Resources r = getResources();
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, r.getDisplayMetrics());
int pixel_height = (int) pixels;
holder.img_children_view.setLayoutParams(new LinearLayout.LayoutParams(80 , 80));
holder.img_children_view1.setLayoutParams(new LinearLayout.LayoutParams(80 , 80));
convertView.setTag(holder);
}
else{
holder = (ViewHolderList) convertView.getTag();
}
// System.out.println(position + " is even number.");
holder.text_child_name.setText(children_list.get(position).getFirst_name());
holder.text_child_name.setText(children_list.get(position).getFirst_name());
holder.text_child_month.setText(display_month);
holder.text_group_name.setText(children_list.get(position).getGroup_name());
Bitmap bitmap = decodeFile(new File( Environment.getExternalStorageDirectory()+"/com.x/y/"+children_list.get(position).getPhoto()), ConfigurationData.staffImageSize,ConfigurationData.staffImageSize);
//holder.img_children_view.setImageBitmap(bitmap);
return convertView;
} //closing getview
}
I have the following GridView:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setScaleType(ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
if (mTemplates.get(position).getResource() != 0) {
LogService.log("", "zzzzzzzzzzzzzzzz======loaded from resource" + mTemplates.get(position).getResource() + "???????????" + mTemplates.size());
imageView.setImageResource(mTemplates.get(position).getResource());
} else {
imageView.setImageDrawable(mTemplates.get(position).getDrawableThumbnail());
// imageView.setImageResource(mTemplates.get(0).getResource());
LogService.log("", "zzzzzzzzzzzzzzzz======loaded from drawable");
LogService.log("", "=========SIZE: " + mTemplates.size());
}
return imageView;
}
Now I have some elements that do not have resources in the Drawables, but instead in a folder on the sdcard, so I create a drawable from the source of this pictures, and try to load that drawable on the gridview:
imageView.setImageDrawable(mTemplates.get(position).getDrawableThumbnail());
But its like the gridview does not recognise these, and if its on the same collumn with other pictures it is shown, but if its on another collumn (lower), it will only show 2-3mm of the picture (also doesn't recognise its position like its not therE).
If I hardcode and use the first picture from Resources like this:
imageView.setImageResource(mTemplates.get(0).getResource());
It all works, what could be the issue?
Because the actually size of the pic, is the small size, but when loading an imageView from Resources it strectches it to its parents size.
Resolved this issue by calculating the height needed, and then load all the pictures with that height:
public AdapterGridView(Context c) {
context = c;
DisplayMetrics metrics = new DisplayMetrics();
((Activity) c).getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (Constants.IS_TABLET) {
height = metrics.widthPixels / 9 - (int) Util.pxFromDp(2, c);
} else {
height = metrics.widthPixels / 4 - (int) Util.pxFromDp(2, c);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
} else {
imageView = (ImageView) convertView;
}
imageView.setScaleType(ScaleType.CENTER_CROP);
imageView.setAdjustViewBounds(true);
if (mTemplates.get(position).getResource() != 0) {
imageView.setImageResource(mTemplates.get(position).getResource());
} else {
imageView.setImageDrawable(mTemplates.get(position).getDrawableThumbnail());
}
imageView.setLayoutParams(new LayoutParams(height, height));
return imageView;
}
I have a problem when I getWidth of layout... It doesn't return a real width after adding new view?... how can I solve it... thanks... This is my sample code.
llDayHeaderTable = (LinearLayout) viewGroup
.findViewById(R.id.llDayHeaderTable);
LayoutInflater vi = (LayoutInflater) parent
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (View v : vTmp) {
llDayHeaderTable.removeView(v);
}
vTmp.removeAllElements();
for (int i = 0; i < dto.arrProductTitleList.size(); i++) {
String productName = dto.arrProductTitleList.get(i).productInfoName;
vItemHeader = vi.inflate(R.layout.layout_kpi_sku_group_accum_item,
null);
TextView tv = (TextView) vItemHeader
.findViewById(R.id.tvHeaderProduct);
tv.setText("" + productName);
llDayHeaderTable.addView(vItemHeader);
vTmp.add(vItemHeader);
}
int width = llDayHeaderTable.getWidth();
Try using getMeasuredHeight() and getMeasuredWidth() instead of getWidth() and getHeight()
llDayHeaderTable.measure(0, 0);
final int w = llDayHeaderTable.getMeasuredWidth();
final int h = llDayHeaderTable.getMeasuredHeight();