How can I add empty space in LinearLayout? - android

I want my to make a compound controls by extending a LinearLayout, here is my code:
public class MemberView extends LinearLayout {
private TextView contactName;
private ImageView removeContact;
private ImageView contactPicture;
public MemberView(Context context) {
super(context);
//Context thisContext = getContext();
onCreate(context);
}
private void onCreate(Context context) {
contactName = new TextView(context);
contactName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
contactName.setText("Mohammad");
removeContact = new ImageView(context);
removeContact.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
removeContact.setImageResource(R.drawable.ic_menu_delete);
contactPicture = new ImageView(context);
contactPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
contactPicture.setImageResource(R.drawable.ic_contact_picture);
setClickable(true);
setOrientation(LinearLayout.HORIZONTAL);
addView(contactName);
addView(removeContact);
addView(contactPicture);
}
}
I want it to look like the following UI Prototype:
But the result of my code is:
I tried adding a VERTICAL LinearLayout and add some spaces to it, but no use:
Here is the edited onCreate code:
private void onCreate(Context context) {
contactName = new TextView(context);
contactName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
contactName.setText("Mohammad");
removeContact = new ImageView(context);
removeContact.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
removeContact.setImageResource(R.drawable.ic_menu_delete);
contactPicture = new ImageView(context);
contactPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
contactPicture.setImageResource(R.drawable.ic_contact_picture);
LinearLayout layout2 = new LinearLayout(context);
layout2.setOrientation(LinearLayout.VERTICAL);
layout2.addView(new Space(context));
layout2.addView(contactName);
layout2.addView(new Space(context));
setClickable(true);
setOrientation(LinearLayout.HORIZONTAL);
addView(layout2);
addView(removeContact);
addView(contactPicture);
}

Don't put space because it is not the proper way.
Use android:layout_width.
set the android:layout_width of the children to "0dp"
set the android:weightSum of the parent
set the android:layout_weight of each child proportionally, i.e.:
weightSum="5" with three children:
layout_weight="1"
layout_weight="3"
layout_weight="1"
For example (this is static, you have to try in dynamic):
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</LinearLayout>

The RelativeLayout will best serve your purpose here. Structure should be as such:
<RelativeLayout>
// image layout, aligned to the right.
<RelativeLayout horizontal alignParentRight >
<ImageView contact icon>
<ImageView deleteIcon alignBottom right of contactIcon>
</RelativeLayout>
// contact name
<RelativeLayout fillparent leftof abovelayout>
<Textview centerparent true this will display contact name />
</Relativelayout>
</RelativeLayout>

Related

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 to make Android LinearLayout to clip its children content?

I have a LinearLayout (horizontal) with several child TextViews inside. I have set fixed layout_width for both the LinearLayout and all the TextViews. When the total width of the children exceeds the width of the layout, it automatically shrink the children, making them narrower, which is not what I want. I want the layout to clip the children content and keep their width.
How can I achieve that?
For example: Let's say my LinearLayout is 200dp wide, and there are 4 TextView each 60dp wide. I want it to show the first three TextViews, and 1/3 of the 4th (60 + 60 + 60 + 20 = 200)
Thanks.
Use android:weightSum in LinearLayout
And use android:layout_width="0dp" and android:layout_weight="1" in TextView
Try this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="hello"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="hello"/>
</LinearLayout>
Also
You can change android:layout_width="match_parent" to android:layout_width="wrap_content" in TextView .
Edit
And you can use in your java code .
You can change the width or weight by yourself .
private LinearLayout mLinearLayout;
private int viewsCount = 4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout);
for (int i = 0; i < viewsCount; i++) {
TextView textView = new TextView(this);
if(i == viewsCount-1){
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
} else {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 3);
}
mLinearLayout.addView(textView);
}
}

Trying to center a horizontal LinearLayout layout

I'm dynamically creating horizontal LinearLayout with 5 views added to each one. It is then added to a vertical LinearLayout that is inside a scroll view.
I set the gravity by layout.setGravity( Gravity.CENTER );, but it is not centred but left algined.
THe code that creates the horizontal LinearLayouts
for(int i=0; i<cGlobals.mNames.length; i+=2)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity( Gravity.CENTER );
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(llp);
AddButton( layout, cGlobals.mNames[i], i);
TextView t=new TextView(this);
t.setText(" ");
layout.addView(t);
if (i+1<cGlobals.mNames.length)
AddButton( layout, cGlobals.mNames[i+1], i+1);
Container.addView(layout);
}
code that creats the buttons added to the linar layout
void AddButton( LinearLayout layout, String name, int i)
{
favBut[i]=new ImageView(this);
if ( cGlobals.conFav.contains( name ) )
favBut[i].setImageResource(R.drawable.heartselected2);
else
favBut[i].setImageResource(R.drawable.heartunselected2);
favBut[i].setId(defStartFavId+i);
favBut[i].setOnClickListener(this);
layout.addView(favBut[i]);
int w=getWindowManager().getDefaultDisplay().getWidth();
w-=200; // hearts
w=w/2;
Button but1=new Button(this);
but1.setText( name);
but1.setWidth(w);
layout.addView(but1);
but1.setOnClickListener(this);
but1.setId(defStartButId+i);
}
XML file for layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/butVol"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="24px"
android:text="Volume"
android:textColor="#ff0000ff"
android:layout_gravity="center"
/>
<Button
android:id="#+id/butRington"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:textSize="24px"
android:text="Rington"
android:textColor="#ff0000ff"
android:layout_gravity="center"
/>
You can try to specify layout parameters like this...
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
layout.addView(favBut[i], layoutParams);
and/or
Container.addView(layout, layoutParams);
and maybe change MATCH_PARENT to WRAP_CONTENT depdning on the results you are after.
http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

