10087

How to line up intger output in custom Android dialog

Question:

I am displaying integer data from an SQLite database using a SimpleCursorAdaptor. Everything shows up but the alignment is all wrong: <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/VgpHq.jpg" data-original="https://i.stack.imgur.com/VgpHq.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

The dialog looks like this:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/lvwScores" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btnOK " android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="OK" android:layout_below="@id/lvwScores"> </Button> </RelativeLayout>

With the row xml file:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0,1,2,3"> <TableRow > <TextView android:id="@+id/tvwPlayer1Score" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/> <TextView android:id="@+id/tvwPlayer2Score" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/> <TextView android:id="@+id/tvwPlayer3Score" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/> <TextView android:id="@+id/tvwPlayer4Score" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right"/> </TableRow> </TableLayout> </RelativeLayout>

Answer1:

TableLayout is a bad choice. It's inherent fluidity will cause the columns to vary in width based on the content inside of them (although the stretching does minimize some of this), which you have no control over (see below). Also, the namespace declaration only needs to be on the root element of the XML, not each one ;)

Simplify your row layout drastically by using this instead:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tvwPlayer1Score" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right"/> <TextView android:id="@+id/tvwPlayer2Score" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right"/> <TextView android:id="@+id/tvwPlayer3Score" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right"/> <TextView android:id="@+id/tvwPlayer4Score" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right"/> </LinearLayout>

The combination of layout_width="fill_parent" and layout_weight="1" on each element tells the system to lay out all four elements, equally spaced (since they have the same weight sum) to fill the row. I almost always use nested LinearLayout in place of TableLayout whenever possible (that's all TableLayout really is anyway).

Another thing from the row XML you posted: it's not a good idea to set the root element of a list item's layout with layout_height=fill_parent like you have in the RelativeLayout tag. Depending on where this layout get's drawn, the layout manager might actually listen to you and one row might end up taking the entire window!

NOTE ABOUT TABLELAYOUT PARAMS:

If you insist on sticking with TableLayout, know that you can (and should) omit all the layout_width and layout_height attributes from every child of TableLayout and TableRow. Those two widgets ignore what you say and set the values of their children to MATCH_PARENT and WRAP_CONTENT (respectively), so adding them to your code will only serve to confuse you if you think they're supposed to take effect.

Hope that Helps!

Answer2:

you should specify the android:gravity attribute:

<TextView android:gravity="right" />

more about this: <a href="http://developer.android.com/reference/android/widget/TextView.html#setGravity%28int%29" rel="nofollow">Android TextView</a>

<strong>Update:</strong>

I've modified a bit the row.xml layout.

<ul><li>changed the TextViews' width to fill_parent (they are stretched anyway, so it shouldn't do any harm), and</li> <li>added some attributes to the TableRow tag</li> </ul>

So it looks like:

[...] <TableRow android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvwPlayer1Score" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" /> <TextView android:id="@+id/tvwPlayer2Score" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" /> <TextView android:id="@+id/tvwPlayer3Score" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" /> <TextView android:id="@+id/tvwPlayer4Score" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:layout_margin="2dp" /> </TableRow> [...]

And the output looks right now:

<img alt="table row" class="b-lazy" data-src="https://i.stack.imgur.com/EKYuZ.jpg" data-original="https://i.stack.imgur.com/EKYuZ.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Please let me know if this helped (still very embarrassed...)

Recommend

  • Fluidity with Cards of Different Lengths
  • How to get user input based on radio button selection?
  • cannot call methods on dialog prior to initialization; attempted to call method 'option'
  • Django multiple forms with one submit button
  • How does this compute ? I am trying to understand how the values of H get assigned in the list
  • Count occurrences Prolog
  • Is there any other advantage to using '_svn' directories instead of '.svn' in a
  • Relative CSS urls in Webpack
  • Javascript iPhone selection
  • psycopg2 and SQL injection security
  • Why call session not working? (codeigniter 3)
  • XslTransform with xml-stylesheet
  • FParsec only parses expr between parentheses
  • Difference between Echo[Special Character]
  • SonataAdmin - sonata_type_choice_field_mask
  • Is the integer width relevant in bitfield declaration?
  • Laravel nested foreach by 2 different Models
  • Sampling unique column indexes for each row of a numpy array
  • Fresh Rails 4 app is not recognizing `rails` commands, insists that I use `rails new`
  • minOccurs and maxOccurs on elements inside xsd:choice
  • Python Pandas global vs passed variable
  • R mlogit on my data giving error 'system is computationally singular?
  • When is InputStream.available() useful?
  • ColdFusion - DateTime Format with GMT offset
  • Sharepoint change Content Type in Sharepoint Designer using workflow
  • Change colour of vertices after they are uploaded using a VBO
  • Radio button show hover between two radio button using jquery
  • Django Rest Framework: getting lists from query_params without request.getlist
  • Simple command-line app I/O in Dart
  • WPF version of .ScaleControl?
  • Primefaces :radioButton inside a ui:repeat
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • bad substitution shell- trying to use variable as name of array
  • Breaking out column by groups in Pandas
  • Unable to get column index with table.getColumn method using custom table Model
  • Assign variable to the value in HTML
  • Bad request using file_get_contents for PUT request in PHP
  • How to handle AllServersUnavailable Exception
  • Android Studio and gradle
  • SQL merge duplicate rows and join values that are different