Center Image inside LinearLayout with weight - android

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;

Related

Display a Linear Layout programmatically in a loop

Hello everyone please i need help. I'm a biginner in Android programming so since i was trying to solve this problem it took me all my days that why i want you to help me please. I'm trying to display a Linear layout according to the loop condition but the issue is that when i'm doing that it's just displaying the last one and i'm not able to see others when it's displaying. Here are my codes
ScrollView sv = new ScrollView(this);
sv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
sv.setPadding(5, 5, 5, 5);
sv.setBackgroundColor(getResources().getColor(R.color.colorWhite));
LinearLayout ll = null;
for (int i=0; i < 2; i++){
try {
//Here iam defining the LinearLayout
ll = new LinearLayout(this);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(params);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackground(getDrawable(R.drawable.circle_view));
//Here iam defining the RelativeLayout
RelativeLayout Rl = new RelativeLayout(this);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Rl.setLayoutParams(param);
//Defining a layout params for widgets
RelativeLayout.LayoutParams image = new RelativeLayout.LayoutParams(100, 90);
image.addRule(RelativeLayout.ALIGN_LEFT);
//Creating widgets
ImageView im = new ImageView(this);
im.setImageResource(R.mipmap.airtel);
im.setId(R.id.image1);
im.setLayoutParams(image);
//----------------
RelativeLayout.LayoutParams txtone = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
txtone.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
txtone.setMargins(0, 5, 0, 45);
TextView txtVone = new TextView(this);
txtVone.setText("ArkaL"+i);
//txtVone.setTextSize(R.dimen.MainTextSize);
txtVone.setTextSize(15);
txtVone.setTypeface(txtVone.getTypeface(), Typeface.BOLD);
txtVone.setLayoutParams(txtone);
//------------------------------------------------
RelativeLayout.LayoutParams txttwo = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
txttwo.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
txttwo.setMargins(0, 45, 0, 0);
TextView txtVtwo = new TextView(this);
txtVtwo.setText("République Démocratique du Congo");
txtVtwo.setTypeface(txtVtwo.getTypeface(), Typeface.SERIF.getStyle());
txtVtwo.setTextSize(10);
txtVtwo.setLayoutParams(txttwo);
Rl.addView(im);
Rl.addView(txtVone);
Rl.addView(txtVtwo);
RelativeLayout.LayoutParams recycler = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RecyclerView rv = new RecyclerView(this);
rv.setLayoutParams(recycler);
ll.addView(Rl);
ll.addView(rv);
}catch (Exception e){
}
}
//Here for assigning Linear to ScrollView
sv.addView(ll);
this.setContentView(sv);
Please help me.
your problem is that you created one LinearLayout and changed it in a loop and then added it to a ScrollView so at the end you will have just one LinearLayout. as you may know an ScrollView should have just one View as a child view so you should add a LinearLayout for a ScrollView and add as many LinearLayout as you want to the primary LinearLayout. your code can be like this:
LinearLayout llMain = new LinearLayout(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llMain.setLayoutParams(params2);
llMain.setOrientation(LinearLayout.VERTICAL);
sv.addView(llMain);
LinearLayout ll = null;
for (int i = 0; i < 2; i++) {
try {
//Here iam defining the LinearLayout
ll = new LinearLayout(this);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(params);
ll.setOrientation(LinearLayout.VERTICAL);
...
llMain.addView(ll);
} catch (Exception e) {
...
note that the Height of primary linearlayour should wrap its content;

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

How to add checkbox alongside button in linearlayout android programmatically?

I'm having a linear layout in which I want to insert a checkbox on left and a button on its right.
Also, it should be in for loop so as to create many number of such combination in that single linear layout.
final LinearLayout LayoutRight = (LinearLayout) findViewById(R.id.linearlayoutbars_register);
LayoutRight.setOrientation(LinearLayout.VERTICAL);
LayoutRight.setWeightSum(1f);
final LinearLayout.LayoutParams pR1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
pR1.weight = 0.3f;
final LinearLayout.LayoutParams pR2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
pR2.weight = 0.7f;
for (int i = l; i <= lastCounter; i++) {
tick = new CheckBox(RegisterActivity.this);
tick.setId(10000 + l);
tick.setOnClickListener(tick_Click);
LayoutRight.addView(tick, pR1);
pdR = new Button(RegisterActivity.this);
pdR.setText(l + ". ");
pdR.setId(l);
pdR.setWidth(800);
pdR.setTextSize(17);
pdR.setGravity(Gravity.START);
pdR.setOnClickListener(pdRclass);
LayoutRight.addView(pdR, pR2);
l++;
}
I'm setting 1f as the setWeightSum; 0.3f and 0.7f and childs weight.
With this code I'm getting output as this
But I want my output exactly like this.
You need to set Layout orientation Like this LayoutRight.setOrientation(LinearLayout.HORIZONTAL);
not VERTICAL
LayoutRight.setOrientation(LinearLayout.HORIZONTAL)?
I'm not cool enough to comment so...
Android weight doc says the effected attribute needs to be set to 0dp so try this.
final LinearLayout.LayoutParams pR1 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT);
pR1.weight = 0.3f;
final LinearLayout.LayoutParams pR2 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT);
pR2.weight = 0.7f;
don't forget to keep your orientation horizontal!

Set other layout parameters imageview

I would like to have an image on android that appears only when I click a button. So I created it in an onClick method, and set its ressource and position with the following code :
public void createAddButton() {
//create the image
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.add_16);
//setting image position : width and height
imageView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) );
}
But this only allows to define width and height of the image. If I want to define other layout parameters (android:layout_gravity or android:marginLeft for example, what should I do ?
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
lp.bottomMargin = ...
lp.leftMargin = ...
lp.rightMargin = ...
lp.topMargin = ...
imageView.setLayoutParams(lp);
You have to use LayoutParams object to access those layout properties as below...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
params.leftMargin = 10;
imageView.setLayoutParams(params);

How to set child weight in the code?

I want that button(child) will have 25% of full width of horizontal LinearLayout (its parent). The code:
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ll.setWeightSum (1.0f);
Button b = new Button (this);
ll.addView(b);
How can I do it? Thanks.
Try this:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENTS,LayoutParams.FILL_PARENT));
lp.weight = 0.25;
b.setLayoutParams(lp);
You can pass it in as part of the LinearLayout.LayoutParams constructor:
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);

Categories

Resources