RadioButton isChecked control state android - controls

Here is the case : i have 4 radioButtons that are not members of a radioGroup. Thats because RadioGroup places children in vertical order by default and cannot place them relative to each other.Right now , i am check all the radioButtons. What i want to achieve is to control their checked state , so that when one is checked(true) the others checked state is set automatically to false. There widgets are independent layout elements , so i guess i have to find a way to group them all together. I tried it by coding 4 RadioButton variables with a simple if..else if function using isChecked and setChecked built-in methods. Then i tried grouping them in an Radiobutton[] array and looping to the array.length with a switch-case loop. Nothing works.
Stack for 2 days now. Any help appreciated.
Thanks for reply , i explain further. I have 4 imageViews and 4 radioButtons exactly below each one , in a RelativeLayout parent , so that the user can make a choise which image he prefers. The imageViews are not vertically placed , so are the radioButtons. Thats why i dont want to use radioGroup. I could use LinearLayout and place imageViews in vertical orientation and have the radioButtons in a group just aside them. Every radioButton checked state is false by default and when i check it , becomes true. The problem is that i can check all of the radioButtons . What i want is when i select one RadioButton , the others are automatically unchecked , so that only one may be checked at any time.
Here is the snippet:
private RadioButton rbtnWater;
private RadioButton rbtnRoad;
private RadioButton rbtnBoy;
private RadioButton rbtnLeft;
rbtnWater = (RadioButton) findViewById(R.id.rbtnWater);
rbtnRoad = (RadioButton) findViewById(R.id.rbtnRoad);
rbtnBoy = (RadioButton)findViewById(R.id.rbtnBoy);
rbtnLeft = (RadioButton) findViewById(R.id.rbtnLeft)
//method called at onClick attribute (XML)
public void checkState(View v) {
if (rbtnWater.isChecked()) {
rbtnLeft.setChecked(false);
rbtnBoy.setChecked(false);
rbtnRoad.setChecked(false);
}
else if (rbtnLeft.isChecked()) {
rbtnBoy.setChecked(false);
rbtnWater.setChecked(false);
rbtnRoad.setChecked(false);
}
else if (rbtnBoy.isChecked()) {
rbtnWater.setChecked(false);
rbtnRoad.setChecked(false);
rbtnLeft.setChecked(false);
}
else if (rbtnRoad.isChecked()) {
rbtnWater.setChecked(false);
rbtnBoy.setChecked(false);
rbtnLeft.setChecked(false);
}
}

It not really clear what is not working, please add details
That being said I suggest you change the technique and use a RadioGroup anyway, just extend it and modify to what you need or take it's source code from AOSP and instead of having it extend a LinearLayout you can extend something else you would rather work with

Related

Xcode UITest scrolling to the bottom of an UITableView

I am writing an UI test case, in which I need to perform an action, and then on the current page, scroll the only UITableView to the bottom to check if specific text shows up inside the last cell in the UITableView.
Right now the only way I can think of is to scroll it using app.tables.cells.element(boundBy: 0).swipeUp(), but if there are too many cells, it doesn't scroll all the way to the bottom. And the number of cells in the UITableView is not always the same, I cannot swipe up more than once because there might be only one cell in the table.
One way you could go about this is by getting the last cell from the tableView. Then, run a while loop that scrolls and checks to see if the cell isHittable between each scroll. Once it's determined that isHittable == true, the element can then be asserted against.
https://developer.apple.com/documentation/xctest/xcuielement/1500561-ishittable
It would look something like this (Swift answer):
In your XCTestCase file, write a query to identify the table. Then, a subsequent query to identify the last cell.
let tableView = app.descendants(matching: .table).firstMatch
guard let lastCell = tableView.cells.allElementsBoundByIndex.last else { return }
Use a while loop to determine whether or not the cell isHittable/is on screen. Note: isHittable relies on the cell's userInteractionEnabled property being set to true
//Add in a count, so that the loop can escape if it's scrolled too many times
let MAX_SCROLLS = 10
var count = 0
while lastCell.isHittable == false && count < MAX_SCROLLS {
apps.swipeUp()
count += 1
}
Check the cell's text using the label property, and compare it against the expected text.
//If there is only one label within the cell
let textInLastCell = lastCell.descendants(matching: .staticText).firstMatch
XCTAssertTrue(textInLastCell.label == "Expected Text" && textInLastCell.isHittable)
Blaines answer lead me to dig a little bit more into this topic and I found a different solution that worked for me:
func testTheTest() {
let app = XCUIApplication()
app.launch()
// Opens a menu in my app which contains the table view
app.buttons["openMenu"].tap()
// Get a handle for the tableView
let listpagetableviewTable = app.tables["myTableView"]
// Get a handle for the not yet existing cell by its content text
let cell = listpagetableviewTable.staticTexts["This text is from the cell"]
// Swipe down until it is visible
while !cell.exists {
app.swipeUp()
}
// Interact with it when visible
cell.tap()
}
One thing I had to do for this in order to work is set isAccessibilityElement to true and also assign accessibilityLabel as a String to the table view so it can be queried by it within the test code.
This might not be best practice but for what I could see in my test it works very well. I don't know how it would work when the cell has no text, one might be able to reference the cell(which is not really directly referenced here) by an image view or something else. It's obviously missing the counter from Blaines answer but I left it out for simplicity reasons.

