How to make child views get "onpress" event from root view? - android

I'm used to the fact that items of listview without any problem (even with complicated layout) handle onpress events (some actions that makes it pressed and unpressed). But now I want the same from LinearLayout - if you press anyway on layout, all descendants should get onpress events.
This is the part of my fragment layout, which should be highlighted onpress event.
<LinearLayout
android:id="#+id/ll_about_fragment_feedback"
android:gravity="center_vertical"
android:layout_height="54dp"
android:background="#drawable/about_item_bkg"
android:layout_width="match_parent">
<ImageView
android:layout_height="wrap_content"
android:background="#drawable/about_item_feedback_icon"
android:layout_width="wrap_content"
android:layout_marginLeft="11dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/about_fragment_feedback"
android:layout_marginLeft="14dp"
android:textSize="16dp"
android:textColor="#drawable/about_item_textcolor"/>
</LinearLayout>
Background of root layout changes onpress, but textcolor of descendants don't. Looks like they don't get event from root.
So, can you help?
UPDATE: LinearLayout ll_about_fragment_feedback has bound OnClickListener and there is everything fine with selecrors

If you want a Selector to apply to all views under your LinearLayout then you just need to add android:duplicateParentState="true" to all of the child views.
<LinearLayout
android:id="#+id/ll_about_fragment_feedback"
android:layout_width="match_parent"
android:layout_height="54dp"
android:background="#drawable/about_item_bkg"
android:gravity="center_vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp"
android:background="#drawable/about_item_feedback_icon"
android:duplicateParentState="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:duplicateParentState="true"
android:text="#string/about_fragment_feedback"
android:textColor="#drawable/about_item_textcolor"
android:textSize="16dp" />
</LinearLayout>

Just bind your onClickListener to root object of your layout (ll_about_fragment_feedback) and that's it.

Related

LinearLayout selector doesn't cover the whole View's children

The following is my XML code, I need to make a selector for the LinearLayout, but when I tap on the child TextView it doesn't show the selector backgroud as tapping other areas of the LinearLayout!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:clickable="true"
android:focusable="true"
android:descendantFocusability="blocksDescendants"
android:background="?android:attr/selectableItemBackground">
<ImageView android:paddingLeft="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/chart"/>
<TextView android:paddingLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/tv_title" />
<ImageView android:paddingRight="15dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_star_border_black_48dp"
android:id="#+id/iv_bookmark" />
</LinearLayout>
Try this:
<TextView android:paddingLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/tv_title"
android:clickable="false"
android:focusable="false" />
I think the reason for the error is because descendantFocusability is only for focusing state. It does not affect touch events. Since the TextView is still clickable it can still consume the touch event, and thus keep the parent from consuming the click.
Edit:
Try setting android:descendantFocusability to beforeDescendants instead of blocksDescendants.
Edit 2:
Changing the parent to android:addStatesFromChildren="true" and removing the clickable and focusable from the child TextView seemed to do the trick. This works because according to the ViewGroup android docs:
addStatesFromChildren: Sets whether this ViewGroup's drawable states also include its children's drawable states.
So when the child will set its state it also sets its parents state.
Try setting android:focusable and android:focusableInTouchMode to false in your TextView.

Android - How to make a visible element push other elements

I got a RelativeLayout:
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/auto_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_marginBottom="5sp"
android:layout_marginTop="5sp"
android:layout_toLeftOf="#+id/ButtonBackspace"
android:completionHint="#string/a_string"
android:completionThreshold="1"
android:gravity="center"
android:hint="#string/a_string"
android:textAppearance="#android:style/TextAppearance.Large" />
<ImageButton
android:id="#+id/ButtonBackspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/myButton"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#android:color/white"
android:soundEffectsEnabled="true"
android:src="#drawable/dialer_btn_bks" />
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="New"
android:visibility="gone" />
</RelativeLayout>
The visibility of myButton starts as Gone. I was hoping that when I change the visibility of the button in run time to Visible, it would push the ImageButton and AutoCompleteTextView to the left but both the button and imageButton appear on top of each other since they both got android:layout_alignParentRight="true". I tried adding the android:layout_toLeftOf="#+id/myButton" to the ImageButton but that did not work.
How can I make the button push the other elemets when it become visible and have its own space?
if these elements are just next to each other, you can use a linear layout and set the gravity to right. the linear layout automatically pushes the elements.
in relative layout all elements are "relative" to another element or the parent view, but just to one element for each direction. and if this object is "gone" the relation in this direction also is gone.

Android - Background reset after show/hide views with animation

