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

Custom alert dialog in recyclerview adapter with JSON parsing data by using active Android library
Why is admob home page and paymets page showing different earning?
BottomNavigation Icon Tint
Fragment no longer exists when clearing whole backstack
How to use custom native java classes / functions in nativescript?
SSLProtocolException when making GET request
How to forcefully stop thread and start same thread with new variable?
Do I need separate Realm instances to “close” my RealmResults and stop auto-updating?
Is it possible to add some buttons or other different View's to Snackbar?
pushwoosh-cordova-plugin Unable to receive push message on android when the app is not in the foreground or background
Fragment is overlapping
Correct way to rotate Android images?
Is it possible to set android permission level for a broadcast receiver at runtime?
BindingAdapter custom getter 2 way data binding
flatMapFirst: flatMap which adds new observable only if the previous one ended
Android-Firebase messages not displayed in RecyclerAdapter

Categories

HOME
xml
nunit
pyqt
battery
appcelerator
cq5
tail
telegram-bot
square
pearson-correlation
code-coverage
recordrtc
elasticsearch-5
bing-maps
rcloud
x-cart
php-5.6
solrcloud
asciidoctor-pdf
qliksense
froala
dapper-extensions
windows-server-2003
xcode-ui-testing
bulletphysics
jackson-modules
appirater
android-things
ceylon
nsurl
boost-asio
scatter3d
testbed
kodi
hreflang
github3.py
sendbird
pop3
indy10
browser-extension
hivemq
docfx
metaprogramming
linq-to-objects
typesafe-config
unordered-map
odoo-website
microsoft-metro
outputcache
apollostack
signal-strength
opencmis
double-click
configurationmanager
launchctl
convex-optimization
azure-cli
skin
libjpeg
sandcastle
change-tracking
taocp
meteor-packages
reporting-services-2012
latin
centrifuge
watch-os-2
mapinfo
jtwig
http-status-code-401
relativelayout
xenomai
include-guards
fuelcms
doctype
viola-jones
dnssec
bufferedinputstream
zxspectrum
phpldapadmin
public-html
delayed-execution
theos
sip-server
crowd
machine-instruction
genymotion-call
linkedin-jsapi
cbind
template-deduction
tinyxml
gnumeric
cffile
octal
actiondispatch
font-awesome-4.0.0
monotouch.dialog
vfs
wcf-configuration
correlated-subquery
lambdaj
inkcanvas
google-floodlight
google-ajax-api
tmx
cdonts
murmurhash
contentpresenter

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