EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

React Native Accessibility

By Rahul DubeyRahul Dubey

Home » Software Development » Software Development Tutorials » React Native Tutorial » React Native Accessibility

React Native Accessibility

Introduction to React Native Accessibility

Accessibility means the ability to access something. Many users are not able to access different websites, apps, or mobile phones easily. So, the accessibility option helps them by providing a screen readers, voice enhancement, navigators, and many more. Web accessibility is a kind of creation of a website that can be used by anyone. Both the major platforms including Android, iOS have their own set of accessibility options like a voiceover in iOS and talkback in Android. React native also provides a set of complementary accessibility options for making the app usable for everyone. This article would explain the use of accessibility through different examples for a better understanding.

React Native Accessibility Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • accessible

<View accessible={true}>
<Text
>Heyoo<
/Text>
</View>

  • accessibilityLabel & accessibilityHint

<TouchableOpacity
accessible={
true}
accessibilityLabel="Press Me!"
accessibilityHint="The previous screen is navigated"
onPress={
onPress}>

React Native Accessibility working with Examples

Example #1

Below, the accessibility label property is used to a custom string on our Touchable. The accessibility label on the TouchableOpacity element would default to “Click me!”, which navigates the window to the previously used window. In the iOS platform, if the user has hints enabled in the device’s VoiceOver settings then VoiceOver will read the hint after the label. An accessibility hint helps users to understand what will happen on account of a certain action is performed by them on the accessibility element when they are unable to understand the result from the accessibility label.

[i] App.js

import React
, { useState
, useEffect } from "react";
import { View
, Text
, StyleSheet
, TouchableOpacity
, ImageBackground
, Alert } from "react-native";
export default function App() {
return (
<ImageBackground
source={
{
uri:
'https://images.pexels.com/photos/4453074/pexels-photo-4453074.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
}
}
style={
{
flex: 1
}
}
>
<View>
<TouchableOpacity
accessible={true}
accessibilityLabel="Lets go in past"
accessibilityHint="The previous screen is navigated"
onPress={() => {}}>
<View
style={{
color: '#991450'
, height: '180px'
, width: '200px'
, backgroundColor: '#8ef73e'
, textAlign: 'center'
, alignItems: 'center'
}
}
accessibilityRole={'button'}>
<Text style={
{fontSize: 36
, marginBottom: 48
, color: '#faf569'
, backgroundColor: '#54f0cb'
, alignItems: 'center'
, borderWidth: 10
, textAlign: 'center'
,borderRadius: 100
, borderColor: '#d938f5'
, justifyContent: 'center'
}
}>
Lets Go Back in Past
</Text>
</View>
</TouchableOpacity>
</View>
</ImageBackground>
);
}

Output:

React Native Accessibility 1

Example #2

Below, the application would use the user’s accessibility information through useAccessibilityInfo and would perform certain actions to record screen and also recording the users activity on account of the accessibility granted or enabled by the users in the settings for the application.

[i] App.js

import React
, { useState
, useEffect } from "react";
import { useAccessibilityInfo } from '@react-native-community/hooks'
import { View
, Text
, StyleSheet
, TouchableOpacity
, ImageBackground} from "react-native";
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center"
, justifyContent: "center"
},
});
export default function App() {
const {
reduceMotionEnabled
, screenReaderEnabled
} = useAccessibilityInfo();
return (
<ImageBackground
source={
{
uri:
'https://images.pexels.com/photos/4681107/pexels-photo-4681107.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
}
}
style={
{
flex: 1
}
}
>
<View style={styles.container}>
<Text style={{fontSize: 36
, marginBottom: 48
, color: '#2976e3'
, backgroundColor: '#faf569'
, alignItems: 'center'
, borderWidth: 10
, textAlign: 'center'
,borderRadius: 100
, borderColor: '#d40820'
, justifyContent: 'center'
}
}>
Activity Recording {reduceMotionEnabled ? "enabled" : "disabled"}.
</Text>
<Text style={{fontSize: 36
, marginBottom: 48
, color: '#9913ba'
, backgroundColor: '#bdf03c'
, alignItems: 'center'
, borderWidth: 10
, textAlign: 'center'
,borderRadius: 100
, borderColor: '#0d8c7f'
, justifyContent: 'center'
}}>
Screen Recording {screenReaderEnabled ? "enabled" : "disabled"}.
</Text>
</View>
</ImageBackground>
);
}

Output:

React Native Accessibility 2

Example #3

Below, the accessibilityActions property contains a list of action objects and each action object contains the name, type, and required fields. The action requests are handled through onAccessibilityAction function. Several custom actions of cutting, copying, and pasting the content are being performed using onAccessibilityAction function.

[i] App.js

