Three Questions - android

First, I want set unchecked all the 8 checkboxes using a loop like this:
for (int i=1;i<8;++i){
CheckBox view1 = (CheckBox) findViewById(R.id.CheckBox0+String.valueOf(i));
view1.setChecked(false);
}
It Doesn't work, but you get the idea what I mean. How can solve it?
Second: With Eclipse I set a form list. What is making that when application starts, it immediately shows the keyboard and is focused in a editing field?. I want that the keyboard appears only after the user touches an editing field.
Third: How I set the properties of the editing field that when user touches enter, the focus doesn't pass to the next editing field. Thanks in advance.

Firstly, asking multiple questions together isn't good; it prevents people from answering when they know only one of the solutions.
1 - Relying on the IDs CheckBox0, CheckBox1, CheckBox2, ... to be in order is very risky and is bad practice. In this case, you should be using getIdentifier; this will fetch IDs CheckBox1, then CheckBox2, etc. reliably.
for (int i=1;i<8;++i){
CheckBox view1 = (CheckBox) findViewById(getResources().getIdentifier("CheckBox" + i, "id", getPackageName()));
view1.setChecked(false);
}
2 - You need to use a stateHidden modifier for this:
<activity
android:name="com.example.NoKeyboardActivity"
android:windowSoftInputMode="stateHidden" />
3 - Use imeOptions for this; actionNone is the one you're looking for, or (as per comments), actionDone to enable the "Done" button.
<TextView
...
android:imeOptions="actionNone" />

For the resources to have fixed ids after each build, you will have to declare the resources in public.xml, then you can access the ids sequentially. Check here
Also R.id.CheckBox0 is an int so do
findViewById(R.id.CheckBox0+ i)
after you have declared all checkboxes in public.xml
For the second question in the activity manifest add
android:windowSoftInputMode="stateHidden"
Third:
I think you will have to reference the same edittext in layout for android:nextFocusDown.
Not sure if this will work give it a try

1 - Remember that this is Java not Javascript, you can't do this (R.id.CheckBox0+String.valueOf(i)). Normally I create an array of int with all the R.id inside and loop through them.
2 - You could use also android:windowSoftInputMode="stateUnchanged" to not hide the keyboard if it's already showing.
3 - That's the way it's supposed to be, but you can use on the edittext the property android:imeOptions="actionNone", so the enter button will do nothing.

Related

How to Repaint FMX Android components in Form in C++Builder 10.4?

I wrote a very simple FMX Adroid App, the function is:
Show Form 2 then write something to record(include title and detail text),
close Form 2 to Main Form, then make a checkbox in Main Form with the title we just recorded in Form 2.
if user check the checkbox, then press "del" buttn then delete the record file and checkbox.
the problem is:
when closed Form 2 and in MainForm::OnActivate we can add a new checkbox for the record.
if we checked checkbox then clicked delete, free the pointer of checked checkbox, the checkbox still in main form until I reopen the APP.
I tried:
Invalidate();
Application->ProcessMessages();
BeginUpdate();
EndUpdate();
Still can't work
does anyone know what's going on ? why FMX TForm member has no "Repaint()" or "Update()" "Refresh()" ? just like VCL has.
If you want your TCheckBox* (or any other control) disappear from a Form, you need to set its Parent property to nullptr before deleting it. If you created your control in runtime using new please remember to call delete.
//init
TCheckBox* checkBox = new TCheckBox(Form2);
//delete
checkBox->Parent = nullptr;
delete checkBox;
Answering the second part of your question, you can call Invalidate() function to repaint your whole Form (but first see first part of this answer). But I think it will run properly without calling this function.
Your controls have Repaint() member and it may be better to call them instead, ie. if your checkbox was placed in TPanel*, repainting only this panel is better idea than repainting whole form.

Android - How do I make sure a widget is the last in that line?

I'm trying to export all widgets on screen to a text. To do that, I'm cycling through all widgets inside the RelativeLayout I have on screen. How do I make sure the view I'm currently looking at is the rightmost (or last) in that line?
for(int i = 0; i < relLayout.getChildCount(); i++) {
View view = relLayout.getChildAt(i);
(...)
}
And then I'm checking if the view I'm looking at is a TextView, EditText, Spinner, etc. So far, so good. But in order to append the new lines to the string, I'd like to programmatically check if that view is the last in that row. Is there a clear/simple way to do that (other than enumerating the id's of those widgets, which I'm doing right now? :D)
Thanks in advance!
PS1: For the purpose of the question, assume there are no compilation errors in the code. I just don't know how to achieve this.
Fixed, but in a curious way.
In my case, all the last fields on screen had Align Parent End flagged. So I found online this would be the way to do it:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
int[] rules = params.getRules();
if ((rules[RelativeLayout.ALIGN_PARENT_END] == RelativeLayout.TRUE)) {
// Do something about it
}
Where RelativeLayout.ALIGN_PARENT_END stands for 21, according to http://developer.android.com/reference/android/widget/RelativeLayout.html
However, my code was returning false for that check. So I started displaying the value for the property, and it really was false. I tried this for a while, then I saw one of my edittexts returned true for it. But the activity xml showed this
android:layout_alignParentRight="true"
in addition to alignParentEnd. Which didn't make sense, since alignParentRight is supposed to be rule 11, according to the same link above.
So I devised this code to expose which rules where true, and put it in my code:
for(int x = 0; x < 22; x++) {
if (rules[x] == RelativeLayout.TRUE) Log.i("Property true:", String.valueOf(x));
}
Which gave me 11 for all fields, and 11 and 21 for the field that responded TRUE before. Remember, all fields had android:layout_alignParentEnd="true" in the activity xml.
So to fix my code, I'm now checking rules[RelativeLayout.ALIGN_PARENT_RIGHT] and it works, but I strongly believe those two properties are switched somehow. Can anybody confirm? Or please englighten me on where I screwed up, which is more likely :)

