I have a screen where I display five buttons inside a Linearlayout vertically.
this linear layout is inside a relative layout and aligned to parent right.
Now I need that when any button is clicked it should shift leftwards while others remain at the original position.
I am trying to give left margin to the button on being clicked but the problem is all the buttons are shifting and not only that one.
Here is the code
final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.rightMargin = 20;
mColorButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
fillDataList(1);
mIsClicked = true;
//[start] setting the colour of button
mColorButton.setBackgroundResource(R.drawable.yellow_stick) ;
mColorButton.setLayoutParams(layoutParams);
mPatternButton.setBackgroundResource(R.drawable.brown_stick);
mDistributionButton.setBackgroundResource(R.drawable.brown_stick);
mShapeButton.setBackgroundResource(R.drawable.brown_stick);
mLesionButton.setBackgroundResource(R.drawable.brown_stick);
//[end] setting the colour of button
}
});
May be this is the problem:
By default all Buttons are left aligned and when you give right margin to a Button it expands its parent layout size. The parent expands it self with its child. So all the Button shifted left side.
try to give android:layout_gravity="right" to all Button in XML or code.
Related
I have a piece of code which I use to create a new LinearLayout. Within the layout I wish to add a TextView which contains both a label and a value. Then next to it on the right I want to display the button. I want the button to be located toward the end of the screen, without stretching the button. I am happy with the button width and height as WARP_CONTENT.
How can I achieve this in code? I have barely any XML so using XML is not an option. I am trying to make the app as dynamic as possible, so I decided to steer clear of XML.
Please see the code below:
// Build a button
final Button addButton = new Button(task.getParent());
addButton.setText("Add New");
addButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Open a file picker here to let the user pick a file
}
});
// Build a new layout to hold all the elements
LinearLayout verticalLayout = new LinearLayout(task.getParent());
verticalLayout.setOrientation(LinearLayout.HORIZONTAL);
verticalLayout.addView(sizeTextView);
verticalLayout.addView(addButton);
Thank you guys in advance.
Try this: Add Space (View) between TextView & Button.
// View space = new View(parent_context);
View space = new View(task.getParent());
// Width:0dp, Height:1 & Weight: 1.0
LinearLayout.LayoutParams spaceLP = new LinearLayout.LayoutParams(0, 1, 1.0f);
space.setLayoutParams(spaceLP);
verticalLayout.addView(sizeTextView);
verticalLayout.addView(space);
verticalLayout.addView(addButton);
Add textview with size and gravity,like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setLayoutDirection(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
verticalLayout.addView(sizeTextView,params);
To achieve this you should use Relative layout and RelativeLayout.LayoutParams. By using LayoutParams you can set the rule to align your views as per your requirements.
for example
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);
I have linear layout but bottom part not display if scrolling data is more then screen size.
Streaching Bottom Part when Using Scrollview inside the linearlayout, when scrolling data more then height of the device height.
If i have less data then vertical screen size then all button of bottom appears correctly.
public class TestGUI extends LinearLayout {
sv = new ScrollView(context);
hsv = new HorizontalScrollView(context);
this.addView(topLinearHorizonal);
hsv.addView(tableLayout);
sv.addView(hsv);
this.addView(sv);
this.addView(fullbottomLinearHorizonal);
}
Edited:
LinearLayout fullbottomLinearHorizonal= new LinearLayout(context);
fullbottomLinearHorizonal.setOrientation(VERTICAL);
fullbottomLinearHorizonal.addView(clearLinearHorizonal);
fullbottomLinearHorizonal.addView(bottomLinearHorizonal);
If you want to show fullbottomLinearHorizonal always at the bottom(visible) you can try below while adding views to TestGUI:
LinearLayout.LayoutParams svParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1);
this.addView(sv, svParams);
LinearLayout.LayoutParams fullbottomParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, WRAP_CONTENT);
this.addView(fullbottomLinearHorizonal, fullbottomParams);
Edit: Also default orientation for LinearLayout is horizontal. See link.
You should better set it vertical at TestGUI:
this.setOrientation(VERTICAL);
If you want fullbottomLinearHorizonal at the end of the scroll content then you must add it inside of the scrollView.
I'm having a difficulty adding buttons dynamically to a ScrollView. The code below is adding the buttons BUT there is no scroller.
If I'm putting the buttons directly in the XML (not dynamically) it's working and I can scroll down/up.
My view:
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="264dp"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="264dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical"
>
** HERE THE BUTTONS SHOULD BE ADDED DYNAMICALLY **
</LinearLayout>
</ScrollView>
The code which adding buttons:
// create new button
final Button newbutton = new Button(this);
// set background color
newbutton.setBackgroundColor(Color.GRAY);
// set width and height
newbutton.setWidth(50);
newbutton.setHeight(20);
// set position
newbutton.setY(((float)numOfButton*20)+20);
newbutton.setX(100);
// set text
newbutton.setText(Integer.toString(numOfButton));
// create patameter
final LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
//set listener
android.view.View.OnClickListener buttonListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// make all the DrawView invisible
for(View view : comments){
view.setVisibility(View.INVISIBLE);
}
// set the chosen comment visible
comments.get(numOfButton).setVisibility(View.VISIBLE);
boardsHandler.setCurrenBoard(numOfButton);
}};
newbutton.setOnClickListener(buttonListener);
// creating a thread to add button
buttons.post(new Runnable() {
#Override
public void run() {
buttons.addView(newbutton, p);
}
});
Is it something with the LinearLayout.LayoutParams p ?
Thanks!
Try following code
first do
LinearLayout myContainer = findViewById(R.id.layoutId);
When you set parameters for a view, they need to correspond to the parent view for your widget.
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT);
finally add button as you are doing.
try and tell if it works
Setting X and Y position will not work. The LinearLayout layouts it's children vertically or horizontally, only taking their width/height into account.
Besides this -- have you tried calling buttons.invalidate() after buttons.addView(...). This should refresh the layout and should show your newbutton.
This is a rather old post but I found it quickly when doing research on that kind of problem. So I'll post am answer anyway, maybe it'll be of help to anyone..
I had a similar problem with a relative layout to which buttons were added dynamically. I found a workaround in defining the layout's size manually when adding the buttons. For your case, adding the line
buttons.getLayoutParams().height = numOfButton*20+40;
after
buttons.addView(newbutton, p);
might help, though it's probably not the best solution.
I thought my mistake was using the RelativeLayout at all, but since you appear to have the same problem...
Ever thought of using a table layout?
I want to do something like XML attr when dynamically adding a view:
android:layout_alignRight="#+id/myView1">
my activity code:
button1.setOnClickListener(new View.OnClickListener(){
#Override public void onClick(View v) {
RelativeLayout ll = (RelativeLayout)findViewById(R.id.aincrad);
TextView view = new TextView(Activity.this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_RIGHT,R.id.Btn1Holder);
view.setText("testing...");
view.setLayoutParams(layoutParams);
ll.addView(view, layoutParams);
}});
the result should aligned the new textview stay right of Btn1Holder
I'm not sure this is the right approach, but I guess you have to call setLayoutParams before adding your view to the layout.
If on the other hand you want be sure of having the view right there, you could add it to your xml and set trigger its visibility from the code.
UPDATE:
AlignRight "Makes the right edge of this view match the right edge of the given anchor view ID." it means that it works if your layout is under the target and you want to align their edges. You need to use RIGHT_OF which "Rule that aligns a child's left edge with another child's right edge. "
I have a question for Android developers. I have a layout with a button (developed programmatically, not with xml) and I want the button to fill the entire layout right now but it currently doesn't and I'm not sure why, I thought I had everything set up correctly with the gravity of the button and the layout params but here's what I have. If you can point me in the right direction I would really appreciate it! Thanks.
LinearLayout bottom = new LinearLayout(this);
bottom.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.FILL;
bottom.setLayoutParams(params);
bottom.setBackgroundColor(Color.BLUE);
Button eqbttn = new Button(this);
eqbttn.setText("=");
eqbttn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
}
});
bottom.addView(eqbttn);
You've only applied the MATCH_PARENT size to the LinearLayout. You need to apply it to the button, too.
Button eqbttn = new Button(this);
LinearLayout.LayoutParams eqparams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
eqbttn.setLayoutParams(eqbttn);
eqbttn.setText("=");
eqbttn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
}
});
bottom.addView(eqbttn);
This will force the button to fill the LAYOUT both vertically and horizontally. If you need the layout itself to take up the whole screen, change its WRAP_CONTENT to MATCH_PARENT as well.
Also, in this case, you do not require the Gravity.FILL layout parameter on the LinearLayout.