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

Android studio V0.8 cannot create new Glass Module with error “The component Glass Module has a minimum SDK level of 19”
MediaPlayer NullPointerException after OnBackPressed Android
Saving answers before leaving fragment. [android]
How to set our own background image to facebook Login Button in xml?
Make an Intent reliable for Fragment
Bluetooth OOB Handover via NFC without User Confirmation (or: how does Android Beam work)
Android HCE service
How to start audio streaming from particular position in seekbar android
Android color background transition like css
How to use correct Android theme when using programmatic components?
how to add images in array from sd card instead of drawable
Adding tabs again to tab fragment activity in android
How to store database value in variable
“Hello World” app giving Unfortunately (app) has stopped working [duplicate]
Android contentprovideroperations insert or update
How to cancel DelayedConfirmationView?

Categories

HOME
batch-file
go
phpword
facebook-oauth
api
time-complexity
linkedin
jmeter
dns
css3
datastore
ns-3
pclxl
sublimetext
appcelerator
dojo
parameters
hpc
settings
frontend
boto3
dkim
enthought
frequency-distribution
standards
mvc-mini-profiler
solrcloud
database-connection
replication
user-experience
rubymotion
springfox
babel
named-entity-recognition
react-toolbox
postscript
gmock
tiki-wiki
wcag
sinch
uitapgesturerecognizer
google-cast
volatile
linkedhashmap
carriage-return
apiary.io
ideascript
sessionstorage
mongoose-schema
apriori
movilizer
placeholder
libgphoto2
opennms
fclose
geotiff
scalding
backbone-collections
avx
type-theory
mouseleave
baidu
materialized-path-pattern
pure-css
envi
adafruit
neon
model-associations
eigenvector
mediametadataretriever
lua-telegram-bot
tracker-enabled-dbcontext
kiosk
ikiwiki
uiactionsheet
database-relations
blitline
piracy-prevention
tfs-workitem
forwarding
jdk1.7
matlab-deployment
openbabel
android-cookiemanager
named-parameters
template-deduction
verold
parallel-testing
tinyxml
dct
vsx
qscrollarea
hibernate-entitymanager
c++builder-xe5
observium
mantle
joomla3.1
garbage
fusefabric
google-maps-mobile
fsevents
automationelement
listactivity
dataform
appledoc
facebook-iframe
hbm2java
django-piston
data-mapping
web-application-project
mathematical-typesetting

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