i want to use a unknown (at begin) number of Views in my CustomView, so I have to set the position of each of them programatically. The problem is that I cannot use setX()/... or seTop()/... , because Im developing for froyo. But how can I set the position else?
With RelativeLayout you can do the following:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)child.getLayoutParams();
layoutParams.leftMargin = x;
layoutParams.topMargin = y;
child.setLayoutParams(layoutParams);
Related
i use this code to change the position of my view
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.topMargin = topmargin;
params.leftMargin = leftmargin;
myview.setLayoutParams(params);
and nothing change.but when i use this code
myview.setTop(topmargin);
myview.setLeft(leftmargin);
it will work.now i want to know what is wrong with my first code??
because in the internet so many people suggest first code to change the position of views.
EDIT
also this code doesn't work
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.setMargins(100,100,0,0);
myview.setLayoutParams(params);
myview.requestLayout();
thanks in advance
Try calling myview.requestLayout(); after myview.setLayoutParams(params);
UPDATE
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.setMargins(left,top, right,bottom);
myview.setLayoutParams(params);
myview.requestLayout();
You need to call requestLayout() after updating the layout params
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myview.getLayoutParams();
params.topMargin = topmargin;
params.leftMargin = leftmargin;
myview.setLayoutParams(params);
myview.requestLayout();
And this is a brief explanation from official documentation:
.setTop() ( same with .setLeft() )
Sets the top position of this view relative to its parent. This method
is meant to be called by the layout system and should not generally be
called otherwise, because the property may be changed at any time by
the layout.
topMargin ( same with leftMargin )
The top margin in pixels of the child. Margin values should be
positive. Call setLayoutParams(LayoutParams) after reassigning a new
value to this field.
I'm adding a RelativeLayout in a FrameLayout ( added in xml ) and I attach a LinearLayout in this RelativeLayout.
The problem is I get an unwanted left/right spacing/padding between the RelativeLayout and its child LinearLayout as you can see here from the uiautomator dump.
The weird thing is that spacing is always 11 pixels from each side tested on emulator N5, N4, N7 images and Moto G and N4 devices.
Thank you
The code is :
rlBottomBarContainer = new RelativeLayout(this);
rlBottomBarContainer.setId(R.id.bottomBarContainer);
rlBottomBarContainer.setClipToPadding(false);
rlBottomBarContainer.setClipChildren(false);
rlBottomBarContainer.setBackground(getResources().getDrawable(R.drawable.bara));
int topMargin = whiteTowerContainerHeight /2 + tower_height/2 + 100;
int containerHeight = whiteTowerContainerHeight - topMargin;
FrameLayout.LayoutParams containerParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
containerHeight);
containerParams.topMargin = topMargin;
rlBottomBarContainer.setLayoutParams(containerParams);
frameLayoutRoot.addView(rlBottomBarContainer); // parentLayout
llBottomBarContainer = new LinearLayout(this);
llBottomBarContainer.setId(R.id.upper_bottombar_ID);
llBottomBarContainer.setClipChildren(false);
llBottomBarContainer.setClipToPadding(false);
llBottomBarContainer.setBackgroundColor(Color.parseColor("#00FFFFFF"));
llBottomBarContainer.setOrientation(LinearLayout.HORIZONTAL);
RelativeLayout.LayoutParams bottomBarParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
getBottomBarTextSize());
llBottomBarContainer.setLayoutParams(bottomBarParams);
rlBottomBarContainer.addView(llBottomBarContainer);
As you can see from the code the LinearLayout is added with match_parent for the width. RelativeLayout width is from 0 to 720px but LinearLayout is from 11 to 709pixels.
First of all I do not understand why you use a FrameLayout and you do not use directly the RelativeLayout as your parent layout.
The error I see on your code is that you set a FrameLayout.LayoutParams to your RelativeLayout and a RelativeLayout.LayoutParams to your LinearLayout.
Probably this is why you get some extra spacing.
I would suggest that you do not create your layouts programmatically, but rather do it on xml and then if you need the layout objects on java you can get them with the findViewById(int id) method
I am trying to programmatically set the position of a view on Android with the following code :
LayoutParams layoutParams = new LayoutParams(100, 100);
layoutParams.leftMargin = 200;
layoutParams.topMargin = 200;
myView.setLayoutParams( layoutParams );
It works on my Nexus 7 and Motorola G devices but not on old devices under Android 2.2.
On these devices the view is alway set at the top left position of the screen.
How can I do the same thing on these devices ?
Here is the solution.
The XML code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id = "#+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
The Java code :
View myView = new View(this);
LayoutParams layoutParams=new LayoutParams(320,240);
layoutParams.leftMargin = 50;
layoutParams.topMargin = 60;
myView.setLayoutParams(layoutParams);
myView.setBackgroundColor(Color.YELLOW);
rootView.addView( myView );
View myView2 = new View(this);
layoutParams=new LayoutParams(320,240);
layoutParams.leftMargin = 200;
layoutParams.topMargin = 120;
myView2.setLayoutParams(layoutParams);
myView2.setBackgroundColor(Color.BLUE);
rootView.addView( myView2 );
And we need to import
import android.widget.RelativeLayout.LayoutParams;
At the beginning I used FrameLayout instead of RelativeLayout. It seems it is not possible to programmatically set the position of a view with Android 2.2 if the parent layout is a FrameLayout.
Thanks a lot for your help.
How to place TextViews on layout depends on TextView's center from code?By default we can create it depends on its top left angle. Method setGravity(Gravity.CENTER_HORIZONTAL) doesn't help.
EDIT
I want to place TextViews in circle. For what I use RelativeLayout. Within layot TextViews place in for-loop with some angle step. Here some code
for (int i = 0; i < 62; i++) {
TextView word = new TextView(context);
word.setTextSize(13);
// word.setGravity(Gravity.CENTER_HORIZONTAL);
word.setTextColor(Color.BLACK);
RotateAnimation rotateAnimation = new RotateAnimation(0,
angle.get(i), 0, (float) (textHeight * 0.9 + rotateCircle));
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(100);
rotateAnimation.setStartOffset(100);
word.setAnimation(rotateAnimation);
word.setGravity(Gravity.CENTER);
word.setPadding(10, 10, 10, 10);
RelativeLayout.LayoutParams layoutparams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutparams.leftMargin = Math.round(x_coords.get(i));
layoutparams.topMargin = (int) (Math.round(y_coords.get(i)
- textHeight * 0.9 - rotateCircle));
textviews.add(word);
words_layout.addView(word, layoutparams);
}
The gravity of a TextView controls how its content is aligned (the text), not how the view itself is aligned in the layout. To control how the View is aligned in the layout use the android::layout_gravity attribute instead, or the gravity field in the LayoutParams.
Use the android:layout_gravity = CENTER_HORIZONTAL to make text view in center.
I would like to know if somebody knows how to animate the translation of the views programatically, not XML, I have the coordinates where these views should go.
This is the creation of the view:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = x;
params.topMargin = y;
my_view.setLayoutParams(params);
holder.addView(my_view);
I need to move and animate this view to other x1,y1 position. Thanks!
TranslateAnimation animation = new TranslateAnimation(fromX,ToX,FromY,ToY);
layout.startAnimation(animation);
This should work.