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" />
Related
I am having an activity and want to create a like button at the end of the layout, So I created a layout file and in a LinearLayout I have set it's layout_alignParentBottom property to true and created button for Likes in it. Now I am including this layout file in some other layout file but when I am applying onClickListener to the button, it does nothing.
When I remove this layout_alignParentBottom from the LinearLayout properties, then OnclickListener start working.
Can you please help me here to resolve this issue?
Some other widget might be coming in its way. if there is something above that button, it wont take clickListener.
For Ex. if there is a list in that layout too,
<Button
android:layout_alignParentBottom="true"
android:layout_marginLeft="#dimen/viewSpace1"
android:layout_marginRight="#dimen/viewSpace1"
android:layout_marginBottom="#dimen/viewSpace1"
android:layout_width="fill_parent"
android:layout_height="#dimen/headerHeight_small"
android:id="#+id/btnShare"
style="#style/ButtonLogin"
android:text="Next" />
<ListView
android:layout_above="#id/btnShare"
android:id="#+id/list"
android:layout_below="#id/layoutHeader"
android:layout_marginLeft="#dimen/viewSpace3"
android:layout_marginRight="#dimen/viewSpace3"
android:layout_marginBottom="#dimen/viewSpace3"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
so your share button stays safe for clickability.
I have kept the list above btnShare. Just for my safety if it overlaps the button.If there is still problem, post your code so exact problem can be pin pointed.
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);
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.
A window can be set to not touchable, which means that you can click anything behind that activity just by setting the whole window to not touchable with the following code:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Now my question is: Is it possible to make only the RelativeLayout NOT TOUCHABLE, I mean the user is able to touch anything behind that RelativeLayout, but he will not be able to touch anything behind the Button in that RelativeLayout.
This is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl"
style="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="Button" />
</RelativeLayout>
I need the relativelayout to be NOT TOUCHABLE, the user can touch anything behind it.
I only need the button in the relativelayout to be touchable
Hence, Button touchable, RelativeLayout Not Touchable.
I can touch the button, but I can't touch the RelativeLayout I only touch things behind it.
When using
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
The whole window is being set to not touchable with the Button as well, which is not what I want.
I hope my question is understandable.
EDIT:
All i'm trying to do is Displaying a single button on another application (Skype, for example)
I only want that button to be clickable with Skype app together.
Try adding android:duplicateParentState="true" to your RelativeLayout this will propagate the events down the view tree.
If that does not work, try overriding this method.
http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)
In your java code write like this
ArrayList<View> touchables;
RelativeLayout RelativeLayout_ = (RelativeLayout) findViewById(R.id. rl);
touchables = RelativeLayout_.getTouchables();
for (View touchable : touchables)
{
if (!(touchable.getId()==R.id.button1))
{
touchable.setEnabled(false);
}
}
Hope it will work
What I want to do is show a "frame" (or new layout) on top of "2" (second LinearLayout), when a button would be pressed. How should I do it? Precreate it and make it somehow hidden if button not pressed?
I have this type of layout:
XML:
<LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
//here would be another view, only shown when a button is clicked
<ViewFlipper>
</ViewFlipper>
</LinearLayout>
<RelativeLayout
</RelativeLayout>
</LinearLayout>
Use FrameLayout to show view over-lapping another view. You can keep the view as INVISIBLE or using GONE in the xml and then just make it visible when the Button is Clicked.
Yes...you should prepare it in xml and give it an id.then you can easily manage its visibility on button click using mLinearLayout.setVisibility(View.GONE); and mLinearLayout.setVisibility(View.VISIBLE); like:
Button mButton=(Button)findViewById(R.id.button);
LinearLayout ll=(LinearLayout)findViewById(R.id.frame_layout);
static int count=0;
mButton.setOnClick.... (new OnClick...()
public void onClick(){
count++;
if(count==1)
ll.setVisibility(View.VISIBLE);
else
{
count=0;
ll.setVisibility(View.GONE);
}
}
);
Here you have two options:
As you said pre-create layouts and set visibility to Visibility_Gone to layouts initially, not to be shown, set Visibitlity to View.Visible to display the layouts.
Another approach is to create views dynamically, and adding to parent on specified index, like to add on top of linearlayout use:
linearLayout.addView(view, 0);
If you want to show any view on button click then first put that view inside xml and make its visibility gone, and on button click make it visible. I have put imageview inside your code which visibility is set as gone so it wont show in layout.
<LinearLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
//here would be another view, only shown when a button is clicked
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:visibility="gone" />
</LinearLayout>
<RelativeLayout
</RelativeLayout>
</LinearLayout>
For making image view visible,
imag1.seVisibility(View.VISIBLE);