activity_main.xmlfragment_blank.xmlBlankFragment.javaenter_from_right.xmlexit_to_right.xmlMainActivity.java
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.application.fragmentexampleapp.MainActivity"> <EditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:stateListAnimator="@null" android:text="send" /> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
<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="@android:color/holo_green_light" android:clickable="true" tools:context="com.example.application.fragmentexampleapp.BlankFragment"> <EditText android:id="@+id/edittext_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:textColor="@android:color/white" /> <Button android:id="@+id/button_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="send back" /> </RelativeLayout>
package com.example.application.fragmentexampleapp; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; /** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the * {@link BlankFragment.OnFragmentInteractionListener} interface * to handle interaction events. * Use the {@link BlankFragment#newInstance} factory method to * create an instance of this fragment. */ public class BlankFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String TEXT = "text"; // TODO: Rename and change types of parameters private String mText; private OnFragmentInteractionListener mListener; private EditText editTextFragment; private Button buttonFragment; public BlankFragment() { // Required empty public constructor } // TODO: Rename and change types and number of parameters public static BlankFragment newInstance(String text) { BlankFragment fragment = new BlankFragment(); Bundle args = new Bundle(); args.putString(TEXT, text); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mText = getArguments().getString(TEXT); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_blank, container, false); buttonFragment = view.findViewById(R.id.button_fragment); editTextFragment = view.findViewById(R.id.edittext_fragment); editTextFragment.setText(mText); editTextFragment.requestFocus(); buttonFragment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String sendBackText = editTextFragment.getText().toString(); sendBack(sendBackText); } }); return view; } // TODO: Rename method, update argument and hook method into UI event public void sendBack(String sendBackText) { if (mListener != null) { mListener.onFragmentInteraction(sendBackText); } } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(String sendBackText); } }
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="100%" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="200" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="200" /> </set>
package com.example.application.fragmentexampleapp; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener { private FrameLayout fragmentContainer; private EditText editText; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentContainer = (FrameLayout) findViewById(R.id.fragment_container); editText = (EditText) findViewById(R.id.edittext); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = editText.getText().toString(); openFragment(text); } }); } public void openFragment(String text) { BlankFragment fragment = BlankFragment.newInstance(text); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right); transaction.addToBackStack(null); transaction.add(R.id.fragment_container, fragment, "BLANK_FRAGMENT").commit(); } @Override public void onFragmentInteraction(String sendBackText) { editText.setText(sendBackText); onBackPressed(); } }