0

I want to extract following part of 'display' as an image to a PNG file.

<LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/Layout">
       <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent">
           <ImageView
               android:layout_width="match_parent"
               android:layout_height="fill_parent"
               android:id="@+id/Image_view"
               />

           <TextView
               android:id="@+id/text_view1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
                />
       </FrameLayout>

   </LinearLayout>

To do that, I use following code

list.add("Raghul s");
list.add("Name 2");
list.add("Name 3");
for(int i=0;i<list.size();i++)
{
LinearLayout view = (LinearLayout) findViewById(R.id.Layout);
txt = (TextView) findViewById(R.id.Text_View1);

txt.setText(list.get(i));

view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

bs = new ByteArrayOutputStream();
bs.compress(Bitmap.CompressFormat.PNG, 40, bitmap);

try {
    file = new File("Image.png");
    file.createNewFile();
    ws = new FileOutputStream(file);
    ws.write(bs.toByteArray());
    ws.close();
    } 
catch (Exception e)
    {
    e.printStackTrace();
    }
 }

When I execute this code, I obtain an PGN image where Raghul s word is partially truncated as in the following image

enter image description here For the Name 2 and Name 3:

enter image description here

What is happening ?

Why "Raghul s" and "name 2" are truncated ?

How to solve this issue ?

Raghul S
  • 3
  • 2
  • In Android Studio. Please give any code to do save the Image correctly with all the text. – Raghul S Mar 09 '19 at 06:47
  • Hello Ragul, welcome to stack overflow. I'm "supervisor" and I must accept your question and I don't understand anything in your question ! Can you please give some lines of your code limited to your problem so that other users can understand your problem and help you. If reading file part is not important, remove it for your explanation. What is 'whole layout' ? Can you add some blank lines in your question so that is it more readable ? – schlebe Mar 09 '19 at 07:03
  • i am trying to save layout as image that's why i mention it as whole layout – Raghul S Mar 09 '19 at 07:48
  • I don't understand your problem beause it is not well explained ! Please make an efford and make what I have explained in my first comment. Are you sure that you want have some help ? Follow my advise ! Give some lines of code to show that you have already worked on this problem. The others are not there to make work at your place. This site is really great and fun.If you put some code, I can perhaps understand your problem and help you to write a good question, help you to know how to use all StackOverflow formatting possibilities. – schlebe Mar 09 '19 at 07:57
  • 1
    above is my code – Raghul S Mar 09 '19 at 08:06
  • Ok thanks. It is a begin. Now can you add the declaration/definition of 'text_View' variable and of 'list' variable ? I continue to read your code ... Can you add also the declaration of R class or variable ? I come back later – schlebe Mar 09 '19 at 09:32
  • I added declarations also sir – Raghul S Mar 10 '19 at 05:01
  • Thanks. I have now formatted your question so it is more readable for me and I hope for others. Please, I'm not Android Java developper but I thing that YOUR XML definition of LinearLayout and TextView is missing in your original question. I have added a sample. Can you EDIT your question again to fill correclty this part. I have suppressed your explanation about input file and string list because your problem can be explained without this part, because your problem is other. Next time you post a question, avoid to give too much explanations when they are not necessary. – schlebe Mar 10 '19 at 07:02
  • To put an image make simply a Paste/Copy of image into your question. I don't know how StackOverflow do that; but this is genious comparing to other sites that I know. – schlebe Mar 10 '19 at 07:04
  • Sorry,but I forget to say. I have changed some variable's names so that code is shorter so that code is more readbable and also to avoid confusion between variable name and variable typename. Can you check if transformed code is always equal to your first posted code ? – schlebe Mar 10 '19 at 07:09
  • Hello Raghul. Thanks, I see that you have completed your XML code. I think that you have also added 'name-2, but that don't correspond to image in your question that contains only Rag ! To be proper, I propose that you remove 'name-2 in your text or add 'nam' in your image :-) but not a mix of 2. – schlebe Mar 10 '19 at 17:35
  • Now, to solve your problem, I propose that you assign a background color to all element in your XML file. You can use following page to do that https://stackoverflow.com/questions/5576822/change-background-of-linearlayout-in-android. All widget, even LinearLayout must have a distinct color. When this is done, I'm interesting to see what contains your PNG file. The following link can certainly help you to find a solution https://stackoverflow.com/questions/432763/whats-the-difference-between-fill-parent-and-wrap-content – schlebe Mar 10 '19 at 17:46

1 Answers1

0

I think that your problem is linked to the fact that your TextView width is too short to display completely your name.

There are 3 possibilities to check if why I see is correct or not.

A. you remove ImageView widget and you replace wrap_content in TextView by match_parent

B. you add following property in TextView widget

android:text="xxxxxxxxxx"

C. you refresh your LinearLayout using view.invalidate() after having assign your TextView using following code:

txt.setText(list.get(i));
view.invalidate();

I think (without testing), that last command will solve your problem.

I have added this possiblities

D. you can colorize ALL widgets present in LinearLayout and see what happens. Doing that, we can see the width of all widgets and perhaps we can have a better understanding of your problem.

schlebe
  • 3,387
  • 5
  • 37
  • 50
  • Sir, Again the same problem is repeating... Please help me with some other methods. – Raghul S Mar 13 '19 at 17:19
  • I'm a C++, Java and VB.Net developper and I try to help you because I have already worked with bitmap in C++ and VB.net and your problem seems to be linked to what is really displayed. I have added another possiblities to try to understand what happen, can you try it and send me the result. I add it now. – schlebe Mar 13 '19 at 18:04
  • Have you try to colorize all your widgets ? – schlebe Mar 15 '19 at 11:33
  • ok, but can you communicate you result in your question so we can see what you have obtained ! – schlebe Mar 16 '19 at 15:09
  • I got it...tq you sir for giving me a idea – Raghul S Mar 16 '19 at 17:21
  • Hello Raghul, you have accepted my answer as the best answer, but my answer is not complete. Have you found a solution ? If YES, can you explain in a new comment, how you have solved your problem ? – schlebe Mar 17 '19 at 15:56
  • Sir in the text view i used only "name" before. Then i changed it to 10 letter eg: "xxxxxxxxxx" like this i randomly typed some letter. After this i get it. – Raghul S Mar 18 '19 at 17:29
  • Ok. I see that your problem is solved. But can you explain why or how you have solved it. On StackOverflow, it is important to know what happens so that other users that encounter same problem can use your question and my answer to solve their problem. You have certainly changed something that is important and your are alone to know what you have changed. Can you find what you have changed and can you find some explanations for other users ? – schlebe Mar 18 '19 at 18:35