I'm trying to create an Icon selector. The icon selector activity opens once I click on an icon in Main activity:
Intent intent_open_icon_picker = new Intent(Activity_Add_Edit_Trip.this, Activity_Icon_Picker.class);
intent_open_icon_picker.putExtra("SELECTED_ICON", current_icon_position);
startActivityForResult(intent_open_icon_picker, Constants.Request_Codes.REQUEST_CODE_SET_TRIP_ICON);
current_icon_position by default is 0, setting the first icon in the array to the ImageView.
Then in row_grid.xml I have this code, for each row in GridView (notice the background):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#drawable/grid_color_selector"
android:clickable="false"
android:focusable="false"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/image"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="fitCenter"
android:focusable="false"
android:clickable="false" >
</ImageView>
</LinearLayout>
And this is the #drawable/grid_color_selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/color_BLUE" android:state_pressed="true"/>
<item android:drawable="#color/color_BLUE" android:state_selected="true"/>
<item android:drawable="#color/color_WHITE"/>
</selector>
When the icon picker activity opens - I want automatically highlight (or mark as selected) the current icon (the one that I corresponds to current_icon_position that I pass in the Intent). So I tried a few things, but none seems to work. I tried to do this:
if(getIntent().getExtras()!=null)
{
curr_icon_position = getIntent().getExtras().getInt("SELECTED_ICON");
}
grid_view = (GridView) findViewById(R.id.gv_icons);
adapter = new GridView_Adapter(this, R.layout.row_grid, get_icons(), curr_icon_position);
grid_view.setAdapter(adapter);
**grid_view.setSelection(curr_icon_position);**
Then I tried to make a boolean flag in my Trip_Icon object and set it like that:
private ArrayList get_icons() {
final ArrayList arr_trip_icons = new ArrayList();
TypedArray imgs = getResources().obtainTypedArray(R.array.trip_icons_blac);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
imgs.getResourceId(i, -1));
Trip_Icon current_icon = new Trip_Icon(bitmap);
**if(i==curr_icon_position)
{
current_icon.setSelected(true);
}**
arr_trip_icons.add(current_icon);
}
imgs.recycle();
return arr_trip_icons;
}
and in my adapter do something like that:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
holder = new ViewHolder();
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layout_resource_id, parent, false);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
Trip_Icon icon = (Trip_Icon) data.get(position);
holder.image.setImageBitmap(icon.getImage());
**if(icon.isSelected())
{
row.setSelected(true);
}**
return row;
}
But as I said, I never see the icon highlighted.
Just to be clearer, this is what I want to do - if the current_icon_position is 0, then I want the first item in the GridView to be highlighted, like in the picture:
What am I doing wrong and how can I make a current icon be highlighted?
Eventually, after many, many tries and errors, looks like somehow I managed to solve this problem. Maybe this solution will be useful to somebody else:
First of all I added this line to my GridView:
android:choiceMode="singleChoice"
Then I added this line to my #drawable/grid_color_selector:
<item android:drawable="#color/color_BLUE" android:state_activated="true"/>
And finally in my icon picker activity I added this line, after the adapter was set to Grid_view:
grid_view.setItemChecked(curr_icon_position, true);
And curr_icon_position, as I stated in the question, is being passed as Intent extra.
Related
I would like to have a Listview in which the rows:
have rounded corners;
are partially colored (the left part of each row);
contain a shape in their colored area which is filled with another color.
Here is an example from another app:
I currently have two leads but I did not succeed with any of them.
My first lead (shape)
I found how to get rounded corners by using a shape and use it as a background for the Listview.
The drawable shape file "rounded_rectangle.xml":
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
The activity file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
// ...
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/splitView"
android:id="#+id/lv_player"
android:layout_alignParentTop="true"
android:background="#drawable/rounded_rectangle"/>
// ...
</FrameLayout>
However, I don't know how to use a shape to have a row partially colored or draw an icon. I can fill the shape with a color but then the rows are completely colored.
My second lead (vector)
I also found that I can use a vector as a background of my Listview. The advantage is that I can draw whatever I want using paths.
An example of a vector file:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<path
android:name="w"
android:fillColor="#000000"
android:pathData="m 290,103 l-200,0 c -50,0 -50,-100 0,-100 l200,0 z"
/>
</vector>
I can easily create a path for the colored area of my rows and another for the icon. However, now the problem is that I don't know how to scale the size of the vector to the size of the line.
Do you know what would be the best option and how can I obtain the expected result?
Thanks in advance.
Consider using a CardView, it will help you to get the rounded corners. Inside the CardView create a RelativeLayout in order to get the rest of the stuff you've mentioned above.
Here is a good tutorial : Using the CardView | CodePath Android Cliffnotes
First of all you need to create your fancy item separately like having xml layout contain the description of the custom item :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/optionName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_weight="10"
android:fontFamily="sans-serif-medium"
android:text="#string/option"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold" />
<CheckBox
android:id="#+id/activated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
</LinearLayout>
than create your adapter that will make your list have your layout as an item
public class ListOptionsAdapter extends BaseAdapter {
List<OptionsObject> Features = new ArrayList<OptionsObject>();
LayoutInflater inflater;
Context context;
public ListOptionsAdapter(List<OptionsObject> features, Context context) {
super();
Features = features;
inflater = LayoutInflater.from(context);
this.context = context;
}
#Override
public int getCount() {
return Features.size();
}
#Override
public OptionsObject getItem(int position) {
return Features.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_list_view, parent, false);
holder = new ViewHolder();
holder.optionName = (TextView) convertView.findViewById(R.id.optionName);
holder.isActivate = (CheckBox) convertView.findViewById(R.id.activated);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView optionName;
CheckBox isActivate;
}
}
after that as above i specified the name of the layout i will use as item in the method getView() and affect every element to the view holder
NB : in getView you have to specify the values for the elements that they will be displayed or leave them blank if u dont need
after that simply in my activity class i call my listview and my adapter
OptionList = (ListView) findViewById(R.id.optionList);
adapter = new ListOptionsAdapter(Features, getApplicationContext(), this);
OptionList.setAdapter(adapter);
In a fragment, I have a ListView that has a custom ParseQueryAdapter<T>. The problem may not have anything to do with Parse, although I'm not sure.
As I was testing my app, I noticed something very strange. When I would scroll down my ListView, all the visible ListView items would be drawn on top of the next ListView item as seen in the second image below.
The list initialized properly as such:
As you can see, in my list item layout, I have an ImageView (ParseImageView to be specific) and a TextView. The TextView now displays some notes (don't mind the ID user_name_text_view) and the ImageView displays a placeholder blank profile picture.
When I scrolled down, the list looked like:
Here's my list view layout named fragment_post_view_list_view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/post_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Here's my list item layout named list_item_post_view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.parse.ParseImageView
android:id="#+id/icon"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentLeft="true"
android:background="#drawable/com_facebook_profile_picture_blank_square" />
<TextView
android:id="#+id/user_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/link_blue" />
</RelativeLayout>
Here's my adapter named PostViewListViewAdapter:
public class PostViewListViewAdapter extends ParseQueryAdapter<Post> {
// call superclass with a query to populate list view
public PostViewListViewAdapter(Context context, final String[] postsObjectIds) {
super(context, new ParseQueryAdapter.QueryFactory<Post>(){
public ParseQuery<Post> create() {
ParseQuery<Post> query = Post.getQuery();
query.whereContainedIn("objectId", Arrays.asList(postsObjectIds));
return query;
}
});
}
// this is similar to getView method in an adapter
#Override
public View getItemView(Post post, View v, ViewGroup parent) {
if(v == null) {
v = View.inflate(getContext(), R.layout.list_item_post_view, null);
}
super.getItemView(post, v, parent);
TextView usernameTextView = (TextView) v.findViewById(R.id.user_name_text_view);
usernameTextView.setText(post.getNotes()); // some string
return v;
}
}
How can I fix this problem?
Is this an issue with XML or Java?
I was following the two tutorials from Parse and the example from the Parse docs:
MealSpotting
Parse Query Adapter
I set the adapter and ListView here:
View rootView = inflater.inflate(R.layout.fragment_post_view_list_view, container, false);
mPostsObjectIds = SOME_STRING[];
PostViewListViewAdapter adapter = new PostViewListViewAdapter(getActivity(), mPostsObjectIds);
ListView listView = (ListView) rootView.findViewById(R.id.post_list_view);
listView.setAdapter(adapter);
I've tried getting rid of the ParseImageView in my list item layout, but my TextViews still draw on top of each other when I scroll.
Edit:
I forgot to mention that the list items display on top of each other after an orientation change.
I tested this on my Galaxy S5 (Android version 4.4.2 and Parse 1.4.1).
In my Activity, I show the Fragment here (called PostViewListViewFragment):
getSupportFragmentManager().beginTransaction().add(android.R.id.content, new PostViewListViewFragment()).commit();
Try below layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/post_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" >
</ListView>
</RelativeLayout >
Make Sure your adapter like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.rowlayout, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
rowView.setTag(viewHolder);
}
// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names[position];
holder.text.setText(s);
return rowView;
}
}
PS:You should watch this Google IO video about Listview,and here is the slides.
First create a ViewHolder class
static class ViewHolder {
protected TextView usernameTextView;
}
Then change your getItemView method like below
public View getItemView (Post post, View convertView , ViewGroup parent)
{
ViewHolder viewHolder = null;
if (convertView == null)
{
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.list_item_post_view, null);
viewHolder = new ViewHolder();
viewHolder.usernameTextView = (TextView)convertView.findViewById(R.id.user_name_text_view);
convertView.setTag(viewHolder);
convertView.setTag(R.id.user_name_text_view, viewHolder.usernameTextView);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.usernameTextView.setText(post.getNotes()); // some string
return convertView;
}
The problem seems to be in your list item layout -
Change it to this -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.parse.ParseImageView
android:id="#+id/icon"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/user_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/link_blue" />
</RelativeLayout>
Probably you have extra background for each list item set that is causing such effect.
Alter and watch.
Hope this gives you idea!
Try changing your list view layout height to match_parent.
Credit to #VedPrakash for helping me fix this.
In case it helps anyone, I fixed the problem by replacing the fragment not adding it. So I changed this line from:
getSupportFragmentManager().beginTransaction().add(android.R.id.content, new PostViewListViewFragment()).commit();
to:
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new PostViewListViewFragment()).commit();
I am trying to set a custom selector to a ListView. It's working fine on newer devices but not working on lower version of devices.I want the ListView selected item to be stay highlighted.
Please help.
Thanks in advance.
ListView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewBell"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector_color" >
</ListView>
</LinearLayout>
list_selectror_color.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/list_selector" />
<stroke
android:dashWidth="2dp"
android:width="1dp"
android:color="#color/white" />
</shape>
I have also tried with selector but nothing happens
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_selector_color" android:state_pressed="true"/>
<item android:drawable="#drawable/list_selector_color" android:state_focused="true"/>
<item android:drawable="#drawable/list_selector_color" android:state_selected="true"/>
<item android:drawable="#drawable/list_selector_normal"/>
</selector>
Here is my Custom adapter getView Method
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_bell_items, parent,
false);
ImageView imageview = (ImageView) convertView
.findViewById(R.id.list_bell_image);
imageview.setImageResource(mDataImage[position]);
TextView textview = (TextView) convertView
.findViewById(R.id.txt_bell_title);
textview.setText(mDataText[position]);
return convertView;
}
I had similar problem. Sorry, cannot comment, so I post a possible answer.
Remove android:listSelector="#drawable/list_selector_color" property from your ListView declaration
In your R.layout.listview_bell_items specify your custom selector for a root layout. E.g., if root layout of your list item is RelativeLayout, try:
<RelativeLayout ... android:background="#drawable/listitem_selector">...
The same goes for any other type of Layout.
If this still still does not give you the result you want, provide more details.
Update
Ok, if nothing else helps, there's a temporary dirty workaround of your problem. I do not see why it would not work.
Introduce a selectedPos variable holding currently selected item.
private class MyAdapter extends .../*your base adapter*/ {
private static final int NOT_SELECTED = -1;
private int selectedPos = NOT_SELECTED;
// if called with the same position multiple lines it works as toggle
public void setSelection(int position) {
if (selectedPos == position) {
selectedPos = NOT_SELECTED;
} else {
selectedPos = position;
}
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == selectedPos) {
// your color for selected item
view.setBackgroundColor(Color.parseColor("#000000"));
} else {
// your color for non-selected item
view.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
return view;
}
}
Now, add the following code after you create and set ListView's adapter:
final MyAdapter adapter = new MyAdapter(...);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.setSelection(position);
}
});
That is what I found out:
To get the list selector working the are two approaches:
1)
You use the OnItemClickListener.
Then the list selector drawable/color will work as expected when set it on the list view.
Then you may use a TouchListener for getting ClickEvents of any child view instead of using a ClickListener.
2)
You have set ClickListener on any child of the row view.
In this case the list selector will not work when been set on the list view, so you have to set your list selector drawable/color as background of the row view.
I am trying to develop a wallpaper application. When the application launches, an activity shows the categories (an image and corresponding name on a textView) on a gridView. When I select a category, it navigates to a sub-category. In sub-category activity, it also shows subcategory image with its corresponding name on textVew.
Problem is in sub-category activity images are not loading. Sometimes it loads a image; when i press back-button, then select that sub-category again, it loads three mode image; pressing back button and selecting than sub-category causes loading another three images (now its total 10 images).
The images is cached on the disk properly.
Here is my configuration:
cacheDir = new File(Environment.getExternalStorageDirectory(),
"peakwallpapers/cache"); // --
// Create configuration for ImageLoader
config = new ImageLoaderConfiguration.Builder(this).enableLogging()
.discCache(new UnlimitedDiscCache(cacheDir))
.memoryCache(new UsingFreqLimitedMemoryCache(2000000))
.denyCacheImageMultipleSizesInMemory().threadPoolSize(10)
.threadPriority(Thread.MIN_PRIORITY + 2)
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.build(); // --
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.cacheInMemory().cacheOnDisc().build();
imageLoader.init(config);
Here is the getView() methood:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View rowView;
final ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
rowView = getLayoutInflater().inflate(
R.layout.item_sub_category_grid, null);
viewHolder.imageView = (ImageView) rowView
.findViewById(R.id.image_item_sub_category);
viewHolder.textView = (TextView) rowView
.findViewById(R.id.text_item_sub_cat_desc);
rowView.setTag(viewHolder);
} else {
rowView = (View) convertView;
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.textView.setText(wpSubCategories.get(position)
.getSubCategoryName());
imageLoader.displayImage(imageUrls[position], viewHolder.imageView,
options, new SimpleImageLoadingListener() {
#Override
public void onLoadingStarted() {
showLoading();
}
#Override
public void onLoadingComplete(Bitmap loadedImage) {
Animation anim = AnimationUtils.loadAnimation(
SubCategoryGridActivity.this,
R.anim.fade_in);
viewHolder.imageView.setAnimation(anim);
anim.start();
}
});
return rowView;
}
}
My Grid layout: sub_category_grid.xml :
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview_sub_category"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:horizontalSpacing="4dip"
android:numColumns="2"
android:verticalSpacing="4dip" />
and My item of grid: item_sub_category_grid.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame_l_item_sub_category_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#drawable/textlines" >
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_item_sub_category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/text_item_sub_cat_desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#drawable/cat_bg"
android:gravity="center_horizontal"
android:text="TextView"
android:textColorHighlight="#656565"
android:typeface="monospace" >
</TextView>
</FrameLayout>
N.B: It shows no error on LogCat.
First of all, I think your thread pool size is too big. Of course it depends on devices your app is targeted. But it will work really slow on not-modern devices. I think 5 is enough, but you should test it yourself. Vary thread pool size and thread priority to achieve best speed and performance.
Second: you have a logical mistake in your getView() method. Move
final ViewHolder viewHolder = new ViewHolder();
after
if (convertView == null) {
and you'll see the problem. Your vars viewHolder and holder logically overlaps each other.
UPD: Don't call ImageLoader.stop() in activity's onStop() if you do.
I am trying to add an image to my ListView to make it look more like a button. I would like the images to be a little smaller, maybe 60% of current. And the images to lign up nicely on the right in a column. Here is a screen of what I currently have:
and here is my list view xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:layout_width="match_parent"
android:drawableRight="#drawable/arrow_button"
>
</TextView>
any idea what I am doing incorrectly?
The ListView that contains this TextView is defined like this:
One note, the way I create and work with my Lists is with the ListAdapter, using code like this:
Question q = new Question ();
q.setQuestion( "This is a test question and there are more than one" );
questions.add(q);
adapter = new ArrayAdapter<Question>( this, R.layout.questions_list, questions);
setListAdapter(adapter);
Thanks!
Ahh. You are doing the correct thing using a compound drawable. Not sure if there is a better way to maybe have the spacing in your compound drawable expand, but I know this'll work.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
<View
android:layout_height="64dip"
android:layout_width="64dip"
android:background="#drawable/arrow_button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
Basically just pointing out using the align parent right and left. You may want to add some margins or padding to them. Also make sure to vertically center your elements.
With the comment and advice that Frank Sposaro gave, you will be able to position your views correctly.
For your next problem, I advice you to make your own adapter similar to this:
private class CustomAdapter extends ArrayAdapter<Question> {
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
super(context, R.layout.row);
mInflater = LayoutInflater.from(context);
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.mTextView);
holder.image = (ImageView) convertView.findViewById(R.id.mImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Fill the views in your row
holder.text.setText(questions.get(position).getText());
holder.image.setBackground... (questions.get(position).getImage()));
return convertView;
}
}
static class ViewHolder {
TextView text;
ImageView image;
}
In your onCreate:
ListView mListView = (ListView) findViewById(R.id.mListView);
mListView.setAdapter(new CustomAdapter(getApplicationContext(), questions));
Another example for a ListView with an Adapter can be found here