How to change visibility of widget views(Buttons) programmatically Android - android

I have two buttons in my android widget and I want to change there visibility at run time i.e. if data is not coming present in local database I want to hide one button.
How can I get the id's of that button and how can I make it visible or invisible.
Please suggest

You can not change the visibility like:
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/button"/>
View button = findViewById(R.id.button);
button.setVisibility(View.GONE);
You have to get the button view like
RemoteViews.findViewById(R.id.button, Visibility.GONE);

to hide or show button you can use Remoteview.setViewVisibility(viewid,Remoteview.visibility)

Use this code:
Button btnFirst=(Button)findViewById(R.id.btn);
btnFirst.setVisibilty(View.INVISIBLE);

Set an id to the buttons you want to change visibility in your xml-layout:
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/button"/>
In your activity call findViewById()-method to locate the button in the ui:
View button = findViewById(R.id.button);
and finally set visibility:
button.setVisibility(View.GONE);

btn.setVisibility(View.VISIBLE);
btn.setVisibility(View.GONE);

Related

Android Studio Make Buttons Disappear From ScrollView

I have a ScrollView that contains a lot of buttons.
I want to create a search bar, so how can I disappear a button from the ScrollView so he will be hidden and the ScrollView will get shorter?
and then ofcourse I want to be able to return the button to the ScrollView.
you can change the visibility of the button from the activity by
if(true){
btn.setVisibility(View.VISIBLE);
}else{
btn.setVisibility(View.GONE);
};
you may also have to change the visibility in XML also
<ButtonView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btn"
android:visibility="gone" />

In android, how to drop down edit text fields when a button is pressed

I have a button Add new address and when it is pressed, I want to show EditText fields to collect the new address details. Is there any layout to do that. Or hiding the text fields when the Button is unpressed, is that the only way to do this?
Define the edit box in a layout as below -
<LinearLayout
android:id="#+id/exp_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/exp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="10dp"
/>
</LinearLayout>
And now use the layout id to get the view like below.
LinearLayout l=(LinearLayout)findViewById(R.id.exp_linear_layout);
And just toggle the visibility on button click event -
l.setVisibility(View.GONE) and vice versa.
I hope it will help u.
There is no built in framework to do it. You can do this by setting View.SetVisibility() to visible or gone. Initially the button is visible but textfield is invisible. When user click on the button, you can set this button visibility invisible or gone and visible the text fields.

How to show a layout on demand in android which overlays previously existed views?

For example suppose I have a button like this:
On clicking the Button I want a layout to be shown under it dynamically which overlays other views.
How can I achieve something like this ?
On clicking the Button I want a layout to be shown under it
dynamically which overlays other views.
you can do same using PopupWindow see below example:
How to create popups in Android
Depending on your parent layout you may have to tweak the code slightly but this overall idea should work for you.
Put your login layout below your loginBtn and set the layout to visibility:gone
<Button
android:id="#+id/loginBtn"
.../>
<RelativeLayout
android:id="#+id/loginLyt"
android:visibility="gone"
android:layout_below="#id/loginBtn"
...>
<!--your EditTexts and enter btn -->
</RelativeLayout>
<!-- rest of your layout... -->
then in your activity use the loginBtn click Listener to toggle the visibility of the loginLyt
loginBtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(loginLyt.getVisibility() == View.GONE){
loginLyt.setVisibility(View.VISIBLE);
}else{
loginLyt.setVisibility(View.GONE);
}
}
});

LinearLayout (with onClick) containing ImageButton - is there a better way to solve it?

At the moment I have something like that:
<LinearLayout android:layout_height="80dip" android:id="#+id/linearLayoutSettings"
android:layout_width="80dip" android:orientation="vertical">
<ImageButton android:layout_height="wrap_content" android:src="#android:drawable/ic_menu_manage"
android:id="#+id/imageButton1" android:layout_width="wrap_content"></ImageButton>
</LinearLayout>
LinearLayout has onClick listener attached to it.
Problem: When a ImageButton is clicked inside the LinearLayout, the event doesn't get triggered.
I could solve it by attaching the same on click to this button as I attached to LinearLayout. But in that case it would mean a lot of repetetive code (have many buttons).
Question: Is there a more effective way to solve this problem?
Change the ImageButton to an ImageView and you will start getting the click event
Attach the following tag to each imagebutton:
android:onClick="yourOnClickFunction"
Then, in your activity, add a corresponding function:
public void yourOnClickFunction(final View v) {
switch(v.getId()) {
//Do whatever is necessary.
}
}
In the switch-block you need to know about the buttons IDs. You can get them via findViewById(R.id.aButton).
You can write loop finding all buttons in layout and attach once created listener to all of them.
If I understand correctly, when the button is clicked, the LinearLayout's onClickListener doesn't fire. Which makes sense.
What you can do is put a yourLinearLayout.performClick(); which will programatically fire the layouts onClick event. If you put it at the end of the buttons onClick then it will perform the buttons, then the layouts, in order.
you must set the attribute setClickable(true);!!
you can access it in xml using android:clickable="true"

How can I remove a button or make it invisible in Android?

How can I remove a button in Android, or make it invisible?
Set button visibility to GONE (button will be completely "removed" -- the buttons space will be available for another widgets) or INVISIBLE (button will became "transparent" -- its space will not be available for another widgets):
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
or in xml:
<Button ... android:visibility="gone"/>
First make the button invisible in xml file.Then set button visible in java code if needed.
Button resetButton=(Button)findViewById(R.id.my_button_del);
resetButton.setVisibility(View.VISIBLE); //To set visible
Xml:
<Button
android:text="Delete"
android:id="#+id/my_button_del"
android:layout_width="72dp"
android:layout_height="40dp"
android:visibility="invisible"/>
To remove button in java code:
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.GONE);
To transparent Button in java code:
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.INVISIBLE);
To remove button in Xml file:
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
To transparent button in Xml file:
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
button.setVisibility(View.GONE);
This view is visible.
button.setVisibility(View.VISIBLE);
This view is invisible, and it doesn't take any space for layout purposes.
button.setVisibility(View.GONE);
But if you just want to make it invisible:
button.setVisibility(View.INVISIBLE);
use setVisibility in button or imageViwe or .....
To remove button in java code:
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(Button.GONE);
To transparent Button in java code
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(Button.INVISIBLE);
You should make you button xml code like below:
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
hidden:
visibility: gone
show:
visibility: invisible
visibility: visible
button.setVisibility(button.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
Makes it visible if invisible and invisible if visible
IF you want to make invisible button, then use this:
<Button ... android:visibility="gone"/>
View.INVISIBLE:
Button will become transparent. But it taking space.
View.GONE
Button will be completely remove from the layout and we can add other widget in the place of removed button.
To completely remove a button from its parent layout:
((ViewGroup)button.getParent()).removeView(button);
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/activity_register_header"
android:minHeight="50dp"
android:orientation="vertical"
android:visibility="gone" />
Try This Code
Visibility works fine in this code
In order to access elements from another class you can simply use
findViewById(R.id.**nameOfYourelementID**).setVisibility(View.GONE);
If you want to make your button invisible such that it doesn't take any space in the layout, then add this in your Java code:
Button button = (Button)findViewById(R.id.button);
button.setVisibility(View.GONE);
Or in XML: android:visibility="gone"
If you want to make your button invisible such that it still takes up space in your layout then replace "View.GONE" with "View.INVISIBLE" in your java code or replace "gone" with "invisible" in your xml code.
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(8);
Try This Code :
button.setVisibility(View.INVISIBLE);

Categories

Resources