import React
, { useState } from "react";
import { StyleSheet
, Text
, TouchableOpacity
, View
, ImageBackground
, Alert } from "react-native";
const Application = () => {
const onPress = () =>
setCount(
prevCount => prevCount + 1);
const [count
, setCount] = useState(0);
return (
<ImageBackground
source={
{
uri:
'https://images.pexels.com/photos/3663082/pexels-photo-3663082.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
}
}
style={
{
flex: 1
}
}
>
<View style={{
flex: 1,
justifyContent: "center",
paddingHorizontal: 10
}}>
<View style={{
alignItems: "center",
padding: 10,
}}>
<Text style={{
color: '#991450'
, height: '50px'
, width: '150px'
, backgroundColor: '#8ef73e'
, borderRadius: '50px'
, borderRightWidth: '5px'
, borderRightColor: '#b30b1b'
, borderLeftWidth: '5px'
, borderLeftColor: '#0b0ebd'
, borderTopWidth: '10px'
, borderTopColor: '#e3431b'
, borderBottomColor: '#22c0f5'
, fontSize: '15px'
}}
accessibilityLiveRegion="polite"
>
Ouchhh: {count} time :(
</Text>
</View>
<TouchableOpacity
style={{
color: '#991450'
, height: '50px'
, width: '150px'
, backgroundColor: '#8ef73e'
, borderRadius: '50px'
, borderRightWidth: '5px'
, borderRightColor: '#b30b1b'
, borderLeftWidth: '5px'
, borderLeftColor: '#0b0ebd'
, borderTopWidth: '10px'
, borderTopColor: '#e3431b'
, borderBottomColor: '#22c0f5'
, fontSize: '15px'
}}
onPress={onPress}
accessible={true}
accessibilityLabel="Lets go in past"
accessibilityHint="The previous screen is navigated"
>
<View
style={{
textAlign: 'center'
, alignItems: 'center'
}
}
accessible={true}
accessibilityActions={[
{
name: 'cuttingg'
, label: 'cuttingg'
},
{
name: 'copyingg'
, label: 'copyingg'
},
{
name: 'pastingg'
, label: 'pastingg'
}
]}
onAccessibilityAction={
(event) => {
switch (
event.nativeEvent.actionName) {
case 'cutting':
Alert.alert(
'Buzz'
, 'successfulll actionn of cuttingg');
break;
case 'copying':
Alert.alert(
'Buzzz'
, 'successfulll actionn of coypingg');
break;
case 'pasting':
Alert.alert(
'Buzz'
, 'successfulll actionn of pastingg');
break;
}
}}
accessibilityRole={'button'}>
<Text style={{
color: '#991450'
, height: '50px'
, width: '150px'
, backgroundColor: '#8ef73e'
, borderRadius: '50px'
, borderRightWidth: '5px'
, borderRightColor: '#b30b1b'
, borderLeftWidth: '5px'
, borderLeftColor: '#0b0ebd'
, borderTopWidth: '10px'
, borderTopColor: '#e3431b'
, borderBottomColor: '#22c0f5'
, fontSize: '15px'
}}
>Attack</Text>
</View>
</TouchableOpacity>
</View>
</ImageBackground>
);
};
export default Application;

Output:

  • On code execution on iOS platform

React Native Accessibility 3

  • On Clicking “Attack” on iOS platform

React Native Accessibility 4

  • On code execution on Web Platform

output

  • On Clicking “Attack” on web platform

output 1

Conclusion

On the basis of the above article, we can understand the concept of React Native accessibility. Different examples are explained in this article for a better understanding and implementation of accessibility according to the specific requirement. This article would help the beginners, who are exploring the react-native environment.

Recommended Articles

This is a guide to React Native Accessibility. Here we discuss the introduction, React Native Accessibility working with Examples and code implementation respectively. You may also have a look at the following articles to learn more –

  1. React Bootstrap
  2. React Native SVG
  3. React Native Modal
  4. React Native Tab Bar

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
React Native Tutorial
  • Basic
    • What is React Native?
    • React Versions
    • React Constructor
    • React Native Architecture
    • React Native Libraries
    • React Components Libraries
    • React Native Components
    • React Component Library
    • React Component Lifecycle
    • React Native Framework
    • React Higher Order Component
    • React Ternary Operator
    • React Native Charts
    • React Native Layout
    • React Native Grid
    • React Native Fetch
    • React Native Modal
    • React Native SVG
    • Button in React Native
    • React List Components
    • React Native Element
    • React Native FlatList
    • React Native SectionList
    • react native dropdown
    • React Native Menu
    • React Native State
    • React State Management
    • React Native Tabs
    • React Native Tab Bar
    • React Format
    • React-Native Switch
    • React Native Firebase
    • React Native Flexbox
    • React Native StatusBar
    • React Native ScrollView
    • React Native ListView
    • React Native TextInput
    • React Native Table
    • React-Native Border Style
    • React Native Search Bar
    • React-Native StyleSheet
    • React Native Vector Icons
    • React Native Login Screen
    • React Native Splash Screen
    • React Native Image Picker
    • React Native Navigation
    • React Native Swift
    • React Controlled Input
    • React Fragment
    • React Native Color
    • React Portals
    • React Refs
    • React shouldComponentUpdate()
    • React ComponentDidMount()
    • React componentWillUpdate()
    • React Native Orientation
    • React Native Animation
    • React Native Form
    • React Props
    • React Native Linear Gradient
    • React Native AsyncStorage
    • React Error Boundaries
    • React Native Progress Bar
    • React-Native Calendar
    • React Native Linking
    • React Native DatePicker
    • React Native Image
    • React Native Drawer
    • React Native Drawer Navigation
    • React Native Fonts
    • React Native Overlay
    • React Native OneSignal
    • React Native Template
    • React Native Router
    • React Router Transition
    • React Dispatcher
    • React Native Redux
    • React JSX
    • React native keyboardavoidingview
    • React Native Permissions
    • React Redux Connect
    • React Native Material
    • React Native Gesture Handler
    • React Native Theme
    • React Native Accessibility
    • React Native Justify Content
    • MobX React Native
    • React Native Authentication
    • React Native UUID
    • React Native Geolocation
    • React Native Debugger
    • React Native Carousel
    • React Native Local Storage
    • React Native TypeScript
    • React Bootstrap
    • React Native SQLite
    • React Native Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More