Moving Focus to GridView's first item view - android

In my activity i have two fragments 1st one contains a linearlayout and second one contains a gridview and i have to support D-pad navigation in my app.
now when user press key to movetowards grid view i want to show the 1st item of gridview as selected view
so every time when focus moves from linearlayout to gridview , gridview's first item should get selected/focused
any leads on these is highly appreciated

on keypress towards gridview you can call:
firstItem.requestFocus();
replace "firstItem" by your element name, such as button, edittext, etc
OR
you can implement OnFocusChangeListener to your activity and do this:
gridView.setOnFocusChangeListener(this);
and then
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
firstItem.requestFocus();
}
}

Related

Android How do i change Items in Recyclerview on scrolling?

I have set up an Recyclerview with textviews and hidden buttons per row.
if the user clicks the item, the buttons are shown.
If the user clicks another item, the buttons are hidden.
The recycleview sends the position to the activity per interface which changes its property.
As long as the recyclerview is not scrolled, everything works perfectly.
Layoutmanager.getchildat(position)
Now, when items exceed the view, and the user scrolls the recyclerview, the position gets wrong. It adds up 1 per item scrolled down that the user has clicked. For Example the third Item is the first to be seen, the user clicks the fifth, the buttons are shown in the eight row.
How to fix that? Can i access a gettarget property? I tried smoothcontroller.gettargetposition and subtracted it from the position but its not the right solution.
Edit:
Just added a onViewClick(View v) to the interface and saved the viee to activity when buttons set to visible. When another view is clicked, the saved view is used to hide the buttons. (View oldView = view)
Just added a onViewClick(View v) to the interface and saved the viee to activity when buttons set to visible. When another view is clicked, the saved view is used to hide the buttons. (View oldView = view)

Keyboard navigation in listview having multiple focusable element

I have a Android ListView which can have contains different types of listItem.
Some of the listitems have multiple focusable element themselves.
I am looking for a way to be able to navigate list using hardware keyboard as well as be able to select focusable elements inside listitem and navigate among them.
For example in below image all of the listItem have multiple buttons. So when I open my Activity, I should be able to navigate through listitem using keyboard up and down arrow.
But when I press Enter ( or D-PAD), then focus should go to first button inside that listItem. After that I should be able to navigate through all buttons using arrow keys
I am able to achieve first case when I set focusable false for all the buttons and set listView.setItemsCanFocus(false) on listView itself.
I have tried setting the setFocusable(false) before returning the view from adapter and then handling them in list OnItemClickListener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View v = listView.getChildAt(position);
v.setFocusable(true);
v.requestFocus();
Button b1 = (Button)(listView.getChildAt(position).findViewById(R.id.butt1));
Button b2 = (Button)(listView.getChildAt(position).findViewById(R.id.butt1));
b1.setFocusable(true);
b2.setFocusable(true);
b1.requestFocus();
}
});
But It is also not giving focus to button.

Custom Listview with EditText: Focus

I have a simple layout with EditText and a Listview. Implemented custom ArrayList adapter to fill in the listview and custom filter so user can search for items displayed in the listview.
When I run the application, the focus is initially set to EditText and the keyboard is displayed as expected. But here is what I want to do:
The focus should be initially set to ListView when the app is launched.
If the user wants to enter text by selecting edittext control, then the keyboard should appear.
( At this point as the user inputs text, the listview items will change - I already implmented this)
With the keyboard still open, if the user select an item in the listview, the keyboard should disappear and trigger the listview onItemClick function.
How can I accomplish this?
1.Add below attribute to your particular EditText in its layout-xml:
android:focusable="false"
android:focusableInTouchMode="true"
2.Then in your Activity add:
mEditText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
return false;
}
});
Hope this helps.

add buttons at the end of selected listview item in android dynamically?

I have a listview in which my list contain textview,means I have a list of text where user can select the text, read it and set margin note at the end of that text and a button is created over there. So when next time the user read that text then the button/buttons is there and when click on that button a popup is open where he can see his margin note.
Now the problem is that he can create more than one margin note at the end. In this case I have to create one or more button dynamically at the end of that selected textview. So please help me I am unable to create more than one button dynamically.
There are a couple of ways to do it.
1) Create the buttons (max the user will require) already in the xml file of the list item element and set their visibility to invisible or gone. When the user selects it you can set the visibility to visible. The visibility can be set dynamically.
2) The other way is to add buttons programatically. List views do reuse views for performance purposes. this could explain as to why you are having trouble with the button moving positions. in this case you have identify the id of the list item and add and remove buttons every time the view is created in your getView method of your list adapter.
Remember to chk the condition on your getView method and set visibility or add/remove button.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
System.out.println("I clicked row item");
button1= (Button)v.findViewById(R.id.button1);
button1.setVisibility(button1.isShown() ? View.GONE : View.VISIBLE);}

Update ListView item's drawable state when child clicked

The problem:
I have a ListView item with a relatively complex layout. One of the children in the list item view is a TextView with links. The TextView can handle the clicks on links found in it(by using Linkify), and has a OnClickListener that handles click on parts of the text that are not links. The listener will just get the position of the view in the ListView and perform a click:
#Override
public void onClick(View v) {
int position = mListView.getPositionForView(v);
mListView.performItemClick(v, position, mAdapter.getItemId(position));
}
Everything works fine, except the click doesn't trigger the ListView to update the list item's drawable state. It does trigger that when you click on other TextViews in the item that are not clickable.
Thanks!
Got it working.
Set the list item view clickable and set its background as a state-list drawable, also set addStatesFromChildren to true. This will make sure the state of the list view item is changed when any of its children is clicked(assume the child is clickable).
However, this will cause the ListView itself not receiving focus, so you need to handle the click by adding View.OnClickListener to the list item itself instead of adding a OnItemClickedListener to the ListView.

Categories

Resources