I am showing and hiding views on a particular actions using some animation, I used LayoutTransition and it seems to work fine except one thing, while the animation is going, the background of some TextViews changes and reset to the default white background!!
Following is the code I am using to apply animation after hide/show.
mRightTransitioner = new LayoutTransition();
mRightTransitioner.setDuration(1000);
vg_rightContainer.setLayoutTransition(mRightTransitioner);
Edited: even without animation i tried, same problem, the background resets once view's position changes
Edited: here how i declared the views in the XML
<LinearLayout
android:id="#+id/ll_req_reasonwrapper"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:baselineAligned="false"
android:gravity="right"
android:orientation="horizontal"
android:visibility="gone" >
<EditText
android:id="#+id/et_req_reasons"
android:layout_width="400dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:gravity="right|center_vertical"
android:inputType="text"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_preasons" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_req_timewrapper"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:baselineAligned="false"
android:gravity="right"
android:orientation="horizontal"
android:visibility="gone" >
<LinearLayout
android:id="#+id/ll_req_endtimewrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/ftv_req_endtime"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:freezesText="true"
android:gravity="right|center_vertical"
android:inputType="numberDecimal"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_endtime" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_req_starttimewrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="50dp" >
<TextView
android:id="#+id/ftv_req_starttime"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:freezesText="true"
android:gravity="right|center_vertical"
android:inputType="numberDecimal"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_starttime" />
</LinearLayout>
</LinearLayout>
for instance: if i hide 'll_req_reasonwrapper', then the LinearLayout 'll_req_timewrapper' moves up, then the backgrounds of the textViews like 'ftv_req_starttime' becomes white!!
Please help.
Edited Answer:
if your are using linear layout then if the child is not being displayed, it is not going to be laid out and not going to be the part of the measurements ( as it looks like ). I recommend to use the RelativeLayout.
You can try
android:background="#null"
for your buttons. if your are using some custom class for button then add
.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
for this specific views.
Need more code. The fact that disabling the LayoutTransition does not solve your issue, means that the animation is not the issue.
How do you reinstantiate child views or populate your view. Are you removing views with setVisibility(View.GONE) and then setting them back with setVisibility(View.VISIBLE)
OR
Are you removing childAt(i) and then dynamically adding custom views
I'm sorry I couldnt add a comment since I dont have the reputation for that but I think I am able to answer your question if you post more code.

How to handle onClick on a RelativeLayout that is part of HorizontalScrollView

I have RelativeLayouts in HorizontalScrollView. When a user clicks on one of the layouts (or any of its childs) I need to change the background color of the certain RelativeLayout and get its ID for further process. I saw this might be done with DuplicateParentState or with onTouchEvent. Which way is recommended?
My XML file after editing the XML file as proposed:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<youme.appyoume.com.ExDialog>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:clickable="true"
android:duplicateParentState="true"
android:onClick="onClick"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:duplicateParentState="true"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="textMultiLine"
android:text="text" >
<requestFocus />
</EditText>
</youme.appyoume.com.ExDialog>
</RelativeLayout>
Thanks,
Simon
If you want to use the custom class youve created in your xml you just use the entire root name of the object. So for instance if you have a class in
com.your.package.customButton
you would write in your xml
<RelativeLayout
...layoutstuff config>
<com.your.package.customButton
...customButton layout config/>
</RelativeLayout>
if your subclassing a relativelayout you would use it like this
<youme.appyoume.com.ExDialog
...layout setup stuff>
<Button
...button setup stuff
/>
</youme.appyoume.com.ExDialog>
You could always assign a tag to it for future reference using setTag and getTag and just use setOn Click Listener and put what ever you need to happen in there

Recreating CheckedTextView's Functionality

I'd like to recreate the CheckedTextView's functionality using my own custom views so I can have two TextViews on the left with a CheckBox on the right, centered vertically between the two TextViews. I have the Layout working for it, which I will include below. I also have it so that when you click on the outer LinearLayout (LinearLayout1) it will pass that click to the checkbox. The only thing that I can't figure out is when you press down on a checkbox it briefly highlights the checkbox (in yellow on my device) before marking it checked. I'd like to have it do the same if you touch anywhere on the outer LinearLayout, but I don't know where I'd need to hook in to make that happen.
Here is my layout.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/LinearLayout1">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:id="#+id/LinearLayout2">
<TextView
android:id="#+id/FieldValueTextView"
android:text="Value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/FieldLabelTextView"
android:text="Label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<CheckBox
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/LinearLayout2"
android:id="#+id/CheckBox"
android:gravity="center_vertical" />
</LinearLayout>
Thanks,
Dan
The problem I was experiencing was solved by putting the following android:focusable="false" on the CheckBox and the two TextViews so that when the ListView is clicked, it gets the focus, not the inner views.
Hope that helps someone else.

Categories

Resources