RelativeLayout onClick fails to get triggered when EditText is embedded inside - android

Expected Result
There is a search bar implemented using a EditText inside a RelativeLayout. For some reason, the query to be searched is not going to be inputted into the EditText direct, but instead, on clicking the search bar, a WebView is triggered and load a search page where the user can input their search query there.
Problem
Set android:onClick="selectCellSearch" to RelativeLayout, click on the EditText the selectCellSearch callback function given by onClick seems not to be triggered.
Source Code
layout.xml
<!-- Start of Search Bar Cell -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="87dp"
android:background="#212121"
android:clickable="true"
android:onClick="selectCellSearch"
android:tag="search" >
<EditText
android:id="#+id/edit_text_search_id"
android:layout_width="fill_parent"
android:layout_height="85dp"
android:layout_marginBottom="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="1dp"
android:hint="#string/edit_text_search_id"
android:textSize="28sp"
android:textStyle="normal"
android:enabled="false"
android:inputType="none" />
</RelativeLayout>
<!-- End of Search Bar Cell -->
Notes
The android:tag="search" is used for the URL generation of the search page to be loaded after clicking on the search bar, say, http://mywebsite.com/search.
Update
The selectCellSearch() is now posted here.
MainActivity.java
public void selectCellSearch(View view) {
final String tag = (String) view.getTag();
if (tag != null) {
buttonForWebViewSlideInAnimation.performClick();
mWebView.loadUrl(WEB_BASE_URI + tag);
}
}

First of all, it's not advisable use the xml attribute onClick to set the onClickListener. Use the setOnClickListener method to do that always.
Second, the EditTest occupy all the size of the relative layout and is "over the relative". This situation produces that the onClick of the RelativeLayout never called.
To receive the click you can:
Are you sure that the RelativeLayout is totally neccesary?
You can set the onClick on the EditText
Override the onInterceptTouchEvent or dispatchTouchEvent on your RelativeLayout. This methods will received the touch events (all, care about this!)

Try android:duplicateParentState="true" in EditText

Related

Disable response of root view in android

I've got a ListView with several rows. Every row consists of 7 TextViews which react to onClick() events.
This all works perfectly fine but when the user clicks on the margins of the row, where no TextView catches the onClick() event, the root view - a LinearLayout - get's highlighted.
This is normal behaviour of course but as nothing happens by clicking there I don't want the Linear Layout to be highlighted.
Is there any way of disabling this behaviour but keep catching the onClick() events on the TextViews?
(The onClick listener is set inside the adapters getView() method)
Here some extract of the xml file. As one can see I've tried some things but they don't work.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:longClickable="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false">
<TextView
android:id="#+id/listelement_weekoverview_tv_mo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#drawable/weeklylist_rndrectangle"
android:gravity="center_horizontal"
android:singleLine="false"
android:textColor="#color/appcolor_red_base" />
...
</LinearLayout>
unclicked version
clicked version
I've found a solution.
Simply overwrite the isEnabled (int position) method in your custom Adapter like this:
#Override
public boolean isEnabled (int position) {
return false;
}

How to detect swipe gesture AND button click on ListView

I have an ExpandableListView and I am using a SwipeDetector to know when a row of this list is swiped.
The implementation of this dectetor comes from this post.
Everything works fine.
Then, I have added a Button on each row and I want to be able to know:
When the button is cliked
When the row is swiped
This is the layout used for each (except for headers) row of the list:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="wrap_content" >
<Button
android:text="Button"
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Now, the problem is that the swipe gesture is no more detected because the Button catches the event. Since they are lots of threads about this subject I tried several things but nothing works (at least I did not manage to make it work). Here is the different things I tried:
Set the android:focusable property to false on the Button
Set the android:descendantFocusability property to blocksDescendants on the RelativeLayout
Follow this post and override the onInterceptTouchEvent
Last points:
I can't set the SwipeDetector directly on the Button instead of the ListView's item because in some cases there will be no button (or many).
I can't use android:clickable="false" on the Button because I need to implement the onClick method.

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.

Adding CheckBox to ListView row view makes the row not clickable

I am trying to create a ListView similar to the Gmail app, where each row has a CheckBox on the left to facilitate multiple selection and actions via the contextual action bar, while at the same time, allow clicking each row to transition to another activity.
Originally, before adding the CheckBox the rows would indicate their selection with a light blue background color from the Holo theme I'm using and call onListItemClick in my ListFragment when clicked and onItemLongClick in my OnItemLongClickListener when long clicked.
When I add the CheckBox to my layout .xml file for the row view, the background no longer changes color and I no longer receive click or long click events. If I add android:longClickable="true" to the top ViewGroup in my view .xml then I start getting long click events again. But android:clickable="true" does not get me click events. And neither allows the background blue selection indication to return. Only the CheckBox itself provides visual touch indication with a blue background when touched.
What do I need to do to get the normal visual selection indication as well as click events on the ListView rows with the CheckBox in the view?
Here's the relevant portion of my view .xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="true"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingRight="12dp"
android:paddingTop="5dp" >
...
</LinearLayout>
</LinearLayout>
I wrote a blog post on this subject awhile back that you may find helpful. The problem is, in fact, that adding focusable elements (like CheckBox or Button) disables the ability to click the overall list item and their focusability has to be disabled. You should not add any clickable flags to the main layout either.
While adding checkbox in CAB please make it focusable="false", this will solved the problem of only one time called onItemCheckedStateChanged.

How to use layout weight when you want to toggle one of 2 visible views

I have a LinearLayout that will have a cancel button and a progress bar, where the progress bar is 70% and the cancel button is 30%, like so:
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ProgressBar
android:id="#+id/uploadProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_weight=".7"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
<Button
android:id="#+id/uploadCancelButton"
style="#style/TitleBarButton"
android:layout_width="0dp"
android:layout_weight=".3"
android:layout_height="wrap_content"
android:text="#string/cancel_btn"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
This works fine, however I realized that actually I either want to show the progress bar or a text view, where the text view could be a small status message (if say the upload failed).
I tried putting a TextView in the the above LinearLayout and having its visibility set to "gone" by default and with the weight set the same as the progress bar. In the code I would only set either the progress bar to visible or the text view, and the other I would set to gone. However the android system appeared to contribute the invisible items weight to the total. I even tried using android:weightSum="1.0" in the LinearLayout xml attributes but that then my button was no longer visible as even though the text was gone, it took space.
ViewFlipper is what you are looking for.
It is very simple to use. You put the views you want to toggle inside the ViewFlipper exactly the same way like you would place them within a Layout inside XML. Then from code you call setDisplayedChild() on the ViewFlipper object containing your views. The parameter of this method is the index of the view that you want to be shown.

Categories

Resources