0

I have a custom dialog preference that have bunch of button.each button represent a number.something like clock application when you want to set alarm. how can I get onClick of each button in my dialog preference? this is part of my dialog layout (time_picker) :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<LinearLayout 
    android:id="@+id/showTimer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="_ _ : _ _"
        android:textSize="20dp"
        android:textStyle="bold" />

</LinearLayout>

<RelativeLayout
    android:id="@+id/Buttons" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_below="@+id/showTimer"
    android:layout_marginTop="10dp">

    <Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button5"
    android:layout_alignBottom="@+id/button5"
    android:layout_alignLeft="@+id/button3"
    android:layout_centerInParent="true"
    android:text="6"
    android:textStyle="bold"
    android:textSize="20dp"
    android:tag="1"
    android:background="@android:color/transparent" />

<Button
    android:id="@+id/button2"
    android:layout_width="100dp"
    ...

this is Setting layout that launch dialog preference:

<com.example.test.TimerForNoti 
    android:key = "pref_sync_noti_timer"
    android:title = "@string/pref_sync_noti_timer"
    android:summary = "@string/pref_sync_noti_timer_summ"
    android:dialogMessage = "@string/pref_sync_noti_timer">
</com.example.test.TimerForNoti>

and this is class of dialog preference (TimerForNoti) :

public class TimerForNoti extends DialogPreference 
{

public TimerForNoti(Context context, AttributeSet attrs) {
    super(context, attrs);

    setDialogLayoutResource(R.layout.time_picker);
    setPositiveButtonText(R.string.ok);
    setNegativeButtonText(R.string.cancel);

    Dialog di = new Dialog(context);
    di.setContentView(R.layout.time_picker);

    OnClickListener test = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String tag = v.getTag().toString();
            if(tag == "1"){
                Log.v("onOne", "ok");
            }
                            // .....

        }
    };

    Button btn1 = (Button) di.findViewById(R.id.button1);
    btn1.setOnClickListener(test);
}

}

Saber Solooki
  • 1,182
  • 1
  • 15
  • 34

2 Answers2

0

If I understand your question right, you can set tag for each button and set one OnClickListener for all of them:

<Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button5"
    android:layout_alignBottom="@+id/button5"
    android:layout_alignLeft="@+id/button3"
    android:layout_centerInParent="true"
    android:text="6"
    android:textStyle="bold"
    android:textSize="20dp"
    android:tag="6"
    android:background="@android:color/transparent" />

Code:

        OnClickListener buttonsListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                String buttonText = v.getTag().toString();
                // your logic here
            }
        };

        Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(buttonsListener);

        Button btn2 = (Button)findViewById(R.id.button2);
        btn2.setOnClickListener(buttonsListener);

        Button btn3 = (Button)findViewById(R.id.button3);
        btn3.setOnClickListener(buttonsListener);

        // your buttons here

Also you can use v.getId() in OnClickListener for determining which button was clicked

Dimmerg
  • 2,113
  • 1
  • 13
  • 14
0

As much I understood your question. You want one method to be called on every button clicked. For that Add this to your every button element in xml:

<Button 
    android:onClick="OnClick"
    ..                       />

And this method to your .java file where you want to perform task:

public void OnClick(View v){
    // do something....
}

Or you can directly call setOnClickListener at button object

button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // do something....
        }
});
Iamat8
  • 3,888
  • 9
  • 25
  • 35
  • I try this method but it isn't work and I don't know why... probably because this is dialog preference not activity – Saber Solooki Jan 26 '14 at 15:41
  • It possible, but may not work, because system will search OnClick method into activity. Look at this: http://stackoverflow.com/questions/4243704/using-onclick-attribute-in-layout-xml-causes-a-nosuchmethodexception-in-android – Dimmerg Jan 26 '14 at 15:41