alertDialog with confusing layout in Fragment

Asked

Viewed 63 times

1

I can’t change the layout of a alertDialog() who inherited a layout I don’t know where. I’ve tried to set a customAlertDialogLayout also unsuccessful. I’ve tried changing the context and nothing seems to work.

Sedefragment.java

package br.gov.rj.pmerj.policialoficialsv.fragments;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class SedeFragment extends Fragment implements RecyclerViewOnClickListenerHack {
  private RecyclerView mRecyclerView;
  private ImageButton mImageButton;
  private List < Sede > mList;
  //public LegislacaoDao legislacaoDao;
  Fragment fragment;
  public Bundle savedInstanceState;
  public SedeDao sededaoDaoselecionado;
  int mpId = 0;
  String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
  String path = (exStoragePath + "/media/alarms/");
  public Sede sede;
  public SedeDao sedeDao;


  public SedeFragment() {
    // Required empty public constructor
  }

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

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_sede, container, false);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_list);
    mImageButton = (ImageButton) view.findViewById(R.id.imgrota);
    mRecyclerView.setHasFixedSize(true);
    GridLayoutManager llm = new GridLayoutManager(getActivity(), 1, GridLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(llm);
    mRecyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(getActivity(), mRecyclerView, this));
    // mRecyclerView.addOnItemTouchListener(new RecyclerViewTouchListener( getActivity(), mRecyclerView, this ));
    //GridLayoutManager llm = new GridLayoutManager(getActivity(), 1, GridLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(llm);
    sedeDao = new SedeDao((MainActivity) getActivity());
    //LegislacaoDao legislacaoDao = new LegislacaoDao((MainActivity) getActivity());
    //mList = (ArrayList<Legislacao>) legislacaoDao.listar();
    mList = (ArrayList < Sede > ) sedeDao.listar();

    // SedeAdapter adapter = new SedeAdapter(getActivity().getApplicationContext(), mList);
    SedeAdapter adapter = new SedeAdapter(getActivity(), mList);

    mRecyclerView.setAdapter(adapter);

    return view;

  }
  @Override
  public void onLongPressClickListener(View view, int position) {

    // acao2(getActivity(),mList.get(position));
    //  return false;
    //saveas();
    //Toast.makeText(getActivity(), "onLongPressClickListener(): "+position, Toast.LENGTH_SHORT).show();
    // Toast.makeText(getActivity(), "onLongPressClickListener(): "+passaro.getCantoAudio(), Toast.LENGTH_SHORT).show();
    /*CarAdapter adapter = (CarAdapter) mRecyclerView.getAdapter();
    adapter.removeListItem(position);*/
  }
  @Override
  public void onClickListener(View view, int position) {
    switch (view.getId()) {
      case R.id.imgrota:
        sededaoDaoselecionado = new SedeDao(getContext());
        Bundle bundle = new Bundle();
        bundle.putInt("iditem", mList.get(position).getId());
        String s;
        s = mList.get(position).getEndereco();
        Toast.makeText(getContext(), mList.get(position).getOpm() + " - " + mList.get(position).getEndereco() + " - " + mList.get(position).getBairro() + " - " + mList.get(position).getMunicipio(), Toast.LENGTH_SHORT).show();

        break;
    }


    //   sededaoDaoselecionado = new SedeDao(getContext());


    //Toast.makeText(getContext(), "teste"+itemdaoDaoselecionado.buscarPorId(position).getId(), Toast.LENGTH_SHORT).show();
    // Toast.makeText(getContext(), "item unico selecionado" + mList.get(position).getId(), Toast.LENGTH_SHORT).show();
    // Toast.makeText(getContext(),  mList.get(position).getOpm() + " - " + mList.get(position).getEndereco() + " - " +  mList.get(position).getBairro()+ " - " +  mList.get(position).getMunicipio() , Toast.LENGTH_SHORT).show();
    // Toast.makeText(getActivity(), "onClickListener(): " + position, Toast.LENGTH_SHORT).show();
    //sededaoselecionado = new SedeDao(getContext());

    //Bundle bundle = new Bundle();
    // bundle.putInt("iditem", 10);
    //mList.get(position).getId()
    // bundle.putInt("iditem", itemdaoDaoselecionado.buscarPorId(position).getId());
    //bundle.putInt("iditem", mList.get(position).getId());
    //fragment = new LeiFragment();
    // fragment.setArguments(bundle);
    // replaceFragment();
    // }


    //URLEncoder.encode("your URL here", "UTF8")
    //String s;
    //Sede sd;
    //SedeDao sddao;
    //sd = new Sede;
    //s = mList.get(position).getEndereco();
    //s= URLEncoder.encode(" "+s, "UTF8")


    //Avallie
    /*
            String my_package_name;
            String url;
            my_package_name="com.google.android.apps.maps";
            try {
                //Check whether Google Play store is installed or not:
                view.getContext().getPackageManager().getPackageInfo("com.google.android.apps.maps", 0);

                url = "market://details?id=" + my_package_name;
            } catch ( final Exception e ) {
                url = "https://play.google.com/store/apps/details?id=" + my_package_name;
            }

*/

    //  Uri gmmIntentUri = Uri.parse("google.navigation:q="+s+","+mList.get(position).getBairro() +" "+  mList.get(position).getMunicipio());
    //Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
    //mapIntent.setPackage("com.google.android.apps.maps");
    //if (mapIntent.resolveActivity(getContext().getPackageManager()) != null) {
    //  startActivity(mapIntent);
    //}






  }

  public void alert(View v, String s) {
    final AlertDialog dialog = new AlertDialog.Builder(v.getContext())
      .setView(R.layout.custom_dialog_layout)
      .create();

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(v.getContext());
    //alertDialogBuilder.setView((R.layout.custom_dialog_layout));
    alertDialogBuilder.setMessage(s);
    // alertDialogBuilder.setView(R.layout.custom_dialog_layout);
    // AlertDialog alertDialog = alertDialogBuilder.create();
    // setView(mLayoutInflater.createView(R.layout.custom_dialog_layout, null));
    //  alertDialog.setView(v.);
    alertDialogBuilder.show();
  }

  private void replaceFragment() {
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    fragmentManager.beginTransaction()
      .replace(R.id.container, fragment)
      .commit();
  }



  private static class RecyclerViewTouchListener implements RecyclerView.OnItemTouchListener {
    private Context mContext;
    private GestureDetector mGestureDetector;
    private RecyclerViewOnClickListenerHack mRecyclerViewOnClickListenerHack;

    public RecyclerViewTouchListener(Context c, final RecyclerView rv, RecyclerViewOnClickListenerHack rvoclh) {
      mContext = c;
      mRecyclerViewOnClickListenerHack = rvoclh;

      mGestureDetector = new GestureDetector(mContext, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public void onLongPress(MotionEvent e) {
          super.onLongPress(e);

          View cv = rv.findChildViewUnder(e.getX(), e.getY());

          if (cv != null && mRecyclerViewOnClickListenerHack != null) {
            mRecyclerViewOnClickListenerHack.onLongPressClickListener(cv,
              rv.getChildPosition(cv));
          }
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
          View cv = rv.findChildViewUnder(e.getX(), e.getY());

          if (cv != null && mRecyclerViewOnClickListenerHack != null) {
            mRecyclerViewOnClickListenerHack.onClickListener(cv,
              rv.getChildPosition(cv));
          }

          return (true);
        }
      });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
      mGestureDetector.onTouchEvent(e);
      return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {}

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
  }


}

