The TextView doesn't fit in the center - android

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"

Related

How create LinearLayout dynamically with aligned elements

I'm very new at android. Need to dynamically create LinearLayout inside RadioGroup with aligned TextViews as following. Here I just moved elements from palette and haven't changed any properties
But get this
What am I missing?
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
1.0f);
final LinearLayout linearLayout = new LinearLayout(this);
final TextView text1 = new TextView(this);
text1.setText("aaa");
text1.setLayoutParams(param);
linearLayout.addView(text1);
final TextView text2 = new TextView(this);
text2.setText("bbb");
text2.setLayoutParams(param);
linearLayout.addView(text2);
final TextView text3 = new TextView(this);
text3.setText("ccc");
text3.setLayoutParams(param);
linearLayout.addView(text3);
radioGroup.addView(linearLayout);
LayoutParams should be both MATCH_PARENT and linearLayout should also be set to that params

How to Align Two Fields Horizontally in Android Programatically

I was Programmatically Creating Linear Layout and I want to Align two Fields on same Line. But i Want to display the result as like expected.
lView = new LinearLayout(Main2Activity.this);
// lView.setPadding(0,150,0,0);
lView.setBackgroundColor(Color.parseColor("#EDFCFC"));
lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Button logout = new Button(Main2Activity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
logout.setLayoutParams(params);
logout.setText("Logout");
logout.setBackgroundColor(Color.parseColor("#F53F37"));
lView.addView(logout);
TextView title = new TextView(Main2Activity.this);
title.setText("ASSOCIATES");
title.setTextColor(Color.parseColor("#2BD865"));
title.setTextSize(25);
title.setTypeface(null, Typeface.BOLD);
title.setGravity(Gravity.CENTER);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title);
TextView title1 = new TextView(Main2Activity.this);
title1.setText("Date :");
title1.setTextSize(20);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title1);
et1 = new EditText(Main2Activity.this);
et1.setWidth(10);
et1.setGravity(Gravity.RIGHT);
et1.setHint("Date");
//lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(et1);
My Actual Output is
Expected
You need to set gravity for your parent LinearLayout to align childs.
set the Linear Layout orientation to horizontal like this:
lView.setOrientation(LinearLayout.HORIZONTAL);
Add LayoutParams to your textview and edittext and you will get the specified view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.CENTER;
TextView title1 = new TextView(Main2Activity.this);
title1.setLayoutParams(params);
title1.setText("Date :");
title1.setTextSize(20);
lView.setOrientation(LinearLayout.VERTICAL);
lView.addView(title1);
params.gravity = Gravity.LEFT;
et1 = new EditText(Main2Activity.this);
et1.setLayoutParams(params);
et1.setWidth(10);
et1.setGravity(Gravity.RIGHT);
et1.setHint("Date");
lView.setOrientation(LinearLayout.HORIZONTAL);
lView.addView(et1);

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);
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);

Setting Layout Height to Wrapcontent to dynamically created textview

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);

How to create a linear layout with textview and image button dynamically

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);

Categories

Resources