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

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.

Related

Improper text appearance in Custom AlertDialog

In my code, I'm presenting a custom alert dialog by inflating a layout and set it as the dialog's view.
The problem I'm having is that the text looks OK in the Android Studio's design tool, but at run-time, the dialog becomes smaller and the text takes more lines.
I tried different approaches to fix it, but didn't acquire the wanted result.
For the layout, I'm using constraint layout and the text is "wrap content".
Here's my code:
//inflate alert layout
LayoutInflater inflater = LayoutInflater.from(this);
final View view = inflater.inflate(R.layout.connectivity_issue_counter, null);
//set builder
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setView(view);
dialog.setCancelable(false);
final AlertDialog alert = dialog.create();
//define dialog buttons and counter......//
alert.show();
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/alertBackground">
<TextView
android:id="#+id/connectivity_issue_title"
style="#style/customAlertTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="50dp"
android:text="#string/connectivity_issue_title"
app:layout_constraintEnd_toStartOf="#+id/connectivity_issue_wait"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/connectivity_issue_wait"
style="#style/customAlertTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="#string/connectivity_issue_wait"
app:layout_constraintEnd_toStartOf="#+id/connectivity_issue_counter_text"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/connectivity_issue_counter_text"
style="#style/customAlertTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/connectivity_issue_main_text"
style="#style/customAlertMainText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="#string/connectivity_issue_main_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/connectivity_issue_title" />
<Button
android:id="#+id/connectivity_isuue_button"
style="#style/customAlertButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="#string/stop"
app:layout_constraintEnd_toStartOf="#+id/guideline21"
app:layout_constraintStart_toStartOf="#+id/guideline22"
app:layout_constraintTop_toBottomOf="#+id/connectivity_issue_main_text" />
<android.support.constraint.Guideline
android:id="#+id/guideline20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.82" />
<android.support.constraint.Guideline
android:id="#+id/guideline21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
<android.support.constraint.Guideline
android:id="#+id/guideline22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
</android.support.constraint.ConstraintLayout>
*Another interesting thing - on a different tablet (both the same model) the dialog looks bigger and wider.
I'll appreciate any help.
As Davidaz suggested, I've changed the text size from sp to dp and it helped to solve the problem.
¿Maybe the text is bigger on that phone? I can't see the customAlertTitle style, but I suppose the text is on "SP", and these are "DP" but text-size dependent (if the phone is set to show the text on a big font from it's settings tab, it will increase it's size).
Maybe a solution could be to set the sizes to "DP", but you won't let the people change it, so it has counter effects.
Another idea that comes to my mind is that the size of the screen you are previewing on the preview is larger than your phone's screen.
The last thing I would try is to change the 'android.R.id.message' TextView's size by hand after creating the dialog. Once you show it you can access the message's textview by calling for it's id,
TextView messageTv = (TextView) alert.findViewById(android.R.id.message);
messageTv.setTextSize(X);
Edit: Don't mind that last idea, as you are inflating your own layout, maybe you can try to access your id and change it the same way, but with R.id.connectivity_issue_wait

Show the whole setTitle in AlertDialog.Builder

I am trying to display the whole sentence of the setTile in AlertDialog.Builder but I am just getting a part display. How can I manage that with AlertDialog.Builder?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Please help us to track the route, has this route arrived the stop? ");
I appreciate any help.
For long text (or the only text you're showing) use setMessage:
builder.setMessage("Please help us to track the route, has this route arrived the stop? ");
Use setTitle for something short and snappy in addition to a message or use no title at all.
More reading about dialogs:
http://www.google.com/design/spec/components/dialogs.html#dialogs-content
As what #Eugene H suggests, you can remove the title of AlertDialog and use a custom layout XML with a TextView that will serve as your Title.
To do that you need to remove the Window.FEATURE_NO_TITLE from the Dialog.
final View dialogView = View.inflate(this, R.layout.custom_dialog, null);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
TextView tvTitle = (TextView)dialogView.findViewById(R.id.tvTitle);
tvTitle.setText("Please help us to track the route, has this route arrived the stop?");
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="8dp"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:drawable/divider_horizontal_textfield">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the title"
android:id="#+id/tvTitle"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the Message"
android:id="#+id/tvMessage"/>
<Button
android:id="#+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:text="OK"
android:layout_height="0dp" />
</LinearLayout>

How to make search widget larger in actionbar?

XML code here
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">
<EditText android:id="#+id/find_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:contentDescription="#string/find_text"
android:background="#drawable/url_bar_entry"
android:singleLine="true"
android:textColor="#000000"
android:textCursorDrawable="#null"
android:inputType="text"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:textColorHighlight="#color/url_bar_text_highlight"
android:imeOptions="actionSearch"
android:selectAllOnFocus="true"
android:gravity="center_vertical|left"/>
<ImageButton android:id="#+id/find_prev"
style="#style/FindBar.ImageButton"
android:contentDescription="#string/find_prev"
android:src="#drawable/find_prev"/>
<ImageButton android:id="#+id/find_next"
style="#style/FindBar.ImageButton"
android:contentDescription="#string/find_next"
android:src="#drawable/find_next"/>
<ImageButton android:id="#+id/find_close"
style="#style/FindBar.ImageButton"
android:contentDescription="#string/find_close"
android:src="#drawable/find_close"/>
</RelativeLayout>
I am inflating this menu by using this:
LayoutInflater inflater = LayoutInflater.from(mContext);
View content = inflater.inflate(R.layout.find_in_page_content, null);
EditText tv = (EditText) content.findViewById(R.id.find_text);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
tv.setLayoutParams(params);
//CODE TO ADD TO only EditText view to menu object I have
The outcome of this is seen here: http://imgur.com/sQZ7Lbh
My question is:, how can I make the url bar take up as much space as availble in the actiobar rather than be restricted to a tiny square. I also want the cursor to show up when pressed but it doesn't. However, I think that this is fixed when I make the url bar longer.
Set this gravity attribute in XML in your RelativeLayout
android:layout_gravity="fill_horizontal"
Taken from https://stackoverflow.com/a/12884297/2158970

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

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.

Using LayoutInflator to create a dialog that takes up whole screen

I am designing an "about" menu for an app and have used the following code in an options menu to generate it:
case DIALOG_ABOUT:
builder = new AlertDialog.Builder(this);
Context context = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.about_dialog, null);
builder.setView(layout);
TextView title = new TextView(this);
title.setText(R.string.about_title);
title.setGravity(Gravity.CENTER);
title.setTextSize(20);
builder.setCustomTitle(title);
builder.setPositiveButton("OK", null);
dialog = builder.create();
break;
I have created two different views for the about menu - for both horizontal and vertical viewings.
When displaying vertically, the about menu looks fine, but when displaying horizontally the bottom part of the text is being cut off. I am using the LayoutInflator and View controls without having that much knowledge about how they work, so I assume they are currently filling to some Android-specified size.
How can I create the dialog to take up the whole screen, or at least 90% of it?
Edit - My layouts looked like this:
Vertical Layout:
<ImageView
android:id="#+id/imageView1"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/game_name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/game_description"
android:textAppearance="?android:attr/textAppearanceMedium" />
Horizontal Layout:
<ImageView
android:id="#+id/imageView1"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/game_name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="#string/game_description"
android:textAppearance="?android:attr/textAppearanceMedium" />
You may call the following on your dialog to fill entire space.
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

Categories

Resources