How to SetText of One Activity/Fragment from another one - java

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?

Related

open dialog fragment in Fragment

I have DialogFragment within just some text
and I want open it in Fragment
It's Dialog Fragment first
public class subscribe_dialog_frag extends DialogFragment {
#Override
#Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);
}
}
and it's my fragment code
public class Main4Fragment extends Fragment {
private ImageButton btn_money;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main4, container, false);
btn_money = (ImageButton)view.findViewById(R.id.btn_momey);
btn_money.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment subscribe_dialog_frag = new subscribe_dialog_frag();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.btn_momey, subscribe_dialog_frag).commit();
}
});
return view;
}
}
I just open the dialogFragment.
possible?? or i do another way?
first use naming convention for classes/variables... Rename your dialog to:
public class SubscribeDialog extends DialogFragment {
#Override
#Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);
}
}
in your Main4Fragment call something like this:
btn_money.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SubscribeDialog subscribeDialog = SubscribeDialog();
subscribeDialog.show(getChildFragmentManager(),"SubscribeDialog");
}
});

A problem with initializing textView in fragment

When I start activity, I also start the fragment
conversion_1 = new Conversion();
There is an OnCreateView in the fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_conversion, container, false);
curr_to_txt_1 = view.findViewById(R.id.curr_to_txt);
curr_to_txt_1.setText("rrr");
return view;
}
When I call curr_to_txt_1.setText("text"); , it works in OnCreateView but the app is stopping if I call it in a void funcion later (the function is called in activity and the fuction itself works)
public void set_curr_to_txt() {
curr_to_txt_1.setText("rrr");
}
It says that curr_ro_text is null, but it have been initialized
I tink....
You can use onViewCreated for create the 'curr_to_txt_1' and you not need the view variable.
public class Test extends Fragment {
private TextView text;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_layout, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/*
* Write your bind here
* */
text=view.findViewById(R.id.view_id);
}
}
considering the fragment life cycle, in the onViewCreated, the fragment exist and can bind with components
Any question, I will be happy to help you!

i always get error on the button part and getActivity. its my first time using fragment in android

public class Supplier_Home extends Fragment {
private static final String TAG = "Supplier_Home";
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.s_1home, null);
view = inflater.inflate(R.layout.supply__home__po, container, false);
Button pOrders=(Button)view.findViewById(R.id.pOrders);
pOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked login1.");
Intent intent = new Intent(getContext(), Login.class);
}
});
}
}
Do like, Create View object like this. Pass container object while inflating layout
View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.dashbord, container, false);
Button btn=(Button)view.findViewById(R.id.btn)
return view;
}
You are returning the complete view in the starting itself making the code below return unreachable.
You have given two layouts, If I am not wrong s_1home is layout of your activity where view pager is resting and supply__home__po is your fragment layout. Considering my assumptions are correct please change the code as follows
public class Supplier_Home extends Fragment {
private static final String TAG = "Supplier_Home";
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.supply__home__po, container, false);
Button pOrders=(Button) view.findViewById(R.id.pOrders);
pOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked login1.");
Intent intent = new Intent(getContext(), Login.class);
}
});
return view;
}
}

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 can I change a Fragment text?

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

Categories