textView.setText not working - java

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;
}

Related

inflater.inflate(R.layout.) doesn't find Fragment.xml

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;
}
}

How to display a dynamic textview in fragment Android [duplicate]

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)

Android - Fragment findViewById() always null?

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;
}

add onclick listener to a button in a fragment's xml

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.

Locate fragment view elements and update them after asynctask is done

I have one activity that holds multiple fragments. Each fragment implements my UpdateRequest interface, where each of these fragments do some asynctask and downloads data from web service. My problem is that I don't really understand how to update existing fragments. I read this
Android Refreshing Fragment View after AsyncTask
but I still can't figure out how to locate views of each fragment and then update them. I'll give an example:
There's an UserFragment, which should represents user profile:
public class UserFragment extends SherlockFragment implements
UpdateRequest {
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_user_info,
container, false);
return rootView;
}
...
public void update(final OnFragmentUpdatedListener context) {
...
asyntask
...
}
}
And fragment_user_info looks like this:
<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" >
<ImageView
android:id="#+id/fragment_user_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/image_of_user" />
<TextView
android:id="#+id/fragment_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/user_info" />
</RelativeLayout>
What I want is to locate instance of UserFragment and then in its AsyncTask.onPostExecute() method change text in fragment_user_name textView to something that asynctask returns.
The easiest way to do this is to create a final variable in the fragment to hold a reference to the TextView. Then the AsyncTask, being an anonymous inner class of the fragment, will have direct access to it. So, something like this:
public class UserFragment extends SherlockFragment implements UpdateRequest {
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_user_info, container, false);
return rootView;
}
...
public void update(final OnFragmentUpdatedListener context) {
final TextView userNameView = (TextView) rootView.findViewById(R.id.fragment_user_name);
...
asyntask
...
onPostExecute() {
userNameView.setText(....);
}
}

Categories