how can I customize this listview just a little? - android

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

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 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().

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?

Is there any flag in spinner to specify that text can be in more than one line?

I have spinner on screen and when I click I get choices ( default behavior of spinner ). Problem is that in some items text is to long so it is cropped. Is there any flag in spinner to specify that text can be in more than one line ? How to solve this problem ?
Change the layout that you pass to the adapter:
final SimpleCursorAdapter quartierAdapter = new SimpleCursorAdapter(
YourActivity.this,
android.R.layout.simple_spinner_item, // the layout to change
curseurInitialQuartier,
new String[] { DBhelper.KEY_NOM },
new int[] { android.R.id.text1 });
Make your own layout and put it there.
a very common issue . possible solution are :
1) set text size in sp .
2) create flexible layout , which can very along with text length .
3) in case 2 is not feasible , fix textView length and crop remaining portion, i guess no trick will be possible .

Categories

Resources