Android - Overlay with Button Not Responding Correctly - android

I have an overlay that covers my entire screen to provide a simple message when the user launches the app for the very first time. On this overlay is a single button that dismisses the overlay (by setting its visibility to gone). I have a catch-22 that I don't know how to resolve.
First, the xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main_rootview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="Activity.MainActivity"
android:orientation="vertical">
<include
android:id="#+id/activity_main_content"
layout="#layout/main_content"/>
<RelativeLayout
android:id="#+id/activity_main_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#B3000000">
<Button
android:id="#+id/button dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:text="#string/ok"/>
</RelativeLayout>
</RelativeLayout>
The very first time they launch the app, I set this overlay to be visible. I want them to be able to click the button, which will set the visibility of the overlay to View.GONE. As this code stands, they can click the button and all is well. However, if they click anywhere else on the overlay, the click passes through the overlay and the content behind the overlay responds as if the overlay was not present. I want this overlay to absorb the click, and only respond to the button.
So, the obvious response is to add clickable="true" to the overlay, right? But then, the button itself doesn't respond to the click and the user is stuck on the overlay, thereby defeating the entire purpose of the overlay. Sometimes, if I wait long enough, the button click will occur, but if this happens at all, it is an unacceptably long delay before the button will respond.
How can I get the button to respond to a click and at the same time have the overlay consume all other tap events? Thanks for any help.

Perhaps it is not the cleanest solution, but you can set onclickListener event on the overlay.
for example
RelativeLayout overlay = (RelativeLayout)findViewById(R.id.activity_main_overlay);
overlay.setOnClickListener(new OnClickListener(){
});
//button click
Button btn = findViewById(R.id.button_dismiss);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//button action
}
);

Related

Android setting the visibility of main activity

Is it possible to set the visibility of the MainActivity page in android to invisible or make it transparent may be? I have tried setting the visibility to invisible and also made height width as 0dp but this still shows the mainactivity with a white background and blue header.
What I am trying to achieve is, I am opening a floating window which is draggable on click of a button in the mainactivity, but I want to directly open the floating window by triggering the button click from the code itself. This works but the mainactivity still shows up for a second before the floating window is opened. I want to make the mainactivity page invisible or transparent so the mainactivity page is not noticeable.
Here is the XML of mainactivity.xml ,
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="Invisible"
android:background="#color/material_on_primary_disabled"
tools:context="com.example.widget_android.MainActivity">
<Button
android:id="#+id/open_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Open Widget" />
on click of open_widget(which is triggered from code rather than the user having to click on it) the floating window is opened up.
I want to take the user directly to the floating window rather than the landing page which is the mainactivity page.
Here is what is happening on the backend java code
findViewById(R.id.open_widget).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startService(new Intent(MainActivity.this,WidgetService.class));
finish();
}
});
findViewById(R.id.open_widget).performClick();
The reason you get a white screen is because that is the default behaviour. You've tried to put an invisible, transparent, or non-existing (size of 0 pixels) View on top of the default View, so you are still seeing the default View. What you need to do is to replace the default View with a transparent View.
See How do I create a transparent Activity on Android? for information on how to get a transparent Activity.

Floating action button captures clicks outside its bounds

I'm trying to use the floating action button widget from the Android support library. The button shows up properly, however, it captures clicks that are not within the button itself. It accepts clicks at least a full radius outside the visible button. I don't know if it's due to the shadow, but I can click even well beyond the visible shadow and the button captures the click. Also, if I set the margins of the button to zero, there is still a large gap between the button and the edges of the screen. I have also tried to set the padding to zero, but it has no effect.
Here is my layout:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
app:elevation="6dp"
app:borderWidth="0dp" />
</android.support.design.widget.CoordinatorLayout>

Android - RadioGroup's first button becomes unselectable after clearCheck() and rotate

I'm having problems with RadioGroups clearCheck method and rotating my app. I think it might be a Google issue?
I have a three button radio group that when launched has the first item selected by default in xml.
My problem is, I have a button that calls radio groups clearCheck(). If you rotate the app, and then try to press the first radio group item, it will not select it. It tries, it shows the animation, but it remains unselected.
I thought I was going crazy and it was something wrong in my code, but I've broken it down to the most simplistic app possible by just making a new app.
If I don't preselect Button A (via XML or code) then it works correctly, but I need this button pre-selected.
Any idea how I can work around this problem and have A selected by default?
Repo steps
Launch app
Select Radio Button B
Press Clear Check
Rotate App
Press Radio Button A
Result: Radio A won't select. If you then press B or C you can once again press A, but not until then.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
Button clearButton = (Button) findViewById(R.id.clearButton);
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
radioGroup.clearCheck();
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:checkedButton="#+id/radioButtonA"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioButtonA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A" />
<RadioButton
android:id="#+id/radioButtonB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B" />
<RadioButton
android:id="#+id/radioButtonC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="#+id/clearButton"
android:layout_below="#+id/searchRadioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="114dp" />
</RelativeLayout>
Upon rotation, the checked id is reset due to android:checkedButton="#+id/radioButtonA".
If there was a button checked, CompoundButton's onRestoreInstanceState resets the checked button again to the correct checked button.
Contrarily, if there was no button checked, buttonA's onRestoreInstanceState will reset it to unchecked. RadioGroup is not handling this scenario because it is not designed to handle unchecks.
So, we end up with RadioGroup tracking buttonA as checked and buttonA tracking itself as unchecked. When you click on buttonA, the check is processed, however the UI does not update due to shortcircuit logic in the check() method because it assumes that since the id already matches the mCheckedId it has on hand, the button should already be checked:
// don't even bother
if (id != -1 && (id == mCheckedId)) {
return;
}
I guess this could be considered a bug, but it ultimately comes down to some strange design choices on your part. You are setting it to default to buttonA, which means that the user cannot uncheck any button under normal circumstances and then you provide a clear button to get them to a non-default state that they could not reach under normal circumstances. So, there are two possible solutions that would bring your UI design choices in to focus:
1) If you want there to always be a checked button, have the reset button reset to default state.
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mRadioGroup.check(R.id.radioButtonA);
}
});
2) If you want there to possibly be no checked button, don't default to anything.
I had this problem in my App .
I cheated . I created a new radio buttom and I selected gone its visibility .
after that , I put setcheked=(true).

Possible to make a View not touchable just like a Window in android?

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

Is it possible to add a button to a scroll view?

I have a simple ScrollView in a layout that displays an About Box in a dialog format. So it just pops up on the phone screen in a dialog. The only way for the user to close the box is to click the back button (it is an intent).
Do I have a way of adding a close button to the box or could I have the user hit the box on the screen with their finger and close it?
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:screenOrientation="portrait"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="#+id/about_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" />
</ScrollView>
The only way for the user to close the box is to click the back button
That's what any Android user would want to do. Anyway, if you want to provide the "close with button" functionality just keep in mind this: ScrollView cannot contain more than one item, so you will have to wrap both TextView and Button inside a LinearLayout or something.
But, my suggestion is that you should build an AlertDialog, which can be automatically configured with an "OK" button which will close the dialog.

Categories

Resources