Sometimes, I cannot fetch string from resource in Android? - android

I found this problem on ASUS fonepad(android version 4.1.2) not on zendfone 3.
(Two devices I only have)
It function normally when I just open my app.
Once I let it idle too long, some of the text would be blank.
--
e.q.:
1.
http://ppt.cc/jcsi
This page should full of text in each block.
2.http://ppt.cc/PY67
There should be some text shown in the blank area.
--
I saved all of the text in the xml file.
Therefore, I think this should be the problem of resource?
Does someone know how to solve this problem?
----code----
/* I use this function for creating textview */
protected TextView customizeTextView(String text,float textSize, int bgc, int gravity,int textcolor)
{
TextView tv= new TextView(this);
tv.setText(text);
tv.setTextSize(textSize);
tv.setBackgroundColor(bgc);
tv.setGravity(gravity);
tv.setTextColor(textcolor);
return tv;
}
/*The parameter of string should be getString(R.string.stopDectecting); or logObject.recordId+"" */

You should replace tv.setText(text); with tv.setText(R.string.MY_STRING);

I know the answer finally.
At first, my app would calculate the ratio of current screen verse 800*480.
But with low possibility, it would get the 0 value.
This is only happened one ASUS fonepad.
Therefore, I change the time of calculation the ratio and it solved.

Related

Duplicate images appear in EditText after insert one ImageSpan in Android 4.x

