I went through several links yet i am not able to find if it is possible to set other attributes of relative layout, like android:gravity,android:orientation through java code.
I want to change the positions of the some of the views through java code and not through xml.
e.g, i want to set these attributes through java code for a particular view. android:gravity="center_vertical" android:orientation="vertical"
how can i do so? please help.
as Daan said you can use Layout params for relative layout in android
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.alignWithParent = true;
params.bottomMargin = 5;
params.height = 50;
params.leftMargin = 50;
But as you said you want to set following attributes for particular view, you cannot assign these attributes to RelativeLayout as these are belongs to LinearLayout and other views
android:gravity="center_vertical" android:orientation="vertical"
For setting gravity for any view please follow the code
tv = (TextView)findViewById(R.id.tv01);
tv.setGravity(Gravity.CENTER_VERTICAL);
You cannot set Orientation of any view.. instead you can set orientation to LinearLayout
like this
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Hope this will help you
Within a relative layout you have to use other layout params:
Layout params within relative layout
Related
I have this section in my layout file:
<RelativeLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView></TextView>
<TextView></TextView>
</LinearLayout>
</RelativeLayout>
My objective is to set the gravity of the items inside the linearLayout and then to add a margin to the whole LinearLayout programmatically.
This is what I have:
linearLayout_textBackground.setGravity(gravity); //where gravity is the int of the desired gravity
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
linearLayout_textBackground.setLayoutParams(layoutParams);
linearLayout_textBackground.requestLayout();
I would like to set the margin in using layoutParams but when I run the above code, I notice that my gravity value had been reset. However, if I comment out linearLayout_textBackground.setLayoutParams(layoutParams);, my gravity value are set correctly.
Why is the gravity resetting after I setLayoutParams to my layout?
When you do this :
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
You are creating new reference for your layout parameters the one you mention here will change the others not mentioned will be changed to default of android. If you want to change gravity then you need to change it after creating new params like:
Use this:
linearLayout_textBackground.setGravity(gravity);
after this:
linearLayout_textBackground.setLayoutParams(layoutParams);
or define your layout gravity in defining layoutParams.
But the recommended way of setting your gravity is inside your xml as follows:
android:layout_gravity="center"
then you dont need to do this :
linearLayout_textBackground.setGravity(gravity);
Thanks to #M.Saad Lakhan's answer, I noticed that I was creating a new reference of the parameters. So what I ended up doing is getting the actual params of the layout:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
linearLayout_textBackground.getLayoutParams();
This fixed the problem.
If you want to set the gravity of a View like LinearLayout programmatically, you have 2 ways:
1)
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
MyLinearLayout.setLayoutParams(params);
2)
MyLinearLayout.setGravity(Gravity.RIGHT);
What is difference between these 2 ways?
It is important you know the difference.
In the first way you are setting that layout gravity of your LinearLayout. It means you are setting the position of your layout in its parent View. It is equivalent to android:layout_gravity="right" in xml layouts.
But in the second way you are setting the position of child views in your Linearlayout and it is equivalent to android:gravity="right" in xml layouts. For example if you put a TextView in your LinearLayout and its width was wrap_content, the TextView will be place in the right side of your LinearLayout.
I need to create a layout dynamically like this through code instead creating through XML.
I am able design either in vertical or horizontal buttons. But I need to create both vertical and horizontal in a same layout.
Please help me to do it in easy way.
Thanks in advance
Its simple, First create the parent linear layout
LinearLayout parentLayout = new LinearLayout(this);
set the parameters and orientation for this parent layout:
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)
parentLayout.setOrientation(VERTICAL OR HORIZONTAL);
parentLayout.setLayoutParams(layoutParam);
Thats it , your parent layout has been generated. Now create new layout and start adding it to this layout. For ex.:
LinearLayout firstLayout = new LinearLayout(this);
LinearLayout secondLayout = new LinearLayout(this);
LinearLayout thirdLayout = new LinearLayout(this);
parentLayout.addView(firstLayout);
parentLayout.addView(secondLayout);
parentLayout.addView(thirdLayout);
Also you need to set the parameters for all the layout seperatly. You can optimise the code according to your need.
Hope it works!
I have created a pagecontroller application and added it on my page in its own linear layout.
However, I can not get the layout width to fill the entire parent page..
This is my code:
pagecontrol = new LinearLayout(this.context);
pagecontrol.setOrientation(LinearLayout.HORIZONTAL);
pagecontrol.setGravity(Gravity.CENTER_HORIZONTAL);
params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT);
pagecontrol.addView(btn,params);
public LinearLayout getView() {
return pagecontrol;
}
I have attached the screenshot of the page. Could anyone please advise?
Thanks in advance.
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
You must use these properties.
android:layout_width="fill_parent"
android:layout_height="fill_parent"
Please check the height and width of layout if you take the outer layout of your linear layout.
Change it:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT);
replace WRAP_CONTENT with FILL_PARENT it will work
I've been trying to add multiple textviews and buttons when onClick, and this is the best code I have found that actually works:
RelativeLayout relative = (RelativeLayout) findViewById(R.id.RelativeLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(lp);
tv.setText("Shift" + mShiftCount);
EditText edittv = new EditText(getApplicationContext());
edittv.setLayoutParams(lp);
relative.addView(tv);
relative.addView(edittv);
This seems to be the best code to add additional items that are alike to that of what I already have in my main.XML file.
My issue is that when these are added, they appear at the top of the screen and I am unsure how to add further parameters to the objects.
How would I go about placing the textview and edittext below my other elements specified in the XML?
What you need to do is create a RelativeLayout.LayoutParams (refer here) when you add the views like tv and edittv to the RelativeLayout. In your RelativeLayout.LayoutParams, you need to define the same parameters you would give to your other RelativeLayout objects. Here is an example:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.idOfWhatYouWantToBeBelowOf);
addView(tv, params);
First off there are definitely some static components to your view and some dynamic components. can you please specify what is your layout file so that i can give you a much more detail in your application.