How can i move items programmatically on the screen? - android

I have an image with an item in the lower left. I would like at the bottom center, and not attached at the lower end but slightly raised. How can I do? Working with some of the calculations on the size of the screen? Obviously I mean in code, not xml. thanks

have a look at LayoutParams.
for exmaple
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Button button1;
button1.setLayoutParams(params);
you can do all differnt alignments, params, margins and padding as in xml

Related

Android: How to dynamically place views with relative spacing

I am dynamically placing views into a RelativeLayout. Currently, I am doing:
layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, position, 0, 0);
relativeLayout.addView(textViewInstance, layoutParams);
where position is incremented by a fixed amount after each TextView object is added to the layout. The problem is that some of the TextViews contain long strings, which makes for awkward formatting on devices with smaller screen sizes because the string will spill into multiple lines and bleed into the whitespace.
Is there any way I can add these TextViews with spacing relative to the TextView right above it? I want to achieve something to the effect of:
layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, previousTextViewInstance.bottomPixel + 50, 0, 0);
relativeLayout.addView(textViewInstance, layoutParams);
where previousTextViewInstance will be saved after each new TextView is added.
Is this possible?
Instead of setting margin for each view, you can dynamically add each view below the previous one in relative layout.
For that, first set an id for each of your textView.
firstTextView.setId(textView_id_1);
while adding second textView, set rule for layout params as:
layoutParams.addRule(RelativeLayout.BELOW, textView_id_1);
secondTextView.setLayoutParams(layoutParams);
secondTextView.setId(textView_id_2);

Android change size programmatically

I need to change the size of the buttons programmatically.
I read some answers and I do the next to change the button size.
button.setLayoutParams(new RelativeLayout.LayoutParams(150, 50));
This code change the size correctly but the problem is that the button not stay in the position I want, it moves the button to the top left corner.
I need to change the button size but I don't want to change de position.
How can I fix this?
You have to use the existing RelativeLayout Rules as well,i.e whether the item is center aligned,align parent left etc. Or you can make use of the existing Params and modify the width and height as follows the RelativeLayout rules will not be modified.
RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) button.getLayoutParams();
layoutParams.width=150;
layoutParams.height=50;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams();
params.width = 500;
params.height = 500;
button.setLayoutParams(params);

How to set Margin Left ,Margin top,Margin right and Margin Bottom alignment for popup window dynamically in android

I am currently doing an android application that contains a popup window named as mypopup. It contains a Image button and four textviews. i want to align the popup window dynamically with margin top,margin bottom,margin left and margin right parameters..In my code setmargin method is not working..please anybody help me to do this...
It depends that which layout you are using. Below example places a RelativeLayout in a LinearLayout.
LinearLayout linearLayoutParent;
RelativeLayout relativeLayout;
RelativeLayout.LayoutParams margin = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
margin.setMargins(0, 0, 0, 7); //7px bottom margin
//create the linear and the relative layouts
//...add other stuff here...
// Add view with its margins
linearLayoutParent.addView(relativeLayout, margin);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0, 5, 0);

Set EditText to percentage of parents's width in LinearLayout

I have a LinearLayout class in which I have:
TexView | ImageView | EditText | ImageView.
I have the last ImageView all the way to the right side of the LinearLayout its wrapped in. The EditText runs very long and in some case pushes the last ImageView out of view (or so it seems to push it out of view).
I want to to have the EditText set to a percentage of the total width. I tried using weight with LinearLayout parameters but it seems to cause the view to get all out of wack. For example here it is for the EditText:
LinearLayout.LayoutParams lpEt = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
.50f);
All the other views follow suit but have lesser weights (.2, .2, .1) and they all add up to 1.0. But the LinearLayout row is never spaced correctly.
Should I find out the width of the parent (which is a ListView) and then set the width of the EditText explicitly based on the parent's width or is there a better way?
Im not sure what your linear layout orientation is but im guessing its horizontal. if so try this.
LayoutParams textViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(textViewParams);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
0,LayoutParams.WRAP_CONTENT,1f);
editText.setLayoutParams(editTextParams);
LayoutParams imageViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(imageViewParams);
This will make the edit text to fill the remaining space between the textview and imageview. If you need something else, just modify the code a bit. When using weights, set the weight or height as 0 for which you are setting weight for.

Android Layout question

I need to create a map with items on it (the map consists of a drawable object, which represents a room) and I thought about using buttons with background images for the items so that they are clickable.
I guess the AbsoluteLayout fits here the best, but unfortunately it's deprecated.
What layout would you recommend me for this kind of application ? Is there another layout which supports X/Y coordinates ?
Relative Layout supports X,Y coords -- and that would probably be the best, since you can set the layout relative to the map instead of the screen.
Cool. THanks so much !
Here is the code, if somebody needs it too.
RelativeLayout RL = new RelativeLayout(this);
RL.addView(V, RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
Button t = new Button(this);
t.setText("text");
t.setBackgroundResource(R.drawable.test);
int top = 200; //y
int left = 100; //x
RelativeLayout.LayoutParams LP= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LP.setMargins(left, top, 0, 0);
t.setLayoutParams(LP);
RL.addView(t);
setContentView(RL);

Categories

Resources