item_legislacao.xml

 <
  android.support.v7.widget.CardView xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: tools = "http://schemas.android.com/tools"
xmlns: card_view = "http://schemas.android.com/apk/res-auto"
android: layout_gravity = "center"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
card_view: cardCornerRadius = "3dp"
card_view: cardElevation = "1.5dp"
card_view: cardUseCompatPadding = "true" >

  <
  LinearLayout
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical"
android: layout_margin = "18dp" >

  <
  TextView
android: textColor = "#000000"
android: id = "@+id/tv_model"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: textSize = "15sp" / >

  <
  TextView
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: id = "@+id/endereco"
android: textSize = "15sp"
android: textColor = "#000" / >

  <
  LinearLayout
android: orientation = "horizontal"
android: layout_width = "fill_parent"
android: layout_height = "30dp"
android: layout_gravity = "center_vertical"
android: gravity = "center"
android: layout_marginTop = "4dp" >

  <
  ImageButton
android: id = "@+id/imgrota"
android: theme = "@style/AppTheme.Button"
android: src = "@android:drawable/ic_dialog_map"
android: layout_height = "fill_parent"
android: layout_width = "0dip"
android: layout_weight = "1"
android: adjustViewBounds = "true"
android: padding = "2dp"
android: scaleType = "fitCenter" / >

  <
  ImageButton