TableView - Display Image If Cell Boolean Value Is True

My TableView is populated with data from a list of objects. The first column is a Boolean value.
Instead of displaying True or False in the cell, I would like to display an image if True and leave the cell empty if it's False.
This is how I populate the TableView:
colStarred.setCellValueFactory(new PropertyValueFactory<>("starred"));
colDate.setCellValueFactory(new PropertyValueFactory<>("date"));
colTime.setCellValueFactory(new PropertyValueFactory<>("time"));
I know that I need to use a custom TableCell and a CellValueFactory but I'm having a hard time wrapping my head around the documentation (I have not used Java factories in the past).
My research has lead to several answers regarding similar situations, but they all seem to deal with just displaying an image in the cell. I have been unable to find a solution for checking a boolean to determine whether an image should be displayed or not.
How do I check the starredProperty of my objects and show an image if it is True?
Thank you for all the help everyone has provided me in the past!
I'll assume the column to be a TableColumn<MyItemClass, Boolean>.
You simply create TableCells that adjust their look according to the item that gets passed to the updateItem method.
In this case we'll use a ImageView as graphic of the cell.
The following images are displayed depending on the item of the cell:
no image if the cell is empty or contains null
imageTrue if the item is true
imageFalse otherwise
You may of course use imageFalse = null for an empty cell when the item is false.
final Image imageTrue = ...
final Image imageFalse = ...
// set cellfactory
colStarred.setCellFactory(col -> new TableCell<MyItemClass, Boolean>() {
private final ImageView imageView = new ImageView();
{
// initialize ImageView + set as graphic
imageView.setFitWidth(20);
imageView.setFitHeight(20);
setGraphic(imageView);
}
#Override
protected void updateItem(Boolean item, boolean empty) {
if (empty || item == null) {
// no image for empty cells
imageView.setImage(null);
} else {
// set image for non-empty cell
imageView.setImage(item ? imageTrue : imageFalse);
}
}
});
What happens when the program is displayed is this:
The TableView creates cells needed to display the items using the cellfactories.
The TableView assigns the items to the cells. These items may be changed multiple times. Cells may also become empty after being filled. When this happens the updateItem methods of the TableCells are called.

Programmatically select and clear selection on an ADF DVT piegraph

