Is it possible to make a checkbox/radio button uncheckable? - android

The program I am making has several views in which the user is trying to basically figure out a lot of boolean values. Sounds strange, but what I mean is that my device will connect to another device via bluetooth, and then read the status of that device. I basically display a checklist of the current status of that device. So depending on what that device is doing, different checkboxes are checked. Is there a way to make all of the checkboxes unselectable?
Even better, is there a way to hardcode this into the XML file?

Have you considered using android:clickable for an xml attribute for your checkboxes?
From the documentation:
Defines whether this view reacts to click events.
If it cannot react to click events, then it effectively becomes uncheckable.

<CheckBox
android:id="#+id/blahblah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false" />
and in your code you can call cbox.setChecked(boolean)

to expand on what crocboy and nicholas.hauschild said.
Setting this in you XML
android:enabled="false"
will make the button/checkbox be grayed-out, to show it is disabled
Another way to do this is to set it to:
android:clickable="false"
Here is the code to make a RadioButton that is red, that turns green when checked (in your code), but can not be checked by the user.
android:checked="false"
android:textColor="#d1d2d4"
android:buttonTint="#d1202d"
android:textColorHighlight="#248d51"
android:clickable="false"
AND here is the code to check it (this is using JSON: so it is checking if english=="yes")
String yes = "yes";
if(yourObject.english.equals(yes)) {
if (m_english.isChecked()) {/*do nothing */}
else m_english.toggle();
}

Check this out: http://developer.android.com/reference/android/widget/CheckBox.html
You can use checkbox.setEnabled(false) to make it unselectable.
Try this in your XML: android:enabled="false"
It's supposedly deprecated, but may work depending on your traget SDK.
Hope that helps!

Related

Android Accessibility: sending several accessibility events on a single touch

Inside one of my recycle view adapters, I have a layout which displays an image and a checkbox.
I am adding accessibility to the app, and I want it so that when a user touches a picture, it will get a reading of the image and the current state of the checkbox (as in, "checked" or "unchecked", which is what the accessibility assistant reads, not true or false)
The two separate lines of code which I have used fit my purpose are as follows:
myImageView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
myCheckbox.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
But if I run that call on the click event, the first instruction is cut short and only the second one is heard. So I believe this might not be the way to go. I probably have to use all of the Accessibility classes provided by Android, but I have no clue where to start.
What should I do for the voice feedback to read "Image one, checked... Image two, unchecked".
Additional info:
I cannot use strings to get the wanted value from the checkbox boolean because the app is multilingual and instead of having to get all the string resources I would rather take advantage of the system.
Thank you,
everyone, for your time.
You can combine both items in a linearlayout and/or you can market the "Image one" part as a label for the checkbox. Both examples are shown below and taken from the link below them.
<LinearLayout
android:id="#+id/consumedContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="false"
android:orientation="horizontal">
<TextView
android:id="#+id/coffeeLimitLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_weight="1"
android:labelFor="#id/coffeeLimitValue"
android:text="#string/coffee_limit_label"
android:textAppearance="#style/TextAppearance.AppCompat.Title"/>
https://www.raywenderlich.com/182100/android-accessibility-tutorial-getting-started

Avoid impact to performance, when designing activity with more than 80 views

I'm new to android. I'm creating an application, which should handle more than 200 settings, mostly Boolean. Settings can be grouped by they type, there are 5 groups. I assume, that users won't like to configure and save each group separately,but would like to configure all the settings and by pressing save -> save all of them at one time, so scenario with having 5 different activities for each setting group doesn't suite.
What I've tried to do, is set up a TabHost, with separate tab for each group. After I've got "more than 80 views" warning, I've split up my layout, and now each group has own layout.xml;
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/cbSetting1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/strSetting1" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/cbSetting2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/strSetting2" />
</TableRow>
...
</TableLayout>
And I'm inflating those views onCreate:
View setting1View= getLayoutInflater().inflate(R.layout.group1_layout, null);
group1Tab.addView(view);
With this steps done, I'm avoiding "more than 80 views warning", but won't have it impact on performance, when all views are inflated -> activity will have more than 80 views in total?
Next question: should I get references to all CheckBoxes on my views onCreate:
CheckBox setting1 = (CheckBox)setting1View.findViewById(R.id.cbSetting1);
or should reference them on my saving button press, like:
onSaveClick{
Boolean setting1Value = ((CheckBox)setting1View.findViewById(R.id.cbSetting1)).value
}
or in some other way, to have as little performance impact as possible?
Is there any "good pattern", for designing apps with a lot of views?
Thanks in advance!
P.S. I know, that "best practice" questions are not recommended on StackOverflow, but I've tried to ask as defined questions as possible, and, hopefully, my post is showing at least my researching effort on the issue.
Update
My fault not mentioning it earlier - those settings are stored remotely, and are accessed using HTTP requests (using Web-api). Does suggestions about Preference activities/fragments and ListViews fit in this case?
Based on the way you have described your application, it seems like you have many similar views. I would recommend using a ListView in each of your tabs. ListViews very efficiently handle tons of views.
You will need to write a custom adapter for you ListViews, but you can do this cleverly in a way that doesn't call findViewById() all the time (using the ViewHolder pattern).
It is hard to give specific advice because you have not described exactly what these settings items will contain, but this general idea works well when you have long lists of (event potentially slightly different) views. This pattern would work if you want to have a lot of flexibility over you layout. If you want some standard settings behavior, check out PreferenceFragments, or check out Android's settings developer guide.
If its just boolean settings [i.e. Checkbox].
Then I would prefer trying PreferenceActivity, its just like another activity but preferably used for settings functionality. Also there is an easy way to group your settings element, try PreferenceCategory for grouping.
A small tutorial is available here.
CheckBoxPreference is what you need.
Hope this helps!

