textIsSelectable property doesn't work with a Toolbar - android

I have a normal TextView with the android:textIsSelectable property on true. This was working perfectly fine, even while I have the TextView inside of a ListView. But now I decided to use the new Toolbar as my ActionBar and it doesn't work anymore. I don't get a crash or anything, I only see the screen flickering on Lollipop devices when I longPress the TextView. On pre-Lollypop devices I see nothing happening.
Does anybody else have the same issue and is there a fix for it?

If you app theme extends one of .NoActionBar themes, you need to enable windowActionModeOverlay, to fix this issue.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionModeOverlay">true</item>
</style>

In your layout.xml file: Replace fill_parent with wrap_contentfor this element.

Try this, it worked for me :
In your toolbar layout, add the textview by yourself, an example :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text = "Hi, there"
android:textColor="#ff0000"
android:textIsSelectable="true"/>
</android.support.v7.widget.Toolbar>
I tested this on android v4.0.4 ICS, its working fine for me, I am able to select text on long press.
Hope this helps...

Related

"Index: 0, Size: 0 View draw error" Android Studio design tab error

I've recently updated my AS to 4.0 version. After that I put an EditText and a TextView inside of my layout, which looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="EditText should be below me!"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="I'm a hint"/>
</LinearLayout>
However, the design tab doesn't show me the EditText at all!
At the bottom of the screenshot, you can see error. Googling it didn't help me, everything I found is Design in Android studio error Index:0 size:0.... View draw failed question, which has no answer.
What I've already done
I invalidated caches and restarted my AS but it didn't help. Also, I've changed the app's theme from
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
which had no impact. However, once I managed to change it to something like a button element style and it worked, even though it's not the solution I'm looking for. If I explicitly change the style of the EditText to things like
#style/Widget.AppCompat.Button.ButtonBar.AlertDialog
or
#style/Widget.AppCompat.Button.Borderless
I can see the EditText...
I'll appreciate any help you give me, guys. Thanks in advance!
UPDATE:
It's possible to fix the problem by switching on the "Use new Layout Rendering Engine" in the settings of the AS.
Anyway, I hope we're able to find this thing out and fix it, guys. Thank you for your attention!

How to implement Toolbar Layout

How do I remove the boundary between action and toolbar?
Bad Case
Good Case, I want to like it.
Activity XML
Fragment XML
What you are looking for is a way to remove the shadow under toolbar.
For android 4.4 or below, you will have to set the window content overlay in your activity theme like this
<style name="Theme.MyApp" parent="Theme.AppCompat.Light.NoActionBar">
...
<!--To hide shadow effect in toolbar for Android 4.4 or below-->
<item name="android:windowContentOverlay">#null</item>
</style>
For Android 5.0+, you will also need to set the toolbar elevation to 0 in the activity layout like this.
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
...
>
<android.support.design.widget.AppBarLayout
...
app:elevation="0dp">
...
</android.support.design.widget.AppBarLayout>
</LinearLayout>
Actually, many people ask about this. See answer here
Try to search for existing answer next time ;)

Toolbar logo and title are centered even though they're not supposed to be

