How to trigger selectors from the layout view? - android

The problem
Suppose you have a view inside a layout view, and you wish that each touching event on the layout would affect the view's background selector.
for example, when you touch the layout , the selector of the view will choose the state_pressed state.
What I've tried
I've read about duplicateParentState and addStatesFromChildren , but I think it's the opposite of what I'm searching for.
I've also tried to use splitMotionEvents, but it didn't help.
The question
How do you do this?
Also, what should be done in case the layout is inside a listView (as an item within it) ?

Consider you have a spinner inside a linear layout. Make focusable property of view(here that is spinner) to false. And onClick of linear layout call performClick on view(spinner in this case).
EDIT:
In focus Change Listener of linear layout if it has focus, call requestFocus on view.
You can follow same method to other states of view
Another method:
do not apply any selector to layout(linearlayout in this case) and add whatever selector you want to view(spinner in this case). For view add this parameter
android:duplicateParentState="true"

Related

Create a Dropdown to show more Views in Android

I am trying to code a layout, but I do not know well how to approach it.
Initially I have the following:
When the user click in this View, I need to show:
I thought I will need a Spinner, but the content that is shown when the user click is not a list but a set of Views (In this case, it will be a LinearLayout with a Spinner and a EditText).
I am a little lost, what would be the best approach to achieve this?
Do I need to implement a CustomView?
Sorry if it is a dummy question, but I can figure out how to code this Layout.
I think the best approach would be to make a LinearLayout in wich you can place you Spinner and your Search EditText and set this LinearLayout ViewGroup setVisible(View.INVISIBLE).
Then when you click on the "registra alimento" View set the LinearLayout below to setVisible(View.VISIBLE)
To fade it in:
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
viewToAnimate.startAnimation(in);
viewToAnimate.setVisibility(View.VISIBLE);
Take one Linear layout add Textview of Registrar alimento than set linear onClickListener with visibility of Spinner and edittext.(Also maintain flag for visible invisible )

ListView: intercept click event to adjust selector

I have a ListView with my own adapter, who uses a own layout.
It's basically a horizontal LinearLayout with an Image and a TextView
I have specified a selector for my ListView, which changes the background color.
Furthermore I want to apply a ImageColorFilter on the ImageView. If the background changes.
Is there a simple way to do it with the build in ListView selector behavior or must I attach my own OnTouchListener to my LinearLayout?
I guess there are two possibilities:
Disbale the ListView selector and write your own OnTouchListener and
handle the touch events.
Use android:drawSelectorOnTop="true" to draw the selector on top of your View instead of drawing in the background.

How to remove a custom view and rearrange linear layour

First of all I will explain my scenario.
I have an activity with a linear layout and I am inflating that linear layout with custom views. In the custom view I have a remove button to remove the particular child.
I know to remove the view I have to add an onClick Listener to the button in the custom view. But what I am not sure about is that how can I remove the view from its parent view from that onClick Listener.
I am populating the linear layout from items stored in shared preferences. So I thought first to update the shared preference by removing the string of the particular custom view I am clicking. But I don't know how I cn manage to re-populate the linear layout, since the function is in the parent activity.
I am a complete newbie in android. Thanks in advance
update
I'll make the scenario a little more simple.
I have a custom view with two imagebuttons - update & remove.
If i click in the remove button, then the custom view should be removed from the linearlayout.
And if I click in the update imagebutton, a function of the Activity which hosts the Linearlayout should be called with the text in the customview as a parameter.
How can i do this. I tried many ways, but failed :-(
I found the answer finally :
For the first requirement, ie., to remove the view, I used:
LinearLayout parentLyt = (LinearLayout) RecentSearch.this.getParent();
parentLyt.removeView(RecentSearch.this);
Thanks Very much to Piyush Gupta (#piyush)
For the second option, to access the hosting activity, I used getContext() and casted it to my activity class, then called the method.
Thanks every one who replied.

Add Dynamic button in android

I have a Linear Layout
and I want to add to this Linear Layout a button (from 3 Buttons) Dynamic in Run Time Depending on an integer value that returns from a function
I know how to add a view to a Layout using AddView , and how to remove views form layout using removeView ...
but my question is how to check if the view Exists in the layout before call the method reomveView
I suggest you create your buttons beforehand and just set the visibility to View.GONE
when you trigger some event you can set other buttons visibility to true while the others are false..
this will work with no problems and you dont have to add them dynamically.. it will just "seem" dynamic :)
You can do that with this code:
button1.setVisibility(View.GONE);
button2.setVisibility(View.VISIBLE);
Given the buttons are "button1" and "button2"
View.GONE will "remove" the view from the screen, however it is still "there" it just doesn't appear for the user, and it doesn't take up any space
View.INVISIBLE will "remove" the view from the screen, how ever the space it took up is still used by it.
View.VISIBLE will show the view as it would usually.

Android: Let Parent View Handle On Click

I have a RelativeLayout that contains a few items: An ImageView and a few small TextView's.
Functionally I want to have the same on click event fire when anything in the RelativeLayout is clicked.
Visually I want to see the background of the RelativeLayout change so that it shows the entire layout (or "item") is being clicked.
My problem is that every time I click on the TextView's the on click doesn't propagate back to the parent view and so the background color doesn't change. How can I do this?
Ensuring you got no OnClickListener assigned to any of the childs of your RelativeLayout shall usually suffice for them to not receive clicks. Also check if you got no android:clickable="true" set by any chance for it. Then once you assing OnClickListener to your RelativeLayout it should get all the clicks.
for some items that has internal OnClickListener and you cannot easily remove their implementation of OnClickListener like SwitchComat, you can set
android:descendantFocusability="blocksDescendants"
on your parent layout. by adding this attribute to the parent view, non of the children will receive click events regardless of having onClickListener or not.
more on descendantFocusability

Categories

Resources