0

I have two buttons in different activity. By clicking "Button A" I need to enable "Button B" "Button A" is in MainActivity.java and "Button B" is in QuestionFragment2018.java

        <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click me"
        android:id="@+id/clickButton"
        android:layout_centerHorizontal="true"
        android:onClick="buttonClicked"/>

need to enable this fragment button

    <Button
        android:id="@+id/btnviewexplanation"
        style="@android:style/Widget.Button.Inset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="15dp"
        android:background="@drawable/testbutton"
        android:enabled="false"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="Solution" />

MainActivity.java

    public void buttonClicked (View view){
}

QuestionFragment2018.java

    Button buttonExplanation = (Button) 
   rootView.findViewById(R.id.btnviewexplanation);
    buttonExplanation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            questionWebView.loadUrl("file:///android_asset/" + 
   currentQuestion2018.getExplanationText());

        }
    });
  • Possible duplicate of [Communicating between a fragment and an activity - best practices](https://stackoverflow.com/questions/14247954/communicating-between-a-fragment-and-an-activity-best-practices) – barotia Sep 14 '19 at 04:40

1 Answers1

0

Most easy way to do this

in your mainActivity class Create a reference for your fragment using fragment id in .xml

QuestionFragment2018 QuestionFragment2018=
    (QuestionFragment2018) 
  getSupportFragmentManager().findFragmentById(R.id.yourfragmentig) 

create a function in QuestionFragment2018 doWorke() call this function from activity

QuestionFragment2018.doWorke()
  • Well it is a solution, but not a proper one, of course it may solve the problem, but it is hard to scale to next levels. It depends on what you try to achieve, if you only need a very simple application, it will do, if you want to learn Android programming, it won't. – barotia Sep 14 '19 at 04:31
  • @barotia i agree with you its not a proper way but it is a easy solution. – Muhammad Usman Butt Sep 14 '19 at 05:36
  • The problem with the easy solutions, that if somebody uses to it, when (s)he passes the code the other developer won't understand the most of code. – barotia Sep 14 '19 at 05:41
  • I am implementing inapp purchase in my app. In-app purchase buy button is in main activity. And I want to enable the fragment button which is having some extra feature. – Suresh Srinivasalu Sep 14 '19 at 05:57