How do I prevent the text from wrapping in a alert dialog? - java

I'm showing a very simple AlertDialog using this little snippet of code:
AlertDialog.Builder bldBuilder = new AlertDialog.Builder(WarningActivity.this);
bldBuilder.setMessage(strTrace).setTitle(strTrace.split("\n")[0]);
AlertDialog dlgTrace = bldBuilder.create();
dlgTrace.show();
The content of my dialog is a stack trace and therefore the lines can be quite long. I'd like the prevent the wrapping of lines in the default alert dialog but I can't seem to figure out how. I've been trying to dig up this answer but I haven't been able to understand how.

You have 2 possible options: First is to create your own layout. Second is to modify the default AlertDialog layouts' textview
//after creating the dialog, get the textview
TextView textView = (TextView) dlgTrace.findViewById(android.R.id.message);
//then set single line to false
textView.setSingleLine(false);

Related

How to access AlertDialog's Message and Title in Unity Android Plugin Library? (I am using custom Font in AlertDialog)

I want to use custom font in my android plugin library while using AlertDialog. Now here is the code to display AlertDialog using custom font.
AlertDialog alertDialog = new AlertDialog.Builder(mainActivity).setTitle("Title").setMessage("Message Text").create();
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", dialogOnClickListener);
TextView alertMessageTextView = (TextView) alertDialog.findViewById(R.id.message); // I am getting error here
TextView alertTitleTextView = (TextView) alertDialog.findViewById(android.R.id.title); // I am getting error here
Typeface customFont = Typeface.createFromAsset(mainActivity.getResources().getAssets(), "fonts/lobster-regular.ttf");
alertMessageTextView.setTypeface(customFont);
alertTitleTextView.setTypeface(customFont);
alertDialog.show();
I am getting error:
Error Unity AndroidJavaException: java.lang.NoSuchFieldError: No static field message of type I in class Lcom/cptech/pluginexample/R$id; or its superclasses (declaration of 'com.cptech.pluginexample.R$id' appears in base.apk)
I have placed my font file in Unity's "Assets/Resources" Folder. Hence in final apk the font resources appear in "assets/fonts" folder. But however, this doesn't matter as, I can use custom font in other TextView(s).
The only problem is that it cannot find AlertDialog's message and title, as I believe. Any help would be appreciated.
Few changes:
Use show() instead of create() when building the AlertDialog:
AlertDialog alertDialog = new AlertDialog.Builder(mainActivity).setTitle("Title").setMessage("Message Text").show();
Then, replace this line:
TextView messageTextView = (TextView) alertDialog.findViewById(R.id.message);
With android.R.id.message instead, like so:
TextView messageTextView = (TextView) alertDialog.findViewById(android.R.id.message);
And make sure you're not accidentally doing import com.android.R at the top of your file. You need this change because the id message is not an asset in your module, it's a default asset from an android library. So the R you're using right now is coming from your package - you need to specify R from android.R.
Last thing quick, watch out for the variable name of the textview when you set the text, should be changed to messageTextView

How to show the line break button in Android keypad?

I am building a text area in my APP using a TextInputLayout and its multiline property to let the users write more than 1 line, I am building it programatically (no xml)
TextInputLayout textInputLayout = new TextInputLayout(context);
textInputLayout.setLayoutParams(new TextInputLayout.LayoutParams(TextInputLayout.LayoutParams.MATCH_PARENT, TextInputLayout.LayoutParams.WRAP_CONTENT));
TextInputEditText field = new TextInputEditText(context);
field.setText(text);
field.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
field.setLayoutParams(new TextInputLayout.LayoutParams(TextInputLayout.LayoutParams.MATCH_PARENT,(int) (100 * dp)));
textInputLayout.addView(field);
myLinearLayout.addView(textInputLayout); // add text to layout
The code above makes my textarea, but is not showing the line break button, it shows a submit button instead.
This is the current result:
This is the expected result (from another app):
Which property I have to add to show the line break button like the second image?
How to get the keyboard to show a return key?
Try setting IMEOptions as None.
field.setImeOptions(EditorInfo.IME_ACTION_NONE);
Also check if the EditText has become SingleLine that also causes the done button to appear.

Why my text is getting greyed-out? Probably Context has to do with something

