image slider works in activity but not in fragment - android

the code from this video https://www.youtube.com/watch?v=1AS5HcXPpqk works fine in activity, but when i try to implement it in fragment it crashes.
java code and xml file respectively
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_facts, container, false);
ImageSlider imageSlider = (ImageSlider) getView().findViewById(R.id.slide);
List<SlideModel> slideModels = new ArrayList<>();
slideModels.add(new SlideModel(R.drawable.ic_launcher_background, "Image1"));
slideModels.add(new SlideModel(R.drawable.ic_launcher_background, "Image2"));
slideModels.add(new SlideModel(R.drawable.ic_launcher_background, "Image3"));
imageSlider.setImageList(slideModels, true);
return view;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#E4E3E3">
<com.denzcoskun.imageslider.ImageSlider
android:id="#+id/slide"
android:layout_width="match_parent"
android:layout_height="250dp"
app:auto_cycle="true"
app:delay="0"
app:period="1000"
app:corner_radius="20"
/>
</RelativeLayout>

Don't
ImageSlider imageSlider = (ImageSlider) getView().findViewById(R.id.slide);
Do
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_facts, container, false);
ImageSlider imageSlider = (ImageSlider) view.findViewById(R.id.slide);

Change this line
ImageSlider imageSlider = (ImageSlider) getView().findViewById(R.id.slide);
to
ImageSlider imageSlider = (ImageSlider) getActivity().findViewById(R.id.slide);
You need to pass correct context getActivity()

Related

Unable to Position Bottom Sheet Dialog at the bottom of the screen

I have a Video Detail Fragment which I want to behave as a bottom sheet behaviour, but unfortunately, the dialog is getting displayed at the centre of the screen. I am unable to figure out the exact problem in the code. Below I am attaching the code and the layout file, in regard to the same.
This is my Video Detail fragment:
public class VideoDetailsFragment extends BottomSheetDialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AppCompatDialog dialog = new AppCompatDialog(getActivity());
dialog.setTitle(titleId);
return dialog;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.video_detail_fragment, container, false);
Button cancelButton = dialogView.findViewById(R.id.track_selection_dialog_cancel_button);
Button okButton = dialogView.findViewById(R.id.track_selection_dialog_ok_button);
return dialogView;
}
public static final class TrackSelectionViewFragment extends BottomSheetDialogFragment
implements TrackSelectionView.TrackSelectionListener {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.exo_track_selection_dialog, container, /* attachToRoot= */ false);
TrackSelectionView trackSelectionView = rootView.findViewById(R.id.exo_track_selection_view);
trackSelectionView.setShowDisableOption(true);
trackSelectionView.setAllowMultipleOverrides(allowMultipleOverrides);
trackSelectionView.setAllowAdaptiveSelections(allowAdaptiveSelections);
trackSelectionView.init(mappedTrackInfo, rendererIndex, isDisabled, overrides, /* listener= */ this);
return rootView;
}
}
}
The xml file for video_detail_fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="270dp"
android:visibility="visible"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<FrameLayout
android:id="#+id/child_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end">
</LinearLayout>
</LinearLayout>
I went through many posts but almost all the posts were showing the solution as setting the app attribute app:layout_behavior="android.support.design.widget.BottomSheetBehavior". But it's not working in my case. Kindly help me understand as to where I am doing wrong?

I need to inflate a RecyclerView inside a Fragment AndroidStudio

I would like to use a Tab/Fragment to show RecyclerView, but I don't know how to do it because the method of the Fragment class return a View, not a Reclycler View:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.fragment_needs, container, false);
return myFragmentView;
}
The code of the fragment.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NeedsFragment">
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/all_user_need_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
</RelativeLayout>
Thank u.
First off, your onCreateView() method makes no sense:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.fragment_needs, container, false);
return needsFragmentRecyclerView; //you should be returning myFragmentView here, not whatever needsFragmentRecyclerView is
}
Second, once you inflate your layout, just use findViewById():
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.fragment_needs, container, false);
RecyclerView recyclerView = myFragmentView.findViewById(R.id.all_user_need_list); //assign to a global variable if needed
return myFragmentView; //replaced with the proper variable
}
Alternatively, you can get the RecyclerView from anywhere in your Fragment, as long as it's called after onCreateView() with the following:
RecyclerView recyclerView = getView().findViewById(R.id.all_user_need_list);
You can return any type of view component and Custom view that is inherit from android.view class in onCreateView() method.

