How to change weight of a button programatically - android

I have a CountDownTimer that is activated by a "Set" button and then can be stopped by a "Cancel" button.
When the activity starts, I want only the set button to show. When the countdown is started, I want to hide the "Set" button and show the "Cancel" button.
I want to do this by changing the weight of the button. How do I do this? I have found similar questions that have answers that don't work when I try them. I think those answers are just fragments of the full code, which is what I'm looking for.
Here is the XML:
<LinearLayout
android:id="#+id/buttonsContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<Button
android:id="#+id/setDelay"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:onClick="activateDelay"
android:text="Set Delay" />
<Button
android:id="#+id/cancelDelay"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:onClick="cancelDelay"
android:text="Cancel Delay" />
</LinearLayout>
I know I'm using hard-coded strings, they're just for development purposes.

Why do you want to use the weight property to hide a button? layout_weight is used to share space proportionately (according to the weights given). I guess you could "hide" a button by setting the layout_weight to 0 (and the weight on the other to something greater than 0). I've never updated the layout parameters after adding the views to viewgroup, but parent.updateViewLayout(childView, updatedParams) looks promising.
In any case, use setVisibility(View.GONE) on the button you want gone; it is hidden and the rest of the Views are laid out as though the button is not present. (You can use View.INVISIBLE to hide the View, but the rest of the Views are laid out as though the button is there - you just can't see it.)
Here's an untested "fragment" ;P
private Button set;
private Button cancel;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// it's typical to use underscore notation for IDs - R.id.set_delay
set = (Button) findViewById(R.id.setDelay);
cancel = (Button) findViewById(R.id.cancelDelay);
}
public void activateDelay(View button) {
set.setVisibility(View.GONE);
cancel.setVisibility(View.VISIBLE);
}
public void cancelDelay(View button) {
set.setVisibility(View.VISIBLE);
cancel.setVisibility(View.GONE);
}
And in your XML, you'd start with the "Set" button visible (by default), and "Cancel" gone, both with match_parent for width:
<LinearLayout
android:id="#+id/buttonsContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<Button
android:id="#+id/setDelay"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:onClick="activateDelay"
android:text="Set Delay" />
<Button
android:id="#+id/cancelDelay"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:onClick="cancelDelay"
android:text="Cancel Delay" />
</LinearLayout>

Related

Android evenly divide horizontal space into N segments

I have a few buttons (ranges between 1-5) that i would like to show in a horizontal space.
Each space should take up equal amount of space.
The buttons, depending on the state of the current user, will be hidden and shown dynamically.
Ideally, in my java code, i only wish to control the visibility of buttons via setVisibility()
Is there a way to organize my layout, so that the width of my button is always EVENLY distributed across the width of screen?
I thought about using a relative layout, and buttons would line up one by one, but i never get that to work. Alternatively, i could use linearLayout with layout sum, and assign each button a weight of 1. But when i am hiding buttons, the rest of the button do not readjust and take up the remaining space.
Use a LinearLayout with a horizontal orientation. For each of your Buttons, use these attributes:
android:layout_width="0dp"
android:layout_weight="1"
Do not put a weightSum attribute on the parent LinearLayout. Omitting this attribute will cause the buttons to resize as some are shown and hidden.
Just use LinearLayout with horizontal orientation (should give you an error when you place more than 1 element in LinearLayout and don't mention the orientation attribute) and simply place your buttons. Just include the weight = 1 attribute in your buttons to distribute the space equally.
XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.nero.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_weight="1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="1"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_weight="1"/>
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:layout_weight="1"/>
</LinearLayout>
MainActivity Class
public class MainActivity extends AppCompatActivity {
Button btn1, btn2, btn3, btn4, btn5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.button1);
btn2 = findViewById(R.id.button2);
btn3 = findViewById(R.id.button3);
btn4 = findViewById(R.id.button4);
btn5 = findViewById(R.id.button5);
btn1.setVisibility(View.GONE);
}
}
By setting the visibility of a button to GONE with automatically adjust the size of the remaining buttons with visible visibility. I've tested this piece of code myself to ensure it's functionality.
An alternate approach to using LinearLayout would be to use a ConstraintLayout. Everything in a horizontal chain will be allocated the same amount of space.
Pseudocode below
<ConstraintLayout>
<View
android:id="#+id/one"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_leftToLeftOf="parent"
app:layout_rightToLeftOf="#id/two"
/>
<View
android:id="#+id/two"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_leftToRightOf="#id/one"
app:layout_rightToLeftOf="#id/three"
/>
<View
android:id="#+id/three"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_leftToRightOf="#id/two"
app:layout_rightToRightOf="parent"
/>
</ConstraintLayout>

Change button alignment of buttonA when buttonB is GONE

