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.
Related
I have a dialog with custom layout, everything is working fine, except there is a big empty spaces at the top which i cannot remove.
Any suggestion how i can remove that empty space.
Here is my Code:
final Dialog dialog = new Dialog(ConferenceDetailsActivity.this);
dialog.setContentView(R.layout.l_custom_dialog_name);
final CustomEditText nameText = (CustomEditText) dialog.findViewById(R.id.confNameFld);
nameText.setText(mConferenceDetails.getConferenceName());
Button button = (Button) dialog.findViewById(R.id.dialogButtonOK);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final CustomEditText nameText = (CustomEditText) dialog.findViewById(R.id.confNameFld);
String text = nameText.getText().toString();
dialog.dismiss();
}
});
dialog.show();
My xml Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/nameBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/name_badge"
android:layout_alignParentTop="true" />
<com.invosys.iconference.CustomControls.CustomEditText
android:id="#+id/confNameFld"
android:layout_height="45dp"
android:layout_width="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginBottom="20dp"
android:layout_alignTop="#+id/nameBackground"
android:layout_alignBottom="#+id/nameBackground"
android:layout_alignLeft="#+id/nameBackground"
android:layout_alignRight="#+id/nameBackground"
android:background="#null"
android:ems="10"
android:hint="#string/namePlaceholder"
android:gravity="center"
android:isScrollContainer="false"
android:inputType="textFilter|textNoSuggestions"
android:textColor="#color/ECharcoalColor" />
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_below="#+id/nameBackground"
android:layout_alignLeft="#+id/nameBackground"
android:layout_alignRight="#+id/nameBackground" />
..........................
After i used this piece of code the size is reduced but it is still not unto the task;
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //before setContentView
dialog.setContentView(R.layout.logindialog);
You can try this:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //before setContentView
dialog.setContentView(R.layout.logindialog);
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();
}
});
My code is as follows:
activity_for_me.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/recco_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
<ImageView
android:id="#+id/checkin"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:onClick="ClickCheckIn"
android:src="#drawable/checkin" />
</RelativeLayout>
recco_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#layout/nine_patch" >
<ImageView
android:id="#+id/outlet_icon"
android:layout_marginLeft="15dp"
android:layout_height="55dp"
android:layout_width="55dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="15dp"
android:hint="Distance"
/>
<TextView
android:id="#+id/outlet_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_marginLeft = "15dip"
android:hint="outlet"
/>
<TextView
android:id="#+id/reward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/outlet_name"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_marginLeft = "15dip"
android:layout_marginTop="5dp"
android:textColor="#color/abc_search_url_text_holo"
android:hint="Reward"
/>
<View
android:id="#+id/rule"
android:layout_below="#id/reward"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_width="fill_parent"
android:layout_marginLeft = "15dip"
android:layout_height="1dp"
android:background="#c0c0c0"/>
<ImageView
android:id="#+id/favourite_icon"
android:layout_below="#id/rule"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_height="15dp"
android:layout_width="15dp"
android:src="#drawable/heart_empty"/>
<ImageView
android:id="#+id/loc_icon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/rule"
android:layout_marginLeft = "15dip"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/outlet_icon"
android:src="#drawable/map_pointer"/>
<TextView
android:id="#+id/locality"
android:layout_toRightOf="#id/loc_icon"
android:layout_toLeftOf="#id/distance"
android:layout_below="#id/rule"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft = "5dip"
android:layout_marginTop="5dp"
android:hint="Locality"
/>
</RelativeLayout>
Activity.java
public class ForMeActivity extends Fragment {
#SuppressLint("ClickableViewAccessibility")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout rootView = (RelativeLayout) inflater.inflate(
R.layout.activity_for_me, container, false);
ImageView favourite_heart = (ImageView) rootView
.findViewById(R.id.favourite_icon);
favourite_heart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(YourActivityName.this,
// "The favorite list would appear on clicking this icon",
// Toast.LENGTH_LONG).show();
System.out.println("Favourite Icon clicked");
}
});
list = (ListView) rootView.findViewById(R.id.recco_list);
ImageView check_in_img = (ImageView) rootView
.findViewById(R.id.checkin);
ImageView outlet_logo = (ImageView) rootView
.findViewById(R.id.outlet_icon);
final String data = getArguments().getString("data");
ForMeFile = new File(getActivity().getExternalFilesDir(filepath),
filename);
final String cookie = getArguments().getString("cookie");
System.out.print(cookie);
check_in_img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getActivity(), CheckInView.class);
startActivity(in);
System.out.println("Checkin Icon clicked");
}
});
}
I want to change image for favourite_icon ImageView when I click on it. But as soon as I click on the icon I get NullPointerException. What is the reason that click of check_in_img is working but click of favourite_icon is not working even though the current layout is the one which contains favourite_icon? Other sources of SO says to check the same.
favourite_icon is a part of recco_list.xml and you are trying to find it in activity_for_me.xml. The view does not exist there and hence, the exception.
R.id.favourite_icon is not there in activity_for_me.xml.
And if you want to access the items in the listview, I suggest you read about ListView and getView().
final AlertDialog builder = new AlertDialog.Builder(activity).create();
LayoutInflater factory = LayoutInflater.from(activity);
View view = factory.inflate(R.layout.inventory_item_pop_up, null);
builder.setView(view);
ImageButton discardButton = (ImageButton) view.findViewById(R.id.popup_discard);
discardButton.setClickable(true);
discardButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
listener.remove(f);
builder.dismiss();
}
});
This button click does not respond at all. How can I make this piece of code work?
I use this code in an Adapter, by the way.
the onCLickListener you have registered, is it DialogInterface.OnClickListener? If you have used onCLickListener from View class then that won't work here. Please recheck.
You need to do
discardButton.setOnClickListener(new DialogInterface.OnClickListener() {
#Override
public void onClick(View v) {
listener.remove(f);
builder.dismiss();
}
});
I hope this will helpfull for you. I implement this way for custom Dialog
public void showUpdateDialog() {
updateDialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen) {
#Override
public void onBackPressed() {
this.dismiss();
loadDataAsync.execute();
}
};
updateDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
updateDialog.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
updateDialog.setContentView(R.layout.updatepopup);
logger.info("Network error popup on");
updateDialog.setCancelable(true);
ImageView dialogImage = (ImageView) updateDialog
.findViewById(R.id.dialogheaderimageupdateupdate);
dialogImage.setBackgroundResource(R.drawable.infoimage);
TextView dialogMesage = (TextView) updateDialog
.findViewById(R.id.dialogmessgetextupdate);
TextView currVersion = (TextView) updateDialog
.findViewById(R.id.currentversionupdate);
TextView newVersion = (TextView) updateDialog
.findViewById(R.id.newversionupdate);
TextView dialogHeader = (TextView) updateDialog
.findViewById(R.id.dialogheadertextupdate);
newVersion.setText("Latest version: " + versionCode);
currVersion.setText("Current version : " + currentVersionName);
dialogMesage.setText("Would you like to update now?");
dialogHeader.setText("A program update is available! ");
Button dialogOk = (Button) updateDialog
.findViewById(R.id.dialogokbuttonupdate);
dialogOk.setText("Update");
dialogOk.setFocusable(true);
dialogOk.requestFocus();
dialogOk.setFocusableInTouchMode(true);
dialogOk.requestFocus();
Button dialogCancel = (Button) updateDialog
.findViewById(R.id.dialogcancelbuttonupdate);
dialogOk.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
updateDialog.dismiss();
downLoad = new DownLoad();
downLoad.execute(apkUrl.trim());
}
});
dialogCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
updateDialog.dismiss();
loadDataAsync.execute();
}
});
try {
updateDialog.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And my updatepopup.xml is displayed below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/layoutsample"
style="#android:style/Theme.Black.NoTitleBar.Fullscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dialoghdrbg"
android:orientation="horizontal" >
<ImageView
android:id="#+id/dialogheaderimageupdateupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/dialogheadertextupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="1dp"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dialogcontentbg"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/currentversionupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#ffffff"
android:textSize="23dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/newversionupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#ffffff"
android:textSize="23dp" />
<TextView
android:id="#+id/dialogmessgetextupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#ffffff"
android:textSize="23dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:layout_marginTop="15dp"
android:gravity="center|center_horizontal"
android:orientation="horizontal" android:layout_marginBottom="10dp">
<Button
android:id="#+id/dialogokbuttonupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:background="#drawable/buttonanimation"
android:text="#string/OKButton"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="#+id/dialogcancelbuttonupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="#drawable/buttonanimation"
android:text="#string/CancelButton"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Well, this is awkward... The code is correct, I just wasn't calling the right createDialog() method.
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);