How to populate the same layout inside a liner layout (Android) - 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?

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 can I customize this listview just a little?

I am absolute beginner.
I have a simple listview which I don't know how its work and the only view code i fount of it is :
String[] from = new String[]{ TITLE };
int[] to = new int[]{ R.id.ViewTitleNotes };
thats it, I want to customize this listview a little like change the font or set a background for listview page, but i cant !
if I change the background of page, every single listview item will adopt that image and every single listview item height will fill entire page !
that "R.id.ViewTitleNotes" doesn't have any code like this : "TextView ViewTitleNotes = (textView) findViewById(R.id.ViewTitleNotes);"
I don't Know how its work !
here is a Screenshot of this listview: that small selection is the textview i want to change the font of it
Thanks in advance.
for change font of textView take a look at :
How to change fontFamily of TextView in Android
for setting image in background of listview look at this link:
http://www.android-examples.com/set-drawable-image-as-listview-background-in-android/
and link bellow for more complicated listView:
http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
also it is recommended to use recyclerview instead of listView.
for recyclerview , link bellow :
https://www.sitepoint.com/mastering-complex-lists-with-the-android-recyclerview/
http://javatechig.com/android/android-recyclerview-example

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

Android programming : how to programmatically create various view types in a grid fashion

I'm not sure how to ask this question exactly, but I'll give it a try.
Here's what I'm trying to do, in one Activity.
Build a Grid, that contains [x] rows of 3 columns each, with this content
[a TextView (containing a name)] [a Spinner (containing a list of states)] [an EditText]
How to start ? The Views I can create programmatically, that's not a problem, I even store them in 3 array lists for later easy reference, but I can't see how to do it right.
Should I create an xml layout with e.g. (and pseudocode)
LinearLayout (horizontal)
TextView ...
Spinner ...
EditText ...
/LinearLayout
and try to inflate it in the loop I use to create each row, and setting the id of each view in a standard way (e.g. viewName[x] where x is the current "i" from my for, but is it of any use?), as we do for example for an ExpendableList Adapter's groups/childs ?
Or is there a way to actually use a GridView/GridLayout to do that (in this case, being in my Activity, how do I put each specific created View into the correct GridView/GridLayout) ?
Or still another way I don't suspect at all ?
Thanks in advance
if you want to add views programatically then, just create a layout.xml with 3 Linearlayouts(horizontal) in it. Also assign id to those LinearLayouts. Then in your java code, just find the views and call addView() on those LinearLayouts.
Example:
LinearLayout ll_1 = (LinearLayout)findViewById(R.id.linearlayout1);
LinearLayout ll_2 = (LinearLayout)findViewById(R.id.linearlayout2);
LinearLayout ll_3 = (LinearLayout)findViewById(R.id.linearlayout3);
...
ll_1.addView(new TextView(this));
ll_1.addView(new Spinner(this));
ll_1.addView(new EditText(this));
...
In detail, say for example, if i want to add spinner to LinearLayout, programatically then,
You need to get layout
LinearLayout linearLayout = findViewById(R.id.layoutID);
Create spinner as below:
Spinner spinner = new Spinner(this);
spinner .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, spinnerList);
spinner.setAdapter(spinnerArrayAdapter);
Then add spinner to view
linearLayout.addView(spinner);

Categories

Resources