The layout which i am inflating into the alertbuilder is having two button. But i am not able to set the onClickListner for that. this exception is occurring. Please see my code.
XML of the custom alertDialogue.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_common"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/user_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:singleLine="true" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Search" />
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
AlertDialog.Builder alert = new AlertDialog.Builder(Myclass.this);
alert.setTitle("title");
alert.setIcon(iconImage);
LayoutInflater inflater = (LayoutInflater)MyClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.search_dialogue, null, false);
user_input= (EditText)view.findViewById(R.id.user_text);
Button cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
if(content.equals("")) {
user_input.setHint(hint);
} else {
user_input.setText(content);
}
alert.setView(view);
searchAlert = alert.create();
searchAlert.show();
You are missing view before findViewById, by excluding view you are refering to the activity..:
Button cancel = (Button) view.findViewById(R.id.cancel);
Button cancel = (Button) view.findViewById(R.id.cancel);
check here
user_input= (EditText)view.findViewById(R.id.user_text);//defined view for user_input
Button cancel = (Button) findViewById(R.id.cancel);//not defined view for button
change to
Button cancel = (Button)view. findViewById(R.id.cancel);
Related
How to get onclick of a view in custom dialog in an activity in android?
My XML is:
<?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:background="#color/default_green"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSetNotificationsHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Set Notifications On"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/rbFriendRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Friend Request"
android:textColor="#android:color/white" />
<RadioButton
android:id="#+id/rbMessages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Messages"
android:textColor="#android:color/white" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:onClick="saveSetNotificationsDialog"
android:text="Save" />
</LinearLayout>
My code is:
public void showSetNotificationsDialog(View v) {
dialog = new Dialog(Activity_Settings.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_setnotificationsdialog);
rbFriendRequest = (RadioButton) findViewById(R.id.rbFriendRequest);
rbMessages = (RadioButton) findViewById(R.id.rbMessages);
dialog.show();
}
I am able to show dialog view but no able to get on click of dialog save button in activity class. Error was no method found exception.
use instance of dialog for findViewById()
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
declineButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
use as following code
dialog = new Dialog(Activity_Settings.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = View.inflate(getActivity(), R.layout.dialog_setnotificationsdialog, null);
dialog.setContentView(view);
//always find your view view.findViewbyid
yourView= (RadioButton) view.findViewById(R.id.rbFriendRequest);
//then you can add on click listner to any of your view i.e.
yourView.setonClickListener(this);(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
I am new to android world and am working on dialog and in that I am trying to create a text view, button and imageview, but it is neither showing me error nor textview or imageview. It is showing a button. Here is the image:
.
But i am want to display my dialogue as follows
My xml file is as follows ....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity">
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/submit_dialog"
android:layout_alignParentRight="true"
android:layout_marginRight="37dp"
android:contentDescription="#string/add_Friend"
android:src="#drawable/add" />
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="64dp"
android:layout_toLeftOf="#+id/add"
android:text="#string/submit" />
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/add"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/add"
android:ems="10" />
</RelativeLayout>
I am calling it in java file as follows
dialog = new Dialog(context);
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.activity_add_friends_dialog, null, false);
dialog.setContentView(view);
dialog.setCancelable(true);
//dialog.setContentView(R.layout.activity_add_friends_dialog);
//dialog.setTitle("Add Friends");
add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);
submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
try {
friendsCount++;
Log.d("addFriendsActivity", "ashish comes here ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
submit_dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
startActivity(i);
}
});
dialog.show();
After some google I understand that the issues might be in r.java or you have imported android .r but i have not done any thing in this ..
Thanks in advance.... :)
<LinearLayout 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"
android:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
There is no error in your java code. Its perfect you have just change your xml. Use this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity" >
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:ems="10" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/writename"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/writename"
android:contentDescription="Add Friend"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/writename"
android:layout_below="#+id/add"
android:layout_marginRight="46dp"
android:text="Submit" />
</RelativeLayout>
For your xml I think it should look like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/add_btn" />
<ImageButton
android:id="#+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/add_btn"
android:background="#null" />
<Button
android:id="#+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/my_edit_text"
android:text="Submit" />
</RelativeLayout>
Be aware that there can be some typo mistakes, because I didn't compile that code, and you should change your real variables. And create your AlertDialog like this:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MyActivity.this);
View mView = mInflater.inflater(R.layout.my_layout, null, false);
EditText mEdit = (EditText) mView.findViewById(R.id.my_edit_text);
ImageButton mImageBtn = (ImageButton) mView.findViewById(R.id.add_btn);
Button mSubmit = (Button) mView.findViewById(R.id.submit_btn);
mBuilder.setView(mView);
AlertDialog mAlert = mBuilder.create();
mAlert.show();
Using AlertDialog.Builder instead of Dialog is lot more easier for your situation (at least in my opinion). And you can add your Submit Button to your AlertDialog as default positive button.
Hope this helps you.
Look at your code i think
add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);
as
add= (ImageView) view.findViewById(R.id.add);
friends1 = (EditText) view.findViewById(R.id.writename);
That should work.
Try this..
final Dialog dialog = new Dialog(AddFriendsActivity.this);
dialog.setContentView(R.layout.activity_add_friends_dialog);
dialog.setCanceledOnTouchOutside(true);
add= (ImageView) dialog.findViewById(R.id.add);
EditText friends1 = (EditText) dialog.findViewById(R.id.writename);
submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
add.setOnClickListener(new OnClickListener() {
//#Override
#SuppressWarnings({ "unchecked", "rawtypes" })
#SuppressLint("NewApi")
public void onClick(View v) {
friendsCount++;
//TextView textview = new TextView(context);
//textview.setId(friendsCount);
//final RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//params.addRule(RelativeLayout.BELOW, friendsCount--);
//textview.setLayoutParams(params);
//textview.setVisibility(View.VISIBLE);
//relativelayout.addView(textview ,params);
Log.d("addFriendsActivity", "ashish comes here ");
}
});
submit_dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
startActivity(i);
}
});
dialog.show();
Your view is covered.
May be you can try to set your parent layout to Linearlayout, not RelativeLayout.
I am looking for an easy popupmenu I could use for phones that operate on 2.3.3 and up.
Something like this, and works differently from a ContextMenu in that it doesn't require a listview:
Try the following :
Create a file dialog.xml in your layout folder and add the following code :
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/ErrorMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Do you want to un-install this app?" />
<LinearLayout
android:layout_below="#+id/ErrorMsgDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:id="#+id/Cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="Cancel" />
<Button android:id="#+id/Ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp"
android:text="OK" />
</LinearLayout>
and then in your class file use following code to display the dialog :
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(
R.layout.dialog, null);
builder.setView(view);
dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
Button cancel = (Button) view.findViewById(R.id.Cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
Button ok = (Button) view.findViewById(R.id.Ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do something here for OK action
}
});
dialog.show();
The easy way to do this is just create an AlertDialog.Builder object and configured its positive and negative buttons and then set the Title and the Messages and done. Removing the need for any layout files.
I have one AlertDialog which is working fine.I have set some background images to it with following code:
Button buttonPositive = (Button)dialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button buttonNegative = (Button)dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonPositive.setBackgroundResource(R.drawable.custom_button);
buttonPositive.setTextColor(Color.WHITE);
buttonNegative.setBackgroundResource(R.drawable.custom_button);
buttonNegative.setTextColor(Color.WHITE);
Now after setting image the two buttons are touching each other, i mean they have no space between them.I have tried with setPadding(...),it's not working.Even if i am changing the image size(i.e. width) it is not working.Any help !!!
I think its better to create layout xml file what you want ...
and set Like alertDialog.setContentview(R.layout.mylayout);
try this code
private Dialog myDialog;
myDialog = new Dialog(ShowReportActivity.this);
myDialog.setContentView(R.layout.alert);// your xml
myDialog.setTitle("Send Email");
myDialog.setCancelable(true);
Button set = (Button) myDialog .findViewById(R.id.alert_bnt_send_email);
Button exit = (Button) myDialog.findViewById(R.id.alert_bnt_exit);
set.setTextColor(Color.WHITE);
set.setBackgroundResource(R.drawable.custom_button);
getMailId = (EditText) myDialog.findViewById(R.id.alert_editT_email_Id);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
.........
myDialog.dismiss();
});
exit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myDialog.dismiss();
}
});
myDialog.show();
use custome dialog using code like given below
Dialog windialog = new Dialog(YourActivity.this);
windialog.setContentView(R.layout.win_dialog);
windialog.setTitle("Congratulation");
windialog.setCancelable(true);
final EditText et_emailverification=EditText)windialog.findViewById(R.id.et_emailveri);
et_emailverification.setText(UserEmailOrName);
Button submit=(Button)windialog.findViewById(R.id.bt_sub_que);
submit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//write here your code what you want onclick
}
});
Button cancel=(Button)windialog.findViewById(R.id.bt_sq_cancel);
cancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
windialog.cancel();
});
windialog.show();
and xml like win_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/et_emailveri"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
</LinearLayout>
<RelativeLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/bt_sub_que"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="#+id/bt_sq_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="Cancel" />
</RelativeLayout>
</LinearLayout>
i am using a layout file for showing a dialog. My xml file is
<?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">
<EditText android:id="#+id/dialogEditText" android:text="Enter Your Text Here" android:layout_height="wrap_content" android:layout_width="match_parent"></EditText>
<DatePicker android:id="#+id/datPicker" android:layout_height="wrap_content" android:layout_width="match_parent" ></DatePicker>
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:text="Add" android:id="#+id/dialogAddBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button>
<Button android:text="Cancel" android:id="#+id/dialogCancelBtn" android:layout_width="200px" android:layout_height="wrap_content" android:layout_centerHorizontal="true"></Button>
</LinearLayout>
</LinearLayout>
I want to do something when user clicks on "OK" or Cancel button . So i have created anonymous handlers for those button. My Coding is
LayoutInflater inflater = LayoutInflater.from(obj);
View inflatedView1= inflater.inflate(R.layout.dialog_view,null);
final Dialog d= new Dialog(obj);
final Window window= d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
window.setTitle("Add Item");
window.setContentView(R.layout.dialog_view);
final EditText input= (EditText) inflatedView1.findViewById(R.id.dialogEditText);
Button okBtn= (Button)inflatedView1.findViewById(R.id.dialogAddBtn);
okBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast msg= Toast.makeText(obj.getApplicationContext(),"Hello",Toast.LENGTH_LONG);
msg.show();
}
});
Button cancelBtn= (Button) inflatedView1.findViewById(R.id.dialogCancelBtn);
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
d.dismiss();
}
});
d.show();
When i am clicking on "Ok" or "Cancel button" nothing is happening. I am not able to understand why this is happening.
Please suggest me some solution.
you are setting the event handlers on your inflated view but at the same setting the content view to your plain layout:
window.setContentView(R.layout.dialog_view);
Just change this line to
window.setContentView(inflatedView1);
Or, perhaps a better approach without using the layout inflater would be to use
d.setContentView(R.layout.dialog_view);
And then setting the event handlers via
Button okBtn = (Button) d.findViewById(R.id.dialogAddBtn);
okBtn.setOnClickListener(...
Michael