Dynamically changing the position of a button with respect to another button - android

I had seen a couple of examples for setting the relative position through program (from java) for views in android. But in my particular case i have 2 buttons (which are not views) say "button_tag" and "button_rate" which are made via xml and are arranged such as "button_tag" above "button_rate" by default. If at any point of time is there a mechanism by which i can make "button_tag" below "button_rate" dynamically.
<Button
android:id="#+id/button_tag"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:background="#drawable/img_whitebackground"
android:gravity="left|center_vertical"
android:paddingLeft="5dip"
android:text="#string/string_tag"
android:onClick="click_addscreen" >
</Button>
<Button
android:id="#+id/button_rate"
android:layout_marginTop="1dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="#string/string_rate"
android:gravity="left|center_vertical"
android:paddingLeft="5dip"
android:onClick="click_addscreen"
android:background="#drawable/img_whitebackground" >
</Button>
The xml part of the two buttons are as shown

You can use LayoutParams object and add rules to that object as per your requirement. and then setlayoutparam to your button.
For example:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80,80); // size of button in dp
params.addRule(RelativeLayout.LEFT_OF, R.id.btnAdd);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.setMargins(0, 0, 20, 60);
btnMyLocation.setLayoutParams(params);

You can add rules to set position dynamically.
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
p.addRule(RelativeLayout.BELOW, R.id.button_rate);
buttonTag.setLayoutParams(p);

Related

Button text cut off when added dynamically

First 5 buttons are added programatically and last one via XML. Both ways I use same parameters. Why are dynamically added buttons text's cut off?
Programatic:
Button b = new Button(getActivity());
b.setText(text);
b.setAllCaps(false);
b.setBackgroundResource(R.drawable.button_tag_rect);
b.setTextColor(getResources().getColor(R.color.colorWhiteText));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
dpToPx(28));
int marginInPx = dpToPx(2);
params.setMargins(marginInPx, marginInPx, marginInPx, marginInPx);
tagCloudTwitter.addView(b, tagCloudTwitter.getChildCount()-1, params);
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_margin="2dp"
android:textAllCaps="false"
android:background="#drawable/button_tag_rect"
android:textColor="#color/colorWhiteText"
android:text="test"/>
EDIT: solved! See my answer bellow.
Funnily enough, what helped was setting a "dummy" padding:
b.setPadding(1,1,1,1);
¯\_(ツ)_/¯
Try setting the button height and width;
b.setWidth(10);
b.setHeight(100);

Weird behavior with buttons and visibility gone

I am having a very weird problem with buttons, vertical centering and visibility gone. In my app I have a list of books, and I am using the swipelistview library to create buttons behind each row. I have 3 buttons: return, send reminder and delete. The return and send reminder buttons visibility is set to Visibility.GONE dynamically if the book isn't lended. Now here's my problem. I have the following xml layout for the back of the rows.
<LinearLayout
android:id="#+id/swipelist_backview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:tag="swipelist_backview"
android:background="#101010">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="55px"
android:layout_gravity="center_vertical"
android:id="#+id/swipe_button1"
android:text="#string/markAsReturned"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:id="#+id/swipe_button2"
android:layout_gravity="center_vertical"
android:text="#string/sendReminder"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:id="#+id/swipe_button3"
android:layout_gravity="center_vertical"
android:text="#string/delete"/>
</LinearLayout>
Now the expected result from this is that the buttons will be vertically centered in the middle of each row, which does happen if the book is lended, and all the buttons are visible. But if the book isn't lended, the only button shown is delete, and it isn't aligned .
I am also setting the left margin of the delete button to 55 px using the following code:
if(!item.isLended())
{
btnReturn.setVisibility(View.GONE);
btnSendReminder.setVisibility(View.GONE);
LayoutParams lp = new LayoutParams(btnDelete.getLayoutParams());
lp.setMargins(55, 0, 0, 0);
btnDelete.setLayoutParams(lp);
}
I thought this might be removing the verical alignment, but LayoutParams doesn't seem to have a way to set layout-gravity, so it doesn't look like it.
Well, I guess it was the LayoutParams. If anyone is intersted, here's how I solved it:
if(!item.isLended())
{
btnReturn.setVisibility(View.GONE);
btnSendReminder.setVisibility(View.GONE);
LayoutParams lp = new LayoutParams(btnDelete.getLayoutParams());
lp.setMargins(55, 0, 0, 0);
lp.gravity = Gravity.CENTER_VERTICAL;
btnDelete.setLayoutParams(lp);
}

Programmatically set weight of children of LinearLayout [duplicate]

