How to create List of CardViews with list? - android

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)

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.

Is there an Android Layout that can be set to automatically wrap "exceding" elements to the next line?

In an Android app, I am trying to dynamically populate a TableLayout from an array, and I want each TableRow to hold at maximum 3 elements.
The way I had to implement it (code below), feels a lot like dynamically creating HTML tables and I was wondering if Android had more ad-hoc layout facilities that don't make me feel as if I am forcing my design idea over a more generic Layout like TableLayout.
Ideally, I would just like to deal with a layout that:
is aware of the number of elements I want per row (maybe via configuration?),
automatically stacks the TextViews horizontally, wrapping them on the next row only if they fill up the previous, so that I could simply cycle through the array elements and do myLayout.add(TextView).
Do we have something like that, or there's no other better way than handcrafting it?
In the latter case, how would you've done that?
TableLayout tab_lay = (TableLayout) viewRef.findViewById(R.id.myTableLayout);
TableRow table_row = new TableRow(contextRef);
int col_counter = 0;
for (TextView aTextView : arrayOfTextViews) {
table_row.addView(aTextView);
col_counter++;
if (col_counter == 3) {
tab_lay.addView(table_row);
table_row = new TableRow(contextRef);
col_counter = 0;
}
}

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);
}

Placing multiple ImageButtons in List View in Android Activity

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 :)

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