Why layout_gravity not work for TextView? - android

As the title description, when set layout_gravity for TextView in LinearLayout, this property does not work well, and when I set it through LinearLayout.LayoutParams.gravity, it does not work as well. But for ImageView with same way, it works.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.openssl.LayoutTestActivity">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
android:background="#color/colorAccent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="xml"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="#drawable/ic_launcher_round"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Java code
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
TextView textView = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.setMargins(10, 0, 10, 15);
textView.setLayoutParams(layoutParams);
textView.setText("Code");
linearLayout.addView(textView);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher_round);
LinearLayout.LayoutParams layoutParams1 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 0, 10, 15);
layoutParams1.gravity = Gravity.BOTTOM;
imageView.setLayoutParams(layoutParams1);
linearLayout.addView(imageView);

in the TextView you are creating programmatically, you are giving the TextView a 15dp bottom margin, If you want to make the TextView itself at the bottom, set the gravity of the layoutParams without giving the view unnecessary margins:
layoutParams.setMargins(10, 0, 10, 0);
And talking about the textView in xml, if you want to align the text itself of the textView to be at the bottom, set the height to match_parent and set view gravity to bottom:
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="xml"/>
for the TextViewin the code, try to set the height to match_parent and give the view a gravity of BOTTOM inside the layoutParams :
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
.LayoutParams.MATCH_PARENT, Gravity.BOTTOM);

Related

How I can use java code for creating design on android

I am trying to put TextView into FrameLayout into Linear layout.
When I do it using XML it's OK, but using Java code there are problems.
So I can't move text view using layout_gravity in Java code. Also, I don't understand why text matching parent when I use a parameter wrap content. must be
XML code
<LinearLayout
android:id="#+id/chatBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_gravity="right"/>
</FrameLayout>
Java code
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(this);
tv.setText(message);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
p.gravity = Gravity.LEFT; tv.setLayoutParams(p);
fl.addView(tv);
LinearLayout chatbox = (LinearLayout) findViewById(R.id.chatBox);
chatbox.addView(fl, 0);
you are adding TextView tv to FrameLayout, so you should use FrameLayout.LayoutParams for this TextView
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
p.gravity = Gravity.LEFT;
tv.setLayoutParams(p);
fl.addView(tv);
You need to LayoutParams for your Textview not Linearlayout Params. That is causing the problem. You can create it like;
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))

Assign margins to childviews

I'm inflating a view which just has a TextView in it. The parent view is a LinearLayout. My parent view looks like this:
<LinearLayout
android:id="#+id/posmSelectedBrandsLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_below="#+id/brandPOSMspinner"/>
and my child view looks like this:
<TextView
android:id="#+id/chip12345"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/tag_background"
android:text="TAG"
android:layout_marginTop="50dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#color/textcolorLogin"
android:textSize="22sp" />
This is how I'm inflating view:
final LinearLayout editParentLL = (LinearLayout) findViewById(R.id.posmSelectedBrandsLL);
final View editChildView = getLayoutInflater().inflate(R.layout.tag_layout, null);
editParentLL.addView(editChildView);
TextView tvChip = editChildView.findViewById(R.id.chip12345);
tvChip.setText(p.getProductName());
And the result that comes out is like this:
What I want is to separate the three TextViews from one another. So instead of a single box, it should come out as three different boxes.
I need your help to do it.
Thankyou.
try this
final View editChildView = getLayoutInflater().inflate(R.layout.view_add_item_with_details, null);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,2,10,2);
editChildView.setLayoutParams(layoutParams);
editParentLL.addView(editChildView);
TextView tvChip = editChildView.findViewById(R.id.chip12345);
tvChip.setText(p.getProductName());
you can set any values in .setMargin method
You should use LayoutParams to set your editChildView margins:
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
editChildView.setLayoutParams(params);
more details reffer here: https://android--code.blogspot.in/2015/05/android-textview-layout-margin.html
if you need TextView in One by one, You have to use like Orientation
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

