android.view.InflateException - Error inflating class android.widget.EditText - android

Hello I'm trying to show a custom AlertDialog on the screen.
I'm inflating an layout into the dialog, this layout includes Two TextViews, one RatingBar and one EditText.
When I try to popup the dialog I'm getting this exception :
E/AndroidRuntime(12989):
android.view.InflateException: Binary XML file line #25: Error
inflating class android.widget.EditText
Important point is this error occurs on Android 2.3.x Devices, It works great on Android 4.x
dialog_comment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RatingBar
android:id="#+id/DialogCommentRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:stepSize="1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/DialogCommentRatingBar"
android:text="Yorumunuz"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/DialogCommentComment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:background="?android:attr/editTextBackground"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine" >
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Oyunuz"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
CommentDialog.java
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater=getActivity().getLayoutInflater();
final View view=inflater.inflate(R.layout.dialog_comment, null);
final RatingBar rating=(RatingBar)view.findViewById(R.id.DialogCommentRatingBar);
final EditText comment=(EditText)view.findViewById(R.id.DialogCommentComment);
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setTitle("Enter Your Comment!");
return builder.create();
}
How do I show AlertDialog
FragmentManager fm = getSupportFragmentManager();
CommentDialog commentDialog = new CommentDialog();
commentDialog.show(fm, "fragment_comment_dialog");

Replace
android:textAppearance="?android:attr/textAppearanceLarge"
with
android:textAppearance="#android:style/TextAppearance.Large"

Deleted my EditView (Plain Text text field), dropped another one into content_....xml and did not change any styling this time. Everything ran correctly with the default styling on the EditView.

Related

Manually inflated TextView's resource string not displaying new lines (\n)

I'm inflating a TextView which references a resource string that contains newline characters (tried \n, \r\n, \n\n) that aren't displaying (get a long, truncated single line of text).
The code is as follows:
Note:
Currently the app is targeting an old version of Android (API 15/4.0.3, upgrade soon) and I just need to get a few bugs fixed for the time being. I have a 4.4 tablet I can try this on to see if it alleviates the issue.
Calling Function
public void ShowExitPromptDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
OkDialogClickListener okListener = new OkDialogClickListener();
LayoutInflater li = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
LinearLayout tv1 = (LinearLayout)li.Inflate(Resource.Layout.dialog_no_connectivity, null);
builder.SetView(tv1)
.SetPositiveButton("OK", okListener)
.SetCancelable(true);
AlertDialog dialog = builder.Create();
dialog.Show();
}
dialog_no_connectivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="5dp"
android:paddingBottom="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/dialog_noconnectivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginLeft="8dp"
android:ellipsize="end"
android:singleLine="true"
android:textStyle="bold"
android:text="#string/no_webservice_connectivity"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
Resources/Values/Strings.xml
<string name="no_webservice_connectivity">1\n2\r\n3\n\nand to the 4
snoop doggy dog and super mario is at the door with your new switch beeotch!</string>
Simply remove
android:singleLine="true"
such that it looks like
<TextView android:id="#+id/dialog_noconnectivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginLeft="8dp"
android:textStyle="bold"
android:text="#string/no_webservice_connectivity"
android:textAppearance="?android:attr/textAppearanceSmall"/>
Change android:singleLine="true" to android:singleLine="false" and add android:maxLines="6" (or more) in your TextView XML definition.

How to change text size of dialog preference message?

