0

Ok so, I'm currently working on a school project, my app has three tabs in the first and the second one it has two different web views with the webs showing properly and in the third one it has two EditTexts for me to be able to change those webs. The problem comes when I try to find those EditTexts from the fragment and save the url. How could I do it properly?

public class WebpageSetter_Fragment extends Fragment {

    EditText direccion1, direccion2;
    Button cambiarUrl;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            return inflater.inflate(R.layout.set_webpage_fragment, container, false);
    }
Alex
  • 1,816
  • 5
  • 23
  • 39
  • 1
    so, in simple words, you want to communicate between fragments, right? – touhid udoy Nov 11 '18 at 11:09
  • Ok, I'll try to explain it simple. Three fragments in my main activity, two of them are web views with a default web showing properly on them, the third one has two edit texts for you tu be able to copy an url and show it in one of the two previos fragments. By now I have the first two fragments working but at the time I want to put the url I copied in the third fragment to show in one of the others it crashes. – Jorge Hetfield Nov 11 '18 at 17:42

2 Answers2

0

Use this code

EditText direccion1, direccion2;
Button cambiarUrl;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.set_webpage_fragment,container,false);
        direccion2 = view.findViewById(R.id.your_edittext_id);
        //same for the other edittext
        //If you have a button to get the text and set the website into webviews,then find the button ID in same way and then use setOnClickListener on the button and then do the stuff

        return view;
}

This goes for any kind of view you want to find anywhere, if there's a View in some kind of container, consider for example a LinearLayout, you can find the views inside the LinearLayout using the same method, initialize the LinearLayout and then use

linearLayoutObject.findViewById(R.id.id_of_view_you_want_to_find);
Vedprakash Wagh
  • 3,595
  • 3
  • 12
  • 33
  • Yeah I know that but when I do it and I try to find the edit text and exception comes out and it says the edit text is null. What I wanna do is if the edit text isn't null I wanna take that String and load it as a web view in another fragment. – Jorge Hetfield Nov 11 '18 at 11:05
  • If you have a button in your app to get the text from EditText, then in onClickListener of your button use this code, `if(editText.getText().toString()!=null){ your stuff}` , if you still get an error, then check if there's actually an EditText with the ID in your layout and if you want to check out how to pass data between different fragments, check this link https://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments – Vedprakash Wagh Nov 11 '18 at 11:07
  • Post the error you're getting so that I'd understand what the actual problem is. – Vedprakash Wagh Nov 11 '18 at 11:22
0

Make method in your parent activity and pass your data from your fragment to activity and then get that data from parent activity to any fragment, in your case every fragment is child of the same parent activity.

Taha wakeel
  • 159
  • 1
  • 8