android


How to Change Visibility of an ImageButton within a PagerAdapter?


I want to change visibility of my image buttons within my PagerAdapter. It doesn't seem like I can refer to parts of a layout in instantiateItem through id, I can only refer to the full layout. This is strange though, because it seems that I can still refer to my webviews through id.
How do I approach this? This is what I'm trying to do, but I'm getting a null pointer exception at the last line shown below:
public Object instantiateItem(View collection, int position) {
ImageButton btnRight = (ImageButton) findViewById(R.id.buttonright);
ImageButton btnLeft = (ImageButton) findViewById(R.id.buttonleft);
if (position == 0) {
resLayout = R.layout.the_webview;
urltoload = theUrls[position];
resId = R.id.webview0;
btnLeft.setVisibility(View.INVISIBLE);
View view = inflater.inflate(resLayout, null);
...
if (position != 2) {
mainWebView = (WebView) view.findViewById(resId);
mainWebView.getSettings().setJavaScriptEnabled(true);
mainWebView.loadUrl(urltoload);
}
((ViewPager) collection).addView(view, 0);
return view;
}
I need the position to set visibility accordingly, so I cannot do this in onCreate. I know for a normal adapter, I can use getView, but I cannot use it for PagerAdapter.
I have also read that I can just create my own UI update thread through handlers, but how do I do that in this case?
Thanks.
Try this, where "itemLayout" is your layout :
View thisLayout = inflater.inflate(R.layout.itemLayout, null);
btnLeft = (ImageButton) thisLayout.findViewById(R.id.buttonleft);
<do stuff with btnLeft>
((ViewPager) collection).addView(thisLayout);
return thisLayout;
Regarding your Q on updating the UI on a thread, check out runOnUiThread here
The answer to this is right here: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/comment-page-1/#comment-14065
Apparently you must refer to the Relative or Linear layout that your button resides in or else findViewById will not be able to find what you are referring to.

Related Links

Firebase Database Listener performance on Android
Audio Overlapping
In Recyclerview Image get misplaced while scrolling
how to set weight of textview Dyanamicaly inside Tablerow in TableLayout?
LinearLayout under GridView does not wrap content
I want to manage toolbar text while using swiping tabs
Setting audio file as Ringtone fails everytime
sqlite delete row by onItemLongClick gridview sqlite [duplicate]
how to send a notification to android using fcm and j2ee [closed]
Error SeachView filter android
How to structure layout with CoordinatorLayout
Scaling issue on Android - why is picture so clumsy?
How to background color of textview within Listview
How do we sort layout at runtime (android)
onCharacteristicChanged getting called multiple times without change in Characteristic values
Google + Integration fails on Android M device

Categories

HOME
search
assembly
cheerio
polymer
linkedin
fpga
github-for-windows
css3
google-spreadsheet
jax-rs
ns-3
pyqt
solver
devise
ms-access-2016
vxml
babeljs
google-admin-sdk
flume
google-chrome-app
reactive-programming
jsonpath
plink
video-editing
sse
selinux
tosca
opentk
pylons
jackson-modules
intersystems-cache
event-flow
left-join
kodi
mailkit
indy10
pdw-file-browser
dm-script
flatmap
android-augmented-reality
unordered-map
outputcache
openal
lightning
trojan
cudd
winusb
seek
gogs
clipboard.js
gcloud-ruby
fuzzywuzzy
aspen
inf
qt5.2
morton-number
video-player
pax-exam
lasso
zxspectrum
deform
human-computer-interface
mdichild
reflector
wxformbuilder
android-usb
gtm-oauth2
ie8-compatibility-mode
gpars
poker
chart-director
virtual-pc
wimax
correlated-subquery
usertype
predicatebuilder
koken
gobject-introspection
listactivity
mbeans
socketserver
luajava
updatesourcetrigger
office-2007
wcf-instancing
sifr3
xpsdocument

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