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 !
Related
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!
I'm using the CameraPreview example API demo. I need to add some views (button, etc..) overlaying the SurfaceView.
For this, I'm trying to set their parameters, but they appear all the time on the top-left side of the screen.
This is the onCreate method of the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
btnTakePhoto = new Button(this);
btnTakePhoto.setBackgroundResource(android.R.drawable.ic_menu_camera);
/*Set container*/
mPreview = new Preview(this);
setContentView(mPreview);
/*Set button params and add it to the view*/
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
addContentView(btnTakePhoto, buttonParams);
numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
defaultCameraId = i;
}
}
}
Have to say that the values here
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
are updating well if I change them. What doesn't change is what contains the addRule() method
Finally solved. When doing setContentView() and addContentView(), I was placing the views in a DecorView which is a FrameLayout. So, LayoutParams referencing RelativeLayout won't work, as for a FrameLayout only generic features of LayoutParams will work.
So, the thing is to first create a relativeLayout, set the params and set it as the content:
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
setContentView(relativeLayout, rlp);
But, now, every time I want to add a view, I have to add it to this relativeLayout this way:
relativeLayout.addView(View, Params);
Just this.
are you wanna to add the btnTakPhoto to the right center of the view? if so, have a try:
btn.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
Try This
// create main linearLayout and set properties
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setBackgroundColor(Color.TRANSPARENT);
// Create camera layout params
LinearLayout.LayoutParams cameralayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// set layout params
linearLayout.setLayoutParams(cameralayoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
int m_Height=ScreenHeight(this);
int buttonWH = 80;
// Create button for capture, save and discard
captureButton = new Button(this);
captureButton.setBackgroundResource(R.drawable.camera);
captureButton.setWidth(buttonWH);
captureButton.setHeight(buttonWH);
saveButton = new Button(this);
saveButton.setBackgroundResource(R.drawable.save);
saveButton.setVisibility(View.INVISIBLE);
saveButton.setWidth(buttonWH);
saveButton.setHeight(buttonWH);
discardButton = new Button(this);
discardButton.setBackgroundResource(R.drawable.discard);
discardButton.setVisibility(View.INVISIBLE);
discardButton.setWidth(buttonWH);
discardButton.setHeight(buttonWH);
// Create layout for controls
controlLayout = new LinearLayout(this);
controlLayout.setOrientation(LinearLayout.VERTICAL);
controlLayout.setBackgroundColor(Color.BLACK);
// Create params for control layout
LinearLayout.LayoutParams controlLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
// Set layout params
controlLayout.setLayoutParams(controlLayoutParams);
int buttonMargin = ((m_Height - (buttonWH * 3)) / 3) / 2;
// Create params for capture button
LinearLayout.LayoutParams buttonCaptureParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
buttonCaptureParams.setMargins(10, buttonMargin, 10, buttonMargin);
LinearLayout.LayoutParams buttonSaveParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
buttonSaveParams.setMargins(10, buttonMargin, 10, buttonMargin);
LinearLayout.LayoutParams buttonDiscardParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
buttonDiscardParams.setMargins(10, buttonMargin, 10, buttonMargin);
// Add button to main layout
controlLayout.addView(discardButton, buttonDiscardParams);
controlLayout.addView(captureButton, buttonCaptureParams);
controlLayout.addView(saveButton, buttonSaveParams);
// Make it full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set main layout as content view
setContentView(linearLayout);
Method to get screen height and width
public static int ScreenHeight(Context ctx) {
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) ctx).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.heightPixels;
}
public static int ScreenWidth(Context ctx) {
DisplayMetrics displaymetrics = new DisplayMetrics();
((Activity) ctx).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.widthPixels;
}
try to set the parent of btnTakePhoto to mPreview
btnTakePhoto.setParent(mPreview);
which will cause the button to appear above the preview.
ScrollView sv = new ScrollView(this);
this.addContentView(sv, new RelativeLayout.LayoutParams(screen.widthPixels, screen.heightPixels));
RelativeLayout FORM = new RelativeLayout(this);
sv.addView(FORM, new RelativeLayout.LayoutParams(screen.widthPixels, screen.heightPixels));
You must be targeting an API older than 14, setParent should work with newer API's. Above code is an alternative way, however you would have to place everything by code, but it works. Hope this helps.
FORM is the container, and you would have to add your other components like FORM.addView(btn) with relative layout 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'm having trouble calculating the weight inside of an scroll view on my android app. I want the app's content to resize proportionally to the screen size. How can I calculate the weight inside a scroll view?
Here some of my code:
LinearLayout layout_principal = (LinearLayout) findViewById(R.id.layout_items);// is horizontal layout and layout of Scrollview
LinearLayout layout_titulo_opcion = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
0,
1f);
layout_titulo_opcion.setLayoutParams(layoutParams);
LinearLayout layout_titulo_opcion2 = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
0,
1f);
layout_titulo_opcion2.setLayoutParams(layoutParams2);
layout_principal.addView(layout_titulo_opcion);
layout_principal.addView(layout_titulo_opcion2);
To get/set the weight, use this:
LinearLayout.LayoutParams mylp = (LinearLayout.LayoutParams)myLayout.getLayoutParams();
int myWeight = mylp.weight; // get weight
mylp.weight = 4; // set weight
To set the weight sum, use this:
myLayout.setWeightSum(10);
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);