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);
Related
I'm trying to add a textview and a button to a linear layout programatically.
The button is showing up but the textview isn't.
Here is my code:
LinearLayout main = (LinearLayout) findViewById(R.id.mainlayout);
LinearLayout first = new LinearLayout(this);
LayoutParams fparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5.0f);
LayoutParams tvparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
LayoutParams btparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
first.setLayoutParams(fparams);
first.setOrientation(LinearLayout.HORIZONTAL);
TextView tv = new TextView(this);
tvparams.weight = 3.0f;
tv.setLayoutParams(tvparams);
Button bt = new Button(this);
btparams.weight = 2.0f;
bt.setLayoutParams(btparams);
first.addView(bt);
first.addView(tv);
main.addView(first);
Try setting some text on the TextView. Use it's setText() method
ok try this to set programmatically
LinearLayout myLayout = (LinearLayout)findViewById(R.id.parent);
LayoutParams fparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5.0f);
LayoutParams tvparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT,3.0f);
LayoutParams btparams = new LayoutParams(0, LayoutParams.WRAP_CONTENT,2.0f);
LinearLayout first = new LinearLayout(this);
first.setLayoutParams(fparams);
first.setOrientation(LinearLayout.HORIZONTAL);
TextView tv = new TextView(this);
tv.setText("asdsad");
tv.setLayoutParams(tvparams);
Button bt = new Button(this);
bt.setLayoutParams(btparams);
first.addView(bt);
first.addView(tv);
myLayout.addView(first);
hope it ll help,working for me correctly.
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);
Here's an image of my problem:
The two small images in the Green and Red rectangles are the images I want centered. They should fall directly in the middle of both of thier parent's respectively.
Here's why it's not working (As far as I can tell):
I've set the width of the wrapper to 0 in order to avoid conflicts with the weight set for each wrapper. This lets me divide the screen right down the middle and divide content on either side. Unfourtunately, I think it also means that each image is trying to center itself on the 0dp portion of the wrapper rather than the dynamic width generated by the weight.
Here's the code, see for yourself:
public void mergeHotels(Hotel keepHotel, Hotel absorbHotel, ArrayList<Hotel> absorbArray){
Context context = getApplicationContext();
//get the current player and let him choose what to do with stock first
Player thisPlayer = players[getPlayerIndexByPlayOrder(CURRENT_TURN)];
//create the dialog for exchanging stocks
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
AlertDialog.Builder stockExchangeDialog = new AlertDialog.Builder(ctw);
stockExchangeDialog.setTitle(getResources().getString(R.string.stock_exchange_dialog_title));
LinearLayout dialogLayout = new LinearLayout(context);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout dialogHeader = new LinearLayout(context);
dialogHeader.setOrientation(LinearLayout.HORIZONTAL);
dialogHeader.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 50, 1.0f));
View blankSpace = new View(context);
blankSpace.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 2.0f));
LinearLayout tileOneWrapper = new LinearLayout(context);
tileOneWrapper.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
tileOneWrapper.setBackgroundColor(Color.GREEN);
ImageView tileOne = new ImageView(context);
String tileOneResourceName = "tile_" + keepHotel.getName().toLowerCase();
int tileOneResourceId = getResources().getIdentifier(tileOneResourceName, "drawable", getPackageName());
tileOne.setBackgroundResource(tileOneResourceId);
LinearLayout.LayoutParams tileOneParams = new LinearLayout.LayoutParams(40, 40);
tileOneParams.gravity = Gravity.CENTER;
tileOne.setLayoutParams(tileOneParams);
tileOneWrapper.addView(tileOne);
LinearLayout tileTwoWrapper = new LinearLayout(context);
tileTwoWrapper.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
tileTwoWrapper.setBackgroundColor(Color.RED);
ImageView tileTwo = new ImageView(context);
String tileTwoResourceName = "tile_" + absorbHotel.getName().toLowerCase();
int tileTwoResourceId = getResources().getIdentifier(tileTwoResourceName, "drawable", getPackageName());
tileTwo.setBackgroundResource(tileTwoResourceId);
LinearLayout.LayoutParams tileTwoParams = new LinearLayout.LayoutParams(40, 40);
tileTwoParams.gravity = Gravity.CENTER;
tileTwo.setLayoutParams(tileTwoParams);
tileTwoWrapper.addView(tileTwo);
dialogHeader.addView(blankSpace);
dialogHeader.addView(tileOneWrapper);
dialogHeader.addView(tileTwoWrapper);
LinearLayout dialogBody = new LinearLayout(context);
dialogBody.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < players.length; i++) {
LinearLayout playerStockDetails = new LinearLayout(context);
playerStockDetails.setOrientation(LinearLayout.HORIZONTAL);
TextView playerName = new TextView(context);
playerName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
TextView playerMoney = new TextView(context);
playerMoney.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
TextView playerTileOneQuantity = new TextView(context);
playerTileOneQuantity.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
playerTileOneQuantity.setBackgroundColor(Color.LTGRAY);
TextView playerTileTwoQuantity = new TextView(context);
playerTileTwoQuantity.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
playerTileTwoQuantity.setBackgroundColor(Color.CYAN);
playerName.setText(players[i].getName());
playerMoney.setText(String.valueOf(players[i].getMoney()));
playerTileOneQuantity.setText(String.valueOf(players[i].getStockQuantityByCode(getHotelCodeByName(keepHotel.getName()))));
playerTileTwoQuantity.setText(String.valueOf(players[i].getStockQuantityByCode(getHotelCodeByName(absorbHotel.getName()))));
playerStockDetails.addView(playerName);
playerStockDetails.addView(playerMoney);
playerStockDetails.addView(playerTileOneQuantity);
playerStockDetails.addView(playerTileTwoQuantity);
playerStockDetails.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
dialogBody.addView(playerStockDetails);
}
dialogLayout.addView(dialogHeader);
dialogLayout.addView(dialogBody);
stockExchangeDialog.setView(dialogLayout);
stockExchangeDialog.show();
}
So how do I get it to center the images based on the dynamic-width (based on weight), rather than the static width I declared in LayoutParams?
Thanks in advance for your help!
JRadTheBad
Try this..
Add this line in your LinearLayout LayoutParams
tileOneWrapper.gravity = Gravity.CENTER;
and also
tileTwoWrapper.gravity = Gravity.CENTER;
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"
How can I programmatically set button in right corner of EditText?
FrameLayout fl = new FrameLayout(this);
lv.addView(fl);
searchTxt = new EditText(this);
fl.addView(searchTxt);
btnSearch = new Button(this);
I tried this
btnSearch.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
fl.addView(btnSearch);
and
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0);
param.gravity = Gravity.RIGHT;
fl.addView(btnSearch, param);
But the button is still in the left corner.
You need to set layout param for button as following:
FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0);
param.gravity = Gravity.RIGHT;
and set it to button
btnSearch.setLayoutParam(param);
f1.addView(btnSearch);
Set the layoutparams to the LinearLayout to Fill_Parent in width