change background of views on hover inside dialog

297 Views Asked by At

im trying to build share dialog. in this share dialog I have facebook,whatsapp,mail and more.

im trying the make the background of every view change on touch to indicate the toch. so far no problem. the problem is that I also want to let the user move is finger into other option and when he do that the previous background return to his original color and the new view background changes.

i just cant get the hover event at all, and I couldn't trigger other onTouchEvent as long as the first one is still alive.

this is my code so far:

public class customDialogFragment1 extends DialogFragment {


public customDialogFragment1() {
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    postShareUrl=getActivity().getResources().getString(R.string.servicePostShareUrl);
    id=getArguments().getString("body");
    postTitle=getArguments().getString("subject");
    Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
    sendIntent.setType("text/plain");

    Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
    LayoutInflater inflater = (LayoutInflater) getActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.share_title_layout, null);
    // Set title divider color


    TextView txtTitle= (TextView) v.findViewById(R.id.share_title);
    Typeface tf = Typeface.createFromAsset(getActivity().getResources().getAssets(),
            "fonts/OpenSansHebrew-Bold.ttf");

    txtTitle.setTypeface(tf);
    View layout=inflater.inflate(R.layout.custom_share_layout_inner,null);
    builder.setView(layout);

    ImageView facebook= (ImageView) layout.findViewById(R.id.imgFaceebook);
    ImageView whatsapp= (ImageView) layout.findViewById(R.id.imgWhatsapp);
    ImageView more= (ImageView) layout.findViewById(R.id.imgMore);
    ImageView mail= (ImageView) layout.findViewById(R.id.imgMail);

    facebook.setOnClickListener(imageClickListener);
    whatsapp.setOnClickListener(imageClickListener);
    more.setOnClickListener(imageClickListener);
    mail.setOnClickListener(imageClickListener);

    List activities = getActivity().getPackageManager().queryIntentActivities(sendIntent, 0);
    Context context=(Activity)getActivity();
    for(int i=0;i<activities.size();i++) {
        ResolveInfo appPacageName = (ResolveInfo) activities.get(i);
        Log.i("pacageName", appPacageName.toString());
        if (appPacageName.toString().contains("com.facebook.composer")) {
            shareCheckList[0] = appPacageName;
        } else if (appPacageName.toString().contains("whatsapp")) {
            shareCheckList[1] = appPacageName;
        } else if (appPacageName.toString().contains("mail")) {
            shareCheckList[2] = appPacageName;
        }
    }
    dialog.setContentView(layout);
    Window window = dialog.getWindow();
    window.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    window.setGravity(Gravity.CENTER);
    window.setLayout(MainActivity.screenWidth-90,350);

    return dialog;
}

and this is my ontouch listener :

private View.OnClickListener imageClickListener=new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT,getArguments().getString("subject"));
        intent.putExtra(Intent.EXTRA_TEXT,getArguments().getString("body"));

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

                    if(!shareCheckList[0].toString().isEmpty()){
                        intent.setClassName(shareCheckList[0].activityInfo.packageName, shareCheckList[0].activityInfo.name);                        
                        ((Activity)getActivity()).startActivity(intent);
                break;
            case R.id.imgWhatsapp:                   
                        intent.setClassName(shareCheckList[1].activityInfo.packageName, shareCheckList[1].activityInfo.name);                         
                        ((Activity)getActivity()).startActivity(intent);
                }
                break;
            case R.id.imgMail:
                try{                      
                        intent.setClassName(shareCheckList[2].activityInfo.packageName, shareCheckList[2].activityInfo.name);

                        ((Activity)getActivity()).startActivity(intent);


                break;
            case R.id.imgMore:
                CustomDialogFragment2 cdf=new CustomDialogFragment2();
                Bundle bundle = new Bundle();
                bundle.putString("body",id);
                bundle.putString("subject", postTitle);
                cdf.setArguments(bundle);
                cdf.show(getActivity().getFragmentManager(), "customDialogFragment2");
                break;

        }

    }
};
1

There are 1 best solutions below

1
On

If I understood right, you should use state list:

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Just create xml drawable file for every ImageView, and set for each state item with source you need.