RelativeLayout Elements are incorrectly overlapping

I'm trying to build a part of a Android View Programmatically, but unfortunately I'm having some problems with the RelativeLayout. it makes my Elements overlay on eachother
This is my Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insertnew_layout);
LinearLayout ll = (LinearLayout) findViewById(R.id.container);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// fill content
for (int i = 0; i < 3; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
RelativeLayout rl = new RelativeLayout(this);
rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView score = new TextView(this);
score.setText(""+i);
RelativeLayout.LayoutParams lpScore = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpScore.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
score.setLayoutParams(lpScore);
TextView description = new TextView(this);
description.setText("this is my description");
RelativeLayout.LayoutParams lpDescription = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());
description.setLayoutParams(lpDescription);
CheckBox checkbox = new CheckBox(this);
RelativeLayout.LayoutParams lpCheckbox = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpCheckbox.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
checkbox.setLayoutParams(lpCheckbox);
rl.addView(score);
rl.addView(description);
rl.addView(checkbox);
tl.addView(rl);
}
ll.addView(tl);
This is what it looks like:
As you can see, the "description" is on top of of the "score".
Here is the same code in xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tableLayoutTextEntry" >
<TableRow
android:id="#+id/tableRowTextEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="score" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/score"
android:text="description" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="select" />
</RelativeLayout>
</TableRow>
</TableLayout>
And here is what it looks like in xml:
As you can see, in xml, the toRightOf-command works as expected - the description is to the right of the score
How is that possible? Shouldnt the line
lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());
do the same thing?
At the moment you are calling it, score.getId() will return -1 (invalid), because it has not been added yet to the screen with the whole layout processed.
You will have to set the id manually with setId() before calling getId() and it should all work fine.

How to programmatically add multiple LinearLayouts into one view and then add to ViewFlipper?

I hope question title gives you a good description of the problem.
I want to create this XML, but programatically (please don't suggest not doing it programmatically ^_^ )
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="Messages:"/>
<EditText android:id="#+id/messageHistory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:layout_weight="1"
android:editable="false"
android:gravity="top"
android:scrollbars="vertical"
android:scrollbarSize="10px"
/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4">
<EditText android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:layout_weight="1"
/>
<Button android:id="#+id/sendMessageButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:text="Send"/>
</LinearLayout>
As you can see, there are two LinearLayout's, one embedded in the other. I need to reproduce this and then add it to a ViewFlipper.
This is what I have so far:
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
Button btn=new Button(this);
EditText messageHistory = new EditText(this);
EditText newMessage = new EditText(this);
TextView windowTitle = new TextView(this);
btn.setText("Send");
btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 4f));
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Clicked send in child index: "+ flipper.getDisplayedChild(), Toast.LENGTH_SHORT).show();
}
});
windowTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
windowTitle.setPadding(0, 5, 0, 10);
windowTitle.setText("Chat with: ");
messageHistory.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
messageHistory.setGravity(Gravity.TOP);
messageHistory.setMovementMethod(new ScrollingMovementMethod());
messageHistory.setClickable(false);
messageHistory.setFocusable(false);
messageHistory.setPadding(0, 0, 0, 5);
newMessage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
newMessage.setGravity(Gravity.TOP);
newMessage.setMovementMethod(new ScrollingMovementMethod());
newMessage.requestFocus();
l1.setOrientation(LinearLayout.VERTICAL);
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setOrientation(LinearLayout.HORIZONTAL);
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l1.addView(windowTitle);
l1.addView(messageHistory);
l2.addView(newMessage);
l2.addView(btn);
l1.addView(l2);
flipper.addView(l1);
Where flipper is defined as:
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewflip);
Without a problem, I can see "l1" in the window when it loads up, but l2 is no where to be found. Did I mess up with LayoutParams? Can I use addView to add a LinearLayout?
I think you forgot to set layout weight (only my opinion though, could be wrong).
because you are adding more views with layout-height set to FILL_PARENT

Categories

Resources