So the Negative and Positive buttons of my AlertDialog are greyed-out, but they shouldn't be.
greyed-out text screen
I suspect it has something to do with Context, becouse once i had identical problem with my ListView. I have repaired that by changing argument in ArrayAdapter's reference from getApplicationContext() to getBaseContext(). Can someone explain it to me? I don't really understand the 'Context'
This is my code
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("What do you want to do with " + getArrayList("ListOfRecipes").get(position) );
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
List<String> list = new ArrayList<>(getArrayList("ListOfRecipes"));
Toast.makeText(getBaseContext(), list.get(position) + "has been removed", Toast.LENGTH_SHORT).show();
list.remove(position);
saveList(list, "ListOfRecipes");
}
});
builder.setNegativeButton("Modify", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
List<String> list = new ArrayList<>(getArrayList("ListOfRecipes"));
SharedPreferences sp = getSharedPreferences("Recip", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("Recip", list.get(position));
editor.apply();
startActivity(new Intent(getBaseContext(), ManageRecipeActivity.class));
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
First of all, if that piece of code is inside an activity, you can simply declare context with "this" (which is what you have done by passing MainActivity.this) to the dialog builder.
What I'm suspecting is that it may be that your MainActivity is getting a theme for the AlertDialog that is making the buttons look gray. You could check that out in your styles.xml (if there is a style defined for the dialog) and in the AndroidManifest file for the theme you are passing to your MainActivity.
If you don't find anything wrong/don't want to change the theme, I can think of two ways to solve that problem.
First way - Changing the button color (less work, but less flexible)
The first is actually changing the dialog button color as it's done in this post to whatever color you want.
Second way - inflating a custom view that meets your needs (more work, but more flexible)
The second way would be to inflate a view and pass it to the dialog. Actually, you don't really have to use the standard dialog style at all, you can inflate your own view inside it to fit your needs.
To do that, you must:
1) Inflate a chosen view
As an example:
LayoutInflater factory = LayoutInflater.from(this);
final View view = factory.inflate(R.layout.image_dialog_layout, null);
2) Pass the inflated view to your dialog builder:
final AlertDialog dialog = new AlertDialog.Builder(this);
dialog.setView(view);
//Additional code to set click listeners, i.e.
dialog.create().show();
}
That way, you'll be inflating whatever layout you want, so you can just put the buttons you want inside it (with the color, size, font type you want).
It is important to notice that, even after inflating a view to it, you can still use methods setPositiveButton and setNegativeButton, they will appear below your inflated layout in the dialog. So, beware inflating buttons AND using those methods, because the buttons will appear duplicated.
Since, in this case, you don't want them to be gray, you want to put buttons inside your layout, with whatever style you want, and inflate them (and reference them in your code through findViewById).
The biggest advantage of the second way is that you can inflate whatever you want, with the styles you want. You can even put images inside it, if you wish.
Hope it helps, let me know if it worked for you.
Context is an interesting topic in android. And one thing to understand is Application Context and Activity Context are different. You should make sure that any thing that is related to UI, you should be using Activity Context.
This can be things like
Showing a dialog
Starting another activity
Inflating a new layout
This is because Activity is the only Context on which the themes defined in your manifest are actually attached.
I also recommend Context, What Context article to get a complete picture.
Happy Coding :)

How I can make links clickable in an AlertDialog without using a TextView?

I have this line in the strings.xml file:
<string name="backup_help_body">Please watch this video if you did not understand the above steps: www.youtube.com</string>
and the above text in an AlertDialog:
AlertDialog.Builder backupHelpDialog = new AlertDialog.Builder(this);
backupHelpDialog.setTitle(getString(R.string.backup_help_title));
backupHelpDialog.setMessage(R.string.backup_help_body);
backupHelpDialog.setIcon(R.drawable.my_notes);
backupHelpDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface backupHelpDialog, int witch) {
// Do Not Do Anything.
}
});
backupHelpDialog.show();
return true;
How can I make the link clickable in the AlertDialog without using a TextView?!
I'm not sure you can do without a TextView, I would use a Custom AlertDialog, however you can create a "temporary" TextView within the code to perform what you need, here I leave the url where you have several examples
How can I get clickable hyperlinks in AlertDialog from a string resource?
Use HTML formatting :
Example :
<string name="my_link">Click me!</string>
For reference :
Android-Is it possible to add a clickable link into a string resource
Now for making the link as clickable, you need to add the LinkMovementMethod to the corresponding Textview.
To get the related Textview, you need to fetch the mAlert field from AlertDialog using reflection and then findviewbyId(android.R.id.alertTitle)
Reference :
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/android/app/AlertDialog.java#AlertDialog.0mAlert
I think the best and fastest way (what I do) would be to add a custom layout with a textview inside, then add an onClickListener on that textview.
#SuperEasyToDo

Android development toggling TextView visibility

Im having some trouble with setting textview to invisible/visible.
basicly i want this to happen when an on/off button has been clicked.
what i did is kind of like
textview.setVisibility(TextView.VISIBLE);
textview.setVisibility(TextView.INVISIBLE);
when i try executing this the emultor says that the app has stopped unexcpetedly
Are you building this from XML or programmatically?
I would make it with an XML file then when the Activity runs change the property. Be sure to use setContentView(R.layout.main); before you try to get the TextView with findViewById(...).
Call .setVisibility(View.GONE); on the TextView to hide it.
Call .setVisibility(View.VISIBLE); to on the TextView to show it.
I have an example that does something like this. You can see the code here: https://github.com/ethankhall/Morse-Messenger/blob/master/src/com/kopysoft/MorseMessenger/Translate.java
Without more code or a stack trace, it's hard to say, but it sounds like you haven't initialized the text view. Here's how to do it:
TextView myTextView = (TextView) findViewById(R.id.tv_text);
Where 'tv_text' is the id of the textview as defined in the xml layout file.
Hope that helped!
Read about DDMS and logcat to obtain a stacktrace and to see what the problem is: http://developer.android.com/guide/developing/debugging/debugging-projects.html
This is what you are looking for:

Categories