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);
Related
I want to parse text, and create for each word - button, but i don't know how to arrange them one after the other
String s = "Alice was beginning to get very tired of sitting";
String[] q = s.split(" ");
for (int i = 0; i < q.length; i++) {
Button myButton = new Button(this);
myButton.setText(q[i]);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
layout.addView(myButton, params);
}
See this custom library: FlowLayout
While you're adding views inside FlowLayout, it automatically wraps when there is no space for the next item.
There's not much wrong about your approach, it's only that relative layout as name suggests requires child views to have some parameters to align the views relative to them e.g. above, below etc. As a result you are getting views overlapping each other and hence only the last added view is visible being on top.
Use FlowLayout instead and you'll be fine.
You need to define RelativeLayout parameters as in example below
Heres an example to get you started, fill in the rest as applicable:
TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);
The docs for RelativeLayout.LayoutParams and the constructors are
here
From: How to add a view programmattically to RelativeLayout?
Check the link below to get more useful informations.
Hope it will help
In the following code, you should change the upper limits of the for, to a variable.
public class MainActivity
extends Activity
implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=13; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=9; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setOnClickListener(this);
tr.addView(b, 30,30);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
public void onClick(View view) {
((Button) view).setText("*");
((Button) view).setEnabled(false);
}
} // class
When creating dynamic buttons I would like them to stack one under the other vertically. I am not sure how to create this effect.
for(int i = 0; i <notificationArrayList.size(); i++)
{
if(i == 0)
{lp.addRule(RelativeLayout.BELOW, R.id.searchButton);}
else
{} //maybe tell the code here to stack under the lastID?
Notification oNote = notificationArrayList.get(i);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText(oNote.NotificationText);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
rl.setLayoutParams(lp);
rl.addView(btn, lp);
}
Maybe in the else statement have it get the last id and add RelativeLayout that way?
The easiest way would be to put all the buttons in a LinearLayout and just add the LinearLayout beneath the search button. This produces easier code, but slightly worse drawing performance. Pseudocode would be like:
LinearLayout ll = new LinearLayout(context);
for(i=0; i<numButtons; i++) {
ll.addView(new Button(context));
}
RelativeLayout.LayoutParam lp = new RelativeLayout.LayoutParam();
lp.addRule(RelativeLayout.BELOW, R.id.searchButton);
relativeLayout.addView(ll,lp);
This example should give you an idea:
public class MyActivity extends Activity {
private RelativeLayout rel;
private EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
rel = (RelativeLayout) findViewById(R.id.main_rel);
editText = (EditText) findViewById(R.id.pref_edit_text);
Button button = new Button(this);
button.setText("Delete");
// create the layout params that will be used to define how your
// button will be displayed
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your button below your object (here a editText)
params.addRule(RelativeLayout.BELOW, editText.getId());
// set the layoutParams on the button
button.setLayoutParams(params);
// add button to your RelativeLayout
rel.addView(button);
}
}
You know this JoystickView from http://code.google.com/p/mobile-anarchy-widgets/wiki/JoystickView ?
Well, I tried to implement it. No Problems with it. I couldn't change It's size because I Added It programmatically. I somehow found out how to change the size, but now It's stuck in the upper left corner and everything I found for three hours got me a NullPointerException on the LayoutParams param I've created or was rejected because It wasn't castable or something to begin with.
public class GameActivity extends Activity implements JoystickMovedListener{
private GraphicsHolder mGraphicsHolder;
private String TAG = "GameActivity";
/*
* creates a new GraphicsHolder and sets it its ContentView
* the GraphicsThread is being included in the GraphicsHolder
*/
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.d(TAG, "created");
FrameLayout gameScreen = new FrameLayout(this);
mGraphicsHolder = new GraphicsHolder(this);
JoystickView Joystick = new JoystickView(this);
// I need to set the Gravity HERE
Joystick.setLayoutParams(new RelativeLayout.MarginLayoutParams(500,500));
gameScreen.addView(mGraphicsHolder);
gameScreen.addView(Joystick);
setContentView(gameScreen);
}
}
Now Here's my Question: can you somehow set the Gravity of this View programmatically?
You can try this
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(500,500);
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
Joystick.setLayoutParams(params);
Try to set gravity center to FrameLayout and use ViewGroup.LayoutParams set Joystick LayoutParams :
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
gameScreen.setLayoutParams(params);
Joystick.setLayoutParams(new ViewGroup.LayoutParams(500,500));
try this.
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) Joystick.getLayoutParams();
params.gravity = Gravity.TOP;// desired gravity
Joystick.setLayoutParams(params);
dont change ur code just add this after setContentView(gameScreen); hope it works
Well, I found the answer, but it's a completely different approach, since none of the above or those I found elsewhere worked for me.
I did the following changes in the onCreate method:
mGraphicsHolder = new GraphicsHolder(this);
LayoutInflater inflate = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setContentView(mGraphicsHolder);
getWindow().addContentView(inflate.inflate(
R.layout.controllayout, null), new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
Joystick = (JoystickView) getWindow().findViewById(R.id.joystickView1);
Joystick.setOnJostickMovedListener(this);
Now I got my Joystick on an XML Layout, but It actually works (at least in my case where nothing else worked), and it's quite easy to make changes in things of Layout etc.
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:
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)