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();
Related
Yes, I know that similar questions were asked before but I tried many of those. So I have a problem with setting text in TextView in the inflated layout. I tried setContentView() then it works but then activity_main.xml with the menu isn't working. So I tried to inflate layout with TextView that I need. When I try to setText() it just doesn't displaying it. Here is my code:
public void bodAlg() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.layout1, null);
EditText editText = (EditText) vi.findViewById(R.id.bod_servers);
String[] serversArray = editText.getText().toString().split(", ");
TextView textView = (TextView) vi.findViewById(R.id.bod_serv_array);
textView.setText(Arrays.toString(serversArray));
}
And here is xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/rl_01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/ll_01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/bod_servers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cray CS-Storm, Vulcan – Blue Gene/Q, Blue Gene/Q, Stampede – PowerEdge C8220, Piz Daint – Cray XC30"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
android:textSize="13sp"/>
<TextView
android:id="#+id/bod_serv_array"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="15sp"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
android:paddingBottom="5dp"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Thank you for answer!
Thanks for comments! Problem is solved!
public void bodAlg() {
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.layout1, null);
EditText editText = (EditText) vi.findViewById(R.id.bod_servers);
String[] serversArray = editText.getText().toString().split(", ");
TextView textView = (TextView) vi.findViewById(R.id.bod_serv_array);
textView.setText(Arrays.toString(serversArray));
// solution
CoordinatorLayout mainL = (CoordinatorLayout) findViewById(R.id.main_view);
mainL.removeAllViews(); // remove previous view, add 2nd layout
mainL.addView(vi);
}
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 !
I build an alertDialog in this way:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title1"
android:textSize="20sp"
android:textColor="#color/white" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/chars1" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/RelativeLayout1"
android:background="#drawable/body_white">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Layout1"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/img1"
android:id="#+id/img1"
android:layout_alignParentTop="true"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img2"
android:background="#drawable/img2"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/img1"
android:layout_toEndOf="#+id/img1" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
I want to do this thing:
img1 = (ImageView) findViewById(R.id.img1);
But doing this I receive a NullPointerException.
I used this:
(EditText)((Dialog) dialog).findViewById(R.id.username);
in a early application where I take value after click.
In this case I don't have an object DialogInterface dialog. So I can't use it. How can I fill img1?
I want so set manually padding but I want to do this in JAVA and not in XML.
Thanks
Edit:
This is method that launch alertDialog:
public void createDialog() {
alertDialog = new AlertDialog.Builder(Instructions.this);
LayoutInflater inflater = this.getLayoutInflater();
alertDialog.setView(inflater.inflate(R.layout.alert_dialog_layout, null));
n1Dialog = (ImageView) findViewById(R.id.img1);
n1Dialog.setPadding(0,0,0,0);
alertDialog.show();
}
I have a simple button that when in onClick() method have
createDialog();
Row with setPadding (that is only a proof, it isn't what I want to do) was mistaken because is NullPointerException
do like this:
alertDialog = new AlertDialog.Builder(Instructions.this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView=inflater.inflate(R.layout.alert_dialog_layout, null);
alertDialog.setView(dialogView);
n1Dialog = (ImageView) dialogView.findViewById(R.id.img1);
n1Dialog.setPadding(0,0,0,0);
alertDialog.show();
This is how you should inflate a custom view for a AlertDialog:
Builder myCustomDialog = new AlertDialog.Builder(yourContext);
LayoutInflater factory = LayoutInflater.from(context);
final View yourInflatedView = factory.inflate(R.layout.your_xml_layout, null);
//Here you get your imageView (or TextView, EditText,...)
ImageView img1 = (ImageView) yourInflatedView.findViewById(R.id.img1);
// Do stuff with your imageview...
myCustomDialog.setView(yourInflatedView);
myCustomDialog.show();
You can create the dialog in the following way also:
public void displayPopup(Context ctx) {
Dialog cd_display = new Dialog(ctx);
cd_display.requestWindowFeature(Window.FEATURE_NO_TITLE);
cd_display.setContentView(R.layout.alert_dialog_layout);
ImageView n1Dialog = (ImageView) cd_display.findViewById(R.id.img1);
cd_display.show();
}
I'm trying to build an custom alertdialog in which there are 3 ImageViews .I want to hide and show these ImageViews dynamically.Here is my showroute() in HomeActivity class in which i'm creating the custom dialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeActivity.this);
alertDialog.setView(getLayoutInflater().inflate(R.layout.routedialog, null));
alertDialog.setTitle("Home Activity");
alertDialog.setIcon(R.drawable.logo);
alertDialog.setMessage(Html.fromHtml("<b>Route Details</b>"));
ImageView iv1,iv2,iv3;
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View convertView = (LinearLayout) inflater.inflate(R.layout.routedialog, null);
iv1=(ImageView) convertView.findViewById(R.id.iv1);
iv2=(ImageView) convertView.findViewById(R.id.iv2);
iv3=(ImageView) convertView.findViewById(R.id.iv3);
iv1.setVisibility(View.GONE);
iv2.setVisibility(View.GONE);
iv3.setVisibility(View.GONE);
convertView.findViewById(R.id.ll).invalidate();
and here is my routedialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<ImageView
android:id="#+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ivred" />
<ImageView
android:id="#+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ivblue" />
<ImageView
android:id="#+id/iv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ivgreen" />
</LinearLayout>
I want these ImageViews to be hidden when the dialog manager pops up and has to be made visible dynamically after checking certain conditions.Please Help.
You are inflating the layout again
LayoutInflater inflater = getLayoutInflater();
View convertView = (LinearLayout) inflater.inflate(R.layout.routedialog, null);
alertDialog.setView(convertView);
Now initialize views and set the visibility for the views. Also check whether you want GONE or INVISIBLE # http://developer.android.com/reference/android/view/View.html#attr_android:visibility depending on your need
Also i do not see anywhere iv4,iv5,iv6,iv7,iv8 in your custom dialog layout.
This
iv4=(ImageView) convertView.findViewById(R.id.iv4);
iv5=(ImageView) convertView.findViewById(R.id.iv5);
iv6=(ImageView) convertView.findViewById(R.id.iv6);
iv7=(ImageView) convertView.findViewById(R.id.iv7);
iv8=(ImageView) convertView.findViewById(R.id.iv8);
will give NullPointerException.
Edit :
AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.routedialog, null);
alertDialog.setView(convertView);
alertDialog.setTitle("Home Activity");
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setMessage(Html.fromHtml("<b>Route Details</b>"));
ImageView iv1,iv2,iv3;
iv1=(ImageView) convertView.findViewById(R.id.iv1);
iv2=(ImageView) convertView.findViewById(R.id.iv2);
iv3=(ImageView) convertView.findViewById(R.id.iv3);
iv1.setVisibility(View.GONE); // GONE oR INVISIBLE according to what you want
iv2.setVisibility(View.GONE);
iv3.setVisibility(View.GONE);
alertDialog.show();
Snap when imageview is visible
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");