How can I change a Fragment text? - java

This is my Fragment class and I have a textView in xmlfile
How can I change the TextView by setText?
public class Frist extends Fragment {
TextView text;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_frist, container, false);
TextView t = (TextView) this.getView().findViewById(R.id.textView1);
t.setText("SOME TEXT");
return rootView;
}
}

Instead of
TextView t = (TextView) this.getView().findViewById(R.id.textView1);
You should be calling findViewById on the view you inflate:
TextView t = (TextView) rootView.findViewById(R.id.textView1);

Related

How to SetText of One Activity/Fragment from another one

I want to change the text of the Fragment_B from the Fragment_A. How can I do this?
Something like this.
Fragment_A
ola = (TextView) v.findViewById(R.id.textView12);
ola2 = (TextView) (FRAGMENT_B).findViewById(R.id.textView2);
AsyncTask(ola,ola2).execute //this should set text on Fragment_A and Fragment_B
Fragment A:
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_second,container,false);
ola = (TextView) v.findViewById(R.id.textView12);
Asynctask(ola).execute
return v;
}
Fragment B:
public class Fragmento_B extends android.support.v4.app.Fragment{
public TextView ola2;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_third,container,false);
ola2 = (TextView) v.findViewById(R.id.textView2);
Asynctask(ola2).execute
return v;
}
}
What is the problem you are facing? Is it showing some sort of error or is it not changing the text as your or anything?

Creating TextView in a View in the Navigation Drawer

I'm new to Android and have a problem with the Navigation Drawer in Android Studio. I want to create dynamically a TextView in one of the Navigation Drawer's view. I cannot create a new TextView and I cannot search by id a TextView
public class StatoServer extends Fragment {
View myView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView tx = new TextView(this); //doesn't work this
tx.setText("text that change dynamically with a function");
container.addView(tx);
myView = inflater.inflate(statoserver, container, false);
return myView;
}
You don't assign any LayoutParams to your TextView, nor do you add it to the correct ViewGroup. It should be added to the View returned by onCreatView(). The following is tested and will work as an example of how to dynamically add a view:
public class OneFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.one_fragment, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getContext());
textView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
);
((ViewGroup)view).addView(textView);
textView.setText("Some Text");
super.onViewCreated(view, savedInstanceState);
}
}
Inside your onCreateView , here an example code on how you do it!
View v = new View(getActivity());
v = inflater.inflate(R.layout.page2, container, false);
View tv = v.findViewById(R.id.Ausgabe);
((TextView)tv).setText("TestText");
View pl = v.findViewById(R.id.PageLayout);
TextView Paper = new TextView(pl.getContext());
Paper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Paper.setText("Inserted TestText");
((LinearLayout)pl).addView(Paper);
return v;

how change fragment textview with own java class?

I tried to change a TextView in Activity of Fragment in three cases, but I can't change the TextView.
Is there any solution to change a TextView of Fragment in own Fragment_Activity or Main_Activity?
Fragment.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
TextView textView = (TextView) getView().findViewById(R.id.textView1);
textView.setText("Done!");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText("Done!");
return view;
}
#Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView tv = (TextView) getView().findViewById(R.id.textView1);
tv.setText("Done!");
}
Fragment.xml
...
<TextView
android:textColor="#color/myBlack"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
....
calling fragment class for view in MainActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_weixin);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
instance = this;
...
View view2 = mLi.inflate(R.layout.fragment_blank_fragment2, null);
views.add(view2);
...
}
Thing to know
In onCreate of fragment getView() returns null because still view in not created.
Create a method in fragment to set value for the TextView from activity using fragment instance.
Example :
Fragment
public class Fragment1 extends Fragment {
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
tv = (TextView) view.findViewById(R.id.textView1);
tv.setText("Done!");
return view;
}
public void setViewText(String text){
tv.setText(text);
}
}
Activity
public class MyActivity extends FragmentActivity {
Fragment1 fragment1;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_fragment);
//as you are using static fragment so create fragment instance using findFragmentById(id)
fragment1 = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
fragment1.setViewText("Message");
}
}
Visit Official doc Communicating with Other Fragments
Because we can't see the whole Fragment.xml file or the the xml of your main activity i can't tell for sure what's the problem but it seems like you may have not specified the fragment class in the Fragment.xml file so the oncreate never gets called. I would suggest you add the fragment attribute in your main_weixin xml and that you remove the view2 inflation in your main activity then you could simply add this in your main_weixin.xml:
<fragment
android:name="your_package_name.Fragment"
android:id="#+id/fragment_id"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Adding a dynamic textview to a fragmment crashes app

I have been trying to add a textview dynamically to my fragment but it is causing the app to crash, the below code works in a non-fragment activity I just cant get it work in a fragment. At the moment my code uses just on textview, but it will eventually return many from a database, hence why I need it to be dynamically created.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View theView = inflater.inflate(R.layout.fragment_list_batches, container, false);
context=getActivity();
//Dynamically create Elements
LinearLayout.LayoutParams SVView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout SV = (LinearLayout)getActivity().findViewById(R.id.listBatchesRelative);
TextView batchName = new TextView(context);
int i = 1;
batchName.setId(Integer.valueOf(i));
batchName.setText("Dynamic Input view");
batchName.setLayoutParams(SVView);
SV.addView(batchName);
return theView
View theView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
theView = inflater.inflate(R.layout.fragment_list_batches, container,false);
LinearLayout.LayoutParams SVView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout SV = (LinearLayout)theView .findViewById(R.id.listBatchesRelative);
TextView batchName = new TextView(theView.getContext);
int i = 1;
batchName.setId(Integer.valueOf(i));
batchName.setText("Dynamic Input view");
batchName.setLayoutParams(SVView);
SV.addView(batchName);
return theView

How to add new Controls (Buttons, TextViews) to a Fragment dynamically

Here's my fragment class:
public class FirstFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
EditText newText = new EditText(getActivity());
newText.setText("This is a fragment");
newText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
I am trying to add newText to the fragment being created.
simply store the view that was returned from the inflater.inflate call , and add anything you want to it .
in the end , return the stored view .

Categories