Why should I use TYPE_TEXT_VARIATION_PERSON_NAME (textPersonName)

I have here an EditText which I copied from a dialog example. The textPersonName however doesn't capiltlize like I would expect, which is to put the keyboard into caps before each word in their name. I find myself using textCapWords to achieve this.
<EditText
android:id="#+id/player_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
</EditText>
What is the point of textPersonName and what might I be loosing switching to textCapWords? I thought maybe there was some locale logic behind textPersonName, but it doesn't seem to even work in English like I would expect so I'm not so sure.
Edit
I have also tried android:inputType="textCapWords|textAutoComplete|textPersonName" to see if it would auto complete peoples names, but it doesn't seem to.
You don't lose anything if you stop using it. Currently Android doesn't use this flag at all. At least I checked the source code of the latest Android, and I didn't find any mentioning of this parameter in widgets.
It is possible that this is something that will be used in the future versions of Android, so I would leave it just in case, but you'll do fine without it.

How to get the keyboard to show a return key?

I think I've tried every combination, but I can't get the alpha keyboard to show the return key. It's always a "Done" button, which is not useful. On a Nexus 7 (4.1), it's worse, and shows a stupid smiley button along with the Done button, which makes no sense for my application. It's fine to have a Done button as long as I have a return button. Here's one of many options I've tried:
<AutoCompleteTextView
android:id="#+id/annotate_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="15dp"
android:completionThreshold="1"
android:inputType="textCapSentences|textImeMultiLine"
android:imeOptions="actionDone"
android:lines="1"
android:maxLength="18"
android:textSize="30px" />
I've tried with and without the imeOpitons line, and various inputType options including removing it. There are options to make it a search button (not helpful), but nothing listed to make it a return key. I've also tried with "lines=2", which did not fix it. Any other ideas?
Right now you have set imeOptions="actionDone". You say you've tried removing it, but all that does it default it to actionDone again, per the docs.
Try setting imeOptions="actionNone" instead. That should give you no action and force a return key.
I don't know that that by itself will guarantee a working multiline AutoCompleteTextView, however. I have one in an app, and it shows a return key on all my devices. The difference is the behaviour of the return key.
For instance, on my GNex (4.1), when you press return it line feeds properly.
On my SGS2 (2.3), nothing happens when you press it. No multiline, no completion. Just a dead button.
It could be the difference in the default EditText style in different APIs, so you should also try setting inputType="textMultiLine". There may be a subtle difference between that and textImeMultiLine.

Using NumberPicker in Android 3

I'm trying to use the NumberPicker widget in Android 3.1. I figure it is now a standard widget, part of a public API, and not an internal widget anymore.
However, it doesn't work. I can see a textEdit with "0" in it, but there are no buttons, I can't scroll it, and I can't enter a new number with keyboard, either.
Update:
Here's the xml I used for testing:
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
By saying doesn't work I mean this: it doesn't have arrows displayed (see the screenshot), and on emulator the keyboard doesn't show up. On a real device it does, but I can't type in the text, I can only erase it.
Any suggestions? I really hate to write my own widget when there is a standard one.
Try to set the values that are possible in the NumberPicker via
setMaxValue setMinValue or setDisplayedValues
maybe the NumberPicker is not working if it doesn't know which values to display.

Categories

Resources