Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set imageview on right top of textview programmatically

I have to set image on Right Top of textview programmatically. But its setting on leftside of textview. Can any one tell me how to deal with this? My code is below:

  FrameLayout frameLayout = KaHOUtility.generateFrameLayout(mContext);
                frameLayout.setPadding(5, 5, 5, 5);
                LinearLayout linearLayout = KaHOUtility.generateLinearLayout(mContext);
                KaHOTextView textView = KaHOUtility.generatePanelHeadingTextViews(mContext);
                textView.setText(name);
                linearLayout.addView(textView);
                frameLayout.addView(linearLayout);
                ImageView imageView = KaHOUtility.generateImageView(mContext, 15, 15, R.drawable.cancel_mark);
                LinearLayout.LayoutParams rPrams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                rPrams.gravity = Gravity.RIGHT|Gravity.TOP ;
                imageView.setLayoutParams(rPrams);
                frameLayout.addView(imageView);
like image 951
Karthik Thunga Avatar asked Oct 18 '22 04:10

Karthik Thunga


1 Answers

You are creating layout params from the LinearLayout.LayoutParams class. However, the imageview is being added to a FrameLayout. This is incorrect because you only apply the layout params of the immediate parent to a view. So, in your case, it should be:

ImageView imageView = KaHOUtility.generateImageView(mContext, 15, 15, R.drawable.cancel_mark);
FrameLayout.LayoutParams rPrams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
rPrams.gravity = Gravity.RIGHT | Gravity.TOP ;
imageView.setLayoutParams(rPrams);
frameLayout.addView(imageView);
like image 181
Shaishav Avatar answered Oct 21 '22 04:10

Shaishav