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);
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);
Related
I'm having problem when adding the Imageview in my relativelayout if a layoutparams is added to the view, the imageview will not add but if the imageview has no layoutparams, it will show but no image. Imageview is created through code.
TableLayout imageCon = (TableLayout)root.findViewById(R.id.home_screen_table_imagecon);
TableRow imageTableRow = new TableRow(root.getContext());
TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageTableRow.addView(getLove());
imageTableRow.addView(getHappy());
imageCon.addView(imageTableRow,rowSpanLayout);
imageCon.setShrinkAllColumns(true);
private ImageView getLove(){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10,10);
imgLove.setLayoutParams(lp);
imgLove.setImageResource(R.drawable.img_emo_love);
return imgLove;
}
private ImageView getHappy(){
imgHappy.setImageResource(R.drawable.img_emo_happy);
return imgHappy;
}
I'm looking forward for your help guys.
Thanks
Chkm8
You need to use the TableRow.LayoutParams , since your adding the ImageView into a TableRow
Change the LayoutParams as
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
lp.setMargins(10, 10, 10,10);
imgLove.setLayoutParams(lp);
If ImageView is added to a RelativeLayout then use RelativeLayout.LayoutParams
If ImageView is added to a LinearLayout then use LinearLayout.LayoutParams
put below line in xml ImageView tag
android:adjustViewBounds="true"
enjoy...
I have created LinearLayout programatically as
LinearLayout wrapper = new LinearLayout(this.getContext());
and TextView as TextView et = new TextView(getContext());
i wanted textviews layout height to be wrapcontent so i did this
et.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
and then i add Textview et to LinearLayout as
wrapper.addView(et);
but when i set LayoutParams as above my textview dissapears and doesn't show in UI.
If I remove it by default Textview takes height as MATCHPARENT.
How can i set textviews layoutheight to WRAP_CONTENT?
Try this..
LinearLayout first_lay = new LinearLayout(this);
LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
first_lay.setOrientation(LinearLayout.VERTICAL);
first_lay.setLayoutParams(lp_icon);
LinearLayout.LayoutParams tests = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
TextView text1 = new TextView(this);
text1.setLayoutParams(tests);
text1.setText("Text");
text1.setTextSize(18);
first_lay.addView(text1);
also have to add that parent linear layout into the main contentlayout parent i.e it all be visible in side activity view
LinearLayout child_insidenew_layout = new LinearLayout(getActivity());
child_insidenew_layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams child_inside_paramsnew = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
child_insidenew_layout.setLayoutParams(child_inside_paramsnew);
child_insidenew_layout.setGravity(Gravity.CENTER_VERTICAL);
child_insidenew_layout.setBackgroundResource(R.drawable.layout_selector);
TextView textrootname = new TextView(getActivity());
LinearLayout.LayoutParams TextView_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textrootname.setText("here is ur text");
textrootname.setSingleLine(true);
textrootname.setGravity(Gravity.CENTER);
textrootname.setTextColor(Color.BLACK);
textrootname.setTextSize(15);
child_insidenew_layout.addView(textrootname, TextView_params);
i want to create a linear layout where inside it it has text view and image button. And i want to create it dynamically.
LinearLayout layoutSection = new LinearLayout(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
params.setMargins(5,5,5,5);
imageView.setImageResource(R.drawable.image);
imageView.setLayoutParams(params);
TextView textView = newTextView(getActivity());
TextView.setText(("Enter text here");
layoutSection.addView(textView);
layoutSection.addView(imageView);
I have a RelativeLayout with ImageView and TextView. I want to the TextView right below the ImageView(with a small padding) and both the ImageView and TextView, aligned to center in the RelativeLayout.
The RelativeLayout is added Programatically
I have read some questions on SO regarding the alignment, but, I can't get them to work for me. Below is my current code...
Code for RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
Code for ImageView
ImageView image = new ImageView(this);
RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpImage.addRule(RelativeLayout.CENTER_IN_PARENT);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
relativeLayout.addView(image);
Code for TextView
TextView textview = new TextView(this);
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTextView.addRule(RelativeLayout.BELOW, image.getId());
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
relativeLayout.addView(textview);
If I set RelativeLayout.CENTER_IN_PARENT for both image and text, they overlap eachother, which is understandable as RelativeLayout supports overlapping of views.
I thought setting RelativeLayout.BELOW for textview will make it align itself below the image, but it's not the case. I even tried RelativeLayout.ALIGN_BOTTOMfor textview, but even that didn't work.
Try this..
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams lprela = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(lprela);
ImageView image = new ImageView(this);
RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpImage.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
relativeLayout.addView(image);
TextView textview = new TextView(this);
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTextView.addRule(RelativeLayout.BELOW, image.getId());
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
relativeLayout.addView(textview);
Or
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(lp_ineer_ver);
ImageView image = new ImageView(this);
LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
linearLayout.addView(image);
TextView textview = new TextView(this);
LinearLayout.LayoutParams lpTextView = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
linearLayout.addView(textview);
Below process may also work for you.
1) Add Image view & Text view in vertical LinearLayout.
2) Add the linear layout to Center of Relative layout.(using CENTER_IN_PARENT).
I dynamically declared some horizontal layouts witch contains some elements like ImageButtons, Buttons, and TextViews, these elements centrally oriented, all of the elements are oriented perfectly but the TextViews. I tried both declaring them directly like the rest of the elements and declaring them each inside a vertical layout but both didn't work:
that's my code:
// the layout params for the Horizontal Layout
LinearLayout.LayoutParams lp_icon = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
0, 1);
lp_icon.setMargins(10, 15, 5, 0);
// the layout params for the elements that contained in the horizontal LinearLayout
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER);
// the layout params for the TextViews
LinearLayout.LayoutParams tests = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
// the layout params for the vertical layouts that contaion TextViews
LinearLayout.LayoutParams temp_lay = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
temp_lay.weight = 1;
// the elements declaration
icon1.setLayoutParams(lp_icon);// icon1 is then horizontal LinearLayout
icon1.setBackgroundResource(R.drawable.ac_overlay);
icon1.setOrientation(LinearLayout.HORIZONTAL);
icon1.setTag(NORMAL);
// TextView contained in a vertical Layout
LinearLayout lay = new LinearLayout(this);
icon1.addView(lay);
lay.setLayoutParams(temp_lay);
lay.setBackgroundColor(Color.TRANSPARENT);
lay.setOrientation(LinearLayout.VERTICAL);
TextView text1 = new TextView(this);
lay.addView(text1);
text1.setLayoutParams(tests);
text1.setText("Master Bedroom");
text1.setTextSize(12);
text1.setTextColor(Color.WHITE);
// other elements
ImageButton image = new ImageButton(this);
icon1.addView(image);
image.setLayoutParams(lp_ineer_ver);
image.setImageResource(R.drawable.grpbuttonfocus6);
image.setBackgroundColor(Color.TRANSPARENT);
Button testbut = new Button(this);
icon1.addView(testbut);
testbut.setLayoutParams(lp_ineer_ver);
testbut.setText(" 8");
testbut.setTextSize(12);
testbut.setBackgroundColor(Color.TRANSPARENT);
testbut.setTextColor(Color.WHITE);
ImageButton testcol = new ImageButton(this);
icon1.addView(testcol);
testcol.setLayoutParams(lp_ineer_ver);
testcol.setImageResource(R.drawable.home_cool);
testcol.setBackgroundColor(Color.TRANSPARENT);
// the TextView that's declared directly in the Horizontal Layout
TextView text2 = new TextView(this);
icon1.addView(text2);
text2.setLayoutParams(lp_ineer_ver);
text2.setText("00");
text2.setTextSize(12);
Try this..
Juse two changes in your code like below for first textview icon1.addView(text1); and the last textview text2.setLayoutParams(tests);
First
TextView text1 = new TextView(this);
icon1.addView(text1); //correction here
text1.setLayoutParams(tests);
text1.setText("Master Bedroom");
text1.setTextSize(12);
and Second
TextView text2 = new TextView(this);
icon1.addView(text2);
text2.setLayoutParams(tests); //correction here
text2.setText("00");
text2.setTextSize(12);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
and you can have Try adding the following code for applying the layout params to the TextView
LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(LinearLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);
in XML file :
android:gravity = "center"