Android: Fragment showing from old fragment

I have two fragments on top of each other when I call the second fragment I still the buttons from the first fragment stay visible and functional.
I tried many many methods but didn't work
This one gave me this error:
java.lang.IllegalArgumentException: No view found for id 0x7f0e0004 (*.test:id/customId) for fragment SecondFragment{adef947 #0 id=0x7f0e0004}
and This
java.lang.IllegalArgumentException: No view found for id 0x7f0e00ae (sy.aya.ayaisp.test:id/gatesViewpager) for fragment SecondSFragment{adef947 #0 id=0x7f0e00ae}
my code is as follows:
FirstFragment.java
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.first_fragment, container, false);
Button FBtn = (Button) myView.findViewById(R.id.Fbutton);
FBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString("second", "f button");
SecondFragment secondFragment = new secondFragment();
secondFragment.setArguments(bundle);
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_in_up);
ft.replace(R.id.s_con, secondFragment).addToBackStack("back").commit();
}
});
SecondFrag.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String bundleString = this.getArguments().getString("second");
View view = inflater.inflate(R.layout.s_rv, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.s_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
if (bundleString == "agents button") {
recyclerView.setAdapter(new SecondAdapter(getContext()));
}
}
first_fragment.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/s_con"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/Fbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="secondFragment"/>
</android.support.constraint.ConstraintLayout>
s_rv.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<sy.aya.ayaisp.ExpandableRecyclerView
android:id="#+id/s_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:clipToPadding="false"
android:scrollbars="vertical" />
</FrameLayout>
Thank you for your help
In second fragment layout "s_rv" in root element of xml add some background, clickable="true" and focusable="true".
And in first fragment layout wrap your button in LinearLayout. two days back I was also facing same issue it was solved after that.
It is because in your first fragment the id of button is "pos_agents_button" but in findViewById you are trying to find "R.id.Fbutton".
Change
Button FBtn = (Button) myView.findViewById(R.id.Fbutton);
to
Button FBtn = (Button) myView.findViewById(R.id.pos_agents_button);
and everything should work fine.

search box with fragment does not respond

Hello I have a problem when I search the page tomorrow the process is successful but when I search the page today there is a problem , I used Fragment
the problem here : https://www.youtube.com/watch?v=FiHGME6i97I&feature=youtu.be
codes :
public void btn_search(View view) {
TextView txtexample = (TextView)findViewById(R.id.txtexample);
txtexample.setText("hello");
}
fragment days
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.weather.Fragment_days">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/txtexample"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
fragment days
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_days, container, false);
return v;
}
and fragment today and tomorrow are empty.
please help me , two weeks ago I was looking for a problem
Use the following (note two lines with txtexample):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_days, container, false);
TextView txtexample = (TextView)v.findViewById(R.id.txtexample);
return v;
}
and
public void btn_search(View view) {
txtexample.setText("hello");
}

Set HorizontalGridView to not Scroll in Vertical

I'm using the HorizontalGridView to create a slide of products, but I only need that the slide scroll in horizontal. I try setVerticalScrollBarEnable(false) but don't work.
I think that the problem isn't the Scroll, because is a movement like as moving all horizontalgridview in all directions.
Part of my code:
public class ListProductsFragment extends android.support.v4.app.Fragment {
private HorizontalGridView gridView;
private OffersAdapter offersAdapter;
private ArrayList<Offer> offers;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
offersKey = getArguments().getStringArrayList(Constants.EXTRA_OFFERS_KEYS);
View view = inflater.inflate(R.layout.list_products_fragment,null);
gridView = (HorizontalGridView) view.findViewById(R.id.product_gridview);
gridView.setNumRows(2);
gridView.setRowHeight(225);
return view;
}}
my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:background="#android:color/transparent"
android:layout_height="500dp">
<android.support.v17.leanback.widget.HorizontalGridView
android:id="#+id/product_gridview"
android:layout_width="match_parent"
android:layout_height="500dp"
/>
</LinearLayout>

Categories

Resources