android list item formatting - android

I have a listview item xml with 3 views - two text boxes and a checkbox.
I need the second textview and the checkbox to always be shown, i.e. they must not get cropped or pushed off the end of the item.
The first textview sometimes contains a long name, and this is making the second textview too narrow and pushing the checkbox off the end of the list item. The first textview needs to be the one that gets ellipsized.
I've tried using android:layout_weight but this makes the views always use the same width, which isn't what I want. For instance if the 2nd textview doesn't contain any text, I want it to completely collapse.
I've tried using various combinations of wrap_content/fill_parent and doing things in code but nothing seems to be working. I am currently using a linearLayout for it.

You can try using TableLayout, setting shrinkColumns to 0, and stretchColumns to "1, 2"
This should allow the checkbox and the second textview to take all available place, and shrink the first textview as necessary

Related

Add views dynamically and get value from those views

I need help regarding adding new set of views and get value from those views.
In my example, I want to add 4 TextViews on Button click and open TimePicker from each view click and display new selected time on respective TextView.
Below is screenshot of view.
If these 4 views are fixed you could just create the xml and add them all to a single holder that you set invisible. If you mean by dynamic that it could be either 4 or 99 views, I'd recommend a RecyclerView. Plenty of examples on the internet. If you create a recyclerview with a custom adapter it is very easy to get the respective data per view.
For the future, please add more context to your question like what you've tried, what the result was and why this isn't your expected result. This is a very broad question.
Assuming that on clicking the clock button under the time section (on the right) you would want to set the selected time into the text field. You can simply call view.getParent() on click of the button and from the parent you can get the first child i.e child at 0 and set the text into the text field. This will work provided the button and textfield are within the same layout.

TextView with autoLink="web" inside ListView has animation issue

I have a ListView. There is a TextView for each list row. For the TextView, I have set the following properties in the xml file:
android:autoLink="web"
android:focusable="false"
android:focusableInTouchMode="false"
Also, for the parent layout of the list row, I have set:
android:descendantFocusability="blocksDescendants"
The three focus-related properties were set after going through several SO answers for the problem in which the TextView used to become un-clickable if it contained a link (e.g., www.example.com).
Now the clicks are all registered properly. However, the problem now is the following: for TextViews which have a link, the setOnItemClickListener and setOnItemLongClickListener animations are lost. The animations are lost only when I click the TextView part of the ListView row. They still occur when I click the remaining part of the ListView row.
That is, the clicks are working, but the animation is lost. How do I get those back?

How to make a listView with textview and spinner and edittext

How can i create an android listview with row 1: textview + spinner row 2: textview + edittext etc.?
I have to make a form and i make it with xml layout but it's too long and bad for perfomance as the eclipse says.
Thank you!
ListView can be used if there is a repetition of same views in every row. This is where you get performance benefits given by adapters' recycling view mechanisms. With every row having different views they cannot be recycled and therefore no point in using the listview there.
So based on what kind of rows you have here are two solutions:
Solution 1:
Row 1 has textview + spinner, Row 2 has textview + EditText. And this combination repeats for the rest of the rows then you can try this. The xml where you create the layout of individual row for the ListView should be made a LinearLayout having two rows. ListView will create the rest of the rows for you by appending this LinearLayout.
Solution 2:
If each row is completely different from other (in terms of views used) then I would suggest using pagination. Divide your form questions into different relevant groups put each group on one page (scrollview). When user answers a set they have to navigate to the next page and so on.
As suggested by #Xaver Kapeller, I'd also suggest not using EditText in a ListView. It is very painful to debug keyboard issues and when the device orientation changes with keyboard open then as well. Instead of EditText ,use TextView which on tapping opens a Dialog box with a EditText. To keep the user interaction minimum.
You can focus the EditText as soon as the dialog open so that keyboard opens and user does not have to tap on the EditText. You can also dismiss the dialog by pressing the imeAction like Done on the soft keyboard or by pressing the hard back button. Dismissing the dialog should trigger a textview.setText("Data entered in the dialog box").

setButtonDrawable to a customized radioButton in ListView can't show the correct result until click it

I use an example to make this issue easy to understand.
I have a ListView. In each row, there's an customized RadioButton which is extended from RadioButton. I use setButtonDrawable to set the image of this button in getView(); I use ViewHolder method to hold the button.
holder.button.setButtonDrawable(drawable);
The ListView has 4 rows. In each screen, we can only see one row. Now the problem is: when I scroll the ListView, I found buttons in the first and the third item are the same, second and fourth are the same. But I'm sure I have set different images to them.
What's the problem? Does the button should be refresh after setButtonDrawable?
Thank you.

Displaying both text and icons in gridview

I want to display the icons in grid view and also text heading for that icon just below it.
can some one help me out.
You are supplying an Adapter to the GridView. Have that Adapter return Views for the cells that have icons "and also text heading for that icon just below it". You might use a vertical LinearLayout with an ImageView and TextView children, for example.
Here is a free excerpt from one of my books that shows how to do this sort of thing for a ListView -- the same technique holds for GridView.
If you are beggining in Android like me, I found out that the simplest thing to start with is to:
1) Set each grid to contain a textview
2) Set background (icons in your case) to each textview; plus add the text.
Like:
//textview is a the textview in the cells
textview.setBackgroundResource(<your icon resource>);
The text is added and modified normally:
textview.setTextColor(Color.BLACK);
textview.setTextAppearance(mContext, R.<style xml>);
textview.setGravity(Gravity.BOTTOM);
You'd most likely need to properly set the text position and color so you have a readable result.
Then you may also set the background properties (you can check textview methods)

Categories

Resources