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
Related
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))
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);
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>
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.
i want to align an ImageView to the right side of the LinearLayout Programmatically.How can i do this.
You don't. That is not what a linear layout does. Use a RelativeLayout instead.
XML
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Programmatically
public void makeView(Context context) {
RelativeLayout rl = new RelativeLayout(context);
TextView tv = new TextView(context);
tv.setText("Hello, World");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rl.addView(tv, lp);
}
You can try this:
ImageView view = (ImageView)findViewById(R.id.imageView);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.LEFT;
But you should use RelativeLayout instead of LinearLayout.