Reputation: 21
am working on an application which has lot of button in the dialog(like keyboard). I need to get the text of the click button and assign to variable. I created a common function here(showtoast). but when I click on the button in the popup, it is unfortunately stopped. showing like no such method. if I rise the event from java means, there is no such problem. Example:
buttona.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button b = (Button)v;
String text = b.getText().toString();
Toast.makeText(getApplicationContext(), "button clicked is" + text, Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
MainActivity .java
package com.example.sample;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Context c = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showdialog();
}
private void showdialog() {
// TODO Auto-generated method stub
Dialog dialog = new Dialog(c);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.popup);
dialog.show();
}
public void showtoast(View v)
{
Button b = (Button)v;
String text = b.getText().toString();
Toast.makeText(getApplicationContext(), "button clicked is" + text, Toast.LENGTH_SHORT).show();
}
}
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showtoast"
android:text="a"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showtoast"
android:text="b"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showtoast"
android:text="c"
/>
</LinearLayout>
LOG:
01-28 12:05:17.386: E/AndroidRuntime(7875): java.lang.IllegalStateException: Could not find a method showtoast(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button
please anyone help to rise the event from xml for buttons in dialog or any other solution.
Upvotes: 1
Views: 1924
Reputation: 169
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b"
android:text="c"
/>
</LinearLayout>
private void showdialog() {
// TODO Auto-generated method stub
Dialog dialog = new Dialog(c);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.popup);
final Button b = (Button)dialog.getWindow().findViewById(R.id.b);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"one", 2000).show();
}
});
dialog.show();
}
Upvotes: 2
Reputation: 466
You cannot directly call the method of activity from the dialog for that you need to implement a listener & a custom dialog class.
public class CustomDialog extends Dialog {
public CustomDialog(Context context, String title,
final ItemReturnListener listener) {
super(context, R.style.CustomDialog);
// TODO Auto-generated constructor stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_custom);
Button tvDate = (Button) findViewById(R.id.tvDate);
Button tvName = (Button) findViewById(R.id.tvName);
Button tvPrice = (Button) findViewById(R.id.tvPrice);
tvDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listener.returnString(tvDate.getText().toString());
dismiss();
}
});
tvName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listener.returnString(tvName.getText().toString());
dismiss();
}
});
tvPrice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listener.returnString(tvPrice.getText().toString());
dismiss();
}
});
getWindow().setLayout(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
}
now open dialog by calling this method
private void openDialog() {
dialog = new CustomDialog(MainActivity.this, new ItemReturnListener() {
@Override
public void returnString(String str) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "button clicked is" + str, Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
Create Interface like this
public interface ItemReturnListener {
void returnString(String str);
}
Upvotes: 0