0

I' ve got a two fragments (layout_a.xml and layout_b.xml). And I just want to do this: click button (btn1) and then change textview (tva) text in layout_a.xml. Here is my codes:

activity_main.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="@dimen/custom_tab_layout_height"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tab"
    android:textColor="@color/colorAccent"
    android:textSize="14sp"
    android:fontFamily="@string/font_fontFamily_medium"/>

layout_a.xml

<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"
    tools:context="info.androidhive.materialtabs.fragments.OneFragment">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="a"
        android:textSize="40dp"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:id="@+id/tva" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1"
        android:id="@+id/btn1"
        android:layout_below="@+id/tva"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

layout_b.xml

<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"
    tools:context="info.androidhive.materialtabs.fragments.OneFragment">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="b"
        android:textSize="40dp"
        android:textStyle="bold"
        android:layout_centerInParent="true"/>
</RelativeLayout>

A_activity.java

public class A_activity extends Fragment{

    public A_activity() {
        // Required empty public constructor

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.layout_a, container, false);
    }
}

B_activity.java

public class B_activity extends Fragment{

        public B_activity() {
            // Required empty public constructor

        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.layout_b, container, false);
        }
    }

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.a,
            R.drawable.b,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
        Button btn1 = (Button) findViewById(R.id.btn1);
        TextView tva = (TextView) findViewById((R.id.tva));
//        btn1.setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                tva.setText("Clicked");
//            }
//        };

    }

    private void setupTabIcons() {
        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("a activity");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.a, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("b activity");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.b, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new A_activity(), "A activity");
        adapter.addFrag(new B_activity(), "B activity");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}
lotomax
  • 113
  • 2
  • 12

1 Answers1

2

Change your A_activity onCreateView() to the following

View view = inflater.inflate(R.layout.layout_a, container, false);

Button btn1 = (Button) view.findViewById(R.id.btn1);
final TextView tva = (TextView) view.findViewById((R.id.tva));
btn1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        tva.setText("Clicked");
    }
};

return view;
SaNtoRiaN
  • 2,212
  • 2
  • 15
  • 25
  • where can i add this code? in A_activiy or MainActivity? – lotomax Jan 01 '16 at 22:31
  • in `A_activiy` inside the method `onCreateView()`, replace the code inside it with the above – SaNtoRiaN Jan 01 '16 at 22:32
  • Did you remove the parameters of the method? – SaNtoRiaN Jan 01 '16 at 22:36
  • I' ve just done it by your help. Thank you so much again. – lotomax Jan 01 '16 at 22:42
  • It's our duty :) we should help each others. – SaNtoRiaN Jan 01 '16 at 22:44
  • how to create toast messages in A_activity? Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_LONG).show();//it doesn' t work because getApplicationContext() undefined – lotomax Jan 02 '16 at 09:27
  • and which can i replace this keyword above; ArrayAdapter adapter = new ArrayAdapter(this,R.layout.mylv, android.R.id.text1, sd);//because this keyword is unknown there – lotomax Jan 02 '16 at 09:55
  • You just need the context, use `getActivity()` instead of `this` inside the constructor as following `ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.mylv, android.R.id.text1, sd);` – SaNtoRiaN Jan 02 '16 at 11:50
  • also do the same for Toast `Toast.makeText(getActivity(), "Message", Toast.LENGTH_LONG).show();` – SaNtoRiaN Jan 02 '16 at 12:00
  • is it possible write view.getContext() instead of this keyword like getActivity()? – lotomax Jan 02 '16 at 14:25
  • and i wanna ask you another thing: here it is: i want to divide 3 part my layout. and 1st part width:1/5 and 2nd part width:2/5 and last part width=2/5. how can i do that by linearlayout. i try to weight but i couldn' t. thank you advance. – lotomax Jan 02 '16 at 19:54
  • I didn't try `view.getContext()` before, you can try it and see the result – SaNtoRiaN Jan 02 '16 at 20:43
  • Set `android:weight_sum="5"` in your `LinearLayout` then set `android:layout_weight="2"` or "3" or "1" as you like in your child views – SaNtoRiaN Jan 02 '16 at 20:45
  • i try and it works view.getContext(). and second: i did it but i couldn' t. what did i miss i don' t know. by the way thank you. – lotomax Jan 02 '16 at 22:20
  • you're welcome. maybe you're using wrong orientation in the view, try changing it from vertical to horizontal – SaNtoRiaN Jan 03 '16 at 13:23