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();
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;
}
I'm trying to inflate a PopupWindow from a button that sits within list items (from a custom list adapter) but am getting NullPointerExceptions in multiple places.
Here's the onClick for the button where I'm trying to make the PopupWindow happen:
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = null;
if (scores[0] == null) {
pw = new PopupWindow(inflater.inflate(R.layout.tag_table_row, null, false), 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText("error!");
} else {
int x = tags.length;
int y = 5;
int z = Math.min(x, y);
for (int i = 0; i < z; i++) {
pw = new PopupWindow(inflater.inflate(R.layout.tag_table_row, null, false), 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText(tags[i]);
int tag1score = Double.parseDouble(scores[i]);
TextView score = (TextView) v.findViewById(R.id.tagscore);
score.setText(Integer.toString(tag1score));
}
}
pw.showAtLocation(v.findViewById(R.id.fragment_content), Gravity.CENTER, 0, 0);
}
I'm getting the NullPointerException at tag.setText(tags[i]);. If I comment that and just try to do scores, I get a NullPointerException at score.setText(Integer.toString(tag1score));. Both tags[] and scores[] are initialized and filled before the onClick, and I've tested to ensure that the results in both are Strings and NOT null.
Any thoughts as to why this NPE is happening?
You need to use the right thing wherein to look for the view. You are inflating into pw, but you are looking in v for your views. I think this might work better;
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = null;
PopupWindow pw = null;
if (scores[0] == null) {
popupView = layoutInflater.inflate(R.layout.tag_table_row, null, false);
pw = new PopupWindow(popupView, 100, 100, true);
TextView tag = (TextView) v.findViewById(R.id.tag);
tag.setText("error!");
} else {
int x = tags.length;
int y = 5;
int z = Math.min(x, y);
for (int i = 0; i < z; i++) {
popupView = layoutInflater.inflate(R.layout.tag_table_row, null, false);
pw = new PopupWindow(popupView, 100, 100, true);
TextView tag = (TextView) popupView.findViewById(R.id.tag);
tag.setText(tags[i]);
int tag1score = Double.parseDouble(scores[i]);
TextView score = (TextView) popupView.findViewById(R.id.tagscore);
score.setText(Integer.toString(tag1score));
}
}
pw.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
it keeps track of the view you have inflated into, so you can search it for your elements.
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(....);
I used a FragmentTransaction to add a Fragment into a FrameLayout. I want to dynamically change the margin of the RelativeLayout used by the Fragment. However, the margins are not changing with RelativeLayout.layoutParams. I also used setMargins() and it didnt work.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.infocard, container, false);
RelativeLayout infoLayout = (RelativeLayout) view.findViewById(R.id.info);
infoLayout.setOnClickListener(new EmptyClickListener());
final int width = 250;
final int height = 320;
int leftMargin = 0;
int topMargin = 0;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
if (x - width < 0) {
leftMargin = 0;
System.out.println("left " + leftMargin);
}
else {
leftMargin = x - width;
}
if (y >= 450 && y <= 480) {
topMargin = y - height;
}
params.leftMargin = leftMargin;
params.topMargin = topMargin;
infoLayout.setLayoutParams(params);
Try using
FrameLayout.LayoutParams
instead of
RelativeLayout.LayoutParams
Layouts expect the params type to be the type from their parent container rather than the View type that you are setting them on