How to add margin to listview header? - android

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

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

Why layout_gravity not work for TextView?

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

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

android programmatic layout

I'm building a simple two-column list in android, and need to populate the number of rows dynamically. The xml layout works fine, but when i try to build the layout programmatically the second column doesn't show. Any ideas?
private void addRow(String text, String value) {
RelativeLayout line = new RelativeLayout(this);
line.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp.addRule(RelativeLayout.CENTER_VERTICAL);
line.setPadding(0, 0, 0, 15);
TextView caption = new TextView(this);
caption.setLayoutParams(rlp);
caption.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
caption.setText(text);
rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp.addRule(RelativeLayout.CENTER_VERTICAL);
TextView val = new TextView(this);
val.setLayoutParams(rlp);
val.setTextColor(0x05b4e4);
val.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
val.setText(value);
line.addView(caption);
line.addView(val);
mLayout.addView(line);
}
The comparable xml for a single row (that works) is this
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="15px" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:text="What"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
/>
<TextView
android:id="#+id/txtClubPath"
android:textColor="#05b4e4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:text="stuff"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
Again - the rows populate, but the second column isn't visible. Here's what it's going into:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollViewRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:keepScreenOn="true">
<LinearLayout
android:id="#+id/viewRoot"
android:paddingTop="20px"
android:paddingBottom="40px"
android:paddingLeft="20px"
android:paddingRight="20px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
The layout params of the view you are adding should be of type LinearLayout.LayoutParams because the parent will be of that type.
This line
line.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
should become
line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
However, I think this should cause an exception if anything.
It is important for the RelativeLayout children to have a unique non-zero id.
You could, on API 17+, use
myView.setId(View.generateViewId());
Or if you have to support older API levels, you could use (and increment each time) a simple static AtomicInteger value.

How to convert my RelativeLayout from XML to dynamically, programmatically produced Layout?

SOLVED - The ID of a View-Element should not be set to 0 !!!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/Button1" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_below="#+id/Button1"/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_toRightOf="#+id/Button2"/>
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_below="#+id/Button2"/>
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_toRightOf="#+id/Button3"/>
</RelativeLayout>
Now that is my Code in the Activity, i tried to to exactly what i did in the xml, but the items are displayed completely different:
public class MainActivity extends Activity {
private RelativeLayout relativeLayout;
public RelativeLayout.LayoutParams layoutParams;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//button 1
Button button1 = new Button(this);
button1.setText("Button 1");
button1.setId(0);
relativeLayout.addView(button1);
//editText1
EditText editText1 = new EditText(this);
editText1.setId(1);
layoutParams.addRule(RelativeLayout.RIGHT_OF, 0);
relativeLayout.addView(editText1, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// button2
Button button2 = new Button(this);
button2.setText("Button 2");
button2.setId(2);
layoutParams.addRule(RelativeLayout.BELOW, 0);
relativeLayout.addView(button2, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//editText2
EditText editText2 = new EditText(this);
editText2.setId(3);
layoutParams.addRule(RelativeLayout.BELOW, 1);
layoutParams.addRule(RelativeLayout.RIGHT_OF, 2);
relativeLayout.addView(editText2, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
this.setContentView(relativeLayout);
}
}
SOLVED:
The main issue is, that an id of 0 for a view-element is not allowed...erm yea, beginner's mistake, i know...
THANK you anyone for helping on this question! SOLVED!!!
Try this! It converts xml to java code. However, I am not quite sure if you really need it in this case...
You're doing all these backwards
relativeLayout.addView(button2, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Should be
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button2.setLayoutParams(layoutParams);
relativeLayout.addView(button2);
You may need to do more to get it the way you want, but why are you wanting to do this? If it works why change it?
you have already added the buttons to the xml file why are you creating dynamic buttons instead inflate the view if you want to add the layout dynamically also when you dynamically add the buttons to the relative layout you must call addRule function to place them at right place
just create objects of button and edittext refer the as you have refere your relative layout
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
and use them.
Edit----
For that you must remove buttons from the xml layout and then create the buttons like you are doing it's prefect just one thing like this here
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
**android:layout_toRightOf="#+id/Button1"** />
to place this editText you must use addRule() function
EditText editText1 = new EditText(this);
editText1.setId(1);
layoutParams.addRule(RelativeLayout.RIGHT_OF, 0);
relativeLayout.addView(editText1, layoutParams);
layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams .addRule(RelativeLayout.RIGHT_OF, 0);
where 0 is your button's id also set the layout params too to the layout like
editText.setLayoutParams(layoutParams );

Categories

Resources