Spacing a textview - java

I'm trying to make a basic text-based adventure game in Android studio. I'm using a textview to display the map. It has weird spacing, like this:
Current Layout:
However, I want it to be spaced out like this:
Desired Layout:
How can I do this?

If your app supports API 16 and higher, you can use the android:fontFamily attribute on your TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"/>

Related

What is an Alternative to the UIPickerView from IOS in android?

I am trying to reproduce the IOS UIPickerView functionality but in android.
In IOS you can set this UIPickerView as an input type of the keyboard. I need this same type of interaction but in android.
UPDATE:
When I say input type I mean the UIPickerView simply replaces the keyboard view. So if a textfield were clicked the UIPickerView would open instead of the Keyboard. Hope that clears stuff up!
not sure exactly what you mean by
as an inputtype of the keyboard
but the android equivalent to that image would be the TimePickerDialog
Update
If you want that picker type of UI you have 3 options for pickers, the TimePicker, DatePicker or the NumberPicker obviously the first 2 are out so you are left with the NumberPicker with the number picker you can populate it with the numbers you want (inches) and then another for feet
I suggest you take a quicl look at the Pickers article
https://developer.android.com/guide/topics/ui/controls/pickers.html
I would suggest making a layout with multiple pickers, that way you can have as many as you need and initialise them with any values you require.
The below layout will give you two spinners side by side similar to the iOS UIPickerView.
I've posted a link to my github repo at the bottom with a simple example showing how to use 3 spinners, one for minutes, hours and days that should help you (Or anyone else coming from iOS with the same question) can use as a base to get started.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/multispinner">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Spinner
android:id="#+id/spinner1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"/>
<Spinner
android:id="#+id/spinner2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp" />
</LinearLayout>
</LinearLayout>
My simple multiple picker example
You can use a number picker, if you're looking for a similar feel. Just make sure to add a personalized style as theme (or else it will take your app theme as default) and add a formatter to the number picker. You'll run into some trouble on the first render (it won't show the first value until you scroll), just use the solution provided here:
Android NumberPicker with Formatter doesn't format on first rendering
You can also put it into a cardview like in the image, it looks tidy enough.
formatted number picker screenshot
Ps.: Actually you can modify several fields, but you have to acces the class fields/methods. To do so, do as this exmaple code.
public void setNumberPickerDivider (NumberPicker picker) {
Field[] fields = NumberPicker.class.getDeclaredFields();
for(Field f:fields){
if(f.getName().equals("mSelectionDividerHeight")){
f.setAccessible(true);
try {
f.set(picker,1);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}

Ability to use XML and Android programmatically

I have a android layout in xml, and I have an image i placed using java/android code. I have placed the image without using xml, but I was wondering if I am able to also use XML to position buttons on the page as well. I don't know how to do this because I am already using setcontentview to draw my image to the screen... and if I use setcontentview(R.exampleXMLfile) it will overwrite my image. Does anyone know a solution or example for this? The reason I am looking is because its a pain to add a button without using XML and eventually I want to have animations on the screen. Thanks,
You could use ImageView in your xml file,
<ImageView
android:id = "#+id/my_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_drawable"
/>
or just View and set Background, or set background on any Layout in your xml.
<View
android:id = "#+id/my_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>

android:gravity fails on API 18+

I have some TextViews on my app, I don't know why but the android:gravity attribute is not centering the text content where it should be on devices running the API 18+ (4.3+).
There is the code I use on my custom TextView, this is a child of RelativeLayout:
<com.package.custom.CustomTextFont
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/seekbar"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/seekbar"
android:layout_marginLeft="#dimen/margin_tiny_double"
android:layout_toLeftOf="#+id/seekbar"
android:gravity="center_vertical|left"
android:paddingRight="#dimen/margin_tiny"
android:text="#string/text1"
android:textColor="#color/black"
android:textSize="#dimen/size_text_normal" />
This code should take the edges of this TextView and align it to the top and bottom and put it to the Left of the SeekBar, this is working, but the TextView gets big, so with android:gravity I center the text to the center and left from it self. It works, but I don't know why, the text is not centered at center|left on devices running android 4.3 and 4.4. This issue can be reproduced on real devices and as well on the layout preview (Graphic layout) of Eclipse.
There is any changes made on API 18+ or on android:gravity that I'm missing?
PS: I'm targetting the API 19 on the project and on AndroidManifest.xml
PS2: My TextView is custom just to set an external font.tff
This is how it looks like on API 17-
This is how it looks like on API 18+
Thanks in advance!
= UPTADATE =
On my Manifest, I changed the android:targetSdkVersion to 17 instead of 19 and the problem disappeared, but this is a "trick", not a solution, what can I do since it could be an issue from the API ? And yes, I have the latest version of the API 18 and 19 (today, 01/30/2014).
This appears to be a known issue in API 18+:
https://code.google.com/p/android/issues/detail?id=59368
https://code.google.com/p/android/issues/detail?id=59700
The problem seems to occur when a TextView is part of a scrollable container (e.g. ListView), making the view ignore the vertical gravity for some reason (some sources suggest this has to do with the TextView being the child of a RelativeLayout, though it's been my experience that this can happen even when no such layout is involved).
A possible workaround (albeit not a particularly elegant one), would be to wrap the TextView in a LinearLayout. You can then use "layout_gravity" on the TextView to center it inside the LinearLayout, instead of relying on "gravity" (just make sure to wrap_content so the text itself is properly centered).
E.g., in your example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/seekbar"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/seekbar"
android:layout_toLeftOf="#+id/seekbar"
android:layout_marginLeft="#dimen/margin_tiny_double" >
<com.package.custom.CustomTextFont
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:paddingRight="#dimen/margin_tiny"
android:text="#string/text1"
android:textColor="#color/black"
android:textSize="#dimen/size_text_normal" />
</LinearLayout>
This method does have the disadvantage of adding an otherwise-unnecessary level to your view hierarchy, but it currently seems to be the only way around this (other than reverting to an earlier API level).
Also see similar question at:
Android sdk 18 TextView gravity doesn't work vertically
Your textview height is "wrap_content", which means the height of the textview will be the same as the height of the text. If you change the background of the textview to black, it might be easier to see the bounds of the view. I'm guessing you'll find that the textview doesn't have as much height as you expect.
Try setting the height of the textview to match_parent. You can wrap the textview inside another view if needed and modify its height as appropriate.

Android Autocomplete TextView drop down width

I want the dropdown of my auto complete TextView to cover the entire screen width. Currently the normal behaviour of the autocomplete textview is such that it covers only the width of the EditText (screenshot below).
How do I do it ? It should look somewhat like the default maps app in android.
http://developer.android.com/reference/android/widget/AutoCompleteTextView.html#attr_android:dropDownWidth
By putting the value -1 or fill_parent it should work
autoCompleteTextView.setDropDownWidth(getResources().getDisplayMetrics().widthPixels);
Use android:dropDownWidth="match_parent" in AutoCompleteTextView inside xml
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dropDownWidth="match_parent"
/>

RelativeLayout and accessebility support

I am adding accessebility support to some application. It works fine with standart UI elements (buttons for example), but for some reason does not work with my custom element, wich is RelativeLayout with ImageView and TextView (it looks like icon). I've defined android:focusable="true" and set contentDescription.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:contentDescription=”my content description”
android:focusable="true">
<ImageView
...
/>
<TextView
...
/>
</RelativeLayout>
Could someone please list here all posible causes?
UPDATE:
Is there some way to know what layouts are on the screen at the moment and what order do they have (some layouts are transparent)?
Use hierarchy viewer for understanding where is your invisible views.
The android docs have a section about designing for accessibility, is this any use to you?

Categories

Resources