I got this Code for changing Fragments with a Navigation drawer in Android Java. Simply followed official Googles Dev Posts and other pages, but it still doesn't work. Just the Header of the page changes but not the layout. Tell me please why, i wouldn't be asking if i havent tried a lot and read other Posts.
So here's my Code:
In MainActivity Java file
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment f = null;
Class fClass = null;
// Tasklist
if (id == R.id.nav_drawer_main_tasklist) {
fClass = TaskList.class;
}
try{
f=(Fragment) fClass.newInstance();
}catch (Exception e){
e.printStackTrace();
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_main,f).commit();
item.setChecked(true);
setTitle(item.getTitle());
// Drawer
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerlayout_main);
drawer.closeDrawer(GravityCompat.START
);
in TaskList Java
public class TaskList extends Fragment {
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_main_tasklist, container, false);
return super.onCreateView(inflater, container, savedInstanceState);
}
in tasklists XML file
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="***">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_main_createtask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|end"
android:onClick="fab_main_createtask_onClick"
app:srcCompat="#drawable/ic_add_white_24dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
</LinearLayout>
</RelativeLayout>
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_main_tasklist, container, false);
return super.onCreateView(inflater, container, savedInstanceState);
}
the above is wrong, return the inflated layout and remove nullable.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_main_tasklist, container, false);
return view;
}
You have to return your own fragment view. Update your TaskList fragment as below:
public class TaskList extends Fragment {
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_main_tasklist, container, false);
return view ;
}
}
Related
I have the Problem that the command view =inflater.inflate(R.layout.doesn't find my Fragment xml
here is the Error:
public class FragmentName extends Fragment {
View view;
public FragmentName() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view =inflater.inflate(R.layout.name_fragment,container,false);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
This is my XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Charackter Name"
android:textColor="#color/tabtextcolor"
android:textSize="20sp"
android:textStyle="bold"></com.google.android.material.textview.MaterialTextView>
</LinearLayout>
I dont find my mistake. I rebuilded and cleaned the project.
Don't
return super.onCreateView(inflater, container, savedInstanceState);
Do
retun View's object
Rectify your onCreateView() method.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.name_fragment, container, false);
MaterialTextView view_id = (MaterialTextView) view.findViewById(R.id.view_id);
return view;
Then Clean-Rebuild Your IDE.
just change your return statement
public class FragmentName extends Fragment {
View view;
public FragmentName() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view =inflater.inflate(R.layout.name_fragment,container,false);
return view;
}
}
Good afternoon,
I am trying to create a fragment with a RecyclerView including items.
Above those items, I need to put a header with some filters.
Here is the fragment's xml (fragment_products_list) :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12sp"
android:id="#+id/filters_frame">
<include layout="#layout/expandable_filters"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Here is the OnCreateView in my Fragment java file (ListFragment.java) :
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView rv = (RecyclerView) inflater.inflate(
R.layout.fragment_products_list, container, false);
//Product filters
expandFiltersButton = (ImageButton) getView().findViewById(R.id.expand_filters);
expandableZone = (LinearLayout) getView().findViewById(R.id.expandable_zone);
expandFiltersButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(expandableZone.isShown()){
expandableZone.setVisibility(View.GONE);
expandFiltersButton.setImageResource(R.drawable.ic_more_24dp);
}else{
expandableZone.setVisibility(View.VISIBLE);
expandFiltersButton.setImageResource(R.drawable.ic_less_24dp);
}
}
});
setupRecyclerView(rv);
mRecyclerView = rv;
loadUpdates();
return rv;
}
I have this error code when I am trying to run the app :
android.support.v4.widget.NestedScrollView cannot be cast to android.support.v7.widget.RecyclerView
What is the problem and how can I solve it?
Thanks for your help!
Try this:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.fragment_products_list, container, false);
//Product filters
RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview);
expandFiltersButton = (ImageButton) view.findViewById(R.id.expand_filters);
expandableZone = (LinearLayout) view.findViewById(R.id.expandable_zone);
expandFiltersButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(expandableZone.isShown()){
expandableZone.setVisibility(View.GONE);
expandFiltersButton.setImageResource(R.drawable.ic_more_24dp);
}else{
expandableZone.setVisibility(View.VISIBLE);
expandFiltersButton.setImageResource(R.drawable.ic_less_24dp);
}
}
});
setupRecyclerView(rv);
mRecyclerView = rv;
loadUpdates();
return view;
}
I have an base Fragment with an FrameLayout and I want to use this Layout in the derived Fragment and add an onTouchListener to it.
BaseFragment.java
public class BaseFragment extends Fragment {
protected FrameLayout baseFrameLayout;
protected View baseView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
baseView = inflater.inflate(R.layout.fragment_base, container, false);
baseFrameLayout = (FrameLayout) baseView.findViewById(R.id.frameLayout);
return baseView;
}
}
fragment_base.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
DerivedFragment.java
public class DerivedFragment extends BaseFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
baseFrameLayout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// do something
return true;
}
});
return baseView;
}
}
Unfortunately the onTouchListener is not executed when I touch the FrameLayout
EDIT:
Finally I add the DerivedFragment to another Fragment in this way:
OverViewFragment.java
public class OverViewFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_overView, container, false);
Fragment derivedFragment = new DerivedFragment();
FragmentTransaction devTrans = getChildFragmentManager().beginTransaction();
devTrans.add(R.id.derivedFragmentId, derivedFragment).commit();
Fragment footerFragment = new FooterFragment();
FragmentTransaction statTrans = getChildFragmentManager().beginTransaction();
statTrans.add(R.id.footerFragmentId, footerFragment).commit();
return view;
}
}
fragment_overView.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/derivedFragmentId"
android:layout_above="#+id/footerFragmentId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:gravity="center"
/>
<FrameLayout
android:id="#+id/footerFragmentId"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I had never used empty Layouts before. So i just tested it.
With the following lines the Layout is not visible and does not get clicks:
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
The following lines work:
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
So either make the Layouts match_parent or add views inside.
Yes, I realize there are questions VERY similar to this one, but unlike the others, my onCreateView() method inflates the view for the fragment. When I don't use findViewById(), everything inflates and my app works as expected (except for the functionality for the views I want to get in findViewById().
Here is my code:
#Override
public View onCreateView(LayoutInflater p_inflater, ViewGroup p_container, Bundle p_prev_state)
{
return p_inflater.inflate(R.layout.fragment_main, p_container, false);
}
Whenever I want to access a ListView within my layout, I do this:
private ListView getListView()
{
return (ListView)getView().findViewById(R.id.main_events);
}
but this is always null. Why? My fragment_main.xml looks like so:
<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"
android:background="#color/white"
tools:context="me.k.cal.MainActivity$PlaceholderFragment" >
<ListView android:id="#+id/main_events"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1dp"
android:divider="#color/background_color" />
</RelativeLayout>
My full fragment code can be found here.
Use Below Code to inflate your view and get id of listview.
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View _view = inflater.inflate(R.layout.fragment1, container, false);
ListView _list= (ListView) _view.findViewById(R.id.main_events);
return _view;
}
Try this,hope this helps
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View view = inflater.inflate(R.layout.fragment_main, container,
false);
ListView eventsList= (ListView) view.findViewById(R.id.main_events);
return view;
}
I'm writing and android 4.4 project using android studio.
I'm new to the fragments idea and trying to create a simple application with a button that the click handler sends a message to the log.
this is the fragment class
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
return rootView;
}
public void addStringClickHandler(View v) {
Log.d("tag","hello");
}
}
this is the fragment layout XML
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.tuxin.myalcoholist.myalcoholist.myalcoholist.MainMenuActivity$PlaceholderFragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_drink"
android:id="#+id/add_drink"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:onClick="addStringClickHandler" />
</RelativeLayout>
as you can see i set in the XML android:onclick to addStringClickHandler
and in the fragment class i created that function, but when I execute the application
I get an error that the runtime could not find a method addStringClickHandler(view)
what am I missing?
Your android:onClick handler method has to belong to the Activity hosting your fragment. Just move addStringClickHandler() method to the activity.
If you want to have a listener method in the fragment you have to set listener in the code like this.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
rootView.findViewById(R.id.add_drink).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("tag","hello");
}
});
return rootView;
}
Try to register the onClick method when inflating the view.
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
rootView.findViewById(R.id.add_drink).setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do what you need
}
});
return rootView;
Or you can set the fragment to be the on click listener
View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);
rootView.findViewById(R.id.add_drink).setOnClickListener(this);
return rootView;
and then make the fragment implement OnClickListener.