1

I´m new to Android and i´m learning on my following app.

The app is a gridview app with a lot of pictures in an array. After clicking on a picture fullscreen is opened. There it´s possible to share the picture or write something on it and share it with the text.

My problem now is that i don´t understand how line break is working And yes i used google and this forum but i don´t know how that function is working...

So my question is Could somebody help me to set an automatic line break after i don´t know let´s say 12 characters or automatic after the line is full...

Thank you dudes!

My Code:

    private Bitmap ProcessingBitmap(){
        Bitmap bm1 = null;
        Bitmap newBitmap = null;

        try {
            bm1 = BitmapFactory.decodeStream(
                    getContentResolver().openInputStream(source1));

            Config config = bm1.getConfig();
            if(config == null){
                config = Bitmap.Config.ARGB_8888;
            }

            newBitmap = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), config);
            Canvas newCanvas = new Canvas(newBitmap);

            newCanvas.drawBitmap(bm1, 0, 0, null);

            String captionString = editTextCaption.getText().toString();
            if(captionString != null){

                Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
                paintText.setColor(Color.WHITE);
                paintText.setTextSize(25);
                paintText.setStyle(Style.FILL);

                Rect rectText = new Rect();
                paintText.getTextBounds(captionString, 0, captionString.length(), rectText);

                newCanvas.drawText(captionString, 
                        0, rectText.height(), paintText);

                Toast.makeText(getApplicationContext(), 
                        "drawText: " + captionString, 
                        Toast.LENGTH_LONG).show();


            }else{
                Toast.makeText(getApplicationContext(), 
                        "caption empty!", 
                        Toast.LENGTH_LONG).show();

            }

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    + File.separator + "tmp1.jpeg");
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //write the bytes in file
            FileOutputStream fo = new FileOutputStream(f);
            try {
                fo.write(bytes.toByteArray());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // remember close de FileOutput
            try {
                fo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d("done","done");
            MediaScannerConnection.scanFile(this,
                      new String[] { f.toString() }, null,
                      new MediaScannerConnection.OnScanCompletedListener() {
                  public void onScanCompleted(String path, Uri uri) {
                      Log.i("ExternalStorage", "Scanned " + path + ":");
                      Log.i("ExternalStorage", "-> uri=" + uri);
                  }
              });

            } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }

        return newBitmap;
    }

