How can we add buttons at dynamic positions in layout? - android

How can we add buttons at dynamic positions in layout or using canvas, not in table layout?
Can anyone please help me on this?

Use RelativeLayout to position your controls where you like them. Have a look at this link:
Dynamic TextView in Relative layout
and here
How to create a RelativeLayout programmatically with two buttons one on top of the other?
If you like to achieve it within XML only. Look here:
http://www.mkyong.com/android/android-relativelayout-example/
Here an example how you could use the RelativeLayout to position your controls dynamically:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creating a new RelativeLayout
RelativeLayout mainRelativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters with Fill_Parent
RelativeLayout.LayoutParams relativeLayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
// Creating a new Left Button
Button buttonLeft = new Button(this);
buttonLeft.setText("Left");
// Creating a new Left Button with Margin
Button buttonLeftWithMargin = new Button(this);
buttonLeftWithMargin.setText("Left with Margin");
// Creating a new Center Button
Button buttonCenterParent = new Button(this);
buttonCenterParent.setText("Center");
// Creating a new Bottom Button
Button buttonBottom = new Button(this);
buttonBottom.setText("Bottom");
// Add a Layout to the Buttons
AddButtonLayout(buttonLeft, RelativeLayout.ALIGN_PARENT_LEFT);
AddButtonLayout(buttonCenterParent, RelativeLayout.CENTER_IN_PARENT);
AddButtonLayout(buttonBottom, RelativeLayout.ALIGN_PARENT_BOTTOM);
// Add a Layout to the Button with Margin
AddButtonLayout(buttonLeftWithMargin, RelativeLayout.ALIGN_PARENT_LEFT, 30, 80, 0, 0);
// Add the Buttons to the View
mainRelativeLayout.addView(buttonLeft);
mainRelativeLayout.addView(buttonCenterParent);
mainRelativeLayout.addView(buttonBottom);
mainRelativeLayout.addView(buttonLeftWithMargin);
// Setting the RelativeLayout as our content view
setContentView(mainRelativeLayout, relativeLayoutParameters);
}
private void AddButtonLayout(Button button, int centerInParent, int marginLeft, int marginTop, int marginRight, int marginBottom) {
// Defining the layout parameters of the Button
RelativeLayout.LayoutParams buttonLayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add Margin to the LayoutParameters
buttonLayoutParameters.setMargins(marginLeft, marginTop, marginRight, marginBottom);
// Add Rule to Layout
buttonLayoutParameters.addRule(centerInParent);
// Setting the parameters on the Button
button.setLayoutParams(buttonLayoutParameters);
}
private void AddButtonLayout(Button button, int centerInParent) {
// Just call the other AddButtonLayout Method with Margin 0
AddButtonLayout(button, centerInParent, 0 ,0 ,0 ,0);
}
}
And you should get something like this:

Related

how to Generate Dynamic Controll in android?

I want to generate Control dynamically by user specific value.
Like i have EditText for ControlId, Control Width, Control Height etc.
based on that value i want to generate control
LinearLayout L1 = new LinearLayout(this);
L1.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams L1paeam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
L1.setLayoutParams(L1paeam);
TextView T1 = new TextView(this);
T1.setText(R.string.Dynamic_text);
L1.addView(T1);
Button B1=new Button(this);
B1.setText("Dynamic Button");
L1.addView(B1);
setContentView(L1);
In this code the control-id Layout_height and Layout_width are specified but i want them as user specified
You can add any View in view container (LinearLayout, RelativeLayout, etc) and remove those views in runtime. Create view and specify layout params for that view.
Here the solution i found.
public void generateButton(String Id,int Width,int Height,String text){
#IdRes
int id = Integer.parseInt(Id);
LinearLayout.LayoutParams L1param = new LinearLayout.LayoutParams(Width, Height);
final Button B1=new Button(this);
B1.setId(id);
B1.setText(text);
B1.setLayoutParams(L1param);
B1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Button click operation goes here
}
});
layout_main.addView(B1);
}

