android


Android - How to set visibility in xml depending on device?


I have a TextView in my activity that should be visible normally, but gone for tablet devices.
I know I can create a new layout file for the tablet, but that seems like a lot of duplication, so what I am trying to do is set in my (single) layout file something like...
android:visibility="#string/my_textview_visibility"
...for the TextView. And then, in my strings resources files, set...
<string name="my_textview_visibility">gone</string> (in values/strings.xml)
...and...
<string name="my_textview_visibility">visible</string> (in values-sw720dp/strings.xml)
...to hide the TextView for tablets.
However, when I try this, the app crashes when trying to show that activity.
Do I need to use the constant values instead of the above string values - e.g.,
"visible" -> 0
"gone" -> 8
..and, if so, what is the correct way to add/reference those values to/from my XML files?
Or is there another/better way of doing it?
NB - I do not want to show/hide the TextView programatically in my Java code.
You should be using /values/integers/ instead:
values/integers.xml
<integer name="my_textview_visibility">0</integer> <!-- 0 = View.VISIBLE -->
values-sw720dp/integers.xml
<integer name="my_textview_visibility">2</integer> <!-- 2 = View.GONE -->
Then called like so:
android:visibility="#integer/my_textview_visibility"
ChrisStillwell's answer is correct. But the values are incorrect according to the Android Documentation
As you said, the best way to do this is to create a specific layout for tablets where your TextView will be hidden. However, it is possible to do this using xml boolean resources : (as res/values-sw600dp/):
<resources>
<bool name="isTablet">true</bool>
</resources>
Because the sw600dp qualifier is only valid for platforms above android 3.2. If you want to make sure this technique works on all platforms (before 3.2), create the same file in res/values-xlarge folder:
<resources>
<bool name="isTablet">true</bool>
</resources>
Then, in the "standard" value file (as res/values/), you set the boolean to false:
<resources>
<bool name="isTablet">false</bool>
</resources>
Then in you activity, you can get this value and check if you are running in a tablet size device:
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
// do something
} else {
// do something else
}
Font : Determine if the device is a smartphone or tablet?

Related Links

How can we develop e commerce with different UI in android [duplicate]
ConstraintLayout Notification Badge
Unable to create call adapter for io.reactivex.Observable
How to use getMapAsync() instead of getMap()
Finding a Fragment after configuration change
running parallel tests - genymotion and cucumber
Error compiling mtpd/mtpd.o: undefined reference to `android_get_control_socket'
How to arrange the chip in second row when the first row is filled in linear layout horizontal?
Want to add horizontal spacing between cards of CardView in android?
Indoor map in android
Error “No static method isCtrlPressed” in Android
Android.mk File: Relative path of all directories and subdirectories
Android change the name of app which is to installed on homescreen
how to set value from textwatcher to specific multiple textviews in android
ExifInterface: Invalid image.java.io.IOException
Reset all checkbox in Android

Categories

HOME
checkstyle
c++11
3d
mysql-workbench
aws-cli
visual-studio-emulator
limit
extjs4.2
octave
ibeacon-android
favicon
comsol
modbus-tcp
event-sourcing
sigma.js
ios10.2
qliksense
comparator
tiki-wiki
licensing
shared
linked-data
openmdao
cppreference
user-defined-functions
sqlpackage
calculation
aws-codecommit
record
sql-server-2000
hibernate-validator
playstation-portable
laravel-eloquent
contacts
indy10
graphhopper
filesystemwatcher
sim-card
signals-slots
datacachefactory
portforwarding
postgresql-8.4
oracle-service-bus
apache-directory
facebook-pixel
google-cse
azure-cli
avx
boo
cpu-cache
py2neo
vibrate
rocks
synonym
google-query-language
phpgrid
custom-errors
sciruby
java-money
file-move
opserver
page.js
typeconverter
aspectj-maven-plugin
jquery-forms-plugin
administration
metatrader5
easyrtc
machine-instruction
gii
insight.database
angular-carousel
kissfft
codeplex
mintty
grails-domain-class
authlogic
decoupling
twitter-bootstrap-rails
visual-studio-express
hibernate-entitymanager
kendo-dataviz
cmmi
route-provider
pclzip
mysql-error-2013
wimax
web-notifications
redis-py
frameworkelementfactory
qtconcurrent
correlated-subquery
thttpd
floating-point-exceptions
cac
squishit
jqueryform
tabpanel
spread
linfu-dynamicproxy
int64
image-capture
great-circle

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App