I have some vertical LinearLayouts in my app, every one contains a 2TextViews, 2ImgaeButtons, and a Button, every Layout has to be dynamically created because of some data that must be obtained through Run_Time, I'm creating it that way:
// declaring Layout Params
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
temp_lay = new LinearLayout.LayoutParams(metrics.widthPixels/4, LayoutParams.MATCH_PARENT);
temp_lay.setMargins(10, 15, 5, 0);
lp_ineer_ver = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER);
lp_ineer_ver.bottomMargin = 2;
lp_ineer_ver.leftMargin = 12;
lp_ineer_ver.topMargin = 2;
// Declaring Layouts and it's contents
List<LinearLayout> inner_ver = new ArrayList<LinearLayout>();
LinearLayout temp_inner_ver = new LinearLayout(this);
temp_inner_ver.setLayoutParams(temp_lay);
temp_inner_ver.setOrientation(LinearLayout.VERTICAL);
temp_inner_ver.setWeightSum(2);
temp_inner_ver.setPadding(7, 7, 7, 7);
inner_ver.add(temp_inner_ver);
LinearLayout icon1 = new LinearLayout(this);
inner_ver.get(0).addView(icon1);
icon1.setLayoutParams(lp_icon);
icon1.setBackgroundResource(R.drawable.ac_overlay);
icon1.setOrientation(LinearLayout.HORIZONTAL);
icon1.setTag(Lights);
TextView text1 = new TextView(this);
icon1.addView(text1);
LinearLayout.LayoutParams elements_params = (LinearLayout.LayoutParams)text1.getLayoutParams();
elements_params.gravity = Gravity.CENTER;
elements_params.leftMargin = 3;
elements_params.weight = 1;
text1.setLayoutParams(elements_params);
text1.setText("Living Room");
text1.setTextSize(12);
text1.setTextColor(Color.WHITE);
text1.setGravity(Gravity.CENTER|Gravity.LEFT);
ImageButton rgb1 = new ImageButton(this);
icon1.addView(rgb1);
rgb1.setLayoutParams(lp_ineer_ver);
rgb1.setImageResource(drawable);
rgb1.setBackgroundColor(Color.TRANSPARENT);
Button rgp_value1 = new Button(this);
icon1.addView(rgp_value1);
elements_params = (LinearLayout.LayoutParams)rgp_value1.getLayoutParams();
elements_params.bottomMargin = 2;
elements_params.topMargin = 2;
elements_params.leftMargin = 12;
elements_params.gravity = Gravity.CENTER;
rgp_value1.setLayoutParams(elements_params);
rgp_value1.setText(status.toString());
rgp_value1.setTextSize(12);
rgp_value1.setBackgroundColor(Color.TRANSPARENT);
rgp_value1.setTextColor(Color.WHITE);
ImageButton cool1 = new ImageButton(this);
icon1.addView(cool1);
cool1.setLayoutParams(lp_ineer_ver);
cool1.setImageResource(Color.TRANSPARENT);
cool1.setBackgroundColor(Color.TRANSPARENT);
TextView cool_value1 = new TextView(this);
icon1.addView(cool_value1);
elements_params = (LinearLayout.LayoutParams)cool_value1.getLayoutParams();
elements_params.gravity = Gravity.CENTER;
elements_params.leftMargin = 12;
elements_params.rightMargin = 3;
cool_value1.setLayoutParams(elements_params);
cool_value1.setGravity(Gravity.CENTER|Gravity.RIGHT);
cool_value1.setText("17");
cool_value1.setTextSize(12);
// Ading the Declared Layouts to the static defined Layout
ver_rooms = (LinearLayout) findViewById(R.id.ver_rooms);
ver_rooms.addView(inner_ver_rooms.get(0));
and this is the statically declared Layouts that contains that dynamically declared Layouts:
<HorizontalScrollView
android:id="#+id/scroll_rooms"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/ver_rooms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
this work gives me that result:
but i want this result:
Related
Im trying to set a width and height for my cardView, so once I download an image from the database, a cardView is added by it stretches the image and can't control the height, im trying to do it programatically. This is what i've got.
public void CreateCardView(final Users u) throws IOException
{
CardView.LayoutParams params = new CardView.LayoutParams(
CardView.LayoutParams.WRAP_CONTENT,
CardView.LayoutParams.WRAP_CONTENT
);
params.setMargins(10,10,10,20);
params.width = 500;
mLoginFormView = findViewById(R.id.containerGrid);
CardView card = new CardView(this);
card.setLayoutParams(params);
card.setRadius(9);
card.setCardElevation(9);
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
LinearLayout lin = new LinearLayout(this);
lin.setOrientation(LinearLayout.VERTICAL);
lin.setLayoutParams(par);
final ImageButton Name = new ImageButton(this);
Name.setAdjustViewBounds(true);
Name.setScaleType( ImageView.ScaleType.CENTER);
mLoginFormView.bringToFront();
// Name.setBackground(bdrawable);
Name.setAdjustViewBounds(true);
//Name.setImageBitmap(i.getImage().get(0));
lin.bringToFront();
mLoginFormView.addView(card);
card.setElevation(15);
System.out.println("Hello?");
//Name.setImageBitmap( scaled);
TextView username = new TextView(this);
TextView sport = new TextView(this);
username.setText(u.getUsername());
sport.setText(u.getEmail());
lin.addView(Name);
lin.addView(username);
lin.addView(sport);
card.addView(lin);
card.setElevation(15);
Instead of this code:
CardView.LayoutParams params = new CardView.LayoutParams(
CardView.LayoutParams.WRAP_CONTENT,
CardView.LayoutParams.WRAP_CONTENT
);
params.setMargins(10,10,10,20);
params.width = 500;
Try this code:
card.setLayoutParams(new LayoutParams(width, height));
I am using this code to show a table dynamically inside a relative layout.
I can see on debug mode that the relative layout and a child of table layout but it is not visible on the screen.
What am I doing wrong?
The code is:
RelativeLayout relative = (RelativeLayout) findViewById(R.id.my_relative_layout);
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
tableLayout = new TableLayout(MyActivity.this);
tableLayout.setBackgroundColor(Color.BLACK);
TableRow.LayoutParams tableHeaderRowParams = new TableRow.LayoutParams();
tableHeaderRowParams.setMargins(5, 5, 5, 5);
tableHeaderRowParams.weight = 0;
tableHeaderRowParams.span = 2;
TableRow tableHeaderRow = new TableRow(MyActivity.this);
tableHeaderRow.setBackgroundColor(Color.WHITE);
TextView txvHeader = new TextView(MyActivity.this);
txvHeader.setLayoutParams(tableHeaderRowParams);
txvHeader.setBackgroundColor(Color.WHITE);
txvHeader.setGravity(Gravity.CENTER);
txvHeader.setTag(10);
txvHeader.setId(10);
txvHeader.setTextColor(Color.BLUE);
txvHeader.setTextSize(TypedValue.COMPLEX_UNIT_SP, rowTextSize);
txvHeader.setTypeface(null, Typeface.BOLD);
txvHeader.setText(“Header”);
tableHeaderRow.addView(txvHeader, tableHeaderRowParams);
tableLayout.addView(tableHeaderRow, tableLayoutParams);
RelativeLayout.LayoutParams lpTable = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTable.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
lpTable.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
lpTable.topMargin = 9 * space + 6 * rowHeight;
relative.addView(tableLayout, lpTable);
Thanks!
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!
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 want to add three linear layouts to an activity programatically each of same width. the problem is i am not able to set the weights of these layouts programmatically. I could do this within xml, but I want to do this in program.
here is what I want:
Here its the solution
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
See Full Solution
LinearLayout ll1, ll2, ll3;
/* Find these LinearLayout by ID
i.e ll1=(LinearLayout)findViewById(R.id.ll1);
*/
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
ll1.setLayoutParams(lp);
ll2.setLayoutParams(lp);
ll3.setLayoutParams(lp);
Use new LinearLayout.LayoutParams(int width, int height, float weight) to set weights when setting layout params to the subviews
Do this way..
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNote = (LinedEditText) findViewById(R.id.txtNote);
lnr = (LinearLayout) findViewById(R.id.lnr);
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
LinearLayout l3 = new LinearLayout(this);
l1.setBackgroundResource(android.R.color.holo_green_light);
l2.setBackgroundResource(android.R.color.holo_orange_dark);
l3.setBackgroundResource(android.R.color.holo_blue_bright);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1);
lnr.addView(l1, param);
lnr.addView(l2, param);
lnr.addView(l3, param);
}
You can do it by setting layout weight property for your individual linear layouts, pass it in LinearLayout - LayoutParams constructor:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParam(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1);
or
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
0,
LayoutParams.MATCH_PARENT, 1);
Hope it may help you !