Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get edittext value from fragment

I am using fragments,I have an edittext in fragment and I want to get value in main activity.

This is my fragment layout

<?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"
    android:orientation="vertical"
    android:background="#878787" >

        <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="dfgdfgdf"
        android:textSize="20dp"
        android:layout_centerInParent="true"
        android:id="@+id/user_name"/>

    <EditText
    android:id="@+id/message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
   />

    <Button 
        android:text="Gönder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="getFromUser"
        android:layout_marginTop="40dp"
        />

</RelativeLayout>

I am loading fragment with this function:

public void startChat(JsonObject user) {
    FrameLayout layout = (FrameLayout)findViewById(R.id.container);
    layout.setVisibility(View.VISIBLE); 
    Bundle bundle = new Bundle();
    bundle.putString("name", user.get("name").getAsString());
    sendTo=user.get("username").getAsString();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ConversationFragment conv = new ConversationFragment();
    conv.setArguments(bundle);
    fragmentTransaction.add(R.id.container, conv);
    fragmentTransaction.commit();
    viewPager.setVisibility(View.GONE);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayHomeAsUpEnabled(true);

}

And this is my fragment class

public class ConversationFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        String name = getArguments().getString("name");
        View rootView = inflater.inflate(R.layout.fragment_conversation, container, false);
        TextView username=(TextView)rootView.findViewById(R.id.user_name);
        username.setText(name);
        return rootView;
    }
}

As you can see when press the button main activity runs "getFromUser" function.I want to get edittext value in this function.How can I do this ?

like image 567
Tolgay Toklar Avatar asked Dec 25 '22 06:12

Tolgay Toklar


1 Answers

It's always the same procedure for these things. You can't access a fragment's views just like that. You need a callback method.

Add this code to ConversationFragment:

private OnGetFromUserClickListener mListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnGetFromUserClickListener ) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnGetFromUserClickListener");
    }
}

public void getFromUser(View v) {
    if (mListener != null) {
        EditText edit = (EditText)findViewById(R.id.message);
        mListener.getFromUser(edit.getText().toString());
    }
}

public interface OnGetFromUserClickListener {
    void getFromUser(String message);
}

Make your MainActivity implement this interface. Replace getFromUser() inside MainActivity with:

public void getFromUser(String message) {
    sendMessage(message);
}

Done.

Edit: Actually, using the XML-onClick attribute is currently bugged (see onClick inside fragment called on Activity): It links to the activity instead of the fragment. You have to set the click listener programmatically to make sure the code won't break at some point in the future. So give the button an ID inside the XML (e.g. get_from_user) and add this code to onCreateView inside ConversationFragment:

v.findViewById(R.id.get_from_user).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.get_from_user) {
            getFromUser(v);
        }
    }
});

Using this code vastly decouples the activity and the fragment from each other.

like image 143
0101100101 Avatar answered Jan 04 '23 21:01

0101100101