My application is supporting accessibility feature and application is having both portrait and landscape mode.
In my screen i have some views like button, textview, listview with custom row.
The issue what i am facing is when user focus any item in portrait mode and rotate screen, application is not focusing the same element in landscape mode. Can some one suggest how to set the focus to the item which was selected in portrait mode to landscape mode?
i even did some research on existed applications like native settings apps-wifi page and "ES file explore", in these applications also accessibility is not maintained when user change the orientation to landscape mode. System is selecting some random elements in landscape to portrait or vice versa.
Below is the code snippet
accessibility_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Name"
android:focusableInTouchMode="true"
android:text="Name" />
<TextView
android:id="@+id/email_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Email"
android:focusableInTouchMode="true"
android:text="Email" />
<Button
android:id="@+id/sample_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Button description"
android:text="ButtonText" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Checkbox description"
android:text="Checkbox Text" />
<ListView
android:id="@+id/sample_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false" >
</ListView>
</LinearLayout>
sample_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/list_row_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</CheckBox>
<TextView
android:id="@+id/list_row_name"
android:layout_width="match_parent"
android:focusableInTouchMode="true"
android:focusable="true"
android:layout_height="wrap_content" />
</LinearLayout>
AccessibilitySampleActivity.java
public class AccessibilitySampleActivity extends Activity {
private String TAG = AccessibilitySampleActivity.class.getSimpleName();
ListView sampleList;
Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accessibility_sample);
sampleList = (ListView) findViewById(R.id.sample_list);
myButton = (Button) findViewById(R.id.sample_button);
ArrayList<String> countriesList = new ArrayList<String>();
countriesList.add("India");
countriesList.add("America");
countriesList.add("China");
countriesList.add("Swis");
countriesList.add("Paries");
countriesList.add("Pak");
countriesList.add("Aus");
countriesList.add("Afg");
countriesList.add("Nedharnalds");
countriesList.add("Bangladhesh");
countriesList.add("Srilanka");
countriesList.add("France");
countriesList.add("Japan");
countriesList.add("SouthAfrica");
countriesList.add("Iran");
countriesList.add("Malaysia");
countriesList.add("Nepal");
sampleList.setAdapter(new CustomArrayAdapter(this, R.layout.sample_list_row, countriesList));
}
class CustomArrayAdapter extends ArrayAdapter<String> {
Context context;
int resource;
ArrayList<String> countriesList;
class ViewHolder {
TextView name;
}
public CustomArrayAdapter(Context context, int resource, ArrayList<String> countriesList) {
super(context, resource, countriesList);
this.context = context;
this.resource = resource;
this.countriesList = countriesList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(resource, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.list_row_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(getItem(position));
return convertView;
}
@Override
public int getCount() {
return super.getCount();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static View findAccessibilityFocus(View view) {
if (view == null)
return view;
if (view.isAccessibilityFocused())
return view;
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View childView = viewGroup.getChildAt(i);
View result = findAccessibilityFocus(childView);
if (result != null)
return result;
}
}
return null;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(TAG, "onConfigurationChanged");
AccessibilityManager am = (AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE);
boolean isAccessibilityEnabled = am.isEnabled();
boolean isExploreByTouchEnabled = am.isTouchExplorationEnabled();
Log.d(TAG, "isAccessibilityEnabled:" + isAccessibilityEnabled + " isExploreByTouchEnabled:"
+ isExploreByTouchEnabled);
if (isAccessibilityEnabled) {
View activityView = this.findViewById(android.R.id.content);
Log.d(TAG, "activityView:" + activityView);
View selectedView = findAccessibilityFocus(activityView);
if (selectedView != null) {
Log.d(TAG, "selectedView:" + selectedView);
selectedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
}
}
}
}
For normal views like TextView, Button, Checkbox it is able to maintain state when user rotate screen.
If the user select list view below are the issues i am facing
- If any view is selected in portrait and rotated screen, then focus is not maintained in landscape. Some times If the selected view is able to display without scrolling(like if user select India and rotate India will be in visible are without scrolling) it maintain the state
- When user rotate screen from portrait to landscape, apply scrolling and select any view(like Japan) and change orientation form landscape to portrait mode, then we could see that it always select first row checkbox ie checkbox of india.