This question already has answers here:
Android Linear Layout Weight Programmatically
(4 answers)
Closed 3 years ago.
I have a vertical LinearLayout LL_p with two children horizontal LinearLayouts LL_1 and LL_2 which in turn have their own children. Based on the visible contents of LL_1 and LL_2, I want to dynamically change their relative weight inside LL_p. I already have an xml layout with a great deal of details that I do not wish to lose, so I only have to make the incremental change to the weights. How do I do that? Here is my xml
…
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=“0.2”
android:orientation="vertical" >
<LinearLayout
android:id="#+id/ll_1”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"
android:background="#drawable/some_image”
android:orientation="vertical" >
<!—- a number of includes —>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_2”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.45"
android:layout_marginBottom="#dimen/dim_1”
android:background="#color/some_color”
android:orientation="horizontal" >
<!—- a number of children —>
</LinearLayout>
</LinearLayout>
</LinearLayout>
So in java activity class I figure I need the following method, but I need help completing it. Notice that I am not instantiating new layout params as that would cause me to lose my xml layout details; instead I am using getLayoutParams to grab the one set from xml. so how do I make weight change to the layout as obtained?
private void adjustMYLayout(boolean flip) {
LayoutParams layout1 = mLL1.getLayoutParams();
LayoutParams layout2 = mBLL2.getLayoutParams();
//now what?
if(flip) {//set one weight system
}else {
//set other weight system
}
}
UPDATE for #nKn
private void adjustMYLayout(boolean flip) {
if (flip) {
mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.8f));
mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.2f));
} else {
mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.55f));
mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 0.45f));
}
}
You can achieve that by declaring a LinearLayout.LayoutParams object
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
The third parameter (1f) is the weight of the layout (in this case 1 (f stands for float)).

Edit position of an Android button created with a linearLayout

I am trying to dynamically create buttons, and they will be of varying size and in varying positions.
I have the code to create a button of varying size, but I am stuck on changing the position.
I am using linearlayout and am trying to use setMargins to move the button around, but it seems to be changing the margin within the button. My code is as follows:
public void button(int a, int b) {
newButton = new Button(this);
newButton.setText("HELLO");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
ViewGroup.LayoutParams params = layout.getLayoutParams();
params.width = a;
params.height = b;
layout.requestLayout();
layout.addView(newButton, layoutParams);
}
Here is my manifest for this bit:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="58dp"
android:layout_toRightOf="#+id/textView1"
android:text="Button" />
<LinearLayout
android:id="#+id/layout"
android:layout_width="50sp"
android:layout_height="40sp"
android:orientation="vertical" >
</LinearLayout>
Do you understand what a LinearLayout is and why you're using it? Every child of the layout snaps in place. If I have a LinearLayout that's vertical and it has 3 children they will be on top of each other. I can change their gravity so they are attracted to different margins but to "change" its position is impossible depending on what you mean by "change".
Check out the other layouts. You may want to use a RelativeLayout.
Why don´t you use button.setX(float x) and button.setY(float y)? You'll need to use RelativeLayout instead of the LinearLayout. It's more easy but it's only available since api 11...

Android layouts not displaying as intended

I need to create one full screen android activity programatically as shown in the image below:
The two buttons should remain at the bottom of the screen.
Dummy content will consist of different components (textviews, radio buttons, checkboxes...) and will be populated dynamically.
This is the code I have so far:
//Main Layout
FrameLayout lLayout = new FrameLayout(this);
lLayout.setBackgroundColor(Color.parseColor("#0099cc"));
lLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
//Navigation layout
LinearLayout l = new LinearLayout(this, null, R.style.ButtonBar);
LayoutParams bottomLayout = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
l.setLayoutParams(bottomLayout);
l.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
l.setBackgroundColor(Color.parseColor("#66000000"));
l.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams buttLayout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//previous section button
previousButton = new Button(this);
previousButton.setLayoutParams(buttLayout);
previousButton.setText("Previous section");
previousButton.setOnClickListener(this);
l.addView(previousButton);
//next section button
Button nextButton = new Button(this);
nextButton.setLayoutParams(buttLayout);
nextButton.setText("Next section");
nextButton.setOnClickListener(this);
l.addView(nextButton);
//add components
TextView tView = new TextView(this);
tView.setText("Dummy text");
tView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
lLayout.addView(tView);
lLayout.addView(l);
setContentView(lLayout);
Here is what the code produces:
Tthere are several points that do not work as intended:
1. Buttons are at the top and not the bottom.
2. Buttons do not spread out nicely
3. TextView I added as a test is shown behind the buttons. I will have many different widgets on the screen and expect them to be larger than one screen. I would like to have a scroll option but with all those widgets not to be seen behind the two buttons that are supposed to be at the bottom of the screen.
The following xml is exactly what you would need:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/dynamiclayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/navigationlayout"
android:orientation="vertical" >
</LinearLayout>
<RelativeLayout
android:id="#+id/navigationlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Previous Section" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Next Section" />
</RelativeLayout>
</RelativeLayout>
Now programatically inflate dynamiclayout and add all your dynamic views into it.
Your root view is a FrameLayout, which is intended for only one child View. It is also frequently used to create overlapping Views, as all of a FrameLayout's children will be drawn in the same place on screen.
Replace your FrameLayout with a RelativeLayout. Make sure you update your LayoutParams references to use RelativeLayout.LayoutParams. You will also need to set the navigation LinearLayout to align with the parent View's bottom like so:
RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lps.addRule(RelativeLayout.LayoutParams.ALIGN_PARENT_BOTTOM, true);
Though I really would suggest using XML. It will make your life far simpler.

Categories

Resources