In my List View, I want to display multiple Icon. Icon1 for list row 1, Icon 2 for list row 2, Icon1 for list row 3.
Override getView in your ListAdapter and then you can provide whatever layout you want for each row.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
You want to use a different icon in each row correct? Well you can use an Integer array of resource IDs of all your pictures and put those in order for your row items. The typical way people do this is hard code the list and specify the R.drawable.iconName that is in your resources. like:
int[] mapPic = {
R.drawable.icon1,R.drawable.icon2,
R.drawable.icon3,R.drawable.icon4};
To me this is a crude solution, hard-coding is a big no-no. A more elegant way is to put the array into your xml file like string.xml or one you create for all your arrays. In your string.xml put in:
<array name="arrayIcon">
<item>#drawable/icon1</item>
<item>#drawable/icon2</item>
<item>#drawable/icon3</item>
<item>#drawable/icon4</item>
</array>
Then in your code when you first create your view before the setting content, add in:
TypedArray mapPic = getResources().obtainTypedArray(R.array.arrayIcon);
You can the access the object which is actually the drawable by:
Drawable drawable = typeArray.getDrawable(RowID);
In my code to set it in my view I had to grab the ViewWrapper and use getIcon() to set it.
ViewWrapper wrapper.getIcon().setImageDrawable(drawable);
And that is how you add your image resources you want to your list by using an array in the xml resources. Hope this helps.
Related
I get the list of elements from the HTTP response, then I want to dynamically insert that list into the textview inside the "box" that you can see, currently it just inserts a string and overlaps them one over the other. I tried changing the layout (all three constraint, relative and linear) and it didn't help. Does anyone know how to position them dynamicly inside the boxes and not overlap but have margins like in the second picture? Otherwise, inside the project, I use a constrain layout.
Here is my code:
RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.layout);
int size = response.toArray().length;
final TextView[] tv = new TextView[size];
TextView temp;
for (int i = 0; i < size; i++)
{
temp = new TextView(Activity.this);
temp.setText(response.get(i).getName());
parentLayout.addView(temp);
tv[i] = temp;
}
Here is the picture how it looks right now:
And here is the picture how I want it to looks like:
This sounds like a typical ListView use case.
Firstly, I'd suggest you go through the documentation -
https://developer.android.com/reference/android/widget/ListView
You can see an implementation example of a list view with an array of strings here -
https://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65
In general, you choose the UI of your item and the listView populates the view to each item in your list (each string in your case).
In the adapter, you give each item the data it needs for the UI.
I would suggest you to use RecyclerView for this type of task. You may use ListView as well.
But RecyclerView is more flexible and advanced than ListView.
Create a simple layout or xml file for your row item to be shown in RecyclerView.
Add that row xml file in onCreateViewHolder method. And inside method onBindViewHolder do necessary task like for example, showing name in the list for each position.
Go to this link for your reference : https://developer.android.com/guide/topics/ui/layout/recyclerview
Instead of Array<String> you can use Array<CustomModel> as well depending on your requirement.
Simple example of RecyclerView with model objects as list : https://www.javatpoint.com/android-recyclerview-list-example
Well, the proper way to do what you need is use ListView or RecyclerView.
Anyway, if you want to use your current solution, you need to specify the position of each TextView.
For example, assign an ID to each textview you create and then set the position of it under the previous one. Here you can find how to do that.
I'm using a ListActivity with an ArrayAdapter. I have an array of Strings, and I want to put a divider after the 5th String in the list. Is there any way to do this? Sorry if there are duplicates, just did not find what I was looking for. Thanks in advance.
There is a hack.
Add a divider view at last in your list_item's layout file and give it an Id. Now in your custom adapter class,you can set that view's visibility depending on the position whether you want it to be shown or not,in getView() method.
A property of Listview will help you:
android:divider = (color or drawable)
And also
android:dividerHeight = (integer in dp)
I have an android app which asks a question followed by x number of options.
Each option contains a textview, ImageView and a radio button.
The value of x (i.e. the number of options) is not constant. I want to dynamically add UI content to satisfy this requirement.
At the moment I have written the code in the layout's xml to display a maximum of 4 options. If number of options is 2 I hide the options 3 and 4 using something like
tvoption1.setVisibility(View.GONE);
tvoption2.setVisibility(View.GONE);
However this is not very scalable. Can anyone tell me how to add options for java dynamically. Or is there a better approach?
A View can be added at runtime by using the inflater like this:
LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.news_categories_item, null);
TextView categoryValueTextView = (TextView)linearLayout.findViewById(R.id.news_category_item_value);
mMainLinearLayout.addView(categoryValueTextView);
In this example, a LinearLayout containing a TextView is inflated. A reference to the constituent TextView is then obtained, and the TextView is dynamically added (at runtime) to the main linear layout (mMainLinearLayout).
The inflater object may be obtained in an Activity by using getLayoutInflater().
create your row layout separately, from the main xml
Get LayoutInflater service from context:
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATE_SERVICE);
use following method to addview to main xml, I assume you have parent layout llParent in xml and you want to add items in this llPaent, from list list.
for(int i=0;i<list.size();i++)
{
LinearLayout llView=(LinearLayout)inflater.inflate(R.layout.row);
//get view id and set values
TextView txt=(TextView)llView.findViewById(R.id.text);
}
A ListView is a good view for displaying several similar items. Here is a tutorial (Other views with adapters are good too, such as GridView or Gallery).
You will probably want to create your own adapter for the list, so you can display all three views (checkbox, image and text) as one item, but there are lots of examples on that available on the net as well as here on SO.
I want to use a GridView to display data that is separated alphabetically by row. Each row of the grid view should contain up to four names, and there should only be names starting with the same letter in a row. I know how to set up a GridView, just not how to set it up in this format. The screenshot below shows what I would like to do:
Make a layout file table.xml for the Grid or TableLayout. Make a layout file cell.xml for the cell. Now order names and put them into appropriate cells by inflating the cell.xml.
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
...
view = inflater.inflate(R.layout.cell, table, false);
tableRow.addChild(view);
view.setText(name);
I think, you'll manage table rows creation by yourself.
The simpliest way is to make static layout e.g. Linear oriented vertically with internal 26 grids (or just put grids in runtime)
-Scroll
----Linaear(vertical)
-------GridView(id=aGrid, w=match_parent, h=wrap_content)
-------GridView(id=bGrid, w=match_parent, h=wrap_content)
...
-------GridView(id=zGrid, w=match_parent, h=wrap_content)
Then, to bind data with UI you can declare Map gridViews and push data accordingly word's first letter.
I have a list that is populated by a listadapter that specifies a layout like so:
SimpleCursorAdapter trips =
new SimpleCursorAdapter(this, R.layout.it_menu_home_row, tripsCursor, from, to);
Inside of that layout is a textview. I would like to be able to dynamically change the width of that textview through code. I have tried just referencing the textview ID with findViewById, but when I try to manipulate it, I get a null pointer exception.
Is there a simple way reference the textview of a listadapters layout?
I am trying to reference the text view like I would if it was sitting in the base layout of the activity:
TextView tv = (TextView)findViewById(R.id.myTV);
I don't know if I have to somehow iterate through each of the list items and manually set the textView that way (and if I do I am not sure how), or if I just have to reference it once, etc
When you use SimpleCursorAdapter you provide the textFields via the constructor itself. In the snippet you have given, to should be an array of id's of the TextView's present.
For example if your layout file has a TextView with the id: "#+id/my_textfield" then you should pass in your constructor an array: int to[]=new int[]{R.id.my_textfield};