Reputation:
I have made a main Dialog
class on which I send the layout ID and shows the layout as a Dialog
now when I send the layout from calling class it pops up the dialog but the contents of dialog i.e. buttons are inaccessible I can't set click listener for them. How to do that?
CALLING CLASS:-
CustomDialog obj=new CustomDialog(MailSenderActivity.this , R.layout.mydialog);
obj.show();
MAIN CLASS
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class CustomDialog extends Dialog implements
View.OnClickListener {
Dialog dialog;
int id;
public CustomDialog(MailSenderActivity mailsender, int id) {
super(mailsender);
this.id = id;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(id);
Button signInbutton=(Button)findViewById(R.id.signInButton);
Button closebutton=(Button)findViewById(R.id.closeButton);
}
public void closebutton(View v) {
Toast.makeText(getContext(), "You clicked a button!", Toast.LENGTH_LONG).show();
dismiss();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
id is:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:padding="30dip" android:orientation="horizontal"
android:weightSum="1"
android:background="@drawable/gmail">
<LinearLayout android:layout_width="296dp"
android:orientation="vertical" android:layout_gravity="center"
android:layout_weight="1.51" android:layout_height="497dp">
<TextView android:text="My Dialog" android:textSize="24.5sp"
android:layout_width="wrap_content" android:layout_gravity="center"
android:layout_height="wrap_content" android:layout_marginBottom="25dip"></TextView>
<TextView android:text="Enter Gmail Id" android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/name" android:layout_width="358dp"
android:singleLine="true" android:layout_height="wrap_content">
</EditText>
<TextView android:text="Enter Gmail Password"
android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
<EditText android:id="@+id/name" android:layout_width="314dp"
android:singleLine="true" android:layout_height="wrap_content"
android:password="false" android:inputType="textPassword"></EditText>
<Button android:text="Sign In" android:layout_width="67dp"
android:id="@+id/signInButton" android:layout_height="wrap_content"
android:clickable="true" android:onClick="signIn"></Button>
<Button android:text="close" android:layout_width="67dp"
android:id="@+id/closeButton" android:layout_height="wrap_content"
android:clickable="true" android:onClick="closeButton"></Button>
</LinearLayout>
</LinearLayout>
Upvotes: 13
Views: 41738
Reputation: 31
In 2019 this work for me. You can change text view with button.
Dialog mdialog = new Dialog(this);
mdialog.setContentView(R.layout.activity_select_type);
mdialog.show();
TextView view = mdialog.findViewById(R.id.viewQuotes);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Working...", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 1
Reputation: 85
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.warning);
builder.setTitle("Warning");
final LinearLayout backAlarmDialogLayout = (LinearLayout) getLayoutInflater()
.inflate(R.layout.custumedialog_layou, null);
builder.setView(backAlarmDialogLayout);
builder.setCancelable(true);
backAlarmDialog = builder.create();
Set these codes to initialize your costumed dialog in onCreate like this, and if you would like to add listener to your button, you can write another method like
public void onClickCancel(View view){
backAlarmDialog.dismiss();
}
then you use
android:onClick="onClickCancel"
in the button in the XML.files you have costumed the dialog, I have tried other ways but they just would not work and I don not know why is this.Maybe someone would solve mine. ||= =
Upvotes: 0
Reputation: 701
dialog_bring_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final Dialog dialog = new Dialog(MyActivity.this);
dialog.setContentView(R.layout.MyCustomDialogxmlfile);
dialog.show();
/* My Cancel Button , and its listener */
my_cancel_btn=(Button)dialog.findViewById(R.id.datesetbtn);
my_cancel_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
Upvotes: 11
Reputation: 11508
dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button);
dialog_btn.setOnClickListener(new View.OnClickListener()
{
// Perform button logic
}
Upvotes: 1
Reputation: 6663
I find that the code is clearer to seperate my click handlers. Add this in the onCreate method:
signInbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do stuff for signInButtonClick
}
});
// same for other button
Upvotes: 4
Reputation: 1787
submit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
CustomDialog dialog1 = new CustomDialog(Classname.this);
dialog1.setContentView(R.layout.submitdialog);
dialog1.setTitle(" SUBMIT :");
TextView text = (TextView) dialog1.findViewById(R.id.message);
text.setText(" Your Answer is correct ");
}
}
Upvotes: 0
Reputation: 15089
There you go:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(id);
Button signInbutton=(Button)findViewById(R.id.signInButton);
signInButton.setOnClickListener(this);
Button closebutton=(Button)findViewById(R.id.closeButton);
closeButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(R.id.closeButton == v.getId()) {
closeButton(v);
} else {
// do the same for signInButton
}
}
Suggest you learn the basic beforehand.
Upvotes: 9