-2

EDITED

Thanks to all that voted me negative without trying help. That is really useful.

This main problem was I had 2 same fragments, one in main and other in the premium flavor. So the system was loading always the fragment stored in main and because of that the button listener is never called.

You can solve it removing the fragment from main path and create the fragment in every flavor you have, by this way the proper fragment will be selected

If you want to have a different version of the same class in the two flavor you'll need to create it in both flavors and remove it from main.

src/flavor1/java/com/foo/A.java

src/flavor2/java/com/foo/A.java

And then your code in src/main/java can do:

import com.foo.A

depending on the flavor selected, the right version of com.foo.A is used.

ORIGINAL QUESTION

I'm trying to set a button in a "premium" flavor that is not shown in the main flavor. The problem is the button is shown in the UI, but the onClickListener is never fired.

My structure is the following:

I have an activity that contains a fragment. The activity is unique for all flavor, but the fragment is going to change depends of the flavor. One of that flavor contains a button that is not shown in the main flavor. When tap on that button, nothing happens.

Here is the code related:

The activity xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    ...
    android:orientation="vertical"
    android:weightSum="10"
    tools:context=".activities.ConfigurationActivity">

<ImageView
        ...
        android:layout_weight="2"/>

<FrameLayout
        android:id="@+id/configurationFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"/>

</LinearLayout>

The activity code is:

package com.abc.project.activities;
public class ConfigurationActivity extends AppCompatActivity {
    private Fragment confFragment;

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

        confFragment = new ConfigurationFragment();
        getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.configurationFragment, confFragment)
            .commit();
    }
}

The main fragment xml is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          ...
          android:orientation="vertical"
          tools:context="com.abc.project.fragments.ConfigurationFragment">

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Main fragment"/>

</LinearLayout>

The main fragment code is:

package com.abc.project.fragments;
public class ConfigurationFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_configuration, container, false);

        return view;
    }
}

The premium fragment xml is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          ...
          android:orientation="vertical"
          tools:context="com.abc.project.premium.fragments.ConfigurationFragment">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Premium fragment!"/>

<Button
        android:id="@+id/premiumButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button premium"/>
</LinearLayout>

And the premium fragment code is:

package com.abc.project.premium.fragments;
public class ConfigurationFragment extends Fragment {
    private Button premiumButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

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

        premiumButton = (Button)view.findViewById(R.id.premiumButton);
        premiumButton.setOnClickListener(premiumOnClickListener);

        return view;
    }

    private View.OnClickListener premiumOnClickListener = new View.OnClickListener()  {
        @Override
        public void onClick(View view) {
            //THIS CODE NEVER IS FIRED
        }
    };
}

So, the "premium" xml is showed properly, but the button does not fire the onClickListener.

Anyone knows why is that happening?

Thanks in advance.

Regards, Kemmitorz.

kemmitorz
  • 351
  • 7
  • 18
  • Is there 2 classes with same `ConfigurationFragment` name? – Ircover Mar 02 '17 at 12:18
  • Yes, but in different packages. One in the package related to the main favor, the other one in the package related to the premium flavor – kemmitorz Mar 02 '17 at 12:20
  • Use view.getId() inside onclick event – Patel Jaimin Mar 02 '17 at 12:20
  • @PatelJaimin the onclick event is not fired so I cannot use the view.getId() inside it – kemmitorz Mar 02 '17 at 12:21
  • 1
    How do you control what fragment class should be used? It seems your premium fragment class is never called. – Ircover Mar 02 '17 at 12:30
  • @Ircover you can see in the premium xml the context defined is the premium package that contains the premium fragment class. So I supposed that it would use the proper fragment class, but it is not true. Then, is there a way to use that specific fragment class instead of the main fragment class? – kemmitorz Mar 02 '17 at 12:38
  • 1
    Try to find solution here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-flavors – Ircover Mar 02 '17 at 12:58

2 Answers2

0

Your fragment need to implement with View.OnClickListener

 public class InputFragment extends Fragment implements View.OnClickListener

then change your code as below

 onActivityCreated(Bundle savedInstanceState){

 btn = (Button) view.findViewById(R.id.btnClick);

 btn.setOnClickListener(this);

}

@Override
public void onClick(View view){

 switch (v.getId()) {
    case R.id.btn:

        //HERE YOU CAN PERFORM YOUR ACTION
        break;
    default:
        break;
    }
}
John Joe
  • 12,412
  • 16
  • 70
  • 135
vijay chhalotre
  • 396
  • 2
  • 11
0

Solution found here:

Android Studio build flavors - How to have same source files in diverse flavors

If you want to have a different version of the same class in the two flavor you'll need to create it in both flavors and remove it from main.

src/flavor1/java/com/foo/A.java

src/flavor2/java/com/foo/A.java

And then your code in src/main/java can do:

import com.foo.A

depending on the flavor selected, the right version of com.foo.A is used.

Community
  • 1
  • 1
kemmitorz
  • 351
  • 7
  • 18