I would like to know if is there a way to call android:layout_gravity property from a Java method. I didn't found any method in Android documentation to do it. This is the picture of the layout I want to implement:
http://www.anddev.org/resources/image/2234
I know to do it through XML, as following:
<FrameLayout
xlmns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<" />
<Button
android:layout_gravity="right|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">" />
</FrameLayout>
But in my situation, I need to do it through Java code, because I'll implement another layout views dynamically. To avoid merging XML layout with Java code, I would prefer make all layout using Java.
Well as far as I understand you looking for this It is for FrameLayout for other layout see appropriate LayoutParams classes. For setting gravity like in your XML use something like:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT | Gravity.CENTER_VERTICAL)
followed by addView with newly constructed LayoutParams
Maybe I misunderstand you, but here it is:
http://developer.android.com/reference/android/widget/TextView.html#setGravity(int)
Watchout! This wouldn't work with LinearLayout though. Because LinearLayout.LayoutParams(...) constructor is different from FrameLayout.LayoutParams(...) constructor. The third parameter is not gravity but weight.
LinearLayout.LayoutParams(int width, int height, float weight)
as opposed to
FrameLayout.LayoutParams(int width, int height, int gravity)
and this call, despite not producing compiler error is actually wrong:
lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT);
button.setBackgroundResource(R.drawable.sound_on);
button.setText("On");
button.setGravity(Gravity.CENTER_VERTICAL| Gravity.RIGHT);
button.invalidate();
To change the layout_gravity attribute for an existing view:
Button button = (Button) findViewById(R.id.some_button);
FrameLayout.LayoutParams params;
params = (LayoutParams) button.getLayoutParams();
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
button.setLayoutParams(params);
The button is contained in a FrameLayout which has a gravity attribute corresponding to the layout_gravity XML attribute. Documentation link.
before adding the button, you should change the orientation of your layout
yourbutton.setGravity(Gravity.RIGHT);
LayoutParams layout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
layout.gravity = Gravity.RIGHT;
yourbutton.setLayoutParams(layout);
yourlayout.setGravity(Gravity.RIGHT);
yourlayout.addView(yourbutton);
Related
You can easily create constraints in the layout xml:
(source: bilder-upload.eu)
However I was not able to do the exact same thing in code.
May somebody help to transfer the following xml into java code?
<TextView
...
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toTopOf="#+id/details"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/image"
app:layout_constraintTop_toTopOf="parent" />
I tried the following code for setting the margins, but it does not work. The textView is always at the same place.
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(textView.getLayoutParams());
layoutParams.setMargins(10,10,10,10);
description.setLayoutParams(layoutParams);
Constraints are not a part of LayoutParams.
What you are looking for is ConstraintSet set on the ConstraintLayout itself.
final ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.textViewId, ConstraintSet.BOTTOM, R.id.details, ConstraintSet.TOP,10 /*"DP"*/);
constraintSet.connect(R.id.textViewId, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END,10 /*"DP"*/);
constraintSet.connect(R.id.textViewId, ConstraintSet.START, R.id.image, ConstraintSet.END,10 /*"DP"*/);
constraintSet.connect(R.id.textViewId, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP,10 /*"DP"*/);
constraintSet.applyTo(constraintLayout);
Bear in mind that value you pass as the last param will be in pixel when in code so you will have to convert it to dimension pixels(DP).
I have created a TextView dynamically and added it to a linear layout. The TextView is supposed to have an icon on its left. I have added the icon and wanted the TextView to be in the center.
The code:
TextView valueTV = new TextView(getContext());
valueTV.setText(richPost.getPostCreateDate());
valueTV.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
valueTV.setGravity(Gravity.CENTER);
llInner.addView(valueTV);
The output:
I want the image to be right before the text and want to remove the huge gap in between. The text is in the proper place, I want the image to be centered beside the text. Where am I going wrong? What should I do?
First, make yoúr TextView wrap its content by adding this to your code:
valueTV.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Additionally, to get the icon and text centered in the LiniarLayout, which you can do by adding this to the LinearLayout:
yourLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
yourLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
This may be caused by TextView layout parameters. It seems to me that in your case width of TextView is match_parent, so the drawableLeft position is defined by left side of LinearLayout.
Here is simple example to feel the difference
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:drawableLeft="#drawable/ic_send_white_24dp"
android:drawablePadding="8dp"
android:text="some text"/>
</LinearLayout>
and try the same layout with match_parent width of TextView
I'm trying to set the imageview's layout_marginTop to one value for different density/screen sizes. In my values-mdpi folder I have the following line in dimensions.xml
<dimen name="marginTop">10dp</dimen>
In the MainActivity
ImageView image = (ImageView) findViewById(R.id.s_image);
But there is no setmargin method for imageview. Is there a way to do this?
You don't need to do that in code, you can do it in your XML file where the image view is defined. See this page for more details.
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
You're on the right track. It's probably easiest to refer to your dimension value within the xml (rather than set this up in java code).
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="#dimen/yourMarginTopValue" />
Try try to put android:adjustViewBounds="true" to
It's probably better to do it in XML since you seem to already have the ImageView defined in XML.
However, the layout_* XML attributes refer to the LayoutParams of the parent layout, not the view itself. To change them in code, access them with getLayoutParams(), do your modifications and call requestLayout() to schedule a re-layout pass. For example:
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)imageView.getLayoutParams();
lp.topMargin = 123;
imageView.requestLayout();
So, I'm trying to make a dynamic UI, and i want to add a seperator to it. unfortunately, i could only find out how do one in XML. is it possible to turn this
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/seperator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:scaleType="fitXY"
android:src="#android:drawable/divider_horizontal_dark" />
into program code?
my best attempt was
ImageView seperator=new ImageView(this);
seperator.setImageDrawable(drawable.divider_horizontal_dark);
Put it in an extra layout file and inflate it when you need it in code - I think that what you want to do and should be the easiest way.
In your Activity:
View v = LayoutInflater.from(this).inflate(R.layout.seperator, null);
If you inflate a Layout:
LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_layout, null);
TextView tv = (TextView) ll.findViewById(R.id.tv);
There is a website can convert that for you. You can design the interface with eclipse then submit the generated xml to XMLtoJAVA online converter and it should do it for you..
You can also create a View, define a background and add it with a LayoutParams
ViewGroup container = (ViewGroup) findViewById(R.id.container);
View separator = new View(context);
separator.setBackgroundColor(Color.Black);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 2);
container.addView(separator, layoutParams);
I'm stuck on a problem and I don't know, what causes it.
I have a very simple Layout like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="NOTE_THIS"
android:textColor="#FFFFFF"
android:textSize="22dp"
android:text="TestText"/>
</LinearLayout>
which is included inside another layout. If I change the gravity inside the xml I see same Result in Layout-Editor and on my phone. If I wanna apply the Gravity programatically like with myTextView.setGravity(Gravity.CENTER) it doesn't Change anything. And I cannot set LayoutGravity in Java on a TextView
I'll tried for debug purposes to include them three times each with another gravity which does work even. So I assume everything is alright with my Layout and there has to be a Bug or something else I miss.
Can someone give me a hint what I also can try, or what causes this problem?
Set your TextView's width as android:layout_width="fill_parent" then you can set it programmatically using myTextView.setGravity(Gravity.CENTER)
You need to set the gravity of the LayoutParams object instead of the View itself:
TextView tv = new TextView(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
lp.gravity = Gravity.CENTER;
tv.setLayoutParams(lp);
When you use LinearLayout as parent, then layout_gravity comes in picture which align control but not content inside the control so,
Instead using android:layout_gravity use android:gravity.