I am new to android and have little time to do an assignment, so I figured out I will use "Templates" like Master/Detail Flow to speed up my work. Maybe it wasn't the best decision, since I needed ages to understand given code and even now I still don't understand everything... But now it's too late to start from a scratch.
I have a list of recipes, when a recipe is clicked I can see details (done with the Master/Detail-Template). A recipe has a list of ingredients and a string with description how to prepare it. I wrote a custom adapter to show the list of ingredients in the details fragment and it didn't work at first. The reason was that the ListView I used was placed in the NestedScrollView in the "recipe_detail_activity.xml". This, as I found out, can not work, because both of them have scrolling mechanisms. I tried to replace NestedScrollView with a LinearLayout, as I thought it's only a simple container, but then the ingredients list is rendered at the top of the screen (instead of where the NestedScrollView once was) and the description is not rendered at all.
The way it works now: RecipeDetailActivity.java registers a fragment with recipe_detail_activity.xml as container and RecipeDetailFragment inflates recipe_detail.xml, which contains a ListView for ingredients and a TextView for the description.
I know, I'm obviously lacking of knowladge and should start with easy things, but my professor is pretty demanding... I would be very glad if someone could give me a hint.
recipe_activity_detail.xml:
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.kalina.kochapp.RecipeDetailActivity"
tools:ignore="MergeRootFrame">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#+id/toolbar">
<android.support.v7.widget.Toolbar
android:id="#+id/detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:layout_margin="#dimen/fab_margin"
app:layout_anchor="#+id/recipe_detail_container"
app:layout_anchorGravity="top|end"
app:srcCompat="#android:drawable/stat_notify_chat" />
</android.support.design.widget.CoordinatorLayout>
recipe_detail.xml:
<LinearLayout 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"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_recipe_ingredients"
style="?android:attr/textAppearanceLarge"
android:padding="16dp"
tools:context="com.kalina.kochapp.RecipeDetailFragment"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recipe_instructions"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textIsSelectable="true"
tools:context="com.kalina.kochapp.RecipeDetailFragment" />
</LinearLayout>
RecipeDetailActivity.java:
protected void onCreate(Bundle savedInstanceState) {
[...]
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(RecipeDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(RecipeDetailFragment.ARG_ITEM_ID));
RecipeDetailFragment fragment = new RecipeDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.add(R.id.recipe_detail_container, fragment)
.commit();
}
}
RecipeDetailFragment.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.recipe_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
((TextView) rootView.findViewById(R.id.recipe_instructions)).setText(mItem.instructions);
}
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ingredients = (ListView)getView().findViewById(R.id.lv_recipe_ingredients);
IngredientsListAdapter ila = new IngredientsListAdapter(this.getContext(), mItem.ingredients);
ingredients.setAdapter(ila);
}
Ok, I fixed it by adding "android:fillViewport="true"" to my NestedScrollView. This way I can use a regular ListView inside of it. I don't know whether this solution is fancy, but it works.
Related
CollapsingToolbarLayout title have a strange top margin but only when it is collapsed and only when inside a fragment.
Prior to the 25.0.0 version of Design Support Library everything worked fine.
Video: https://www.youtube.com/watch?v=7WUrFhmvs_Q&feature=youtu.be
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="#drawable/image_placeholder"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Lorem ipsum dolor sit amet enim ..."
android:textSize="20sp"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Fragment :
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.test_fragment, container, false);
return view;
}
#Override
#CallSuper
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
CollapsingToolbarLayout toolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar_layout);
toolbarLayout.setTitle("Lorem Ipsum");
}
}
Any ideas?
I have the same issue right now, but it's because I'm hosting it inside a FrameLayout and LinearLayout and they aren't calling their children's "OnApplyWindowInsets" methods.
Even when fixing that I still get the issue.
But for you, try to only have android:fitsSystemWindows="true" on your CoordinatorLayout, not the rest. I think what you're seeing is a double insets because you have it on almost all your controls.
I have a viewpager inside in a activity. This activity launch three fragements (not tabbed), as created three different class extends fragment. It is working fine with horizontal scrolling in between fragment.
Now I want to use collapsingToolbar with separate image display in fragment.
Activity - having viewpager, Fragment1 XML having collapsingToolbar, Fragement1 also have 'content_launch.xml' Fragement1 class - toolbar added but while fetching (CollapsingToolbar object it is returning null.
Activity XML as below :
<LinearLayout 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"
android:orientation="vertical" tools:context=".activity.LaunchActivity">
<android.support.v4.view.ViewPager
android:id="#+id/launchpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</LinearLayout>
Activity Class to launch viewpager
ViewPager viewPager = (ViewPager) findViewById(R.id.launchpager);
viewPager.setAdapter(new LaunchAdapter(getSupportFragmentManager
(),getApplicationContext()));
Fragement1 XML
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/quote_coordinator"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#android:color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="#dimen/detail_backdrop_height"
android:id="#+id/quote_appbar"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/quote_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim ="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="#android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/quote_backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode ="parallax"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal" android:orientation="vertical">
<TextView android:id="#+id/quote_main_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_title"
android:textColor="#android:color/white"
android:textSize="#dimen/main_title"/>
<TextView android:id="#+id/quote_sub_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_sub_title"
android:textColor="#android:color/white"
android:textSize="#dimen/main_sub_title"/>
</LinearLayout>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/quote_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_quote"/>
</android.support.design.widget.CoordinatorLayout>
Fragment1 Class:
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar quoteToolbar = (Toolbar) getActivity().findViewById(R.id.quote_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(quoteToolbar);
final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) getActivity().findViewById(R.id.quote_collapsing_toolbar);
}
collapsingToolbar object returned as null, not able to find any suggestions, can someone suggest possible way to get object..
First thing: you should not use onCreate to initialize view elements in fragment, you should use onCreateView instead. Second one you don't use getActivity() context.
So instead of this code:
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar quoteToolbar = (Toolbar) getActivity().findViewById(R.id.quote_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(quoteToolbar);
final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) getActivity().findViewById(R.id.quote_collapsing_toolbar);
}
use this:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
// inflate your layout.
View view = inflater.inflate(R.layout.yourlayout, container, false);
Toolbar quoteToolbar = (Toolbar) view.findViewById(R.id.quote_toolbar);
((AppCompatActivity) view).setSupportActionBar(quoteToolbar);
final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) view.findViewById(R.id.quote_collapsing_toolbar);
return view
}
Best regards and happy new year! :)
So i was using SwipeRefreshLayout and Fragments for a simple app that i am working on and i have three files:
1.app_bar_main.xml which contains a FrameLayout which is a container for the fragments heres the code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.example.surafel.tryingfragment.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="58dp"
>
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
2.fragment_main.xml which contains components of the main fragment and a SwipeRefreshLayout heres the code:
<android.support.v4.widget.SwipeRefreshLayout 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"
android:id="#+id/refreshMainContent"
tools:context="com.example.surafel.tryingfragment.MainFragment">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAIN"
android:padding="8dp"
android:textColor="#fff"
android:background="#color/colorPrimary"
android:textSize="28sp"
android:id="#+id/buttonmain"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
3.MainFragment.java this is the java code for the fragment_main.xml heres the code
//i have imported all the necessary classes
public class MainFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout swipeView;
public MainFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_main,container,false);
swipeView = (SwipeRefreshLayout) layout.findViewById(R.id.refreshMainContent);
swipeView.setColorSchemeColors(getResources().getColor(android.R.color.holo_blue_dark),getResources().getColor(android.R.color.holo_red_dark),getResources().getColor(android.R.color.holo_green_light),getResources().getColor(android.R.color.holo_orange_dark));
swipeView.setOnRefreshListener(this);
return inflater.inflate(R.layout.fragment_main, container, false);
}
public void onRefresh() {
Log.d("Refreshing","The refresh is working");
}
}
4.i also have MainActivity.java file which contains all the necessary codes
the problem is its not changing the color of the swipeView and its not even calling the onRefresh() method. when i run the
app and swipe down the refresher comes but its color is black and its not executing the onRefresh() method.
and also there are no compiletime and runtime errors.
So can anyone please help me,i posted all the code because i thought it could be usefull,Thanks.
It seems like you have called the code inflater.inflate(R.layout.fragment_main, container, false); twice.
Try returning the object layout instead of inflater.inflate(R.layout.fragment_main, container, false)
Right now my app works fine at regular screen.
I am having a tough time figuring out how I can manage my current UI to handle the two panel layout.
Right now my DetailActivity has some UI elements to load, 3 fragments on separate tabs, controls what each FAB does, FAB animation and a fragment page adapter for the tab layout.
What should I do to manage all of these elements and logics in a separate class and also in the main activity for the two pane layout?
Here is my Main Activity
public class MainActivity extends AppCompatActivity {
public static boolean TWO_PANE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.movie_detail_container) != null) {
// The detail container view will be present only in the large-screen layouts
// (res/layout-sw600dp). If this view is present, then the activity should be
// in two-pane mode.
TWO_PANE = true;
if (savedInstanceState == null) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}else{
TWO_PANE = false;
}
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentMain())
.commit();
}
Fragment Main displays a movie grid layout, when clicked if it's regular phone open another activity, otherwise the movie details should display at the right corner.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Defines the xml file for the fragment
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
imageAdapter = new ImageAdapter(getContext());
gridView = (GridView) rootView.findViewById(R.id.gridView);
gridView.setAdapter(imageAdapter);
//Start DetailActivity with the movie details.
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if (MainActivity.TWO_PANE){
Bundle bundle = new Bundle();
bundle.putParcelable(Constants.BUNDLE_CONSTANT, mMovieElements.get(position));
FragmentDetail fragment = new FragmentDetail();
fragment.setArguments(bundle);
getChildFragmentManager()
.beginTransaction()
.replace(R.id.movie_detail_container, fragment)
.addToBackStack(null)
.commit();
}else{
//Starting detail activity
Intent intent = new Intent(getContext(), DetailActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, mMovieElements.get(position));
startActivity(intent);
}
}
});
return rootView;
}
land\Activity-main:
<LinearLayout 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"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
tools:context=".ui.ui.MainActivity">
<!--
This layout is activity_main two-pane layout for the Items master/detail flow.
-->
<FrameLayout
android:id="#+id/container"
class = "com.joaonogueira.nmovies.ui.ui.FragmentMain"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
tools:layout="#android:layout/list_content" />
<android.support.design.widget.CoordinatorLayout
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:name="com.joaonogueira.nmovies.ui.ui.FragmentDetail"
android:layout_width="0dp"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_weight="4"
android:id="#+id/movie_detail_container"
tools:context="com.joaonogueira.nmovies.ui.ui.DetailActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:expandedTitleGravity="center"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:id="#+id/posterImage"
android:background="#drawable/nopicture"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/MovieContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/btn_star_big_off"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end"
app:backgroundTint="#color/colorPrimaryDark" />
</android.support.design.widget.CoordinatorLayout>
My Fragment Detail doesn't start, I am getting the following error:
No view found for id 0x7f0c0070 (com.joaonogueira.nmovies:id/movie_detail_container) for fragment FragmentDetail{1b71bc14 #0 id=0x7f0c0070 bundle}
You are trying to replace a fragment from the fragment itself
getChildFragmentManager()
.beginTransaction()
.replace(R.id.movie_detail_container, fragment)
.addToBackStack(null)
.commit();
But since movie_detail_container is not present in the fragment view and is a part of the activity layout instead, hence you are getting that error.
What you need to do is to have a listener interface implemented in your activity and make your fragment listen to that. Finally, you invoke that listener to make your activity replace a new fragment.
If you don't know how to do that read this.
My activitymain.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/app_bar" layout="#layout/toolbar"/>
<view
android:id="#+id/recycler_view"
android:layout_below="#id/app_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="android.support.v7.widget.RecyclerView"/>
</RelativeLayout>
Because my main.java file only contains Recyclerview and nothing else, i can't directly add 2 buttons into it.
I tried to add 2 imageButtons in the the recyclerView.
RecyclerView does not properly render itself in AndroidStudio - there is an issue from google.
So i can't see how exactly recycler view and toolbar(app_bar) stand to each other(I assume app_bar toolbar is below recyclerview(layout_below attribute) )
I suspect i should totally change the design of the app to finish it.
MainActivity.java, it only has recyclerview and adapter to it, it sends context into next SearchActivity:
public class MainActivity extends BaseActivity {
private static final String LOG_TAG = "mAIN ACTIVITY";
private List<Book> bookList = new ArrayList<>() ;
private RecyclerView recyclerView;
private BookRecyclerViewAdapter bookRecyclerViewAdapter;
private boolean FIRST_TIME_LAUNCH = false ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
bookRecyclerViewAdapter = new BookRecyclerViewAdapter(new ArrayList<Book>(), MainActivity.this);
recyclerView.setAdapter(bookRecyclerViewAdapter);
}
If i try to add button it stands in upper left corner:
If you want the button to appear at the end of your list after the list has been scrolled down have a look at this question.
If you want the button below your list and be always visible, this is how you do it:
You 'squeeze in' the RecyclerView between the Button and the Toolbar giving it a layout_below and layout_above attribute:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/app_bar" layout="#layout/toolbar"/>
<Button
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<view
android:id="#+id/recycler_view"
android:layout_below="#id/app_bar"
android:layout_above="#id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="android.support.v7.widget.RecyclerView"/>
</RelativeLayout>
Try this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/app_bar" layout="#layout/toolbar"/>
<view
android:id="#+id/recycler_view"
android:layout_below="#id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="android.support.v7.widget.RecyclerView"/>
<Button
android:id="#+id/button"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="#id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Hope this help!