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 !
Related
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 created a custom dialog but I got an unexpected result.
The final appearance of the dialog is:
As you can see, my custom dialog is shown with a kind of padding in both top and bottom of the dialog, and I don't code it to do this...
The code is the next:
#Override
protected Dialog onCreateDialog(int id){
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newAsig = inflater.inflate(R.layout.nuevaasignatura_act, null);
//Also, down here I tried to shape the dialog by setting a functional resource, but
//the stroke should be blue, not grey.
RelativeLayout re = (RelativeLayout)newAsig.findViewById(R.id.layoutCrearAsig1);
re.setBackgroundResource(R.drawable.borde);
Typeface tf = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
ImageView ico = (ImageView)newAsig.findViewById(R.id.ivicono);
ico.setBackgroundResource(R.drawable.colorprincipal);
LinearLayout ly = (LinearLayout)newAsig.findViewById(R.id.layoutCrearAsig2);
ly.setBackgroundColor(Color.rgb(214, 217, 224));
TextView txt = (TextView)newAsig.findViewById(R.id.textviewnuevaasig);
txt.setTypeface(tf);
TextView txt1 = (TextView)newAsig.findViewById(R.id.textViewCarpeta);
txt1.setTypeface(tf);
txt1.setBackgroundResource(R.drawable.colorprincipal);
Button btn = (Button)newAsig.findViewById(R.id.buttonCrearAsig);
btn.setTypeface(tf);
switch (id){
case 0:
return new AlertDialog.Builder(this)
.setView(newAsig).create();
}return null;
And the XML file...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layoutCrearAsig1">
<ImageView
android:id="#+id/ivicono"
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingLeft="4dp"
android:background="#color/DarkGray"
android:contentDescription="Icono ClassMarks"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src="#drawable/icono_cm" />
<TextView
android:id="#+id/textViewCarpeta"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_toRightOf="#id/ivicono"
android:background="#color/DarkGray"
android:clickable="true"
android:gravity="center"
android:text="Carpeta"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/layoutCrearAsig2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ivicono"
android:orientation="vertical">
<TextView
android:id="#+id/textviewnuevaasig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="Crear nueva asignatura"
android:textSize="20sp" />
<EditText
android:id="#+id/edittextAsig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp" >
</EditText>
<Button
android:id="#+id/buttonCrearAsig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="20sp"
android:text="Crear"/>
</LinearLayout>
</RelativeLayout>
I hope you can help me. Thanks!
The idea is AlertDialog always has 5dp top and bottom padding.
Try something like this:
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(newAsig, 0, 0, 0, 0);
Try this validation.
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
builder = new AlertDialog.Builder(getActivity());
builder.setInverseBackgroundForced(true);
} else {
builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
}
}
I am trying to add a linearlayout to a scrolview
this is my code
the code compiles, but it doesn't show me the new layout
this is the original layout (that i want to add to it)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_margin="15dp"
android:layout_marginTop="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="#+id/ViewHistoryImageLayout">
<ImageView
android:id="#+id/HistoryImage"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.76"
android:gravity="center"
android:padding="10dp"
android:src="#drawable/upload"
android:contentDescription="#string/HistoryImage"/>
<TextView
android:id="#+id/TranslatedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/translateImageButton" />
</LinearLayout>
and this is the layout that i want to add several times:
<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/TranslationMenuLayout" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numStars="5" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
and the java code for adding the new layout is:
setContentView(R.layout.activity_view_history_image);
ScrollView sv = new ScrollView(this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
sv.addView(ll);
the code compiles fine, and the app is running but nothing happens
is there a problem with one of the .xml files?
tnx
you should use an inflater,
change:
LinearLayout ll = new LinearLayout(R.layout.translation_menu);
with
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
sv.addView(ll);
The LayoutInflater creates a View object from the corresponding XML file.
LayoutInflater li = LayoutInflater.from(context);
LinearLayout ll = (LinearLayout) li.inflate(R.layout.translation_menu, this)
This is the Correct way.
Use a LayoutInflator.
LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
In other words, it takes as input an XML file and builds the View objects from it.
ScrollView sv = new ScrollView(this);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
LinearLayout ll = (LinearLayout) li.inflate(translation_menu, this);
sv.addView(ll);
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");
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();