I am trying to set a RippleDrawable as background to a list view row.
When using view.setBackgroundResource(R.drawable.ripple) everything works fine.
When trying
view.setBackground(view.getContext().getDrawable(R.drawable.ripple))
it works but the ripple doesn't show when quickly touching the item (I also have an activated/checked state for the item), it appears only when I keep the view pressed.
The setBackgroundResource method in View class looks like this :
if (resid != 0 && resid == mBackgroundResource) {
return;
}
Drawable d = null;
if (resid != 0) {
d = mContext.getDrawable(resid);
}
setBackground(d);
mBackgroundResource = resid;
so basically the exact thing I am trying to do manually.
NOTE: I want to use setBackground method because I want to create the RippleDrawable programatically.
Does anybody have an idea why this is happening ?
Instead of setting the ripple on the row View, set it on the ListView using the listSelector attribute:
<ListView
...
android:listSelector="#drawable/ripple" />
Programmatically you can set it with mListView.setSelector(...).
call View.invalidate(); along with View.requestLayout();
the CompoundButton function setChecked(boolean) when used invalidates the View hence the View is asked to re-check itself and then re-drawn or layed out
Related
I have some Views in my Activity that I inflate and populate at runtime.
The View itself is a RelativeLayout to which I add at runtime several TextViews.
When the View is selected I change the color of the background (and that is easy).
Now I need to change the color of all the TextViews inside the View.
Please how do I do that?
Consider that I do not have a reference to the TextViews themselves, since they are created at runtime, nor do I know how many are in there.
Consider that I do not have a reference to the TextViews themselves
Since you "add at runtime several TextViews", you are certainly welcome to "have a reference to the TextViews themselves". Add them to some sort of collection (e.g., ArrayList<TextView>) at the time when you "add at runtime", then iterate over the collection to change their color.
You can do this another way:
Iterate over all children of the RelativeLayout (which you have a reference to):
for (int i = 0; i < mRelativeLayout.getChildCount(); i++) {
if (mRelativeLayout.getChildAt(i) instanceof TextView) {
// Set text color
((TextView)(rl.getChildAt(i))).setTextColor(Color.RED);
}
}
Be careful about mRelativeLayout.get(i) instanceof TextView. This will return true for all Views returned by mRelativeLayout.getChildAt(i) that are subclasses of TextView. For example, if you have a Button inside mRelativeLayout, instanceof will return true since Button extends TextView. To avoid this, use:
if (mRelativeLayout.getChildAt(i) instanceof TextView &&
!(mRelativeLayout.getChildAt(i) instanceof Button))
I would like to do the same effect with linearlayout like when you call showDialog. It is easy to disable all components of linear layout, but how can I change the color?
Is it possible to throw some shadow on layout?
Thank you for answer.
Thanks to Asok advice I found this:
Set Alpha/Opacity of Layout
It is working correct for me.
The way how I setClickable to ALL child:
TraverseChildren(GetChildren(_llRest), false);
where
private void TraverseChildren(ArrayList<View> childrenList, boolean b) {
for (View view : childrenList) {
view.setClickable(b);
view.setEnabled(b);
if (view instanceof ViewGroup)
TraverseChildren(GetChildren((ViewGroup)view), b);
}
}
private ArrayList<View> GetChildren(ViewGroup view) {
ArrayList<View> children = new ArrayList<View>();
for (int i = 0; i < view.getChildCount(); i++)
if (view.getChildAt(i) != null)
children.add(view.getChildAt(i));
return children;
}
I don't think I understood well what you want to do. If you just want to change the color of the layout you can do it in the XML declaration using the android:background attribute or programmatically using the method setBackground or setBackgroundResource on your layout object. If you want to make the activity that you are going to display look somewhat like a dialog (with a translucent background and such) the easiest way is to leave everything on that layout like you would normally do and apply a theme to the activity on the manifest:
<activity android:theme="#android:style/Theme.Dialog">
You can setAlpha to the parent to apply a transparency to all of the Views children.
Here is an example of the approach I took when I created a View to overlay my primary layout:
RelativeLayout mainRelativeLayout = (RelativeLayout)findViewById(R.id.mainlayout);
mainRelativeLayout.setAlpha((float).45);
My mainRelativeLayout, in this case, contains many ImageViews and TextViews which all inherited the transparency. Which gave me the effect of having a shadow.
setAlpaha of the View class is only supported for API 11+ or Platform Version / Android OS Ver 3.0+
I have an ImageView in my layout for the individual items of a list.
The ImageView's src is an XML file in the drawable folder that defines which images to use during the various states of pressing an item.
However, I've noticed when you click the list row (and not the ImageView itself) the selector assigned to the ImageView is activated. It doesn't actually hit the ImageView's onClick code, but the image toggles as if it has been clicked.
This is actually a desirable effect in some cases, but in this specific case it is not. Is there a way I can stop this from happening?
set android:duplicateParentState in child to true
I think you should read Cyril Mottier's blog. He has a post about this here.
In a word, you have to extends your Child views to Override the method setPressed(boolean) like this:
#Override
public void setPressed(boolean pressed) {
if (pressed && getParent() instanceof View && ((View) getParent()).isPressed()) {
return;
}
super.setPressed(pressed);
}
My probel is that images in my Gallery are bleeding in into each other once I begin scrolling towards the next image.
I am using a android.widget.Gallery connected to a custom adapter I extended from BaseAdapter.
The adapter's GetView() method is like this
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
if (mImageBitmap != null && position < mImageBitmap.length)
i.setImageBitmap(mImageBitmap[position]);
return i;
}
Did you try using android:spacing ( in xml) or setSpacing(int spacing) (in code) on the Gallery?
I actually found the solution to my problem. In getView(), the ImageView I was returning had no background and thus the ImageViews would overlap. I set the background of the ImageView to black before returning it and it looks great
It seems the fading is added automatically and disabling through XML doesn't work.
However disabling programatically seems to work:
Gallery carousel = (Gallery)findViewById(R.id.image_carousel);
carousel.setHorizontalFadingEdgeEnabled(false);
If you have access to the Gallery instance, you can also call the following method to remove the bleed in area:
gallery.setUnselectedAlpha(1.0f);
This also removes the white-ish haziness of the Gallery view.
I'm trying to restore the background Color of a View.
I have several selectable Views. When the user clicks one of those Views, the following code is executed and the View becomes Yellow:
View newSelection, previousSelection;
...
if(previousSelection != null) {
previousSelection.setBackgroundColor(Color.BLACK); // problem here
}
newSelection.setBackgroundColor(Color.YELLOW);
However, I want to reset the color of the previously selected View. However, I do not know which color it was (I'm setting it to Color.BLACK in the above code). I was not able to find a getBackgroundColor or similar method in the View class. If I had it, I could save the previous color and just put it back when the new View is selected.
use View.getBackground(), it returns the current 'Drawable' background of the view which can then be used in View.setBackgroundDrawable()
View theView;
Drawable originalBackground;
...
originalBackground = theView.getBackground();
theView.setBackgroundColor(Color.YELLOW);
...
theView.setBackgroundDrawable(originalBackground);
I'm not sure exactly what you are trying to accomplish but perhaps a ColorStateList would come in handy here.
You can try setting the previous color as a tag of the view.
For example
View newSelection, previousSelection;
newSelection.setTag(Color.Green);
previousSelection.setTag(Color.Black);
if(previousSelection != null) {
previousSelection.setBackgroundColor((int)previousSelection.getTag());
}
newSelection.setBackgroundColor(Color.YELLOW);
I haven't tried the code if there is an error but the flow on how to implement is there.