0

I like to make an application with tabs. There are three tabs in my application with some buttons in each tab. And there are three fragments also. I like to display some text in to a common TextView when the buttons in each tab is pressed.My tabs are working fine. But I have some three issues. 1.How to give a common TextView for all the tabs (perhaps outside the tabs) 2.My button method in the fragments dont work 3.When I try to display cutom language fonts it gives me an error "The method getAssets() is undefined for the type FragmentA"

The following is my main activity

public class MLkeyboardActivity extends Activity {
/** Called when the activity is first created. */
//public static TextView textView1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mlkeyboard);


    final ActionBar actionBar = getActionBar();        
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);




    Tab tabA = actionBar.newTab();
    tabA.setText("Tab A");
    tabA.setTabListener(new TabListener<FragmentA>(this, "Tag A", FragmentA.class));
    tabA.setIcon(R.drawable.ic_launcher);
    actionBar.addTab(tabA);

    Tab tabB = actionBar.newTab();
    tabB.setText("Tab B");
    tabB.setTabListener(new TabListener<FragmentB>(this, "Tag B", FragmentB.class));
    actionBar.addTab(tabB);

    Tab tabC = actionBar.newTab();
    tabC.setText("Tab C");
    tabC.setTabListener(new TabListener<FragmentC>(this, "Tag C", FragmentC.class));
    actionBar.addTab(tabC);

    if (savedInstanceState != null) {
        int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
        getActionBar().setSelectedNavigationItem(savedIndex);
    }

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    outState.putInt("SAVED_INDEX", getActionBar().getSelectedNavigationIndex());
}

public static class TabListener<T extends Fragment> 
    implements ActionBar.TabListener{

    private final Activity myActivity;
    private final String myTag;
    private final Class<T> myClass;

    public TabListener(Activity activity, String tag, Class<T> cls) {
        myActivity = activity;
        myTag = tag;
        myClass = cls;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

        // Check if the fragment is already initialized
        if (myFragment == null) {
            // If not, instantiate and add it to the activity
            myFragment = Fragment.instantiate(myActivity, myClass.getName());
            ft.add(android.R.id.content, myFragment, myTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(myFragment);
        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

        if (myFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(myFragment);
        }

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

}}

and this is my code in one of the fragments

public class FragmentA extends Fragment {
public static TextView textView1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
    return myFragmentView;      
}
public void button1(View view) {
    TextView tv = (TextView) getActivity().findViewById(R.id.textView1);
    Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/AnjaliOldLipi.ttf");
    tv.setTypeface(typeFace);
    tv.setText("അ");            

}}

I will be extremely thankful to you for any help.

2
  • Why someone has voted down my question? Wasn't the information clear? I really need help. Please Feb 11, 2013 at 7:16
  • Thanks for all vote ups. Though I didn't get any answers. Feb 11, 2013 at 9:32

1 Answer 1

0

Finally I got the solution. We can provide a common TextView in the Main XML file as follows.

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

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />    

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

Now the button is not required to ba called from fragment. It can be directly called from the Main Acivity, by giving the FindVuewById method. The Button name should be different in Tab1.XMl, Tab2.xml etc. The code is as follows,

public void butn1(View view) {
    TextView tv = (TextView) findViewById(R.id.textView1);      
    tv.setText("abc");  

}

method butn1 corresponds to the Button1 in Tab1, similerly we can give for other buttons in other tabs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.