how can i set RadioGroup unchecked by default in android?

I am doing an app to conduct simple quiz.I have given the answers are multiple choice using RadioGroup. So on loading each question how can I set RadioGroup unchecked by default ?
Just set the checked Property in your XML for every RadioButton to false:
android:checked="false"
That should solve the issue.
Have you looked on the android developer guide?
http://developer.android.com/reference/android/widget/RadioGroup.html
Intially, all of the radio buttons are unchecked.
Although you can do this in code with the clearCheck() method
Clears the selection. When the selection is cleared, no radio button in this group is selected and getCheckedRadioButtonId() returns null.
Perhaps your problem might be a different from my understanding. Perhaps explain more about how you are loading questions and so forth.
Try
RadioGroup.check(0)
0 is an id which will nevere be generated by adt
upd: from source of RadioGroup
public class RadioGroup extends LinearLayout {
// holds the checked id; the selection is empty by default
private int mCheckedId = -1;
...

Displaying an array of objects, one at a time through a single dialog... instead of several dialogs

In my application I have a list of questions stored in an ArrayList, and I want to display a dialog that shows one question, and then continues to the next one after the question is answered. The way that I'm currently doing it (iterating through a loop) hasn't been working because it just layers all of the dialogs on top of one another all at once which causes a host of other issues. What I'm looking for is a way to still iterate through the questions, but just change the layout of the dialog each time until it has finished each question in the list. Can anyone give me a good pointer for how to get this going?
You can make a function that takes title and message as parameters and shows a dialog.
showDialog(String title, String message){ // Show dialog code here}
Within that dialog's answer button's listener call another function (showQuestion(currentQuestion)) that iterates the arrayList till it is over
int currentQuestion=0;
ArrayList<QuestionObject> questionList;
showQuestion(int i){
if(i<questionList.size()){
showDialog(questionList.get(i).getTitle,questionList.get(i).getMessage);
currentQuestion++;
}else{
//quiz is over
}
}
I assume you mean that you just want to change 1 single layout(created within XML i.e main.xml). In order to do this, make sure that the class your working on is pointing to that layout. From there (assuming your using an Event listener for when the user submits an answer) you can change do as you want by the following:
TextView txt = (TextView) findViewById(R.id.textView); // references the txt XML element
and in your Event listener, if the answer is correct then change(Have i be a global variable thats initially set to 0).
if(i<arrayList.size()){
txt.setText(arrayList.get(++i));
}else{
txt.setText("You Finished");
}
From there, in the else statement, you can change arrayLists and reset i to 0;
If you are trying to use the positive, neutral, and negative buttons; then you may have problems with multiple dialogs. Try defining a customized layout with your own TextViews, ListViews, and Buttons. You can implement listeners and everything else like a regular layout. Then just pass your customized layout to the dialog through AlertDialog.Builder.setView().
PS If you include code examples of what you are currently doing we can provided answers that are less vague.

How do I loop through all dynamically created radiobuttons and how do I identify them?

I've got an app that dynamically adds radiobuttons from json data. I don't really know how to find out which ones are selected though. There are the radioButton.isSelected() and the radioGroup.getCheckedRadioButtonId() but since it's dynamically created I cannot use names for all the objects.
Is there a way to add them to a group upon creation and cycle through all of them later? I've got multiple radiogroups and not all of them but most are supposed to be checked.
I'm using api lvl 7 (2.1) and I'm fairly new to this. Please explain in detail.
You can use an ArrayList.
ArrayList<RadioButton> radioButtons = new ArrayList<RadioButton>();
//create your new button and add it here.
radioButtons.add(radioButton);
Then to iterate through the list if you need to retrieve each button, use a for loop:
for (int i=0;i<radioButtons.size();i++){
RadioButton button = radioButtons.get(i);
//do what you need with the button
}
And if you need each RadioButton to have certain data, then you could do:
radioButton.setTag("tag");
and then when you are iterating through the loop, you can do button.getTag();
Or if you just want to get the selected one:
RadioButton button = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
I think I covered everything from your question and maybe more. Let me know if I missed anything or need to give any additional explanation.

Categories

Resources