I want to add Accessibility features to my Android app. To provide the best UX for users, I need to add focusable="true" and contentDescription tags to all control elements on the screen. The problem is, I have used ConstraintLayout. For example, I have button that consists of two ImageViews:
<ImageView
android:id="#+id/outside"
app:layout_constraintTop_toBottomOf="..."
app:layout_constraintBottom_toTopOf="..."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
style="#style/outsideElement"
/>
<ImageView
android:id="#+id/inside"
app:layout_constraintBottom_toBottomOf="#+id/outside"
app:layout_constraintEnd_toEndOf="#+id/outside"
app:layout_constraintStart_toStartOf="#+id/outside"
app:layout_constraintTop_toTopOf="#+id/outside"
style="#style/insideElement"
/>
Now, when TalkBack or Accessibility Scanner applied, first it shows outside element and then inside element. I would like to have focusable both of them. The problem is in flat structure. If I would have both of them in RelativeLayout, LinearLayout, etc., I just need to add focusable="true" to the upper layout. But I want to keep flat structure and ConstraintLayout. Any idea how to solve it with flat structure? Thanks in advance.
Related
I am trying to achieve the list of buttons for "account, privacy, security etc" like in the image. I have tried radio buttons so far but cant set their background color and they do not last from left side to right side. Any suggestions?
You may use buttons, but you need to set the attribute android:layout_width="match_parent" for them to span the whole width of the activity.
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Account"/>
I have a layout with a ChipGroup whose chips will be populated programmatically, but I want to visualise how chips will look like within the layout, is there a way to show fake chips like for example using the tools namespace?
Not really the solution, you asked for, but it gets the job done.
If someone's got a better solution, let me know.
I solved it with a hardcoded chip, which has the visibility set to GONE and inside the tools namespace visibility set to VISIBLE.
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
tools:visibility="visible"
tools:text="Leichter Genuss" />
Why all my controls(buttons, textfields) are all in upper left of my emulator when I test run it?
When you first add widgets to your App, it automatically goes to the Top-Left of the screen. Try adding some Constraints to your widgets.
To make a View position in the center of your App, add these lines of code to the bottom of a widget in your XML:
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
Example:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
/>
It Looks like you may be using a constraintLayout. Make sure that in your layout you have a vertical and horizontal constraint set on each control.
Your views are not setup up correctly in your layout. First of all, if you could give the code for your layout (activity_main.xml or something similar) it will be easier to solve your problem.
The likely culprit is either that you are using a RelativeLayout and haven't set the view to be in relative position to another or, more likely, that you are using a ConstraintLayout and haven't linked your views correctly.
If you are using constraint layout the easiest way to link your views is from the Design tab, looking at the UI of your app. Click your TextView/EditText/etc and drag from the little squares that appear on the sides to literally link to either the side of the screen or to another view.
Consider this situation:
<LinearLayout style="#style/Basic_W_mp_H_wc_Vertical">
<!-- IMAGE-1 -->
<TextView style="#style/Copyright_TextView_1" />
<ImageView style="#style/Copyright_Image_1" />
<include layout="#layout/layout_copyright_info"/>
<View style="#style/HorizontalSeparatorGray"/>
<!-- IMAGE-2 -->
<TextView style="#style/Copyright_TextView_2" />
<ImageView style="#style/Copyright_Image_2" />
<include layout="#layout/layout_copyright_info"/>
<View style="#style/HorizontalSeparatorGray"/>
<!-- IMAGE-X -->
[...]
</LinearLayout>
Now I want to hide copyright information for IMAGE-1. I have two options:
Create a wrap-layout (LinearLayout) with an Id, so I can find and hide it
Give Id for every View find and hide them.
The first option will work, but I don't want to create an extra nested layout for this.
The second option will also work, but my layout will get unreadable if every view has an Id.
I know there is a third memory-optimized option to group multiple views, but I can't remember how it works and google search gives me always ViewGroup...
Any idea?
You could try to use what's called the merge tag and pair it with the include tag. What this does is it will allow you to use the same layouts in different files (since you seem to have multiple groups together). The <merge/> tag will merge the elements in to the top-level layout (in this case a LinearLayout). I have not used it with this scenario, so I imagine what will happen is you will not be able to retrieve them in a group. It is worth a shot though.
I think option 1 is not a bad choice either for these reasons:
It's easier to code. If you don't group them together than you'll have an extra lines and remember extra IDs. With just one Layout wrapped around it, then you can just hide one layout.
It's easier to maintain. What happens if you change the copyright look? What happens if you want to change the rest of the layout? With one wrapped, it's easier to just fiddle around with one view and everything inside stays intact.
The performance hit isn't that bad. Overall, this looks like the top-most root view. Inflating this Layout won't be as bad as if you were say, inflating a ListView item. This is a one-and-done operation and that's it. You don't want to fall in to the trap of doing premature optimization where you sacrifice code maintainability in favor of optimizations without any benefit.
On an iOS App I saw such a Button:
The same I would like to do in Android, how could I achieve this?
What I tried is the following code:
<Button
android:id="#+id/widget41"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Email1 testmail#gmail.com"
android:textStyle="bold" />
Looks something like that:
Well just a normal (ugly looking) Button. I have no idea, how I could style it like in iOS. Any suggestions or links would be appreciated!
The best solution would be to create your own custom view that behaves like the iOS counterpart (though, as other users have mentioned, Android does have it's own design guidelines, and the view that you are seeing is an iOS implementation that is designed for that platform).
If you look at the iOS image above (a copy of yours with some parts highlighted), I have split it up into sections.
You could use an Android ViewGroup like a LinearLayout to create the overall image, and give the LinearLayout a border or background (which can be a bitmap image of a rounded rectangle for example (See Android Nine Patch for an example of how to make this fit multiple screens).
Firstly, for the mail icon you would need a LeftAligned ImageView
with appropriate dimensions.
Next up we have a Bold TextView containing the text "Email1".
This is followed by another TextView which is blue and uses the
elipsize property (as defined in an Android XML layout) to create
"..." at the end once the text has reached the max width it can
consume. (Use android:ellipsize="end" in the XML)
Finally we have an indicator image, which again can be an ImageView
sized appropriately.
You could also achieve this with a RelativeLayout, which would allow you to RightAlign the indicator image, LeftAlign the mail icon, and allow the text to fill the space in between that it can get hold of.
Example of Nine Patch use for the background here
That is UITableView in iOS(just like ListView in android). It depends on the list item design you do it. There is no such Button Control in Android.
You should design your own button to looks like iOS one.
Android has it own design guidelines:
http://developer.android.com/design/patterns/pure-android.html
Well, as others have clearly mentioned there is no default Button in Android like this, and for your info neither is in iOS. Its all about the design. Anything is possible, in the end it all comes to how far are you willing to go to achieve it.
Below is a simple code, that will be close to your design.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="#drawable/text_background"
android:drawableLeft="#drawable/envelope"
android:drawablePadding="10dp"
android:drawableRight="#drawable/right_arrow"
android:ellipsize="end"
android:gravity="center_vertical"
android:text="#string/email" />
There can be other ways also, like that whole view could be a ViewGroup, either a LinearLayout or a RelativeLayout and there could be multiple TextViews and ImageViews inside that.
Here is a tutorial for creating stylized android buttons. You can round the corners and change the background colors to look like the buttons in ios.
Here is a similar question.
Hope this helps.