Set constraint widgets dynamically - java

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).

Related

DatePickerDialog leaves white space android

I have a date picker dialog ,
when ever dialog opens it shows the white space as shown in image(in right side of datePicker) ,
i tried setting layout Params
DatePickerDialog dialog = new DatePickerDialog(_context, this,
_birthYear, _month,
_day);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getDatePicker().setLayoutParams(params);
dialog.setCancelable(false);
dialog.getDatePicker().setSpinnersShown(false);
dialog.getDatePicker().setCalendarViewShown(true);
still it aint working
any suggestions??
Here is the best way to fit this is to use scale in your code.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<DatePicker
android:id="#+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="37dp"
android:scaleX="1.2"
android:scaleY="1.2"/>
</LinearLayout>
Try out this code I hope that will help you
Try to set the width to MATCH_PARENT over here.
ViewGroup.LayoutParams.WRAP_CONTENT,
TO:
ViewGroup.LayoutParams.MATCH_PARENT,
Updated ans:
There seems to be a size mismatch with the datepicker dialog and your
view.
Try setting the size of the dialog like this:
datepikerdialog.getWindow().setLayout(width,height)

Issues placing image at the left of TextView generated dynamically

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

TextView Weight via Java code

I have this code in xml:
<TextView
android:id="#+id/rankLVs"
android:text="Rank"
android:background="#drawable/cell_shape"
android:layout_weight="1"
android:textColor="#2980b9"
android:padding="20dip"
android:textSize="12sp"
android:gravity="center"/>
I tried to make the same code but this time via java, with this code:
rank.setText(""+json.getInt("ranking"));
rank.setBackground(getResources().getDrawable(R.drawable.cell_shape));
rank.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
rank.setTextColor(Color.parseColor("#2980b9"));
rank.setPadding(0,dpAsPixels,0,0);
rank.setTextSize(getResources().getDimension(R.dimen.textsize));
rank.setGravity(Gravity.CENTER);
The result was this:
The top row is made with xml, that means the code I wrote in java disappeared. When I put
rank.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1f));
in comment the result is:
Which is really weird.
How can I make it look normal?

TextView setGravity() doesn't work in java

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.

Java method for android:layout_gravity

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);

Categories