Thursday, December 15, 2011

Access View inside Fragment from Activity

In this exercise, TextView inside Fragment can be accessed from Activity.

Access View inside Fragment from Activity

Main Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity:" />
<EditText
android:id="@+id/activitytext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/sendtofragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Text to Fragment" />
</LinearLayout>
<LinearLayout
android:id="@+id/myfragment"
android:layout_width="0px"
android:layout_weight="3"
android:layout_height="match_parent" />

</LinearLayout>


Main Activity:
package com.exercise.AndroidFragment;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidFragmentActivity extends Activity {

EditText textActivity;
Button buttonSendToFragment;
MyFragment myFragment;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textActivity = (EditText)findViewById(R.id.activitytext);
buttonSendToFragment = (Button)findViewById(R.id.sendtofragment);

// get an instance of FragmentTransaction from your Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

//add a fragment
myFragment = new MyFragment();
fragmentTransaction.add(R.id.myfragment, myFragment);
fragmentTransaction.commit();

buttonSendToFragment.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String text = textActivity.getText().toString();
TextView textFragment = (TextView)findViewById(R.id.fragmenttext);
textFragment.setText(text);
}});
}
}


Fragment Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment:" />
<TextView
android:id="@+id/fragmenttext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


Fragment:
package com.exercise.AndroidFragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragmentlayout, container, false);
return myFragmentView;
}

}


Download the files.



3 comments: