I am launching a dialog box using this code:
//show login dialog
final Dialog loginDialog = new Dialog(this);
loginDialog.setTitle(getString(R.string.Login));
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = li.inflate(R.layout.logindialog, null);
loginDialog.setContentView(dialogView);
loginDialog.setCancelable(false);
loginDialog.show();
Button cmdLogin = (Button)findViewById(R.id.cmdLogin);
Button cmdSignup= (Button)findViewById(R.id.cmdSignup);
if(cmdLogin==null)Log.d("Null Check","cmdLogin");
if(cmdSignup==null)Log.d("Null Check","cmdSignup");
The XML file for R.layout.logindialog 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/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="#string/EmailAddress"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
/>
<EditText
android:id="#+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="#string/Password"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
/>
<Switch
android:id="#+id/switchRemember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/RememberMe"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
/>
<LinearLayout
android:id="#+id/ButtonBoxes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/cmdSignup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Signup" />
<Button
android:id="#+id/cmdLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Login" />
</LinearLayout>
</LinearLayout>
Notice the two null checks on the Java code in first snippet, they are returning null. I cant findout a reason why :|
You should find as follow
Button cmdLogin = (Button)dialogView.findViewById(R.id.cmdLogin);
Button cmdSignup= (Button)dialogView.findViewById(R.id.cmdSignup);
Right now you're searching in the activity layout and not in the dialog layout, your code to find the buttons id should be like this:
Button cmdLogin = (Button)loginDialog.findViewById(R.id.cmdLogin);
Button cmdSignup= (Button)loginDialog.findViewById(R.id.cmdSignup);
Use View dialogView for get Buttons..
Button cmdLogin = (Button)dialogView.findViewById(R.id.cmdLogin);
Button cmdSignup= (Button)dialogView.findViewById(R.id.cmdSignup);
You have to get view from dialog view.
Button cmdLogin = (Button) dialogView.findViewById(R.id.cmdLogin);
Button cmdSignup= (Button) dialogView.findViewById(R.id.cmdSignup);
The findViewById without specified view will fetch child views from main activity's view. So that only you got returned null.
Try this:
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = li.inflate(R.layout.logindialog, null);
loginDialog.setContentView(dialogView);
loginDialog.setCancelable(false);
loginDialog.show();
Button cmdLogin = (Button)loginDialog.findViewById(R.id.cmdLogin);
Button cmdSignup= (Button)loginDialog.findViewById(R.id.cmdSignup);
if(cmdLogin==null)Log.d("Null Check","cmdLogin");
if(cmdSignup==null)Log.d("Null Check","cmdSignup");
Related
I have set a custom layout resourse file as View for Alert Dialog but im unalbe to fetch/ get values from views in the layout. Can someone point out what im doing wrong here? or is there another way to do achieve this?
some_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText android:inputType="textAutoComplete"
android:textSize="20sp"
android:id="#+id/title"
android:scrollHorizontally="true"
android:layout_height="70dp"
android:hint="Title / Username"
android:layout_width="250dp"
android:layout_gravity="center"
android:autofillHints="username" />
<EditText android:inputType="textVisiblePassword"
android:textSize="20sp"
android:id="#+id/password"
android:scrollHorizontally="true"
android:layout_height="70dp"
android:hint="Password"
android:layout_width="250dp"
android:layout_gravity="center"
android:autofillHints="password" />
</LinearLayout>
Code :
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Credentials");
alertDialog.setView(R.layout.some_layout);
EditText title = (TextView) some_layout.findViewById(R.id.title);
Error : ( 'some_layout' in EditText definition is highlighted in red and it has this message)
Cannot resolve symbol 'password_item_dialog'
I solved it myself
LayoutInflater layoutInflater = LayoutInflater.from(context);
final View alertView = layoutInflater.inflate(R.layout.some_layout, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setView(alertView);
EditText title = (EditText) alertView.findViewById(R.id.title);
When I click on the edittext it focuses, but I'm not able to type anything it. I have tried copying the XML edittext code into another activity and it worked fine, which suggests it's something to do with the java; but I have no idea what.
Code:
ap_overview_add.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"
android:id="#+id/ap_overview_add_LL"
android:background="#drawable/ap_button_background"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add amount"
android:textSize="30sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="15dp">
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/ap_et_add"
android:background="#drawable/edit_text_background"
android:inputType="numberDecimal"
android:padding="10dp"
android:drawableLeft="#drawable/pound_24dp"
android:backgroundTint="#color/abc_secondary_text_material_light"
android:imeOptions="actionSend"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:layout_weight=".75"/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="#+id/ap_button_add"
android:text="Add"
/>
</LinearLayout>
</LinearLayout>
And I inflate this within a BaseAdapter, in the getView method like so:
public Integer[] mButtonIds = {
R.id.ap_overview_tab_LL,
R.id.ap_overview_add_LL,
R.id.ap_overview_rem_LL
};
public Integer[] mLayoutIds = {
R.layout.ap_overview_tab,
R.layout.ap_overview_add,
R.layout.ap_overview_rem
};
LinearLayout b;
View rootView;
LayoutInflater inflater;
inflater = LayoutInflater.from(mContext);
rootView = inflater.inflate(mLayoutIds[index], null);
b = (LinearLayout) rootView.findViewById(mButtonIds[index]);
return b;
And in the main activity class I have:
GalleryImageAdapter galleryImageAdapter;
galleryImageAdapter = new GalleryImageAdapter(this);
// Initializing the custom gallery //
gallery.setAdapter(galleryImageAdapter);
I wrote an application which have to call dialog to get name. I need the custom view in dialog so I used .setContentView(). But in android with api less 20 I have problems with displaying it.
This is calling dialog:
public void setName(View v) {
nameDialog = new Dialog(this);
//using our dialog
nameDialog.setContentView(R.layout.new_name);
EditText text = (EditText) nameDialog.findViewById(R.id.form2_dialog_name);
//if we have previous name we show it
if (haveName) {
text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
}
//request focus and call keyboard for input name
text.requestFocus();
text.selectAll();
nameDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
nameDialog.show();
}
the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark"
tools:context=".CreateNoteActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/form2_dialog_name_of_event"
android:id="#+id/textView11" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/form2_dialog_name"
android:maxLines="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close_button"
android:id="#+id/button8"
android:onClick="onCancelDialog"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/okay_button"
android:id="#+id/okay_form2_dialog_button"
android:onClick="onFinishDialog" />
</LinearLayout>
</LinearLayout>
How it looks when api upper then 20:
http://i.imgur.com/pJ1oZky.png
Less then 20:
http://i.imgur.com/TJGEwXh.png
OK the answer is quite simple: Use the android.support.v7.app.AlertDialog
Your setName will have to look like this then:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.new_name, null);
builder.setView(dialogView);
EditText text = (EditText) dialogView.findViewById(R.id.form2_dialog_name);
//if we have previous name we show it
if (haveName) {
text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
}
//request focus and call keyboard for input name
text.requestFocus();
text.selectAll();
AlertDialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
I guess you will have to play a little with your layout file because the buttons are quite a bit too far away but I think this will be just a little problem ;)
I'm trying to display a custom Dialog. I replicated a class which perfectly works and used it for this dialog but it don't display anything but a little box in the middle of the screen.
I can't figure out what's wrong....
Dialog onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
final View v = inflater.inflate(R.layout.fleet_select_dialog, null);
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
int width = (display.getWidth() );
int height = (display.getHeight() );
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,(width/3)*2 );
}
xml layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/fleet_select_dialog">
<EditText
android:drawableLeft="#android:drawable/ic_menu_search"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/edit_fence2"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="15dip"
android:textColor="#color/themeapp"
android:background="#drawable/layout_corner_white"
android:singleLine="true"
android:inputType="textCapWords"/>
<ExpandableListView
android:layout_below="#+id/edit_fence2"
android:layout_marginTop="5dip"
android:id="#+id/lvExp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#color/themeapp"
android:dividerHeight="1dp"
android:layout_marginBottom="30dip"
android:layout_above="#+id/footerview" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dip"
android:id="#+id/footerview"
android:layout_alignParentBottom="true">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirm"
android:paddingBottom="10dip"
android:id="#+id/button_view"
android:textColor="#color/themeapp"
android:textSize="24dip"
android:background="#ffffffff"
android:paddingLeft="20dip"
android:paddingRight="8dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:id="#+id/waiting"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="Loading..."
android:id="#+id/textView16" />
</RelativeLayout>
</RelativeLayout>
the dialog is used like this:
fleetSelectDialog = new FleetSelectDialog(context);
fleetSelectDialog.show();
what am I doing wrong??
Try setting the windowLayout of the dialog before you set the content view with the inflater.
You can create your view directly from the Layout Inflater, you only need to use the name of your layout XML file and the ID of the layout in file.
Use the following code to set the layout of your AlertDialog
LayoutInflater inflater = getLayoutInflater(); View dialoglayout = inflater.inflate(R.layout. fleet_select_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();
I accidently deleted one important line:
this.setContentView(v);
final Dialog dialog = new Dialog(mContext);
// hide to default title for Dialog
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// inflate the layout dialog_layout.xml and set it
// as contentView
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.customize_dialog, null, false);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(view);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.show();
Enjoy full code.
Cheers !
Anyone know why my button isn't showing up in the dialog window?
Dialog d = new Dialog(AddContact.this);
LayoutInflater li = (LayoutInflater) getSystemService(Service.LAYOUT_INFLATER_SERVICE);
ViewGroup contentView = (ViewGroup) li.inflate(R.layout.dialog,null);
d.setContentView(contentView);
d.setTitle("Please correct these errors:");
TextView error = (TextView) contentView.findViewById(R.id.textView1);
Button closer = (Button) contentView.findViewById(R.id.button1);
closer.setText("Close");
error.setText(errorMessage);
d.show();
This my dialog.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:padding="5dp">
<TextView android:text="TextView" android:id="#+id/textView1"
android:layout_height="fill_parent" android:layout_width="fill_parent" />
<Button android:text="Button" android:id="#+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
What do I need to do so my button shows in the dialog window?
Your dialog is not visible when you use findViewById(), which means that the view with that id is not yet in your view hierachy and can not be found. This results in error beeing null, which will throw a NullPointerException in the following line.
You can solve this by inflating the layout seperately and use findViewById() on the inflated view.
Dialog d = new Dialog(AddContact.this);
LayoutInflater li = (LayoutInflater) getSystemService(Service.LAYOUT_INFLATER_SERVICE);
ViewGroup contentView = (ViewGroup) li.inflate(R.layout.dialog, null);
d.setContentView(contentView);
d.setTitle("Please correct these errors:");
error = (TextView) contentView.findViewById(R.id.textView1);
error.setText(errorMessage);
d.show();