-1

I have a form and in my form I have Text View with title and textEdit for input(TimePicker input) I have a button in this page, after clicking on my button I want to print textView title and textedit input on another page(another fragment or another text-filed) just to show my result

would you please help me in this implementation! appreciated any sample or hints!

Thanks in advance!

Jon Hudson
  • 17
  • 1
  • 13

2 Answers2

1

1. You can use Toast. And let's say that TextView has id "title", editText has id "timePicker" and button with id "button".

So in onCreate method you have:

TextView textViewTitle = (TextView) findViewById(R.id.title);
EditText EditTextTimePicker = (EditText) findViewById(R.id.timePicker);
Button button= (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "Title is: " + textViewTitle.getText().toString() + " and time picker is: " + EditTextTimePicker .getText().toString() ,
   Toast.LENGTH_LONG).show();
    }
});

2.

You can open new view(activity) with intent and to this intent you add data that you want to carry to new view.

Intent i = new Intent(curentView.this, newView.class);
i.putExtra("title", <valueOfTitleField>);
i.putExtra("timePicker", <valueOfEditTextField>);
startActivity(i);

And when new activity starts you can get those values with.

Bundle extras = getIntent().getExtras();
String title= extras.getString("title");
String timePciker = extras.getString("timePicker");

And in this new activity you can have 2 TextView which you set with this variables.

I hope that you understand if not I can create simepl app for you and give you source files.

EDIT: Oh you are asking for fragments, then my answer is not much help to you, sry.

Jure
  • 799
  • 6
  • 25
0

Jure's implementation will work great if you are using multiple activities. I have provided an answer that will work if you are using one activity and multiple fragments.

In order to print the title inside and EditText within another fragment I would recommend you implement a listener in your main activity that you are sending the title from because it is best to not link fragments together. So instead you use this listener inside of your MainActivity to connect your fragment communication. For example

MainActivity.java

    public class MainAcitivity extends FragmentActivity implements SendingFragment.Communicator{

}

public void respond(String title){
       ReceivingFragment.changeData(title);
}

SendingFragment.java

public class SenderFragment extends Fragment {
public SenderFragment() {
    // Required empty public constructor
}

Communicator comm;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_sender, container, false);
    this.setCommunicator((Communicator) getActivity());

    EditText send = (EditText)view.findViewById(R.id.send);
    Button btnClick = (Button)view.findViewById(R.id.click);

    btnClick.setOnClickListener(new VIew.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String timePicker = send.getText().toString();

            respond(timePicker);

        }
    });
    return view;
}

public void setCommunicator(Communicator comm){
    this.comm = comm;
}
public interface Communicator{
    void respond(String timePicker);


}

This way, when you send out the string from the fragment using comm.respond(timepicker) your MainActivities respond method is called, which then calls the fragment you would like to send the information to via the changeData method you create inside your ReceivingFragment.java. Once you have received the data you can set the text via EditText.setText(Timepicker).

user3331142
  • 1,222
  • 1
  • 11
  • 22