I have placed two buttons prev and next in a RelativeLayout. When I reach the last page of my app, I am disabling the next button using :
next.setVisibility(View.GONE);
The alignment of prev button is disrupted. I want it to be aligned to the centre of the RelativeLayout, as if there was only one button.
Here is my code :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#+id/radgroup"
>
<Button
android:layout_marginTop="14dp"
android:layout_marginLeft="20dp"
android:text="#string/previous"
android:textAllCaps="false"
android:textSize="23dp"
android:layout_width="150dp"
android:layout_height="70dp"
android:id="#+id/prev"
android:onClick="viewPreviousQuestion"
/>
<Button
android:layout_marginTop="14dp"
android:layout_marginLeft="20dp"
android:text="#string/previous"
android:textAllCaps="false"
android:textSize="23dp"
android:layout_width="150dp"
android:layout_height="70dp"
android:layout_toRightOf="#+id/prev"
android:id="#+id/nxt"
android:onClick="viewNextQuestion"
/>
</RelativeLayout>
and the onClick event is
public void viewNextQuestion(View view) {
if(currqstn==lastqstn){
next.setVisibility(View.GONE);
}
}
Don't use a Relative Layout here,Use a Linear Layout Instead.
Since in a Relative layout Views are positioned with respect to one another, when you remove one view,it might disrupt other views that were positioned with respect to the deleted view.
EDIT
Use can use the following attributes for your views: weightSum, weight and padding.
Use Linear Layout to cover both the buttons Inside YOur main Relative Layout and and set
android:layout_weight="1"
android:layout_gravity="center_horizontal|center"
to your prev button.
When you using View.Gone its remove view place and other views are disturb
AND
when you using View.Invisible its always there just view is hidden and others views are not disturb.
use this...........
next.setVisibility(View.Invisible);
enjoy coding .....

How to replace a Button's TextView with a custom layout

Instead of just a textview on a button, I'd like the button to contain a layout with two TextViews. See the mockup below. When a user uses the button to add a category, I'd like to update the percentage on the right. I have this working with an included layout, but I want to use a button so that the user instinctively knows to click it.
Button is a View not a ViewGroup. To achieve that, use a horizontal LinearLayout, style it as a button and add a ClickListener to it. Something like this:
<RelativeLayout
android:clickable="true"
android:background="#android:drawable/btn_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text1"
android:text="Category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text2"
android:text="value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Note that the background of the layout is set to the default android button so it will look like a button.

How to change a TextView width Dynamically

I have a TextView1, buttons1 and buttons2 Horizontally.
When Button2 click Button1 is invisible.
When Button1 is invisible, I need to increase the width of the textview to cover the space created when button1 is invisible and vice versa.
Can Please anybody help How can I change the Textview width dynamically.
NB: Textview tv.setWidth(pix) is not working. when I tried to find the value of tv.getWidth() gives always zero.
`
<TextView
android:layout_width="odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hello" />
<Button
android:id="#+id/btn1"
android:layout_width="odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hello"
android:visibility="visible" />
<Button
android:id="#+id/btn2"
android:layout_width="odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hello" />
</LinearLayout>`you can use weight for three element and button1.setVisibility(View.GONE)
Please try to use GONE instead of using INVISIBLE. When using INVISIBLE even though you can't see it, it still takes a space.
Wrap your widgets in a RelativeLayout and then check. You will be able to bring your textview on top of your button if it is invisble. You cant do this if you are using LinearLayout because it sets widgets horizontaly or verticaly and does not overlap.

what would be the best way to have an Android homescreen widget alternate between textviews?

I'm trying to create a homescreen Android widget and have it alternate between two different textviews I would send to it. Is this possible?
Why not maintain the same textview and just change the text shown?
If you really must use 2 text views you can use the setViewVisibility method in the RemoteViews object to alternate between GONE (which means not shown to user, takes up no screen space) and VISIBLE (shown to user, takes up screen space).
You could use a ViewFlipper to switch between multiple text views if that's what you meant.
<ViewFlipper android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:outAnimation="#anim/push_left_out"
android:inAnimation="#anim/push_left_in">
<TextView android:layout_height="fill_parent"
android:layout_width="fill_parent" android:padding="16dip"
android:id="#+id/txt1" android:textSize="8pt"
android:textColor="#ffffffff"
android:text="#string/text1"/>
<TextView android:layout_height="fill_parent"
android:layout_width="fill_parent" android:padding="16dip"
android:id="#+id/txt1" android:textSize="8pt"
android:textColor="#ffffffff"
android:text="#string/text2"/>
</ViewFlipper>
ViewFlipper mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper));
You could use a button event to switch between the text Views.
Button learn_more = (Button) findViewById(R.id.button);
learn_more.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mFlipper.showNext();
}
});
Hope it helps.

Categories

Resources