I am using a pieGraph and doing some page interactions based on clicking on the pie-graph. These work just fine.
<dvt:pieGraph id="graph1" tabularData="#{dc.bean.tabularData}" dataSelection="single"
selectionListener="#{dc.bean.transfersGraphSelectionListener}"/>
However I am not able to support the following use cases
Clicking outside the graph(or clicking a selected data set again) should cause the pie-graph to lose its selection.
Having a clear button on the page which forces the graph to lose its current selection.
Programmatically select one of the data sets in the graph
I checked the UIGraph API but couldn't find much information.
Any hints would be really helpful.
please add the right code to your original post. this is what your code look like
transfersGraphSelectionListener(SelectionEvent selectionEvent){
Set<GraphSelection> selectionSet = selectionEvent.getGraphSelection();
for (GraphSelection selection : selectionSet) {
if (selection instanceof DataSelection) {
DataSelection ds = (DataSelection) selection;
Set seriesKeySet = ds.getSeriesKey().keySet();
for (Object key : seriesKeySet) {
Object selectedKey = ds.getSeriesKey().get((String) key))
}
Looks like something is missing!

How to retrieve the controls inside the selected item from a listbox in WP

I'm now facing the most common problem faced my many while working with listboxes. Though I found many answers the the forum, nothing seems to work for me or else i have got it wrong. .
I have created a listbox through code. Every listbox item has a stackpanel and within it two textblocks. the stackpanel has vertical orientation.The foreground of the textblocks have been set to specific colors. When an item has been selected or clicked it moves to another page and on the close of the new page it returns to the old page.
My problem is that, when a listbox item has been clicked, it does not show the selection color which is by default the phones accent color before moving to the next page. Is it because the color of the textblocks have already set while creating the listbox?
So i tried to set it the foreground of the selected item through the SelectionChanged() like this
ListBoxItem selItem = (ListBoxItem)(listboxNotes.ItemContainerGenerator.ContainerFromIndex(listboxNotes.SelectedIndex));
selItem .Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
But this does not work, and i assume its cuz there is a stackpanel inside the item.
How exactly this needs to be done? Do i need to retrieve the textblocks inside the stackpanel and set the foreground?? I have not used binding here. Visual Tree Helper???
Thanks
Alfah
In general, the selected color doesn't change on lists where you're navigating.
From my experience with android, there's no 'selector' background on WP7. If you're looking for a cool UI effect that shows some action is happening, the TiltEffect is definitely recommended, and very easy to implement.
http://www.windowsphonegeek.com/articles/Silverlight-for-WP7-Toolkit-TiltEffect-in-depth
However - if you're creating an app that doesn't have immediate navigation, it is possible that you might want a 'selected' cell color / etc. I've attached an image:
https://skydrive.live.com/redir.aspx?cid=ef08824b672fb5d8&resid=EF08824B672FB5D8!366&parid=EF08824B672FB5D8!343
If you note here, the buttons are related to the selected item on the list - I.e. the user can perform 4 different actions based on the selected item, (but they must select an item first!).
internal void SelectionChanged()
{
var item = (((ListBoxItem) _view.servers.SelectedItem).Content) as StackPanel;
if (item != null)
{
for (int i = 0; i < _view.servers.Items.Count; i++)
{
var val = (((ListBoxItem) _view.servers.Items[i]).Content) as StackPanel;
var tb = val.Children[0] as TextBlock;
var tb2 = val.Children[1] as TextBlock;
if (i == _view.servers.SelectedIndex)
{
tb.Foreground = tb2.Foreground = (SolidColorBrush) App.Current.Resources["PhoneAccentBrush"];
}
else
{
tb.Foreground = tb2.Foreground = (SolidColorBrush) //regular color here, b/c all these should no longer be selected
}
}
}
}
The ListItemContainer will have it's Foreground changed automatically. To inherit this, simply don't specify a colour (or style) on your TextBlock.

How to implement this logic flow?

ok, here is the story...
I have 3 textfield for user to select....
[textfield A][textfield B][textfield C]
and a confirm button, the user need to add three textfield, after that , the user need to click the confirm button.... but based on different select order, the result is different, for example:
A>B>C, I will show red.
When the user select in this order:
B>A>C I will show green.
When the user select in this order:
C>B>A I will show the color blue....
based on different user select order, it will show different color....
But the question is, when I add more and more textfield, how can I implement this logic?
First, I design to have an array , when the user select one textfield, I store the textfield id to array, when user select the second one, I will store in the array, until the user click confirm, I read back the array to display the color....
But I think it will become very big & messy when more and more textfield is added, any better ideas? Thank you.
It's a bit of a hack, but what I'd be inclined to do is store the selections in a string that gets appended to each time (starting with empty string of course), trimming to the rightmost x characters. Then you can do a simple switch/case statement to determine the color. For example (C# fragments, sort of):
string selectStr = string.Empty;
void Select(string btn) {
selectStr += btn;
selectStr = selectStr.Remove(0, btn.Length - 3);
}
void Confirm() {
switch (selectStr) {
case "ABC" : /* make red */ break;
case "BAC" : /* make green */ break;
// etc.
}
}

Resources