android: id = "@+id/imgtel"
android: src = "@android:drawable/ic_menu_call"
android: theme = "@style/AppTheme.Button"
android: layout_height = "fill_parent"
android: layout_width = "0dip"
android: layout_weight = "1"
android: adjustViewBounds = "true"
android: padding = "2dp"
android: scaleType = "fitCenter" / >

  <
  ImageButton
android: id = "@+id/imginfo"
android: src = "@android:drawable/ic_dialog_info"
android: theme = "@style/AppTheme.Button"
android: layout_height = "fill_parent"
android: layout_width = "0dip"
android: layout_weight = "1"
android: adjustViewBounds = "true"
android: padding = "2dp"
android: scaleType = "fitCenter" /
  >

  <
  ImageButton
android: id = "@+id/imgshare"
android: src = "@android:drawable/ic_menu_share"
android: layout_height = "fill_parent"
android: layout_width = "0dip"
android: layout_weight = "1"
android: theme = "@style/AppTheme.Button"
android: adjustViewBounds = "true"
android: padding = "2dp"
android: scaleType = "fitCenter" / >

  <
  /LinearLayout>

  <
  /LinearLayout>

Sedeadapter.java

myViewHolder.btninfo.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {

    SedeFragment sedefragment;
    sedefragment = new SedeFragment();
    String s;
    s = (mList.get(position).getOpm() + "\n" + "\n" + "Endereço:" + " + mList.get(position).getEndereco() + "\
      n " +
      "Bairro:" + mList.get(position).getBairro() + "\n" + "Município:" + mList.get(position).getMunicipio() + "\n" + "\n" +
      "SOP/OF DE DIA:" + mList.get(position).getTel());
    sedefragment.alert(v, s);


  }
});

Styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>


</style>


<style name="AppTheme.Button" parent="Base.Widget.AppCompat.Button" >
    <item name="colorButtonNormal">#FF424242</item>
    <item name="android:textColor">@android:color/white</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

Usar:

the exit is being:

inserir a descrição da imagem aqui

  • There’s little code, enough not to know what’s going on.

  • updated with more code

  • Now it’s a mess. I realized that your Alert method is not being called anywhere within your code.

  • sorry the confusion ... should post only the parts of the relevant code ?? missed I include the Adapter where it is being called ... I will include

  • i instated Sedefragment to be able to access the method created there. before had a method within onBindViewHolder but gave the same problem .. :(

  • Probably this is happening because you are calling Alert where you mount each item.

  • how it would be right to do this then , since the boot is inside a recicle view ... as I can call without being the inside... should make an interface ??

  • You should put Alert inside the Adapter but only show it when you click a button, or something close to it;

  • I’m not even looking at the code because it’s a zone, hehe... but looking at the image, you can see that each item is generating an Alert, and that’s not what you want right?! Then you should only show Alert when you click on a button inside the item.

  • It was like this before, but the same problem happened. So I put him inside a method and called only from there, he runs, but it doesn’t solve the problem... I also tried to apply a new xml for it but also did not solve... I do not know what is interfering

Show 6 more comments

1 answer

3


Friend, try to change this style of theme, android: theme = "@style/AppTheme.Button", I’ve been through a problem like that,.

  • worked by changing android: Theme = "@style/Apptheme.Button" by android:background="@color/cardview_dark_background" ... thanks friend. I don’t know why I was inheriting this layout applied there as Theme.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.