One click listener for different kind of Android
I have implemented a single task listener for different text view in sherlock fragment Here is my code
getView().findViewById(R.id.tv_plan).setOnClickListener(btn1Listener);
getView().findViewById(R.id.tv_plan1).setOnClickListener(btn1Listener);
and my listener code
private View.OnClickListener btn1Listener = new View.OnClickListener() {
@Override
public void onClick(View v)
{
android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
String title=null;
String tag=null;
switch(v.getId())
{
case R.id.tv_plan:
System.out.println("Plan ids" +v.getId());
title= "My Plan"; tag = "viewplan_dialog";
Utilsdialog dialog_vwplan = new Utilsdialog(context, R.layout.child,title, tag);
System.out.println("Before showing dialog tag");
dialog_vwplan.show(fm, tag);
System.out.println("SUCCESS");
break;
case R.id.tv_plan1:
System.out.println("Plan ids" +v.getId());
title= "Change plan"; tag = "changeplan_dialog";
Utilsdialog dialog_chgplan = new Utilsdialog(context, R.layout.child,title, tag);
dialog_chgplan.show(fm, tag);
break;
}
}
};
My code always calls the last listener specified, which is tv_plan1 , but I need to provide an onclick listener for both text views. My current code calls tv_plan1 for both text clicks .... pls help
+3
source to share
2 answers
try this, use Button
instead TextView
.
Implements your activity first OnClickListener
.
public class Auctions extends Activity implements OnClickListener {
private Button menu_Btn;
private Button deal_Btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.xml);
menu_Btn = (Button) findViewById(R.id.menu);
deal_Btn = (Button) findViewById(R.id.deals);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.menu:{
System.out.println("Plan ids" +v.getId());
title= "My Plan"; tag = "viewplan_dialog";
Utilsdialog dialog_vwplan = new Utilsdialog(context, R.layout.child,title, tag);
System.out.println("Before showing dialog tag");
dialog_vwplan.show(fm, tag);
System.out.println("SUCCESS");
break;
}
case R.id.deals:{
System.out.println("Plan ids" +v.getId());
title= "Change plan"; tag = "changeplan_dialog";
Utilsdialog dialog_chgplan = new Utilsdialog(context, R.layout.child,title, tag);
dialog_chgplan.show(fm, tag);
break;
}
default:
break;
}
}
+2
source to share
Can you use this?
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="test"
android:text="Datepicker"
android:textColor="#FFFFFF" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="test"
android:text="Test 2"
android:textColor="#FFFFFF" />
in the documentation
Then in your activity:
public void test(View v) {
Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
}
0
source to share