lundi 20 mars 2017

Checkbox not visible in Android 4.x but is visible in Android 5+

I have listview which contains textview and a checkbox. I am showing the listview in a dialogfragment. The problem is that the checkboxes do not show up for devices(emulator) running Android version 4.1 to 4.4.4. But they show up normally for Android 5.0+. I have been trying to fix this for hours. But no success. Can someone please tell me what am I doing wrong?

Android 4.1, No Checkbox

Android 4.1, Checkbox visible

Listview:

<RelativeLayout
        android:id="@+id/rl_cats_overlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_game_info">

        <ListView
            android:id="@+id/lv_select_cats"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:layout_marginLeft="35dp"
            android:layout_marginRight="35dp"
            android:fadeScrollbars="false" />

        <TextView
            android:id="@+id/tv_overlay_pro"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="35dp"
            android:layout_marginRight="35dp"
            android:background="#A6000000"
            android:gravity="center"
            android:text="Available with \n'Quiz Time Pro'"
            android:textColor="@color/android_green"
            android:textSize="24sp" />

    </RelativeLayout>

Listview row:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<CheckBox
    android:id="@+id/cb_row_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>

<TextView
    android:id="@+id/tv_row_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/cb_row_item"
    android:layout_alignBottom="@+id/cb_row_item"
    android:layout_marginLeft="12dp"
    android:layout_marginStart="12dp"
    android:layout_toEndOf="@+id/cb_row_item"
    android:layout_toRightOf="@+id/cb_row_item"
    android:text="Category name goes here"
    android:textSize="15sp" />

</RelativeLayout>

DialogFragment Class inflating the listview:

public class SelectGameModeDialogFragment extends DialogFragment {

boolean[] checkedCategories = new boolean[Constants.CATEGORIES.length + 1];
boolean[] checkedDifficulty = new boolean[Constants.DIFFICULTY_LEVELS.length + 1];

List<String> categories = new ArrayList<>();
List<String> difficultyLevels = new ArrayList<>();

TextView gameInfoTV;
Spinner spinner;

public static SelectGameModeDialogFragment newInstance(String title) {
    SelectGameModeDialogFragment frag = new SelectGameModeDialogFragment();
    Bundle args = new Bundle();
    args.putString("title", title);
    frag.setArguments(args);
    frag.setStyle(DialogFragment.STYLE_NO_FRAME, 0);
    return frag;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    categories.addAll(Arrays.asList(Constants.CATEGORIES));
    difficultyLevels.addAll(Arrays.asList(Constants.DIFFICULTY_LEVELS));
    return inflater.inflate(R.layout.select_game_mode, container);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final ListView lvCategories = (ListView) view.findViewById(R.id.lv_select_cats);

    gameInfoTV = (TextView) view.findViewById(R.id.tv_game_info);
    gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[0]);

    final TextView proTV = (TextView) view.findViewById(R.id.tv_overlay_pro);
    final Button btnPlay = (Button) view.findViewById(R.id.btn_restart_game);

    spinner = (Spinner) view.findViewById(R.id.spinner_gamer_categories);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.game_types_spinner, R.layout.spinner_item);
    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if (pos == Constants.MODE_MILLIONAIRE
                    || pos == Constants.MODE_UNLIMITED
                    || pos == Constants.MODE_20QUESTIONS
                    || pos == Constants.MODE_60SECONDS) {
                ViewGroup.LayoutParams params = lvCategories.getLayoutParams();
                params.height = 0;
                lvCategories.setLayoutParams(params);
                proTV.setVisibility(View.GONE);
            } else if (pos == Constants.MODE_CATEGORIES) {
                MultipleCategoryListAdapter adapter = new MultipleCategoryListAdapter(getActivity(), categories);
                lvCategories.setAdapter(adapter);
                ViewGroup.LayoutParams params = lvCategories.getLayoutParams();
                params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
                lvCategories.setLayoutParams(params);
            } else if (pos == Constants.MODE_DIFFICULTY) {
                SingleCategoryListAdapter adapter = new SingleCategoryListAdapter(getActivity(), difficultyLevels);
                lvCategories.setAdapter(adapter);
                ViewGroup.LayoutParams params = lvCategories.getLayoutParams();
                params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
                lvCategories.setLayoutParams(params);
            }
            lvCategories.setVisibility(View.INVISIBLE);
            if (pos == Constants.MODE_MILLIONAIRE) {
                gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[0]);
            } else if (pos == Constants.MODE_20QUESTIONS) {
                gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[1]);
            } else if (pos == Constants.MODE_CATEGORIES) {
                gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[2]);
                lvCategories.setVisibility(View.VISIBLE);
            } else if (pos == Constants.MODE_60SECONDS) {
                gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[3]);
            } else if (pos == Constants.MODE_UNLIMITED) {
                gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[4]);
            } else if (pos == Constants.MODE_DIFFICULTY) {
                gameInfoTV.setText(getResources().getStringArray(R.array.game_types_info)[5]);
                lvCategories.setVisibility(View.VISIBLE);
            }
        }

        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    btnPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int spinnerPosition = spinner.getSelectedItemPosition();
            ArrayList<String> cat = new ArrayList<>();
            if (spinnerPosition == Constants.MODE_MILLIONAIRE) {
                goToPlayingFragmentWithCategories(cat, Constants.MODE_MILLIONAIRE);
            } else if (spinnerPosition == Constants.MODE_20QUESTIONS) {
                goToPlayingFragmentWithCategories(cat, Constants.MODE_20QUESTIONS);
            } else if (spinnerPosition == Constants.MODE_CATEGORIES) {
                for (int i = 0; i < checkedCategories.length; i++) {
                    if (checkedCategories[i]) {
                        cat.add(categories.get(i));
                    }
                }
                goToPlayingFragmentWithCategories(cat, Constants.MODE_CATEGORIES);
                Arrays.fill(checkedCategories, false);
                System.out.println();
            } else if (spinnerPosition == Constants.MODE_60SECONDS) {
                goToPlayingFragmentWithCategories(cat, Constants.MODE_60SECONDS);
            } else if (spinnerPosition == Constants.MODE_UNLIMITED) {
                goToPlayingFragmentWithCategories(cat, Constants.MODE_UNLIMITED);
            } else if (spinnerPosition == Constants.MODE_DIFFICULTY) {
                String diff = "";
                for (int i = 0; i < 3; i++) {
                    if (checkedDifficulty[i]) {
                        diff = difficultyLevels.get(i);
                    }
                }
                goToPlayingFragmentWithCategories(cat, Constants.MODE_DIFFICULTY, diff);
            }
            dismiss();
        }
    });

    Button btnBack = (Button) view.findViewById(R.id.btn_game_mode_back);
    btnBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dismiss();
        }
    });
}




Aucun commentaire:

Enregistrer un commentaire