I have below code slice to insert a bitmap into an EditText widget. With Android 5.x, it works fine, but with Android 4.x, duplicate images will show after insert one bitmap. Dose anyone know how to fix this with Android 4.x?
insertPicIntoEditText(getBitmapSpannable(resized_bm, upload_uri));
private SpannableString getBitmapSpannable(Bitmap pic, String uri_string) {
SpannableString ss = new SpannableString(uri_string);
ImageSpan span = new ImageSpan(this, pic);
ss.setSpan(span, 0, uri_string.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return ss;
}
private void insertPicIntoEditText(SpannableString ss) {
Editable et = mContentEditor.getText();
int start = mContentEditor.getSelectionStart();
et.insert(start, ss);
et.insert(start + ss.length(), "\n");
mContentEditor.setText(et);
mContentEditor.setSelection(start + ss.length() + 1);
}
This was being caused by the height set in setBounds being greater than that of the Bitmap the drawable was created from earlier in the activity. When this occurs there are two things that seem to happen..
First, if the size only slightly (I haven't extensively tested this once I got it working so I'm unsure of exact figures) exceeds the size of the Bitmap then a large blank space is added to the span, this blank space is the same size as the bitmap inserted.
Second, an extra copy of the Drawable is added to the span, directly below the blank space.
The resolution was relatively simple.. Ensure that the Bitmap used to create the Drawable was set to the intended final size before creating the Drawable and calling setBounds.
This may not work in all cases but worked for me and hopefully will be helpful for someone.
I also noticed images are repeated if the spannable string has line breaks.

Prevent Android's TextView from breaking links

This question is probably the same as this one, but since none of its answers really solve the problem, I'll ask again.
My app has a TextView that occasionally will display very long URLs. For aesthetic reasons (and since the URLs contain no spaces), the desirable behaviour would be to fill each line completely before jumping to the next one, something like this:
|http://www.domain.com/som|
|ething/otherthing/foobar/|
|helloworld |
What happens instead, is the URL being broke near the bars, as if they were spaces.
|http://www.domain.com/ |
|something/otherthing/ |
|foobar/helloworld |
I tried extending the TextView class and adding a modified version of the breakManually method (found here) to trick the TextView and do what I need, calling it on onSizeChanged (overridden). It works fine, except for the fact that the TextView is inside a ListView. When this custom TextView is hidden by the scrolling and brought back, its content goes back to the original breaking behaviour, due to the view being re-drawn without onSizeChanged being called.
I was able to work-around this problem by calling breakManually inside onDraw. This presents the expected behaviour at all times, but at a high performance cost: since onDraw is called whenever the ListView is scrolled and the breakManually method isn't exactly "lightweight", the scrolling gets unacceptably laggy, even on a high-end quad-core device.
Next step was to crawl through the TextView source code, trying to figure where and how it splits the text, and hopefully override it. This was a complete failure. I (a newbie) spent the whole day fruitlessly looking at code I mostly couldn't understand.
And that brings me here. Can someone please point me the right direction about what I should override (assuming that it's possible)? Or maybe there is a simpler way of achieving what I want?
Here's the breakManually method I mentioned. Due to the use of getWidth(), it only works if called after the view is measured.
private CharSequence breakManually (CharSequence text) {
int width = getWidth() - getPaddingLeft() - getPaddingRight();
// Can't break with a width of 0.
if (width == 0) return text;
Editable editable = new SpannableStringBuilder(text);
//creates an array with the width of each character
float[] widths = new float[editable.length()];
Paint p = getPaint();
p.getTextWidths(editable.toString(), widths);
float currentWidth = 0.0f;
int position = 0;
int insertCount = 0;
int initialLength = editable.length();
while (position < initialLength) {
currentWidth += widths[position];
char curChar = editable.charAt(position + insertCount);
if (curChar == '\n') {
currentWidth = 0.0f;
} else if (currentWidth > width) {
editable.insert(position + insertCount , "\n");
insertCount++;
currentWidth = widths[position];
}
position++;
}
return editable.toString();
}
To everyone who bothered reading this, thanks for your time.
If you're not using a monospace font, then in some cases it's not even possible to align it very well. Since you have no whitespaces in a URL, it's not likely that a Justify-like alignment will solve the problem. I suggest that you use a monospace font for that particular TextView. Then, decide on a fixed number of characters per line and break the string to display with "\n" after those many characters.
This isn't answering your question but it's the smoothest way possible, I guess.

Changing the image in an Android compound TextView - not working

OK people, I'm going seriously nuts here. That late night stuff when you've tried everything...
I've got a compound TextView with an image on the left. The image (a little pic that just says "loading") is set with:
Drawable img = getBaseContext().getResources().getDrawable( R.drawable.loading );
img.setBounds( 0, 0, 120, 120 );
tv.setCompoundDrawables( img, null, null, null );
tv is the TextView variable. That works fine. However, later I want to replace the image with a new drawable, I've tried to call setCompoundDrawables again:
tv.setCompoundDrawables( new_img, null, null, null );
I've also tried getting the array of drawables for the TextView and replacing the left one, as shown here:
Drawable[] drw = tv.getCompoundDrawables();
drw[0] = new_img;
The code runs ok in debug mode. No exceptions occur. All UI handling performed within the UI thread. Images seem to be ok, etc, but the display does not change. Do I have to refresh the display in some way?
By the way, the TextViews are added within a vertical LinearLayout if that matters. I'm pretty sure I'm missing something obvious here but I can't see it. Many thanks in advance.
(Yes, I know I could replace my design with an ImageView and a vanilla TextView, however for performance reasons I'd prefer to stick with a Compound TextView if possible)
setCompoundDrawables() is unable to decode the bounding box for drawables. Instead, use TextView::setCompoundDrawablesWithIntrinsicBounds() for using the implicit bounds for the drawable. This will ensure better handling across different screen densities.
OK. I got this to work, but Android seemed to be very picky about how it will work. I'm posting the answer in case it helps other people.
Here's the code that worked. Nothing special, until you read the comments below showing what other methods didn't work.
// resize the images. these methods seem to be very particular about how the bounds are set
img.setBounds(new Rect(0, 0, 120, 120)); // it likes this
tvp.setCompoundDrawables( img, null, null, null ); // this works for sure when combined with setBounds(Rect)
// what didn't work... (would have to more experimenting with what works and what doesn't)
//img.setBounds( 0, 0, 240, 240 ); // it didn't like this!
//Drawable[] drw = tvp.getCompoundDrawables();
//drw[0] = img; // it didn't like this technique of assigning a new image
//tvp.setCompoundDrawablesRelativeWithIntrinsicBounds(img, null, null, null); // no good, as it uses original (intrinsic) size of the image
//tvp.setCompoundDrawablesRelative(img, null, null, null); // doesn't like this - nothing bad happens; it just doesn't change the image
Hope this helps someone. Cheers, Tom

label over label overwriting , while displaying the health lable in cocos2d android

I have to display the palyers lifes in my game. every time the player hits the wrong enemy the lifes decrease.the default lifes are 5.
My code is as follows
first I declare this in my gamelayer
static int lifes=5;
CCLabel _lifes;
then a method as follows at bottom of the code
public void showLable(CCLabel _lifes){
if(_lifes != null){
this.removeChild(_lifes,true);
}
_lifes = CCLabel.makeLabel("" + lifes, "Verdana", 20);
_lifes.setColor(ccColor3B.ccbrown);
_lifes.setPosition(winSize.width/2,(winSize.height/2));
addChild(_lifes,3)
}
then I wrote this condition where the player hits the wrong enemy
lifes--;
showLable(_lifes);
1) everything works fine, the lable is displayed and label gets decreased but the label does not show up until the player hits the wrong enemy, as you see I gave default value as 5, the label shows up after the player hits wrong enemy ie, from 4.
2) Another major problem is the label displayed is displaying without removing the previous value. For ex. the lifes are 5 as default. 5 is displayed when when game starts. when the life decreases the lifes value should be 4, So here in my game the 4 is placed on 5 itself.
And then the lifes are placed on the same number as 3 or 2 or 1. All the nuumbers show on each other. Now I think you understand me
thanks in advance
1) check that your are calling showLable(_lifes); at the start of your game after its initialized, so the label show up with the value 5.
2) I'm not really sure about it but CCLabel should extends a CCSprite and thus have a setVisible method.
you may try something like :
if(_lifes != null){
_lifes.setVisible(false);
this.removeChild(_lifes,true);
}
I only found the c++ api reference version :
http://www.cocos2d-x.org/reference/native-cpp/d4/de7/classcocos2d_1_1_c_c_sprite.html

