How can I generate the following layout in an Android list view ?
I need the background of a divider to be a gradient stretching across different items. Is this possible?
If you want to achieve this just extend an adapter, like BaseAdapter and do something like this:
int[] drawables = {R.drawable.back_one, R.drawable.back_two, R.drawable.back_three};
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
.....check if view is null otherwise inflate a new layout.....
if(drawables.length < 3) {
//You can assign one of the backgrounds
view.setBackdroundResource(drawables[i]);
}
}
Related
I have a problem with making negative margin for view of item which belongs to listview.
My items look like this:
I would like to lift second item and third and so on. The final effect should look like this
I tried with setTop method.
private class MyCustomAdapter extends ArrayAdapter<Integer> {
public MyCustomAdapter()
{
super(ListOfBeaconPlaces.this, R.layout.list_of_places_item_view, drawables);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View takenView = convertView;
if (takenView == null)
{
takenView = getLayoutInflater().inflate(R.layout.list_of_places_item_view, null, false);
}
final CuttedImageView cuttedImage = (CuttedImageView) takenView.findViewById(R.id.list_item_view);
cuttedImage.setImageResource(drawables.get(position));
if (cuttedImage.getHeightOfDrawnTransparentTriangle() != 0)
{
cuttedImage.setTop( (int)-cuttedImage.getHeightOfDrawnTransparentTriangle());
}
return takenView;
}
}
CuttedImageView is my custom class which inheriting from ImageView
Height of this triangle is knew after I first draw it on layout, but it's not a problem. I try to play with LayoutParameters, but it's also doesn't work for my.
Is there any posibility to make whole item margin negative?
I saw question, which talk about setting margin for item in linearlayout or relative layout or inside the item of listview, but I can't find nothing aboout changing margin of whole item.
In your xml file, in your listview set the divider height to a negative number to get an overlaping effect.
like this
android:dividerHeight="-10.0dp"
i like to implement a layout for a listview like this:
Any idea how i can implement this?
Thanks
You can do it in a couple of ways, but I think the most convenient way is to create two 9-patch background images so that you can define which sides should scale in what way. (Check here for information about 9-patch)
Make sure that the background image has a transparent part, where you want the one list item to overlap with the other, and set the layout_marginTop of the second item negative on an amount that gives you the desired effect.
This Approach must Help,Using two Different View and Displaying One at one Time based on Position
To implement listView elements as the screenshot, you need to have a CustomListViewAdapter (extends ArrayAdapter ) and in your CustomListViewAdapter class you need to specify in the method getView() the background to use in each element as below:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
if ((position % 2) == 0) {
rowView.setBackgroundResource(R.drawable.trapeze_down);
} else {
rowView.setBackgroundResource(R.drawable.trapeze_up);
}
rowView.refreshDrawableState();
return rowView;
}
Note:
* rowLayout represent the layout of an element in the listView
* trapeze_down and trapeze_up are two png images representing the trapeze form you want to have in background
* when you create your list view, don't forget to specify :
android:divider="#null"
android:dividerHeight="0dp"
*And finally you just set the adapter to your listView
I have a ListView and posibility to select one element (single choice).
How can I set background color for all elements of ListView (maybe which are visible at least) when some item was selected?
adapter = new ArrayAdapter<Orderline>(activity, simple_list_item_single_choice, orderlines) {
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
...
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
convertView.setBackgroundColor(BLACK);
// so here currently selected element is set to BLACK, but also other elements have to be set to WHITE
}
});
return convertView;
}
}
I don't have much code for you because I'm not at my workstation right now, but I'm thinking you can just set the background of the selected item to black by via your onItemClick as you've already suggested. Cool.
To change the color of the other(unselected) views when a particular view is selected, I'm guessing you can call your Adapter's getCount() and loop through that list, make a call to getChildAt(i) of your ListView. This returns a View which you can call setBackgroundColor(Color) on. Hope this helps
You can do
parent.setBackgroundColor(BLACK);
this.notifyDataSetChanged();
to set your list view background.
I would use a Drawable selector.
Check this link for a pretty good example:
http://www.charlesharley.com/2012/programming/custom-drawable-states-in-android/
Basically you create the XML drawable which contains a mapping (which the system uses automatically) to what you would like displayed when certain click events occur.
You can assign the background drawable to any view, so you could do it in your XML for your adapter, or in Java code. Which in your case might be something like this:
convertView.setBackgroundDrawable(R.drawable.MySelector);
parent.setBackgroundColor(while_color);
v.setBackgroundColor(black_color)
I want to make ListView with images. Something like this.
But some of items have text only and don't have images. For those items I want to show text only.
How can I do that?
Each item in a list view is a separate view, which can be created as needed. Simply inflate a view from two separate layouts in GetView method of your adapter class.
Something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return new MyViewItem(getImage(), getText(), parent);
}
*****
class MyViewItem extends LinearLayout
{
public MyViewItem(ImageClass image, String text, ViewGroup parent) {
super(parent);
View.inflate(parent, image==null?R.layout.layout1:R.layout.layout2, this);
//Now assign correct text and image using this.findViewById() function.
}
}
I leave it to you to define layout1 and layout2 as well as how to handle actual image and text, but this should do the trick.
You could also have 1 view, but just set visiblity to GONE on the image when you dont want the image to be there. As long as you've set up your layout appropriately, the text should pop over to the edge again just fine!
I am using a ListView to display the main screen of my application.
The main screen is essentially a menu to get into the different sections of application. Currently, I have the ListView whose contents are added programmatically in the onCreate method.
Here is the code snippet that does this:
String[] mainItems = {
"Inbox", "Projects", "Contexts", "Next Actions"
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, mainItems));
registerForContextMenu(getListView());
}
So the menu is essentially just a bunch of nodes with the text contained in the mainItems array. I know that I can create an XML layout (i.e. R.layout.mainMenu_item) that has an ImageView and TextView in it, but I am unsure how to set the ImageView's icon. I have seen that there is a setImageResouce(int resId) method, but the way to use this when generating with an ArrayAdapter is eluding me. Is there a better way to do this?
What I typically do for a ListView is to implement my own Adapter by extending the handy BaseAdapter class. One of the abstract methods you'll implement will be getView() as the previous poster mentioned. From there you can inflate a layout containing an ImageView, get a reference to it using findViewById, and set the image to whatever drawable you've added into your resources.
public View getView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.menu_row, null);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
icon.setImageResource(..your drawable's id...);
return view;
}
From the google docs for ArrayAdapter.
To use something other than TextViews
for the array display, for instance,
ImageViews, or to have some of data
besides toString() results fill the
views, override getView(int, View,
ViewGroup) to return the type of view
you want.