Set layout params dynamically - android

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.

Related

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!

android dynamically created layouts overlapping each other

I want to create a relative layout inside a linear layout dynamically.This relative layout contains a text view and button which are also created in dynamic method.The alignment of text view and button not work properly.The code which i have tried is given below.
final LinearLayout lab_linear = (LinearLayout) findViewById(R.id.contact_list_layout);
lab_linear.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < Size_contact; i++)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(lp);
final TextView Questions_value = new
android.widget.TextView(getApplicationContext());
Questions_value.setText(contact_name.get(i));
Questions_value.setTextSize(18);
Questions_value.setId(i);
layout.addView(Questions_value);
Button myButton = new Button(this);
myButton.setBackgroundResource(R.drawable.close);
myButton.setLayoutParams(new LayoutParams(20,20));
layout.addView(myButton);
lab_linear.addView(layout);
}
Try this..
Give layout parems for RelativeLayout individually to TextView and Button and also add addRule(RelativeLayout.ALIGN_PARENT_RIGHT); or addRule(RelativeLayout.ALIGN_PARENT_LEFT); for that views
Or add the below in for Button
lp1.addRule(RelativeLayout.RIGHT_OF, Questions_value.getId());
for (int i = 0; i < Size_contact; i++)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
final TextView Questions_value = new
android.widget.TextView(getApplicationContext());
Questions_value.setText(contact_name.get(i));
Questions_value.setTextSize(18);
Questions_value.setId(i);
layout.addView(Questions_value,lp);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//Or below remove above code and uncomment the below code
//lp1.addRule(RelativeLayout.RIGHT_OF, Questions_value.getId());
Button myButton = new Button(this);
myButton.setBackgroundResource(R.drawable.close);
myButton.setLayoutParams(new LayoutParams(20,20));
layout.addView(myButton,lp1);
lab_linear.addView(layout);
}
Might I suggest using LinearLayouts instead of RelativeLayouts in your loop? That way, you won't have the overlap problem.

Android Linear Layout Weight Programmatically

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 !

How to Create multiple buttons in android

I tried the below code to create multiple buttons through programatically but it creates a single button as per my application i need to create a button based on input. For example if the input is 3 means i need to create three buttons in a layout. For your reference i attached the sample image and my code.
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
if in your example, your global container (main_layer) is a relativelayout or a frame layout, you have placed them on top of eachother. So you can't see the one on the back order.
Try this pls,
LinearLayout main_layer= (LinearLayout) findViewById(id.main_layer);
for (int i = 0; i < 10; i++)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
main_layer.addView(layout);
}
You are creating multiple new linear layouts within the for loop. Linear layout need to be taken out of the for loop and just try adding the buttons in it.
1st run of for loop creates a new linear layout and adds a button
2nd run of for loop creates a new linear layout and adds a button on the top of previous added layout
Here you are creating a new LinearLayout per button... Try something more like...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
for (int i = 0; i < array_of_btn_input.size(); i++) {
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button button1 = new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
}
main_layer.addView(layout);
Try this
LinearLayout llParent = (LinearLayout) findViewById(R.id.llParent);
llParent.removeAllViews();
for (int i = 0; i < array_of_btn_input.size(); i++) {
BSView b = new BSView(this, new SimpleThumbBean(i + 1, bsList
.get(i).getLabel(), bsList.get(i).getThumbUrl()));
LinearLayout.LayoutParams llDynamic = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
llDynamic.weight = 1f;
llParent.addView(b, llDynamic);
}
llParent is a defined Layout and make sure you call removeAllViews() before adding layouts dynamically into it.
BSView is my own custom View. You can set BSView into ordinary Button as you need it.

Button is not take its properties?

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

Categories

Resources