And here my layout file if you need it too:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:background="@color/black"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/loadimage1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/green"
        android:text="Load Image" />

    <TextView
        android:id="@+id/sourceuri1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/caption"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionNext"
        android:background="@color/white" />

    <Button
        android:id="@+id/processing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/green"
        android:text="Show text on Image" />

    <ImageView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
  • Please why are you posting all that code? Only the canvas code had been enough. Concentrate on the problem. – greenapps Apr 27 '14 at 21:30
  • Because i´m new to android and i don´t understand that line break thing. And maybe other people who are new to android could need that in the future... So could you help me do you know the answer to my question? @greenapps – user3578109 Apr 28 '14 at 04:05
  • Even if you are new is no reason to post irrelevant code. Post only code where you need support. You force us to read 300 lines of code where 20 would suffice. Why claim our time so much? – greenapps Apr 28 '14 at 06:11
  • ok dude i´m changing it but do you have an answer for me? Thank you! @greenapps – user3578109 Apr 28 '14 at 11:44
  • Canvas:drawText() is unaware of linebreaks like "\n" or "\r\n". It will print the whole string on one line within the rect. If you are lucky you see dots or squares for the linebreaks. What do you see exactly? So it is up to you to split your string in lines first and then -in a loop- print the lines at increasing y positions. – greenapps Apr 28 '14 at 12:29
  • At the moment if i´m writting something long it´s going out of the picture. If i´m using the enter from the keyboard it´s working in the field i´m writting but it´s only shown as an empty space and in one line. I don´t understand your last sentence i mean i know that .split but i don´t know how to use it correct. @greenapps – user3578109 Apr 28 '14 at 12:51
  • I tried also: .replaceAll("\\n", "
    ") but i see only the
    . @greenapps
    – user3578109 Apr 28 '14 at 12:54
  • String [] lines = captionString.split("\n"); And for replaceAll("\\n", "
    "); you ment replaceAll("\n", "
    "); drawText is also unaware of a html br tag.
    – greenapps Apr 28 '14 at 13:04
  • Ok so do i understand you right that with drawText i can´t use linebreaks? What are my other options? I mean what would you do if you want to add a text to a picture and want to have an automatic line break. Or let´s see my problem maybe from another side: Enter Button(line break) is working for the field where i´m writting the text what would i´m looking for when i want that the program make that line break on the picture too instead of only a space? @greenapps – user3578109 Apr 28 '14 at 13:17
  • That I already told you: split the string in lines and call drawText() for every line. – greenapps Apr 28 '14 at 13:20
  • Sry i don´t know how that is working. @greenapps – user3578109 Apr 28 '14 at 13:53
  • Do you know some good examples for that? So i can see who that looks like? @greenapps – user3578109 Apr 28 '14 at 14:00
  • What kind of problem do you have exactly to loop trough the lines of String [] lines = captionString.split("\n"); an call drawText() with an y parameter that increses with a lineheight? – greenapps Apr 28 '14 at 15:54
  • I have the problem that i don´t know what to do with lines? Where do i set a variable for that? Because: The value of the local variable lines is not used calling drawText() with the y-parameter is no problem that i learned yesterday how that is working :) and to increase it with a lineheight i don´t know now but i take a look at that don´t give me the answer to that for the moment! Thx that you are helping me by the way i´m learing a lot with you! :) @greenapps – user3578109 Apr 29 '14 at 06:27
  • You had drawText(captionString) and now where you split captionString in lines you use drawText(lines[i]) in a loop for i. – greenapps Apr 29 '14 at 07:26
  • ok i think that i understood but i´m doing something wrong i have a IndexOutOfBoundsException Error and the app is crashing. the new code is: `String [] lines = captionString.split("\n"); for (int i = 0; i < lines.length; ++i) { paintText.getTextBounds(lines[i], 0, captionString.length(), rectText); newCanvas.drawText(lines[i], 0, 20, paintText);` @greenapps – user3578109 Apr 29 '14 at 09:43
  • Something new: `String [] lines = captionString.split("\n"); for (int i = 0; i < lines.length; ++i) { paintText.getTextBounds(captionString, 0, captionString.length(), rectText); newCanvas.drawText(lines[i], 0, 20, paintText);` with that code i have no crash and enter button and draw is now working that way that i have for example line1 (line break) line2 in the same line on the same postition. Is that now the thing with `y parameter increases with a lineheight` what u wrote? @greenapps – user3578109 Apr 29 '14 at 09:49
  • It should be paintText.getTextBounds(lines[i], 0, lines[i].length(), rectText); Yes now you have to increase y. Change ++i to i++ in the for() statement. – greenapps Apr 29 '14 at 09:55
  • Thank you soooooo much dude!!!!!!! It´s working!!!! :D If i press enter now it´s in the next line :D The Code i´m using now is: `String [] lines=captionString.split("\n"); int lineHeight = 0;int yoffset = 0; for (int i = 0; i < lines.length; ++i) {paintText.getTextBounds(captionString, 0, lines[i].length(), rectText);lineHeight = (int) ((float) rectText.height() * 1.2); newCanvas.drawText(lines[i], 0, 20 + yoffset, paintText); yoffset = yoffset + lineHeight;` If you are putting that in the answer i will check it as correct :) THX again dude :)!!!!!@greenapps – user3578109 Apr 29 '14 at 10:07

1 Answers1

1
String [] lines=captionString.split("\n"); 

float y = rectText.height(); 

for (int i = 0; i < lines.length; i++) 
   {
   paintText.getTextBounds(lines[i], 0, lines[i].length(), rectText);

   newCanvas.drawText(lines[i], 0, y, paintText); 

   y += rectText.height() * 1.2;  // why 1.2?
   }
greenapps
  • 11,154
  • 2
  • 16
  • 19
  • if you are asking me with the //why 1.2? you are right i don´t want the text to be bigger in every line for 20% was so happy that it´s working that i didn´t see that i don´t need that ^^ I learned a lot with your help thank you again have a nice day :)!!! – user3578109 Apr 29 '14 at 10:23
  • Funny that you first posted +300 lines and now -after some days- have the solution with less than 10 code lines. Read this: http://stackoverflow.com/questions/966800/mythical-man-month-10-lines-per-developer-day-how-close-on-large-projects – greenapps Apr 29 '14 at 10:30
  • Haha yeahh :D but only with your help :) – user3578109 Apr 29 '14 at 15:03