Multiple buttons in a popup, trying to stay organized

925 views Asked by At

I'm implementing a popup in fragment Tab3Fragment and would like to keep the popup code in,

 public void showPopup(View anchorView){
 } 

and minimized elsewhere in the Tab3Fragment as much as possible to keep things tidy.

Currently showPopup looks something like this,

public void showPopup(View anchorView) {    
    Button btnDismiss, btnFirstRecord, btnPreviousRecord, btnNextRecord, btnLastRecord;
    LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemServi (Context.LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
    final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);    
    TextView tv = (TextView)popupView.findViewById(R.id.tv);
    tv.setText("Blah, blah, blah");

    btnDismiss = (Button)popupView.findViewById(R.id.btnDismissxml);
    btnFirstRecord = (Button)popupView.findViewById(R.id.btnFirstRecordxml);
    btnPreviousRecord = (Button)popupView.findViewById(R.id.btnPreviousRecordxml);
    btnNextRecord = (Button)popupView.findViewById(R.id.btnNextRecordxml);
    btnLastRecord = (Button)popupView.findViewById(R.id.btnLastRecordxml);

    popupWindow.setFocusable(true);
    int location[] = new int[2];
    anchorView.getLocationOnScreen(location);
    popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0);
    }   
}

QUESTION: Is there any way to implement an onClick method case-switch statement inside of showPopup that would handle this? Perhaps something like,

@Override
public void onClick(View v) {       
    switch (v.getId()) {
    case R.id.btnFirstRecordxml:
        //firstRecord(v);
        break;
    case R.id.btnPreviousRecordxml:
        //previousRecord(v);
        break;
    case R.id.btnNextRecordxml:
        //nextRecord(v);
        break;
    case R.id.btnLastRecordxml:
        //lastRecord(v);
        break;
    case R.id.btnDismissxml:
        //closePopup(v);
        break;  
    }}

Other solutions such as putting onClick in popup_layout.xml thusly,

<Button
    android:id="@+id/btnFirstRecordxml"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/pszFirstRecordButton" 
    android:onClick="Tab3Fragment.firstRecord"/>

to handle button clicks would be highly appreciated. Thanks in advance...

Update, 23 Nov 2014. Here is a solution that does work for more than one button's code in showPopup(). I'll have several buttons in the popup....

private void showPopup(){
    LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
    View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
    Button btnDismiss=(Button)popupView.findViewById(R.id.btnDismissxml);
    Button btnSaveRecord=(Button)popupView.findViewById(R.id.btnSaveRecordxml);
    final PopupWindow popupWindow=new PopupWindow(popupView,480,500,true);
    popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 40);                                   
//first button
    btnSaveRecord.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            saveRecord();
        }
    });
 //second button
    btnDismiss.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }
    });
}

Any way to insert a case-switch structure to allow other button code in showPopup()? This would avoid creating separate onClickListener-onClicks for each button as shown above.

2

There are 2 answers

6
prakash On

Modify the xml like this, use android:onClick="ClickEvent" this formet in all the button

<Button
android:id="@+id/btnFirstRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pszFirstRecordButton" 
android:onClick="ClickEvent"/>
<Button
android:id="@+id/btnPreviousRecordxml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pszPreviousRecordButton" 
android:onClick="ClickEvent"/>

use the click funcationality like this,

public void clickEvent(View v)
{
    switch (v.getId()) {
case R.id.btnFirstRecordxml:
    //firstRecord(v);
    break;
case R.id.btnPreviousRecordxml:
    //previousRecord(v);
    break;
case R.id.btnNextRecordxml:
    //nextRecord(v);
    break;
case R.id.btnLastRecordxml:
    //lastRecord(v);
    break;
case R.id.btnDismissxml:
    //closePopup(v);
    break;  
}
}  

Or try this,

btn.setOnClickListener(btnaction); set like this listener for all buttons,

 OnClickListener btnaction = new OnClickListener() {
   @Override
   public void onClick(View v) {
     //use the switch conditons...
   }
 };
btn1.setOnClickListener(btnaction);
btn2.setOnClickListener(btnaction);
4
Rohit Heera On
public void showPopup(View anchorView) {    
    Button btnDismiss, btnFirstRecord, btnPreviousRecord, btnNextRecord, btnLastRecord;
    LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemServi (Context.LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
    final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);    
    TextView tv = (TextView)popupView.findViewById(R.id.tv);
    tv.setText("Blah, blah, blah");

    btnDismiss = (Button)popupView.findViewById(R.id.btnDismissxml);
    btnFirstRecord = (Button)popupView.findViewById(R.id.btnFirstRecordxml);
    btnPreviousRecord = (Button)popupView.findViewById(R.id.btnPreviousRecordxml);
    btnNextRecord = (Button)popupView.findViewById(R.id.btnNextRecordxml);
    btnLastRecord = (Button)popupView.findViewById(R.id.btnLastRecordxml);

btnFirstRecord.setOnClickListener(new Listener());
btnPreviousRecord.setOnClickListener(new Listener());
btnNextRecord .setOnClickListener(new Listener());
btnLastRecord .setOnClickListener(new Listener()); 

    popupWindow.setFocusable(true);
    int location[] = new int[2];
    anchorView.getLocationOnScreen(location);
    popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0);
    }   
}


Class Listener implements OnClickListener
{
@Override
        public void onClick(View v) {
 switch (v.getId()) {
    case R.id.btnFirstRecordxml:
        //firstRecord(v);
        break;
    case R.id.btnPreviousRecordxml:
        //previousRecord(v);
        break;
    case R.id.btnNextRecordxml:
        //nextRecord(v);
        break;
    case R.id.btnLastRecordxml:
        //lastRecord(v);
        break;
    case R.id.btnDismissxml:
        //closePopup(v);
        break;  
    }

}

}