How do you set the layout width of a cardview inside a linearlayout programmatically?

I am trying to change the layout width of two cardview element between MATCH_PARENT and 0dp. When I simulate the scenario via xml changes, it is correct, however, when I do it programmatically, it is not rendering as expected.
xml
<LinearLayout
android:id="#+id/lDates"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/card_margins"
android:layout_marginEnd="#dimen/card_margins"
android:layout_below="#id/lnrButtons"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:id="#+id/cardSectionA"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:cardElevation="0dp"
android:layout_marginEnd="#dimen/card_margins"
android:layout_marginBottom="#dimen/card_margins"
app:cardCornerRadius="4dp"
android:layout_weight="1">
<TextView
android:id="#+id/textLabelA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/sample_value"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardSectionB"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:cardElevation="0dp"
android:layout_marginEnd="#dimen/card_margins"
android:layout_marginBottom="#dimen/card_margins"
app:cardCornerRadius="4dp"
android:layout_weight="1">
<TextView
android:id="#+id/textLabelB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sample_value"/>
</android.support.v7.widget.CardView>
</LinearLayout>
java
//as-is scenario; cardSectionA and cardSectionB should be shown
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
cardSectionA.getLayoutParams().width,
LinearLayout.LayoutParams.WRAP_CONTENT
);
cardSectionA.setLayoutParams(params);
cardSectionB.setLayoutParams(params);
//should only show cardSectionA scenario
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
cardSectionA.setLayoutParams(params);
Thanks a lot.
Figured it out. To behave as expected, the margin and layout weight must also be set.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
int margin = 10;
params.setMargins(margin, margin, margin, margin);
params.weight = 1;
cardSectionA.setLayoutParams(params);

How to add margin to listview header?

I am trying to add a textview to listview header and give left margin to it. Here is what i do:
//create a textview, not inflating from layout
TextView selectAdressText = new TextView(getContext());
selectAdressText.setTextSize(12);
selectAdressText.setTextColor(getResources().getColor(R.color.text_black));
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 0, 10, 10);
selectAdressText.setLayoutParams(lp);
addressesLV.addHeaderView(selectAdressText);
But it gives nullpointerexception. I also tried AbsListViewLayout params instead of LinearLayoutLayoutParams but it has no setMargins method. So which LayoutParams should i use for this?
Thanks
LinearLayout header = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 0, 10, 10);
TextView t = new TextView(this);
header.addView(t,lp);
addressesLV.addHeaderView(header);
try it.
Margin is overridden by ListView, so you can't use margin directly on the root view of your xml layout. Use padding instead.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingLeft="0dp"/>
or wrap a layout (as root node) around it:
<Framelayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="0dp"/>
</Framelayout>
just create your textView in xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="0dp"/>
View view = inflater.inflate(R.layout.textView, yourListview, false);
yourListview.addHeaderView(view);

Textview layout center vertical and horizontal in ScrollView by programmatically

Application has scrollview in XML layout.
<ScrollView
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
>
</ScrollView>
I want to one textView into this scrollView center.
I tried This code, set horizontal center, but vertical top.. I want both centered.
LinearLayout l1 = new LinearLayout(getActivity());
l1.setOrientation(LinearLayout.VERTICAL);
l1.setGravity(Gravity.CENTER);
l1.setBackgroundColor(Color.WHITE);
TextView errorView = new TextView(getActivity());
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
errorView.setText("TextView");
errorView.setTextColor(Color.BLACK);
errorView.setLayoutParams(params);
errorView.setGravity(Gravity.CENTER);
l1.addView(errorView);
scrollViewCon.addView(l1, lparams);
Why is this happening?
Change your xml as shown below -
Add fillViewport as true.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:fillViewport="true"
>
Removed the relative layout addition part as it is not required.

Categories

Resources