Android: Align a Button to the Right in LinearLayout

I have a piece of code which I use to create a new LinearLayout. Within the layout I wish to add a TextView which contains both a label and a value. Then next to it on the right I want to display the button. I want the button to be located toward the end of the screen, without stretching the button. I am happy with the button width and height as WARP_CONTENT.
How can I achieve this in code? I have barely any XML so using XML is not an option. I am trying to make the app as dynamic as possible, so I decided to steer clear of XML.
Please see the code below:
// Build a button
final Button addButton = new Button(task.getParent());
addButton.setText("Add New");
addButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Open a file picker here to let the user pick a file
}
});
// Build a new layout to hold all the elements
LinearLayout verticalLayout = new LinearLayout(task.getParent());
verticalLayout.setOrientation(LinearLayout.HORIZONTAL);
verticalLayout.addView(sizeTextView);
verticalLayout.addView(addButton);
Thank you guys in advance.
Try this: Add Space (View) between TextView & Button.
// View space = new View(parent_context);
View space = new View(task.getParent());
// Width:0dp, Height:1 & Weight: 1.0
LinearLayout.LayoutParams spaceLP = new LinearLayout.LayoutParams(0, 1, 1.0f);
space.setLayoutParams(spaceLP);
verticalLayout.addView(sizeTextView);
verticalLayout.addView(space);
verticalLayout.addView(addButton);
Add textview with size and gravity,like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setLayoutDirection(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
verticalLayout.addView(sizeTextView,params);
To achieve this you should use Relative layout and RelativeLayout.LayoutParams. By using LayoutParams you can set the rule to align your views as per your requirements.
for example
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);

Adding Buttons dynamically in RelativeLayout to LinearLayout

When the user inputs a word, he creates a number of Buttons equal to the length of the word. For example: if user inputs "aaaa" he will create 4 Buttons, side by side, in the first row. Then if the user enters "bb" he will create 2 Buttons, side by side, in the second row. And "ccc" he creates 3 Buttons...
Image to demonstrate:
I dynamically create a RelativeLayout, then dynamically add Buttons to that layout. And finally I add the RelativeLayout to my existing LinearLayout. But the problem is, only one Button is added per row. And my program currently looks like this:
Can someone please me fix this problem?
CODE:
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
RelativeLayout relativeLayout = new RelativeLayout(view.getContext());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length(); //the user input number of buttons
int id = 1;
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
myButton.setId(id);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
relativeLayout.addView(myButton, rlp);
id++;
}
linearLayout.addView(relativeLayout, llp);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
This line says that myButton should be added to right of myButton, which doesn't make any sense.
simple way to resolve this is to use the following line instead
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);
But this isn't the best way to do this, you should use LinearLayout with horizontal orientation instead.
The structure should be simple
Just need to add your buttons in 3 different linear layout with orientation horizontal.
Like
<Relative layout>{
<LinearLayout global container with vertical orientation >{
<LinearLayout for 'a' type buttons container with horizontal orientation>
<LinearLayout for 'b' type buttons container with horizontal orientation>
<LinearLayout for 'c' type buttons container with horizontal orientation>
}
}
You guys are right. It is much easier using a LinearLayout. For those interested
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
LinearLayout linearLayout2 = new LinearLayout(view.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
linearLayout2.addView(myButton, rlp);
}
linearLayout.addView(linearLayout2, llp);

Having trouble positioning android dynamically loaded buttons