I have a class that extends EditTextPreference and I use it in my own layout, such as:
<com.app.MyEditTextPreference
android:key="#string/key"
android:title="#string/title"
android:summary="#string/summary"
android:dialogTitle="#string/title"
android:dialogMessage="#string/message"
android:layout="#layout/preference_edittext"/>
My app theme inherits from Theme.AppCompat.Light.DarkActionBar and I have changed the values of the text sizes:
<style name="TextAppearance.Small">
<item name="android:textSize">18sp</item> <!-- was 14sp -->
</style>
However, when the preference dialog is displayed, the size of the message is different than the one I have defined in my style.
So how do I set the text size of the dialog message correctly?
EDIT
I copied the dialog layout from the sdk:
<LinearLayout
android:id="#+android:id/edittext_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:id="#android:id/message"
android:layout_marginBottom="48dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:textColor="?android:attr/textColorSecondary"
style="#style/TextAppearance.Small" />
</LinearLayout>
However, I get a build error Resource is not public on the line android:id="#+android:id/edittext_container".
And if I change it to "#*android:id/edittext_container", the edit text is not visible anymore.
Is there a simpler way to just change the textsize of the message?
First create new xml file name cus.xml in layout folder.
it need for set the view for dialog at runtime .
<?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">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Name :"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18dp" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:textSize="18dp" />
As you need dialog:
here's dialog code:
Dialog d=new Dialog(getActivity());
d.setTitle("Image Title");
d.setContentView(R.layout.cus);
d.show();
I finally managed to change the message text size by:
a) Changing the id of the LinearLayout to: #+id/edittext_container, and
b) Adding the following code in my class extending EditTextPreference:
#Override
protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
ViewGroup container = (ViewGroup) dialogView
.findViewById(R.id.edittext_container);
if (container != null) {
container.addView(editText, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
This works fine and is clearly a valid answer, however a simpler solution would be to just change the message text size through themeing.

How to removeDialog(int dialogID) Android?

I have extended my class with a dialog in which I have set content view and other button actionlisteners etc... what is happening is that when I create my dialog it shows background properly.
but opening it again and again is crating problems with content view, things are added haphazardly and most of the UI elements are repeating and background image is disappeared.
I know that dialog is created once and is used as a cache, I don't want that I know there is a method in Android Activity removeDialog(int dialogID) but I don't know who to use this, I don't give any id to my dialog I don't know how to give id to dialog.
#dialog code
public class OptionsDailog extends Dialog implements OnClickListener {
public OptionsDailog(Activity pContext) {
super(pContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
mContext = pContext;
setContentView(R.layout.option_menu);
setBasicContents();
}
#options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/rl_root_option_menu">
<RelativeLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#drawable/bg_episode" android:id="#+id/rl_option_screen">
<LinearLayout android:id="#+id/ll_options" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true">
<Button android:text="Achievements" android:textSize="18sp" android:id="#+id/btn_achievements" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:text="Tutorial" android:textSize="18sp" android:id="#+id/btn_tutorial" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:text="Leaderboard" android:textSize="18sp" android:id="#+id/btn_leaderboard" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:id="#+id/btn_music" android:text="Music" android:textSize="18sp" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:id="#+id/btn_sound" android:textSize="18sp" android:text="Sound" android:layout_marginBottom="10dip" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
</LinearLayout>
</RelativeLayout>
<ImageButton android:background="#drawable/btn_back" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/ib_back_options_menu"></ImageButton>
</RelativeLayout>
#Dialog showing code
final OptionsDailog mDailog = new OptionsDailog(JungleCrashLand.this);
mDailog.show();
The Dialog class works with its own set of identifiers. If you create a dialog with createDialog(int x) you can pass this x as your own identifier. removing the dialog follows the same structure: removeDialog(int x) where x is, again, the identifier of your dialog type. It is done this way to be able to distinguish between different "types" of dialogs, defined by your own, without the need to extend the dialog class.
so basically:
static final int OPTIONS_DIALOG = 0;
[...]
createDialog(OPTIONS_DIALOG);
[...]
removeDialog(OPTIONS_DIALOG);
as you can have only one dialog per activity, this will remove only this dialog and every(!) reference to it. Read http://developer.android.com/guide/topics/ui/dialogs.html to fully understand what is going on here.

Android 3.1 Dialog not the right size

I am creating a custom dialog in my app and it looks fine in the Layout Editor, but is not the right size on the device. Here's the layout for the dialog:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/dialog_border"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:src="#drawable/wifi" />
<TextView
android:layout_height="wrap_content"
android:id="#+id/heading"
android:layout_below="#+id/image"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:layout_alignLeft="#+id/cancel_button"
android:layout_alignRight="#+id/continue_button"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/lost_connection"
android:layout_centerHorizontal="true" />
<TextView
android:layout_height="wrap_content"
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_below="#+id/heading"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerHorizontal="true"
android:layout_alignLeft="#+id/cancel_button"
android:layout_alignRight="#+id/continue_button"
android:gravity="center"
android:layout_margin="10dp"
android:text="#string/try_again" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/cancel_button"
android:layout_below="#+id/description"
android:layout_centerHorizontal="true"
android:id="#+id/dismiss_button"
android:text="#string/dismiss" />
</RelativeLayout>
This is what it looks like in the Layout Editor:
But this is what it looks like on the device:
It has that weird bit of extra space at the top and right side and the button is compress vertically.
That blank area is the dialog title. It can be turned off when creating the DialogFragment as:
DialogFragment df = new MyDialogFragment();
df.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
df.show(ft, "dialog");
Where the 0 in set style lets the platform choose an appropriate style and "dialog" is whatever tag you'd like to set for your dialog fragment.
I think that extra weird space at the top is the place for the dialog title. As i see on the screenshots you used a dialog and inflated a custom view into that dialog ?
I guess worth a try to hide the title of the dialog with yourDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

Wrong width/height inflating AlertDialog from XML

I'm having problems inflating an AlertDialog with a custom XML. The problem is that it doesn't respect the width/height I've specified.
volume.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="6dp" android:gravity="center"
android:layout_gravity="center_horizontal" android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="250dip"
android:id="#+id/layoutRoot">
<TextView android:layout_width="wrap_content"
android:layout_gravity="left" android:id="#+id/textView1"
android:layout_height="wrap_content" android:text="Volume:"
android:layout_marginBottom="10dp" android:textSize="22dp"
android:layout_marginTop="10dp"></TextView>
<SeekBar android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="#+id/volumeBar"
android:minHeight="10dp" android:maxHeight="15dp" android:paddingLeft="10dp"></SeekBar>
<TextView android:layout_width="wrap_content"
android:layout_gravity="left" android:layout_height="wrap_content"
android:layout_marginBottom="10dp" android:textSize="22dp"
android:text="20%" android:id="#+id/volumeText"></TextView>
</LinearLayout>
Code:
Builder alertbox = new AlertDialog.Builder(this);
AlertDialog alertDialog;
LayoutInflater mInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View VolumeView = mInflater.inflate(
R.layout.volume, (ViewGroup) findViewById(R.id.layoutRoot));
alertbox.setView(VolumeView);
alertDialog = alertbox.create();
alertDialog.show();
Your problem may be the layoutRoot you are providing as the second parameter of your call to mInflater.inflate(). This will affect the layout of the XML file being inflated.
Your question needs more information for anyone to give a better answer than this.

Categories

Resources