Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding images to LinearLayout in code in Android
    primarykey
    data
    text
    <p>I have a list of images that I need to show on the page one after another and all that bellow some text. I have LinearLayout in xml of the file where I added the part related to that text (some TextViews). As I need to call service to get image urls I add images to that LinearLayout in code. I use this code to get image using its url and to add it to the layout (in the onPostExecute part of the AsyncTask implementation).</p> <pre><code>/** * Download image using image url and show it in ImageView */ private class DownloadImageTask extends AsyncTask&lt;Void, Void, Bitmap&gt; { public DownloadImageTask() { super(); } protected Bitmap doInBackground(Void... arg0) { String urlDisplay = mDocumentUrls.get(mDocumentIdx); mDocumentIdx = mDocumentIdx + 1; Bitmap bmp = null; try { InputStream in = new java.net.URL(urlDisplay).openStream(); bmp = BitmapFactory.decodeStream(in); } catch (Exception e) { e.printStackTrace(); } return bmp; } protected void onPostExecute(Bitmap result) { // We add a new ImageView to the LinearLayout of the page and set it // source to the downloaded image ImageView newImageView = new ImageView(getActivity()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.setMargins(convertDpToPx(20), convertDpToPx(5), convertDpToPx(20), 0); newImageView.setImageBitmap(result); mDocumentDetailsLayout.addView(newImageView, params); //Next image if(mDocumentIdx &lt; mDocumentUrls.size()) new DownloadImageTask().execute(); } } </code></pre> <p>And this is the xml of the page (the part related to LinearLayout)</p> <pre><code>&lt;ScrollView android:id="@+id/details_form" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/splashScreenGray android:visibility="gone" &gt; &lt;LinearLayout android:id="@+id/details_form_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/splashScreenGray" android:clickable="false" android:orientation="vertical" ... </code></pre> <p>Images are shown on the page, but I have two problems: 1. I can't center them horizontally (prtsc1.png) 2. When my phone is in portrait mode I have this margins (top margin on the first image and margins between images) that I don't have when my phone is in portrait mode (I don't need them) - compare prtsc1.png and prtsc2.png.</p> <p><img src="https://i.stack.imgur.com/KTrye.png" alt="prtsc1.png"></p> <p><img src="https://i.stack.imgur.com/7GQV8.png" alt="enter image description here"></p> <p>I tried everything, but I am not sure what I am doing wrong.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload