Placing multiple ImageButtons in List View in Android Activity - android

I am new to Android and working on an app that needs to make use of a ListView such that each row of the list view has 3 clickable images. There could be several rows but each row must have 3 ImageButtons. Each ImageButton will have an onClick event which will be used to determine which image was selected by the user. The image below depicts what I need:
I am creating the ListView and the ImageButtons on-the-fly and adding them to my activity's layout. I have named all images like bigavatar1, bigavatar2, bigavatar3 (42 in total) and so no, to be able to use them in a loop.
In the OnCreate function of my activity, this is what I have been able to come up with so far:
LinearLayout mainScreen = (LinearLayout) findViewById(R.id.mainAvatarScreen);
mainScreen.setOrientation(LinearLayout.VERTICAL);
String PACKAGE_NAME = getApplicationContext().getPackageName();
int[] imgId = new int[42];
String fnm = "bigavatar"; // this is image file name
ListView avatarListView = (ListView) findViewById(R.id.membersListView);
for(int i=0; i<42; i++) {
imgId[i] = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+fnm+(i+1) , null, null);
//ImageView img = new ImageView(this);
ImageButton img = new ImageButton(this); //create new ImageButton
//Set ImageButton's Properti es
img.setMaxHeight(64);
img.setMaxWidth(64);
img.setImageBitmap(BitmapFactory.decodeResource(getResources(),imgId[i])); //src
img.setBackground(getResources().getDrawable(R.drawable.arrow_imagebutton_selector));
mainScreen.addView(img);
}
Obviously, the above code is incomplete and is able to only create the images but not make use of the ListView yet.
I need help at this stage. How should I:
Add the ImageButtons to the ListView
Would it be a good idea to create a LinearLayout for each row, set it's orientation to Horizontal to organize my buttons neatly within each row?
Will I have to create a custom adapter for the ListView? I have tried adding Strings from an ArrayList but the requirement here is to add a set of 3 ImageButtons to each row of the ListView.
I need an onClick event on each ImageButton that will return be the name/id of the button clicked. This will not create a conflict with the ListView and will work as intended?
It would be great if someone can help me with the code and the strategy here. I am not very experienced so pardon me if I am using or suggesting incorrect designing strategies, I want to learn and get this activity ready asap. Please do explain your answers. Thanks :)

Related

How to dynamic insert elements of a string array into textview and position it?

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.

How to create List of CardViews with list?

Please tell me how it is possible to make a list of lists. I tried to use Expandable listView but, unfortunately,I failed to create separate each group in the form of cards. Please tell me how it is possible to do both on the attached photo.
If number of cards and items isn't too big you can try something like this (creating 5 cards with 5 items):
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
for(int i=0;i<5;i++){
LinearLayout card = getLayoutInflater().inflate(R.layout.card, null);
for(int i=0;i<5;i++){
View item = getLayoutInflater().inflate(R.layout.cardItem, null);
card.addView(item);
}
mainLayout.addView(card);
}
Offcourse you will need to create xml files (cardItem.xml, card.xml)

How to show a list of items with 2 fields?

I'm developing my first app and I want to show a grid in which there is a list of entries. I get those entries through queries on local SQLite database so this it is a dynamic list. Every item of this list should have 2 field: a string and a value.
How to correctly do it in an activity?
I see ListView but it doesn't seem to suit my needs and I do not need clickable items.
Can you suggest a better solution?
I would still use a ListView.
They don't have to be clickable, you can customise the view to have 2 columns (or to be whatever you want) and they have great performance benefits with view recycling, etc during scrolling.
Essentially you need to return a custom view as the row - this row view will have data aligned horizontally and so you can get columns.
Here is an example.
Create programatically or in XML LinearLayout with vertical orientation:
LinearLayout layout = findViewById(r.id.layout);
layout.setOrientation(LinearLayout.VERTICAL);
Then create Textview for every item with text consisting of string and value of item and add them to the layout:
for (int i=0; i<items.size; i++) {
TextView tv = new TextView(context);
tv.setText(items[i].stringName + ": " + items[i].value);
layout.addView(tv);
}

How to add blocks of linearLayouts and is there a better way?

This is the main idea of what I am trying to do.
I would like to ask you what is the best way to make such a design. the problem is that these gray/black blocks might not show up (user will choose which should show up). So I would like to find out do I need to make these 3 text views inside linearLayout programatically? Is there anyway to create some sort of template which I would only have to edit by setting new texts for textViews and add them to some sort of layout?
Another option is to just have a LinearLayout and an xml for the row entry. Then you can do:
LinearLayout layout = (LinearLayout) this.findViewById(R.id.your_layout_id);
List<Blocks> userBlocks = getMyUserBlocks();
for(Block b : userBlocks) {
View blockView = LayoutInflater.from(getBaseContext())
.inflate(R.layout.your_row_layout, layout, false);
TextView someData = (TextView) blockView.findViewById(R.id.your_text_view_id);
someData.setText(b.someAttribute.toString());
layout.addView(blockView);
}
You will need a separate xml layout for the block row.
You can use a ListView with large dividers. Your dark gray blocks are the row views, and you can just append them to a dataset and update the adapter for the ListView. For the dividers, see setDivider() and setDividerHeight().

How to populate the same layout inside a liner layout (Android)

I need to create a list. The list will contain similar objects. each object will have some images, text and buttons. I can put those items in a table layout and arrange them (child). But i need to have a list of these(childs) items. And i need to populate them by dynamically. So i just cant code them in an XML at the beginning.
How can i do it. Can you post some tutorials or samples.
Thank you
This helped me a lot in designing custom views as part of a listview -
http://saigeethamn.blogspot.in/2010/04/custom-listview-android-developer.html
EDIT -
never tried may be where this is -
new String[] {"pen","price","contactid"},
new int[] {R.id.text1,R.id.text2}
replace text by imageviews in xml file
and then where this is
HashMap<String,String> tempnew = new HashMap<String,String>();
temp.put("pen",cur.getString(1).replace("+", " "));
repalce it with
HashMap<String,Image> tempnew = new HashMap<String,Image>();
temp.put("pen",Image1);
Image1 will be your image
Is this what you meant?

Categories

Resources