i followed the ApiDemo to create a gallery.
but i don't need the background of each item , so i marked these code:
public ImageAdapter(Context c) {
mContext = c;
// See res/values/attrs.xml for the <declare-styleable> that defines
// Gallery1.
**// TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
// mGalleryItemBackground = a.getResourceId(
// R.styleable.Gallery1_android_galleryItemBackground, 0);
// a.recycle();**
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(48, 48));
// The preferred Gallery item background
**// i.setBackgroundResource(mGalleryItemBackground);**
return i;
}
then , i met a problem :
the default first selected item( also the first item) is bright ,
but after a start to drag , and the selected item be changed , all items looked dark.....
i don't know how to set the alpha back , let the item looks bright.......could someone help me?
Try set the cacheColorHint of your ListView. IE with
<listView android:cacheColorHint="#android:color/transparent" ...
EDIT:
Do not comment out these lines, but set the android:background in the Gallery1_android_galleryItemBackground Style to #android:color/transparent or another color you want.
Related
I created a simple gallery following a tutorial.
Everything works fine but i wasn´t able change the border/background color.
Maybe a small image helps to understand my Problem.
desired colorchanges
I get a scaled image with a dark grey filled background. These rectangle is bordered in a light gray which I want to change.
I tried:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(imageBitmaps[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(300, 200));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setBackgroundResource(imageBackground);
**imageView.setBackgroundColor(Color.BLUE);**
...
but this doesn´t work because the dark grey part is painted in blue, too.
Any ideas?
Edit: adding some code snippets and wrong result
Using setBackgroundColor will lead to this result
My gallery creation:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGallery = (Gallery)findViewById(R.id.imgGallery);
mAdapter = new ImageAdapter(this);
mGallery.setAdapter(mAdapter);
But when
public View getView(int position, View convertView, ViewGroup parent)
is called
convertView is null.
Try something like this (I haven't tested though):
switch (position) {
case 1:
imageView.setBackgroundColor(convertView.getContext().getResources().getColor(R.color.blue));
break;
case 2:
break;
//... and so on
}
You could alse try this one imageView.setBackgroundColor(new ColorDrawable(Color.BLUE));
Let me know if this didn't help you!
EDIT:
Try with this and let me know if anything changes:
imageView.setBackgroundColor(Color.parseColor("#FF0000"));
Another option is, instead of instantiating that ImageView programatically, just build your own xml with your ImageView inside and later do an LayoutInflater.from(mContext).inflate(R.layout.your_image_view, parent, false);
Is it possible to set the backgroundResource and backgroundColor of a ListView/ExpandableListView in a custom adapter.
I have a transparent png that will serve as the border for each row in an expandable listview. This image is just an inner shadow and a border on the bottom.
The goal is to do something like this:
When I set the backgroundResource and then backgroundColor, only one of the two shows up. I can't get the resource to overlay the color. Does anyone know if this is possible?
Here is my code to get a better idea:
private int[] colors2= new int[] { Color.parseColor("#e2e8e9"), Color.parseColor("#f1f2f2") };
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder;
ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.expandlist_group_item, null);
holder.title = (TextView) convertView.findViewById(R.id.tvGroup);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
int colorPos = groupPosition % colors.length;
convertView.setBackgroundResource(R.drawable.row_forground);
convertView.setBackgroundColor(color2[colorPos]);
holder.title.setText(group.getName());
return convertView;
}
setBackgroundResource and setBackgroundColor both use the same api setBackgroundDrawable internally to do their tasks. So one overwrites the other. So you wont be able to achieve your goal using this api.
You will have to use setBackgroundResource with a custom drawable
If you want to use setBackgroundResource and setBackgroundColor you can do this:
...
int colorPos = groupPosition % colors.length;
convertView.setBackgroundResource(R.drawable.row_forground);
GradientDrawable drawable = (GradientDrawable) convertView.getBackground();
drawable.setColor(color2[colorPos]);
...
There is for sure code which can take background drawable and use paint some color on it. But I can't rember now :-)
But there is other ways how can you achieve your goal. Look at this site:
http://developer.android.com/guide/topics/resources/drawable-resource.html
Look at 9patch drawables which you can prepare, which will be only small images which will shrink/expand as needed. You prepare one with and other color inside.
The second way is to use ShapeDrawable. In XML you create rectangle and some solid color inside it.
In both cases you just swap background drawables as needed.
I don't understand what you want exactly achieve, but I hope that this can help.
Just add a ColorFilter after setting your background ressource to the view like this
view.setBackgroundResource(R.drawable.yourRessource);
view.getBackground().setColorFilter(
Color.yourColor,
PorterDuff.Mode.DST_OVER
);
Learn more about Manipulating images and Drawables with Android’s ColorFilter
How can I grey out one of my list item entirely, I dont mean set background to grey or sth. else.
public View getView(Context context)
{
View view = _inflater.inflate(R.layout.transfer_item, null);
view.setBackgroundColor(_unmoveable ? 0xFF111111 : Color.TRANSPARENT);
//view.setAlpha(0.5f);//? it says method did not exist ...
put a View into the row on top of everything else and set that View to your grey color.
In Android, by default when you long click on a list item, it goes from the highlight color to white indicating that the user has held it down and a context item will be displayed. Also, you can use the trackball (or arrow buttons on some phones) to select list items rather then using your fingers.
However, I have a ListView whose item's views I am calling setBackgroundColor on, and both of those expected behaviors no longer work. Anybody know why this is and how to fix it?
Notes: Setting the background color in the xml is not an option because I need to be able to set/change the color dynamically.
The code for my newView function is as follows:
#Override
public View newView(Context ctx, Cursor cursor, ViewGroup parent)
{
View view = new View(mCtx);
final LayoutInflater li = (LayoutInflater) mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.task_list_item, null);
return view;
}
By default, ListView has a Selector that will play a TransitionDrawable when you longpress a list item. However, if your list item view has a solid background, then you won't be able to see the selector's longpress animation (or on any other states) , because it's covered up by the list item's background.
If you want to see the selector's longpress animation/selected/pressed state, then the list item's has to have a transparent background when the item is selected/pressed/longpressed. You can do it by using a StateListDrawable as your item's background instead of a solid color.
Here is a sample of StateListDrawable for that purpose:
public class ColorfulListItemDrawable extends StateListDrawable {
private final PaintDrawable mColor;
public ColorfulListItemDrawable(int color) {
mColor = new PaintDrawable(color);
initialize();
}
private void initialize() {
Drawable color = mColor;
Drawable selected = new ColorDrawable(Color.TRANSPARENT);
addState(new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled,
android.R.attr.state_window_focused}, selected);
addState(new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled,
android.R.attr.state_window_focused,android.R.attr.state_selected}, selected);
addState(new int[] {android.R.attr.state_enabled,android.R.attr.state_window_focused, android.R.attr.state_selected}, selected);
addState(new int[] {}, color);
}
public void setColor(int color) {
mColor.getPaint().setColor(color);
mColor.invalidateSelf();
}
}
When you set the background color, you're overriding the default background, which seems to be a color animation. If you want a different color, you'll have to create the animation with the color you want.
You want to create a Selector that deals with the Selected state.
As you can't use an XML solution, you want to create a StateListDrawable programmatically and set it with ListView.setSelector.
This probably is happening because you are just setting single color to the layout.. where as by default it has something like stateList and it has different colors for different state (clicked, focussed, lingClicked) etc... you can create one and use it to set the backGround...
I have a one line solution if you're using ListView (it probably works on all views), in your ListView code, add android:drawSelectorOnTop="true".
For example:
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listID"
android:drawSelectorOnTop="true"
...
/>
I have a listview with a custom arrayadapter that handles about 15 strings. The style of each row alternates (between labels and values for those labels--for example row 1 could be "email address" and row 2 would be the actual email address). I'm changing the style of each row to alternate like this in the arrayadapter's getView() method. So if the item at the current position is a label, I'll change the styling from the default row style (which is what the values have applied to them). When the listview first loads, the styling is perfect and just how I want it to be. If I scroll the list slowly up or down, it stays that way. However, if I scroll the list fast up and down, the styling of the value rows starts changing to that of the label ones until all of the rows have the styling of a label row. Does anyone know why this would be happening? I've used custom adapters on other listviews in the app with no problems like this.
Edit: Found out that it also changes all of the rows to the label styling on portrait->landscape orientation changes. Doesn't do this on landscape->portrait changes. Below is the adapter I'm using. Am I missing something?
public class DetailsAdapter extends ArrayAdapter<String> {
private TextView text = null;
private String item = null;
public DetailsAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
text = (TextView) super.getView(position, convertView, parent);
item = getItem(position);
if (item.equals("Name") || item.equals("Mobile") || item.equals("Home") || item.equals("Email") || item.equals("Address")) {
text.setBackgroundColor(0xFF575757);
text.setTextSize(15);
text.setTypeface(null, Typeface.BOLD);
text.setPadding(8, 5, 0, 5);
} else {
text.setPadding(15, 15, 0, 15);
}
return text;
}
#Override
public boolean isEnabled(int position) {
item = getItem(position);
if (item.equals("Name") || item.equals("Mobile") || item.equals("Home") || item.equals("Email") || item.equals("Address")) {
return false;
} else {
return true;
}
}
}
Android reuses views fairly aggressively, and it is quite possible that a view that was used as an email address row gets reused on a row that's supposed to display a label, and vice-versa.
As a result, you cannot rely on "default" values. Set your padding, typeface, text size and background color in all cases:
if (item.equals("Name") || item.equals("Mobile") || item.equals("Home") || item.equals("Email") || item.equals("Address")) {
text.setBackgroundColor(0xFF575757);
text.setTextSize(15);
text.setTypeface(null, Typeface.BOLD);
text.setPadding(8, 5, 0, 5);
} else {
text.setBackgroundColor(DEFAULT_BACKGROUND);
text.setTextSize(DEFAULT_TEXT_SIZE);
text.setTypeface(null, DEFAULT_TYPEFACE);
text.setPadding(15, 15, 0, 15);
}
Don't need to do anything. I too faced the same problem and solved it like this:
Just inside the getView method add a first line
convertView=null;
It wont redraw the view immediately destroyed but instead would create new ones each time based on your logic (even odd or whatever)