I was trying to add a text view to a LinearLayout programmatically using the following:
Resources r = getResources ();
TextView text = new TextView (this);
text.setText (R.string.no_passwords);
Log.d ("My App", r.getString (R.string.no_passwords));
text.setTextSize (r.getDimension (R.dimen.prompt_size));
Log.d ("My App", "Text size: " + r.getDimension (R.dimen.prompt_size));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
parent.setGravity (Gravity.CENTER);
text.setLayoutParams (params);
parent.addView (text);
Explanation:
parent is the LinearLayout with the id parent_layout (You will see this in the code below). I am logging with the tag "My App" because I am using a custom filter to log stuff.
But the view is not added. I looked at the log message and see something relly weird:
10-17 09:33:26.764 11877-11877/com.passwordgen D/My Appīš [ 10-17 09:33:26.764 11877:11877 D/My App ]
Text size: 43.132
R.string.no_password should be "You have not saved any passwords yet" and R.dimen.prompt_size should be 9pt.
Quuestions:
Why the first log message isn't showing the right text but [ 10-17 09:33:26.764 11877:11877 D/My App ]? And why the second message is not prefixed by the tag "My App"?
You will need to call invalidate() to make the view be redrawn. Also it is recommended you do this via an xml file vs. in code.
Related
I want to make a bold text in my activity and show it in a dialog box.
I did it (I think) but I don't know how should I call it in my dialog box.
I'm trying in with HTML format:
String text = "<b>" + "My text" + "</b> " + "is bold";
StringCharacterIterator tv = null;
tv.setText(Html.fromHtml(text ));
Here it should work, but how can I call the "tv"?
I'm trying this way:
builder.setMessage(tv);
...but isn't working since the error is:
error: incompatible types: Spanned cannot be converted to String tv.setText(Html.fromHtml(text ));
You can create custom dialog box(example: How to create a Custom Dialog box in android?) and set your string like below.
String text = "<b>My text</b> is bold";
TextView tv = yourdialog.findViewById(R.id.ur_text_view_id);
tv.setText(Html.fromHtml(text));
I answer by myself:
builder.setMessage(Html.fromHtml("<b>Write something" + "</b> " + "here"));
I'm currently finishing my game app for android, but I have encountered one problem that keeps crashing my app.
I was trying to place the text from strings.xml inside the toast message and my code looks like this:
// THIS IS THE PART THAT DOES NOT WORK:
int stringrank = getResources().getIdentifier("rank"+points, "string", this.getPackageName());
rank = getString(stringrank);
// I want to get different rank String depending on the collected points,
// so it would be rank1, rank2, rank3 and so on - thats why "rank"+points.
// It worked for me in the different parts of the code, but when I want to
// use it in the Toast Message it does not.
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.done_toast, (ViewGroup) findViewById(R.id.done_toast));
TextView text = (TextView) layout.findViewById(R.id.text);
// APP CRASHES when I use "rank" String here, if I place other text it works just fine.
text.setText(rank);
final Toast donetoast = new Toast(getApplicationContext());
donetoast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 35);
donetoast.setDuration(Toast.LENGTH_SHORT);
donetoast.setView(layout);
// Rest of the Toast Message code is below, not important.
(...)
When I place the normal text in the Toast Message (does not matter whether in Java or in the done_toast.xml) it works perfectly, but if I want to call the String from strings.xml the app crashes.
Thank you for your help in advance!
You can't create a Toast object like that. It must be done using Toast.makeText().
Try this
final Toast donetoast=Toast.makeText(getApplicationContext());
donetoast.setDuration(Toast.LENGTH_SHORT);
donetoast.setView(layout);
I want to get JOptionPane to open up images from a link, because I have an assignment due next week where I have to include images in JOptionPane, and while it works with local files I'm not sure if that'll show when I turn it in. Anyways, what I have is:
ImageIcon thing = new ImageIcon("http://i.imgur.com/OGxr68g.jpg");
String[] finalOutputChoices = {"Help", "Please"};
int finalOutput = JOptionPane.showOptionDialog(
null //
, "Message" //
, "Final Output" //
, JOptionPane.YES_NO_OPTION //
, JOptionPane.PLAIN_MESSAGE //
, thing //
, finalOutputChoices //
, "Awesome" //
);
If I replace the link with the path to an image on my desktop it works, but it doesn't with the link.
Any help would be appreciated :)
Works with a URL object.
ImageIcon thing = new ImageIcon(new URL("http://i.imgur.com/OGxr68g.jpg"));
PS: weird image ;-)
// fetch window manager object
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
// set layout parameter of window manager
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
30, // width of layout 30 px
200, // height is equal to full screen
WindowManager.LayoutParams.TYPE_PHONE, // Type Ohone, These are non-application windows providing user interaction with the phone (in particular incoming calls).
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
// WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH // this window won't ever get key input focus
PixelFormat.TRANSLUCENT
);
mParams.gravity = Gravity.LEFT | Gravity.TOP;
//mParams.setTitle("Load Average");
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
mWindowManager.addView(touchLayout, mParams);
touchLayout.setOnTouchListener(this);
This is just a snippet of the code. My first issue was receiving touches in the view which this will do, however, the soft keyboard fails to open when touching edit text boxes in other applications while this service is running. I have tried various flags which fix the issue, but I loose the ability to register the touch event to the portion of the screen as displayed above in the code.
I'm out of ideas, does anyone have any idea of where I am going wrong, or an alternative solution for Android 4.2.2+?
try this:
Dialog dialog = new Dialog(this,android.R.style.Theme_DeviceDefault_Dialog_NoActionBar_MinWidth);
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.setContentView(new EditText(this));
dialog.show();
I have a CheckBox with a string that says "I have read and understood the terms and conditions". Now I want to make the words "terms and conditions" to a link which opens a alertdialog where the terms and conditions can be read. Nothing special.
I'm thinking something in the line of:
<string name="cont_agree">I have read and understood the <a ref="open alertdialog">terms and conditions.</a></string>
Is it possible, and what should I use where it now says "open alertdialog"?
If it can't be done this way, how should I do?
Addition:
To open a url you would use this code:
<string name="cont_agree"><a ref="http://www.stackoverflow.com">Stackoverflow</a></string>
But how do you open a alertdialog, or say another screen, from a string? I have seen apps who does this so it is possible, of course, but how?
EDIT:
This is the code I use for the SpannableStringBuilder:
SpannableStringBuilder text = new SpannableStringBuilder();
text.append(getString(R.string.before));
//Now create a ClickableSpan
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
d.show(); //Here dialog will be displayed
}
};
//Now append the TOS string
text.append(getString(R.string.popup));
//Declare that the "TOS" string is a clickableSpan
text.setSpan(clickableSpan, getString(R.string.before).length(),getString(R.string.before).length()+getString(R.string.popup).length(), 0); //Check on API setSpan method
((CheckBox)findViewById(yourview)).setMovementMethod(LinkMovementMethod.getInstance());
((CheckBox)findViewById(yourview)).setText(text, BufferType.SPANNABLE);
I still get some markers at the first "text.append" line.
Multiple markers at this line:
Return type for the method is missing
Syntax error on token ")", { expected after this token
R.string.before cannot be resolved to a type
Syntax error on token ")", invalid VariableDeclaratorId
Syntax error on token "append", Identifier expected after this token
First setup your dialog
Dialog d = new Dialog(context);
d.setTitle... etcetc
In your values.xml create 2 string
<string name="before">I have read and understood the</string>
<string name="popup">TOS</string</string>
Now you can use SpannableStringBuilder
SpannableStringBuilder text = new SpannableStringBuilder();
text.append(getString(R.string.before));
//Now create a ClickableSpan
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
d.show(); //Here dialog will be displayed
}
};
//Now append the TOS string
text.append(getString(R.string.popup));
//Declare that the "TOS" string is a clickableSpan
text.setSpan(clickableSpan, getString(R.string.before).length(), getString(R.string.before).length()+getString(R.string.popup).length(), 0); //Check on API setSpan method
((CheckBox)findViewById(yourview)).setMovementMethod(LinkMovementMethod.getInstance());
((CheckBox)findViewById(yourview)).setText(text, BufferType.SPANNABLE); //AAAAND WE'RE DONE!