Get index of focused cell and set focus programmatically - java

I am developing an Android App where I have a TableLayout and each cell has an EditText in it. I want to navigate through the cells using four buttons. For example: Two buttons for up-down navigation (same column, different rows) and two buttons for left-right navigation (same row, different columns). Then I want to highlight the cell that has the focus at that moment (e.g. by changing the background color of that EditText).
I want to know if there is a way I can set the focus (for navigation) programmatically and get the index of the focused cell (for highlighting)? Thanks in advance.

You can use View.requestFocus() to force focus to a view. For the index, you can use a variable for each axis. For example, everytime the right button is pressed you can increase the value of your x-axis variable by 1. And decrease when the left button is pressed. You should initialize those axis variables correctly.

Related

How to change items' properties in RecyclerView when chosing a specified item programatically?

I want to change the background colour of an item in a list in RecyclerView using LinearLayoutManager.scrollToPosition(mPosition). I have two buttons which increment or decrement the position number. So, on specifying an item (going to that position using scrollToPosition), I want to highlight it (changing background colour for instance) to indicate the specified one. The case is that we don't have to use touch to scroll up and down and selecting the items (something like using tab key on keyboard to jump between items in the recyclerview).
I've tried OnFocusChanged and assigning an xml instead of background to see the different behaviors like on state_selected or state_focused but none of them was correct. So, What should I do?
If I understand correctly, you want to change the background color after scrolling to this position?
Try this:
LinearLayoutManager.scrollToPosition(mPosition)
View view = LinearLayoutManager.getItem(mPosition);
view.setBackgroundColor(color);

How to navigate through Vaadin grid and select an item using keyboard?

I'd like to know if it's possible to navigate through Vaadin grid or treegrid and select an item using only keyboard arrow keys? From what i've seen while testing the components, the default behavior seems to be either to move only to one specific cell in grid or to a specific row in treegrid. Selection can be achieved if the user presses spacebar.
I've tried to add a shortcutListener to grid but it doesn't seem to work with arrow keys. And the grid scrollbar doesn't move with the selected item.
grid.addShortcutListener(new ShortcutListener("Down", KeyCode.ARROW_DOWN, null) {
#Override
public void handleAction(Object sender, Object target) {
//..//
selectedItem = dataSource.get(currentSelectedItemIndex);
grid.select(selectedItem);
grid.scrollTo(currentSelectedItemIndex); // this doesn't seem to do anything??
//..//
}
});
I guess my problem is that i don't know how to acquire event that moves selection to other cell/row.
Here's an image to represent the problem which i'm facing. The item that has only blue border was selected using arrow keys. I'd like to select an item automatically when user presses arrow keys (Down or Up) without the spacebar.
Image taken from here: https://demo.vaadin.com/sampler/#ui/grids-and-trees/grid/features
Edit1:
I'm using latest version of Vaadin - 8.1.6.
Edit2:
I tried to add couple of listeners to see if i could at least register the movement to the next/previous cell by using arrow up/down but without any luck.
Here's a list of listeners i've tried:
addSelectionListener - only registers selection after spacebar press
or mouse click. Not quite what i'm looking for.
addItemClickListener - only registers selection from mouse click.
addShortcutListener - registers pressed key but it doesn't work with arrows.
Is there any listener that could potentially help me with this issue?
The Grid component has basic keyboard navigation. If you need advanced options, like you have mentioned, for keyboard navigation, I would warmly recommend to test this add-on:
https://vaadin.com/directory/component/gridfastnavigation-add-on

How can I prevent a CodenameOne GUI Element from scrolling (tickering?) when it changes to Bold?

I am building my first CodenameOne app using the GUI Builder. I've defined a style for my buttons where the "unselected" state is regular text and the "selected" or focus state has Bold text.
The problem I'm seeing is that once a button gains focus, the bold text is slightly wider than the original text and it starts scrolling (I think the effect is referred to as "tickering"):
Unselected Button Image
Selected Button Image
I see this in the simulator and on my Samsung SG6 testing. Is this a bug or is there something I can do to pre-size it for the bold text first so it doesn't start scrolling when it gets focus? I don't want to make the button larger than it needs to be (ie: fit it to a container)
You need to do 2 things,
first reduce the left and right padding of the selected button UIID and then call this in your code
mybutton.setEndsWith3Points(false);
mybutton.setTickerEnabled(false);
As an extra, place the button in a container with the right layout.
Note that since the text is now Bold, it's normal for the button to gain extra width. So I will suggest you make a room for that.
Edit:
I noticed that the button was tickering when you took the snapshot so ignore the first point about padding.

Want to change color of a grid on second click

I have created a custom calendar, in which i'm setting the color for a single grid when it is clicked. My requirement is to change the color to some other color on second click. I tried achieving this by placing a onclick listener inside another onclick listener. It worked but its very buggy. sometimes the 2nd color stays in the previous grid or sometimes it doesnt work and sometimes it 'll turn to second color when i click on a previously clicked grid.
My requirement is to change the color of a grid to color1 on 1st click and color2 on second click on same grid cell.
try using counter for onClick. and according to counter change the color of grid..
set initially 0, if clicked once set it to 1 change the color and if clicked again increase counter and change color again. don't use onclick inside onclick.
use index counter and in onClick listener update the counter and change the color.
you can go for
GestureDetector
you can find more of it here - Detecting Common Gestures
with this you can detect common gestures like touch, down, fling, single tap, double tap, long press scroll

How to set the button unmovable?

In android app, I have display the number of buttons in number of rows in GridView using adapter, If i click the button in first row right corner automatically the second row buttons are moved to first row and hide the first row buttons by second row buttons. i need to fix the solution for, the button is not move.
Anyone can give idea to fix the solution for this?
1. Use fixed size Buttons.
2. The professional way to handle it, is usingIcons Images.
In GridView display the buttons inside the EditText and and set the background color as white for EditText, Now the buttons are not moving when click the button in first row on the GridView.

Categories

Resources