I'm creating buttons dynamically in my class, I try to position them using 'offsetLeftAndRight()' or '.leftMargin' and '.topMargin' as follows,
public class instruction extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instruct);
final Button btn = new Button(this);
RelativeLayout.LayoutParams paramsd2 =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsd2.leftMargin = 500;
paramsd2.topMargin = 500;
paramsd2.height = 60;
paramsd2.width = 200;
btn.offsetLeftAndRight(300);
btn.setLayoutParams(paramsd2);
addContentView(btn, paramsd2);
}
But the button always stays in the top left corner, how can I position it, what am I doing wrong?
AddContentView() is not the proper way to add a view in an already set layout.
make your main layout a RelativeLayout (check this in the instruct.xml layout file)
use its id to retreive a reference on it in your onCreate() method using
myRelativeLayout = (RelativeLayout) this.findViewById(R.id.itsId)
then add your button to this layout :
myRelativeLayout.addView(myButton);
the layout params of your button seems fine for positioning so it should work.
Set margin on button rather then layout
MarginLayoutParams marginParams = new MarginLayoutParams(backToMainScreenImageView.getLayoutParams());
marginParams.setMargins(0, 0, (int) UIUtil.getRadialButtonMainMargin(this), 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
backToMainScreenImageView.setLayoutParams(layoutParams);
Try something like this :
paramsd2.setMargin(500, 500, 0, 0);
btn.setLayoutParams(paramsd2);

Android RelativeLayout My Own MousePadView

Please help me to fix the code below.
I am making a mousepad view for my Android Remote Application.
public class MousePadView extends RelativeLayout {
private float scale;
// MOUSE BUTTONS LEFT CLICK, MIDDLE CLICK, RIGHT CLICK
private LinearLayout layoutMouseButtonBody;
private RelativeLayout.LayoutParams paramsMouseButtonBody;
private Button left;
private Button middle;
private Button right;
private LinearLayout.LayoutParams paramsButtons;
// BUTTONS FOR MOUSEWHEEL (UP AND DOWN)
private LinearLayout layoutWheelsBody;
private RelativeLayout.LayoutParams paramsWheelBody;
private Button up;
private Button down;
private LinearLayout.LayoutParams paramsWheelButton;
public MousePadView2(Context context, AttributeSet attrs, int defStyle) { // CONSTRUCTOR
super(context, attrs, defStyle);
scale = context.getResources().getDisplayMetrics().density; // GET SCALE FOR CONVERTING DPI TO PIXELS
// MOUSE BUTTON LAYOUT
paramsButtons = new LinearLayout.LayoutParams(DpiToPixels(0), LayoutParams.WRAP_CONTENT, 1);
left = new Button(context);
left.setText("L");
left.setLayoutParams(paramsButtons);
middle = new Button(context);
middle.setText("M");
middle.setLayoutParams(paramsButtons);
right = new Button(context);
right.setText("R");
right.setLayoutParams(paramsButtons);
paramsMouseButtonBody = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
paramsMouseButtonBody.addRule(ALIGN_PARENT_BOTTOM); // RELATIVE LAYOUT RULES
paramsMouseButtonBody.addRule(ALIGN_PARENT_LEFT); // RELATIVE LAYOUT RULES
paramsMouseButtonBody.addRule(ALIGN_PARENT_RIGHT); // RELATIVE LAYOUT RULES
layoutMouseButtonBody = new LinearLayout(context);
layoutMouseButtonBody.setBackgroundResource(android.R.drawable.bottom_bar);
// layoutMouseButtonBody.setPadding(0, DpiToPixels(4), 0, 0);
layoutMouseButtonBody.setOrientation(LinearLayout.HORIZONTAL);
layoutMouseButtonBody.setLayoutParams(paramsMouseButtonBody);
layoutMouseButtonBody.addView(left);
layoutMouseButtonBody.addView(middle);
layoutMouseButtonBody.addView(right);
// WHEELS
paramsWheelButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
up = new Button(context);
up.setText("U");
up.setLayoutParams(paramsWheelButton);
down = new Button(context);
down.setText("D");
down.setLayoutParams(paramsWheelButton);
paramsWheelBody = new RelativeLayout.LayoutParams(DpiToPixels(32), LayoutParams.WRAP_CONTENT);
paramsWheelBody.addRule(LEFT_OF, layoutMouseButtonBody.getId());
paramsWheelBody.addRule(ALIGN_PARENT_BOTTOM);
layoutWheelsBody = new LinearLayout(context);
layoutWheelsBody.setOrientation(LinearLayout.VERTICAL);
layoutWheelsBody.setBackgroundResource(android.R.drawable.bottom_bar);
layoutWheelsBody.setLayoutParams(paramsWheelBody);
layoutWheelsBody.addView(up);
layoutWheelsBody.addView(down);
// PARENT
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(layoutMouseButtonBody); // add mousebutton layout in parent (relativelayout)
addView(layoutWheelsBody); // add mousewheel button layout in parent (relativelayout)
}
private int DpiToPixels(int dp) {
return (int)(dp * scale + 0.5f); // converting DPI to Pixels
}
}
The image on the Left is the output generated by Android SDK and The right one is the output that I want.
Please Help me.
I don't want to inflate layout from XML.
I think the only way to achieve what you're looking for is by grouping both your wheel and button layout into another layout. After that, simply align the result to the bottom of the parent.
Add something like this:
LayoutParams paramsTotal = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
paramsTotal.addRule(ALIGN_PARENT_BOTTOM);
LinearLayout layoutTotal = new LinearLayout(context);
layoutTotal.setOrientation(LinearLayout.VERTICAL);
layoutTotal.setLayoutParams(paramsTotal);
layoutTotal.addView(layoutWheelsBody);
layoutTotal.addView(layoutMouseButtonBody);
// PARENT
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(layoutTotal); // add the combined layout
That should place the wheel buttons right above the mouse buttons (or vice versa: the mouse buttons directly below the wheel buttons) and the combination all the way at the bottom.
//Edit: alternatively, you could make the root layout a LinearLayout and add a dummy view with a weight of '1' at the top, which will push the other elements down to the bottom. I'd probably prefer the RelativeLayout option though.
Just out of curiousity: why not just inflate the layout? Personally I find that way more managable.
Your layout management is messed up. I suggest using this code:
// MOUSE BUTTON LAYOUT
paramsButtons = new LinearLayout.LayoutParams(DpiToPixels(0), LayoutParams.WRAP_CONTENT, 1);
left = new Button(context);
left.setText("L");
middle = new Button(context);
middle.setText("M");
right = new Button(context);
right.setText("R");
paramsMouseButtonBody = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
paramsMouseButtonBody.addRule(ALIGN_PARENT_BOTTOM); // RELATIVE LAYOUT RULES
layoutMouseButtonBody = new LinearLayout(context);
layoutMouseButtonBody.setBackgroundResource(android.R.drawable.bottom_bar);
// layoutMouseButtonBody.setPadding(0, DpiToPixels(4), 0, 0);
layoutMouseButtonBody.setOrientation(LinearLayout.HORIZONTAL);
layoutMouseButtonBody.addView(left, paramsButtons );
layoutMouseButtonBody.addView(middle, paramsButtons );
layoutMouseButtonBody.addView(right, paramsButtons );
// WHEELS
paramsWheelButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
up = new Button(context);
up.setText("U");
down = new Button(context);
down.setText("D");
paramsWheelBody = new RelativeLayout.LayoutParams(DpiToPixels(32), LayoutParams.WRAP_CONTENT);
paramsWheelBody.addRule(ABOVE, layoutMouseButtonBody.getId());
layoutWheelsBody = new LinearLayout(context);
layoutWheelsBody.setOrientation(LinearLayout.VERTICAL);
layoutWheelsBody.setBackgroundResource(android.R.drawable.bottom_bar);
layoutWheelsBody.addView(up, paramsWheelButton);
layoutWheelsBody.addView(down, paramsWheelButton);
// PARENT
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(layoutMouseButtonBody, paramsMouseButtonBody ); // add mousebutton layout in parent (relativelayout)
addView(layoutWheelsBody, paramsWheelBody); // add mousewheel button layout in parent (relativelayout)

Categories

Resources