0

I have a RecyclerView , in each item there's a (Share) button that prompts a custom dialog which contains a TextView and a Button. so, I build the custom dialog inside the Adapter of RecyclerView , everything is fine except for the button's appearance.

it looks like there's a white overlay on top of it and when I click on it, the overlay slightly disappears.

This is what the Button looks like in xml: enter image description here

and here's what I get: enter image description here

here's my custom dialog layout:

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

    <TextView
        android:id="@+id/tv_url"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:padding="@dimen/fab_margin"
        android:textSize="8pt"
        />

    <Button
        android:id="@+id/btn_copy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/copy_address"
        android:theme="@style/button"/>


</LinearLayout>

and this is the java code creating the dialog:

AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(inflater.inflate(R.layout.share_dialog, null));
    builder.setTitle(view.getContext().getResources().getString(R.string.share));
share = builder.create();
share.show();

Note: I think the problem is related to the Inflater, particularly this line:

LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

because in MainActivity it works fine when I replace it by this:

LayoutInflater inflater = MainActivity.this.getLayoutInflater();

but I can't use it since I can't get Activity from inside the adapter.

I hope my question is clear, I'd appreciate your help.

Edit: code of @style/button

<style name="button" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorControlHighlight">@color/colorPrimaryDark</item>
        <item name="colorButtonNormal">@color/colorGray</item>
        <item name="android:textColor">@color/colorWhite</item>
        <item name="android:colorAccent">@color/colorPrimary</item>
    </style>
Outlandish
  • 243
  • 1
  • 2
  • 13
  • Is the `theme` and `style` tag is required on your button? – Tepits Sep 10 '18 at 08:28
  • @Tepits yes, `theme` is for the green background and white text, I tried removing both but still the same problem. I removed the `style` tag now, I found that it's unnecessary. – Outlandish Sep 10 '18 at 08:50
  • Since it is from `style` tag, its better to use `style` tag than `theme` tag. Like `style="@style/button"`. Check if this solve your issue. – Tepits Sep 10 '18 at 08:59
  • @Tepits I tried it but still the same problem. – Outlandish Sep 10 '18 at 09:06
  • Okay. Can you share your code on your `button` style? `@style/button` – Tepits Sep 10 '18 at 09:09
  • @Tepits sure, I added it in the question. Although, I don't believe it's related to the style because I used the dialog in MainActivity and everything worked fine, the problem is when I build the dialog inside the adapter. – Outlandish Sep 10 '18 at 09:28
  • I see. Then try to pass your `activity` or `context` inside your adapter so that you can do the `LayoutInflater inflater = MainActivity.this.getLayoutInflater();` which is you have said that is working. – Tepits Sep 10 '18 at 09:41
  • Check this link on getting activity from adapter. https://stackoverflow.com/questions/12142255/call-activity-method-from-adapter – Tepits Sep 10 '18 at 09:44
  • @Tepits Thanks ! I tried it first but it didn't change, but it worked when I changed the android:layout_width and android:layout_height to "wrap_content". – Outlandish Sep 10 '18 at 10:11

2 Answers2

0

change your parent="ThemeOverlay.AppCompat.Light" to ThemeOverlay.AppCompat.Dark.ActionBar

hossam scott
  • 466
  • 5
  • 28
0

Thanks to @Tepits comment , and some changes .. i found a solution .

first, as this suggested Call Activity method from adapter

I changed LayoutInflater inflater to this:

LayoutInflater inflater=  ((MainActivity)context).getLayoutInflater();

and then changed android:layout_width and android:layout_height in the parent layout in dialog.xml to wrap_content and it worked !

Outlandish
  • 243
  • 1
  • 2
  • 13