I'm having trouble implementing a Toolbar in my Android application. I have several problems, really.
First off, here's my MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar_Main);
toolbar.setLogo(R.drawable.logo);
toolbar.setTitle(R.string.app_name);
toolbar.inflateMenu(R.menu.main_actions);
setActionBar(toolbar);
}
}
activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:nestedScrollingEnabled="false">
<Toolbar
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:id="#+id/toolbar_Main"
android:background="?android:attr/colorPrimary" />
<!-- There's an EditText here, but I think that's not the problem -->
</LinearLayout>
styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme.Material.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:colorPrimary">#color/primaryColor</item>
<item name="android:colorPrimaryDark">#color/primaryColorDark</item>
</style>
</resources>
The problems I'm having are:
the menu is gone;
the logo and title text are centered in the Toolbar, even though I'm pretty sure I haven't set any property to center or whatever.
Now the first problem I can fix by removing the setActionBar part (not sure if that's good practice though), but second one, not so much. The logo and text remain centered no matter what I try. I've tried setting the Toolbar's gravity to top|left, as well as some other things, all to no avail.
When searching on Google (or StackOverflow), all I get are results asking to center the text, which is what I don't want.
I should also mention that I'm developing the app only for API level 21, so no AppCompat and all that fancy stuff, just a Toolbar that I wish to use as the app's main ActionBar.
I'm probably just missing some tiny thing, so thanks in advance.
To me:
You should not remove the setActionBar() call;
Your menu might be disappearing because maybe you have a hardware menu button on your device. Try tapping and see what happens. To fix however, try deleting the inflateMenu() line and inflate the menu during onCreateOptionsMenu(), as usual;
Title and logo issues, as well as menu disappearing, might be due to:
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
Why these lines? Just remove them if you don't need.
If this doesn't fix, try calling setActionBar(toolbar) first, and then set title using getActionBar().setTitle() . However I'm pretty sure that removing the two window lines from your style will be enough, so do that first.

Android Toolbar too high in landscape mode

My min and target sdk is 21, and I am not using the support library.
I am using the new Toolbar widget, and generally it works, I just have a glitch in the way it looks. The action icons are centered in portrait mode, whereas the toolbar in landscape mode is higher, and the icons are not centered. Please take a look at the screenshots (read lines are just to make the problem more visible):
portrait:
landscape:
This might look like nothing, but when the actions are selected (like the 'up' arrow at the very left of the bar) the result is that a strip of few pixels below it is visible. I don't like the way it looks.
The activity does not manage any configuration changes itself.
This is my code:
styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Material.NoActionBar">
<item name="android:toolbarStyle">#style/MyToolbarStyle</item>
</style>
<style name="MyToolbarStyle" parent="android:Widget.Toolbar">
<item name="android:background">?android:attr/colorPrimary</item>
<item name="android:elevation">2dp</item>
</style>
</resources>
XML usage:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.TestActivity">
<Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
I tried messing around with the android:minHeight attribute, but setting to so, say, 30dp just moved the icons even more upwards, also in portrait mode.
I found this: https://code.google.com/p/android/issues/detail?id=77874
But there is no ?attr/actionBarSize...
How, if at all, can this problem be fixed? The phone the screenshots were taken on is a Nexus 6 (xxxhdpi), and the icon is from the google icon pack, and the biggest resource is xxhdpi. Is this the problem?
Update: the workaround from the aforementioned link does work when I use ?android:attr/actionBarSize. But, is this the only way? Seems a bit wrong, to need such workarounds for a new shiny component like this, even more so that the workaround requires an attribute value for a component to be replaced by the new one.
try to use:
android:height="?android:attr/actionBarSize"
for your toolbar.
Add this in your toolbar style:
<item name="maxButtonHeight">?attr/actionBarSize</item>
I had the same problem with android.support.v7.widget.Toolbar.
My solution:
Put your toolbar into a RelativeLayout with the height you want for your toolbar. Set your toolbars layout_height to "wrap_content" and align it to the vertical center. I hope it helps.
<RelativeLayout
android:id="#+id/main_toolbar_relative_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<android.support.v7.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
</android.support.v7.widget.Toolbar>
</RelativeLayout>

Style TextView like a Spinner with appcompat v21

I want to make a TextView look like a spinner with the new Material style.
I managed to do it with "Widget.Material.Light.Spinner" style, but I didn't find any alternative in AppCompat (v21) resources.
My xml:
<TextView
android:id="#+id/sp_league_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
tools:text="Premier league"
style="#android:style/Widget.Material.Light.Spinner"
android:textColor="#android:color/white" />
I'd go with:
style="#style/Widget.AppCompat.Spinner"
But feel free to pick another one:
The style solution didn't work for me, but I've found another solution:
I use AppCompatButton instead, and have this :
XML:
<android.support.v7.widget.AppCompatButton
android:background="#drawable/abc_spinner_mtrl_am_alpha"
... />
Java:
((AppCompatButton)findViewById(...)).setSupportBackgroundTintList(new ColorStateList(new int[][]{new int[0]}, new int[]{0xff52A1E8}));
EDIT: seems it won't work well anymore, but found another solution :
<style name="Widget.MyTheme.HeaderBar.Spinner" parent="Widget.AppCompat.Light.Spinner.DropDown.ActionBar">
</style>
It's true that it's for the action bar , but it should work for other cases too.
I Tested the the solutions mentioned here 1.Using style="#style/Widget.AppCompat.Spinner) and
2.Using Widget.AppCompat.Light.Spinner.DropDown.ActionBar
Both these works only on Android 5.x And Above Devices and do not work well for Android devices running on 4.x and below.
Hence, I am posting my solution for those who wanted to have a Drop-down like effect to TextView for all devices.
App needs to create a Drawable inside drawable folder in App, let's say dropdown_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_icon_arrow"
android:gravity="end" />
And then simply use this as a background for the TextView as below:
<TextView
android:id="#+id/mySpinnerTextView"
android:background="#drawable/dropdown_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
In AndroidX, use style="#style/TextAppearance.AppCompat.Widget.TextView.SpinnerItem"

Categories

Resources