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;
}
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;
}
}
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 ;
}
}
This question already has an answer here:
Set textView text from a Fragment won't work [closed]
(1 answer)
Closed 6 years ago.
It drives me crazy. I have made research for one day, although I tried everything, nothing work.
this is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/testo_tab_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#000000"
android:textSize="80dp"/>
</RelativeLayout>
My java's code is:
public class SocietaTabOne extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.societa_tab_one, container, false);
TextView titolo = (TextView)view.findViewById(R.id.testo_tab_one);
titolo.setText("wow");
return inflater.inflate(R.layout.societa_tab_one,null);
}
}
I should see the text in my fragment, but I don't see anything.
Why?
because here you are recreating the layout (erasing the text you set)
return inflater.inflate(R.layout.societa_tab_one,null);
just return view you inflated at the beginning
return view;
Your Fragment should look Somthing like this
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.societa_tab_one, container, false);
TextView titolo = (TextView)view.findViewById(R.id.testo_tab_one);
titolo.setText("wow");
return view;
}
Just return view instead of this line inflater.inflate(R.layout.societa_tab_one,null)
I have a TextView in a fragment called "frontpage". In my MainActivity (also called frontpage, and the fragment is also there), I want to change the text of my textview. Here is my code in the Mainactivity:
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_frontpage, container, false);
return rootView;
}
public void setText(String text){
TextView textView = (TextView) getView().findViewById(R.id.NameView);
textView.setText("HI!");
}
}
And here is my code in the xml layout for the textview:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/NameView"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
No errors are being shown, but when running the app, the textview stays empty. Why is that?
Because you never called your setText(String) function! JVM will follow what you tell it to and you never asked it to go and execute seText(String) function :)
Add this code above return rootView.
setText("Hi");
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_frontpage, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.NameView);
textView.setText("Hi");
return rootView;
}
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.