How to add text to image and save as new image

I'm trying to create an Android app that adds a random quote to images.
The general process is this:
Start from a custom given image that shows when starting the app.
From this image all the user can do is tap on it and generate a new random "quote" that get overlaid on the image.
The user can save the newly created image with the quote he chose and set it as wallpaper.
I have got to the point where I can display the image in an ImageView.
My list of quotes is stored in my strings.xml file.
I do something like this in an app. Use Canvas.
I edited down a piece of my code, which actually adds a couple of other images on the background and stuff too.
Meat of code:
private static Bitmap getPoster(...) {
Bitmap background = BitmapFactory.decodeResource(res, background_id)
.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(background);
Typeface font = Typeface.createFromAsset(res.getAssets(), FONT_PATH);
font = Typeface.create(font, Typeface.BOLD);
Paint paint = new Paint();
paint.setTypeface(font);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
paint.setShadowLayer(2.0f, 1.0f, 1.0f, Color.BLACK);
float fontSize = getFontSize(background.getWidth(), THE_QUOTE, paint); //You'll have to define a way to find a size that fits, or just use a constant size.
paint.setTextSize(fontSize);
canvas.drawText(THE_QUOTE, (background.getWidth() - paint.measureText(THE_QUOTE)) / 2,
background.getHeight() - FILLER_HEIGHT, paint); //You might want to do something different. In my case every image has a filler in the bottom which is 50px.
return background;
}
Put your own version of that in a class and feed it the image id and anything else. It returns a bitmap for you to do whatever you want with (display it in an imageview, let the user save it and set as wallpape).
I know i did this for the PC with imagemagick a few years ago(save image with text on)
Seems like imagemagick have been ported to android, so I would start digging into thier documentation.
https://github.com/lilac/Android-ImageMagick
Ok! Francesco my friend, I've an idea although not a working code ('cuz I'm not really good at it). So, here it is:
Implement an onClickListener() on your ImageView like below:
ImageView iv = (ImageView)findViewById(R.id.imageview1);
iv.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
/** When I say do your stuff here, I mean read the user input and set your wallpaper here. I'm sorry that I don't really know how to save/set the wallpaper */
}
});
When it comes to reading user input/generating random quotes, you can do this:
You said you already have the quotes saved in the strings.xml file. Using the ids of those strings, I think you can implement a switch case scenario where it uses java imports - java.util.Scanner and java.util.Random. Ultimately, using these in your ImageView onClickListener could/should result in the desired output.
I know my answer is too vague, but I've a faint hope that it has given you a minute lead as to what you can implement. I seriously hope there are better answers than this. If not, then I hope this helps you some, and I also hope that I'm not leading you in the wrong direction since this is just a mere speculation. Sorry, but this is all I've got.

Categories

Resources