I am developing an UI(Home screen) just like this screen
given in this link
http://www.flickr.com/photos/78431491#N07/7023063473/
-all information are comming from xml which is available on server means
1- number of button on screen
2- size, background images of screen and buttons,text on button each and everythings coming from
server
so we can't use xml for creating layout.I have been comleted tabbar and when I am creating buttons that is not taking any properties code used by me is as-
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
int id = getResources().getIdentifier("com.correlation.edumationui:drawable/" + img, null, null);
intent = new Intent().setClass(this, HomeScreen.class);//HomeScreen.class);
Bundle objbundle = new Bundle();
objbundle.putSerializable("screen", mscreen);
intent.putExtras(objbundle);
spec = tabHost.newTabSpec("title").setIndicator(title,getResources().getDrawable(id))
.setContent(intent);
tabHost.addTab(spec);
for creating button i am using this code--in homescreen Activity( HomeScreen.class)
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
for (int r = 0; r < 6; ++r)
{
Button btn = new Button(this);
btn.setText("A");
btn.setHeight(30);
btn.setWidth(224);
btn.setPadding(10,10,10,10);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); // Verbose!
lp.weight = 1.0f; // This is critical. Doesn't work without it.
buttonsView.addView(btn, lp);
}
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setContentView(buttonsView, lp);
give me your valuable help....
Instead of
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); // Verbose!
lp.weight = 1.0f; // This is critical. Doesn't work without it.
buttonsView.addView(btn, lp);
Use
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(30, 224); // Verbose!
lp.weight = 1.0f; // This is critical. Doesn't work without it.
buttonsView.addView(btn,lp);
to reflect the width and height of buttons in layout parameters.
Use LayoutParameters to set width, height and margins on button.
Try setting Height, Width and Padding using setLayoutParams as:
Button btn = new Button (this);
LayoutParams params = Prams // Set Params Which you want to set for your view.
btn.setLayoutPrams(params);
Hope this will help you.
Set Height and width programmatically, you have use set layoutparams like below.
LinearLayout ly = (LinearLayout) findViewById(R.id.verify);
Button buyButton = new Button(this);
buyButton.setText("button");
buyButton.setLayoutParams(new LayoutParams(50,
50));
buyButton.setPadding(10, 10, 10, 10);
ly.addView(buyButton);
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.
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 !
I created a Dialog using LinearLayout and in this Dialog, I have a Button. I want to set its width to 50% of the Dialog's width, but no matter what I do, the button is still filling the entire width. The main problem seems to be that I cannot set the button's weight programmatically. I tried creating a second horizontal LinearLayout that would hold the button with weight set to 0.5f (the parent has a weightSum of 1), but that did not change anything. How can I set the button to be half the width?
I've looked at all the other questions but most of them were using xml so they were not relevant, and the one that solved it programmatically was using hardcoded values in pixels which is obviously an incorrect solution to the problem.
EDIT: Adding some code
LinearLayout linearLayout = new LinearLayout(mContext);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setWeightSum(1);
// I use these params when I call addContentView
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
testButton = new Button(mContext);
testButton.setText("Test");
testButton.setId(1);
testButton.setWidth(0);
testButton.setGravity(Gravity.CENTER);
LinearLayout buttonLayout = new LinearLayout(mContext);
butonLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f);
testButton.setLayoutParams(buttonParams);
buttonLayout.addView(testButton);
linearLayout.addView(buttonLayout);
...
this.addContentView(scrollView, layoutParams);
As I tried more and more options, the code became a mess. I decided to start from scratch and this code worked in the end:
LinearLayout forButton = new LinearLayout(mContext);
forButton.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams forButtonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
forButton.setGravity(Gravity.CENTER);
DisplayMetrics dm = new DisplayMetrics();
this.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
forButton.addView(testButton);
testButton.getLayoutParams().width = width/2;
Are you setting the width value to 0dp as well? This is required for the layout engine to automatically to use layout weight respectively.
Edit
I think this line will get you the results you are looking for:
LayoutParams buttonParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f);
To find dialog width
DisplayMetrics dm = new DisplayMetrics();
dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels ;
To Set Value programmatically
final LayoutParams lparams = new LayoutParams(Height,width/2); // Width , height
final Button button = new Button(this);
button.setLayoutParams(lparams);
EDIT
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 200;
lp.height = 200;
alertDialog.getWindow().setAttributes(lp);
I need do design programatically one activity with 6 buttons (same size h and w), and all buttons show a fullsize of activity.
I tried do this: RelativeLayout with buttons and modify for tests.
Show one button!!!
`
setContentView(R.layout.main);
ArrayList<Button> buttons = new ArrayList<Button>();
//RelativeLayout bg = (RelativeLayout) findViewById(R.layout.main);
for (int i = 0; i < 10; i++) {
Button newButton = new Button(this);
newButton.setId(100 + i + 1); // ID of zero will not work
newButton.setText("XXXX");
buttons.add(newButton);
// New layout params for each button
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
// using getId() here in case you change how you assign IDs
int id = buttons.get(i - 1).getId();
lp.addRule(RelativeLayout.RIGHT_OF, id);
}
this.addContentView(newButton, lp);
}`
please look this line if ok: this.addContentView(newButton, lp);
Thanks!!!
mateus
It's a bit not clear from your question whether you want all buttons to distribute evenly across the activity space or each button to take over all activity space?
In first case what you need is a LinearLayout with layout_weight for buttons set to 1.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.weight = 1;
In the second case I think it's better to use a FrameLayout and just let buttons take over all space by setting
FrameLayout.LayoutParams.FILL_PARENT
to both dimensions.