android


android fragment button in include tag does not work


I have faced a problem, that my button does not work when I use
Here is my fragment_category_sub xml file:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<include
layout="#layout/content_unavailable"
/>
....
And here is my include xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/content_unavailable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<Button
android:id="#+id/try_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/error_default_tryAgain"/>
</LinearLayout>
Here is my fragment were I try to implement setOnClickListener:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_category_sub, container, false);
ButterKnife.bind(this, rootView);
contentUnavailable = rootView.findViewById(R.id.content_unavailable);
Button buttonAgain = (Button)
contentUnavailable.findViewById(R.id.try_again);
buttonAgain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (utils.isConnectingToInternet()) {
categorySubPresenter.getAllListItems(uid, category_id);
}
}
});
return rootView;
}
Does anyone know why this button does not work when it is pressed in fragment and how to solve it? Because same code in Activity works without problem. Thank you in advance.
I don't think this is because of the include tag. I was warned once that I cannot set listeners in a fragment. So, if you want to set listeners on some button you have to do it within an activity.
There is a fragment callback onActivityCreated() that is called when the activity is created, so starting with this moment you can call the Fragment's getActivity() method.
Here is an example
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final Activity activity = getActivity();
activity.findViewById(R.id.toast).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(activity, "My toast", Toast.LENGTH_SHORT).show();
}
});
}

Related Links

Filter social app contacts from my phone contacs
Admob native express ads in listview disappear
Error signing android app on phonegap
Inherit layout_width on custom view
Only Error icon is visible while setting error to EditText in InputTextLayout
Implement bluetooth pair and sync feature in companion app like official android wear app
How to store and retrieve multiple values in HashMap using POJO (Android)
Is it correct to call WebServices inside onReceive of BroadcastReceiver?
problems with images in recyclerview using picasso
“Could not find method task()…” in build.gradle
Cursor adapter adapts text views to wrong location on list view
crystax ndk link error
Kivy layout object pushed down
Android RecyclerView scrollToPosition not scrolling to bottom of view
How to record total distance from starting point to end point while running in android? [closed]
Pass the Bluetooth connection between Activities in Android Studio

Categories

HOME
grok
microsoftgraph
google-spreadsheet
twig
character-encoding
toolbox
yahoo-finance
graphics
yandex
soa
switch-statement
alsa
web-hosting
gembox-spreadsheet
zip
modal-dialog
fabric.io
jquery-ui-sortable
mod-security
line
radgridview
shared-libraries
flash-player
libusb
mvc-mini-profiler
aws-sdk
microsoft-ocr
dpdk
pagespeed
roslyn
comparator
xcode-ui-testing
shared
has-and-belongs-to-many
android-maps
bringtofront
ceylon
eviews
mockjax
jbutton
hibernate-validator
playstation-portable
autolisp
biztalk-rule-engine
sql-delete
xajax
python-pptx
swagger-php
zoomify
opensmpp
magento-1.9.3
branching-and-merging
naming
angular-http
wc
getchar
automapper-5
chai-as-promised
lightning
corruption
boxing
colorbar
avx
bbpress
universal-analytics
objectscript
myspace
delta
lean
nservicebus5
yajsw
cycle2
httpruntime.cache
page.js
dnssec
dot.js
viewer
spy++
xml-namespaces
jqmodal
with-statement
parallel-testing
cancellation
gnumeric
fpdi
rfc5322
sphinxql
observium
android-icons
twisted.web
tree-balancing
josql
urlrewriter
mbeans
metaweblog
aspmenu
cfwindow
image-capture
pnrp

Resources

Encrypt Message