0

I'm trying to add a bunch of ImageView on my UI using a loop, the problem is that I have no Idea if the ImageView is being added or not because when I run the app it just gives me a blank white screen.

for(int i = 0; i < jsonArray.length(); i++) {
                Log.d("test", "ok"); //the loop works btw
                poster.setId(i);
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                ImageView poster = new ImageView(getApplicationContext());
                poster.setBackgroundResource(R.drawable.myPoster);


                RelativeLayout.LayoutParams posterParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT
                );
                posterParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
                posterParams.addRule(RelativeLayout.CENTER_VERTICAL);
                posterParams.width = 160; //is this DP?
                posterParams.height = 220;
                relativeLayout.addView(poster, posterParams);

            }

Any suggestion is welcome.

EDIT

I added another piece of code just to test if a widget will be added without using a loop:

  //test
  LinearLayout layout = new LinearLayout(this);
  Button btn = new Button(this);
  btn.setText("this is a button");
  btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
  layout.addView(btn);

And I still get the same result, just blank.

Kevin Bryan
  • 1,846
  • 2
  • 22
  • 45

2 Answers2

1
  • First of all, you are giving exactly the same parameter to each ImageView this causes all your ImageView lays on Like STACK only the last one will be visible to you. You may use ScrollView to see if the ImageViews actually added to your root layout

  • Secondly, set layout parameters to your dynamic ImageViews not your root layout.

Update

How to use ScrollView,

First of all, your XML should contain ScollView and a child (LinearLayout in our case but you can choose any layout depending on your use case) for placing your ImageView

    <ScrollView>
      <LinearLayout
       android:id = "imageViewPlaceHolder">

      </LinearLayout>
    </ScrollView>

Secondly, in your Java code, you should find an inner layout to add views as follows

LinearLayout placeHolder = findViewById(R.id.imageViewPlaceHolder);

Then you are ready to add ImageViews into placeHolder since your placeHolder wrapped with ScrollView it will create a scroll dynamically if the content height goes beyond the dedicated height.

placeHolder.addView(yourImageView);

Adding everything from Java

HorizontalScrollView hsv = new HorizontalScrollView(context);
hsv.setLayoutParams(yourLayoutParams);

LinearLayout myPlaceHolder = new LinearLayout(context);
myPlaceHolder.setLayoutParams(yourLayoutParamsForLinearLayout);

myPlaceHolder.addView(yourDesiredImageView);

hsv.addView(myPlaceHolder);

yourRootLayout.addView(hsv);

Hope it makes sense, feel free to ask any clarification

Jasurbek
  • 2,946
  • 3
  • 20
  • 37
  • What do you mean by "use ScrollView", can you give me an example code? thanks – Kevin Bryan Jun 16 '19 at 15:45
  • Thanks, what I did is created the LinearLayout using xml then create an instance of it in java //findViewbyID() then addView(). I'm still wondering though how can I make this work without creating the LinearLayout using xml but using java instead. – Kevin Bryan Jun 16 '19 at 16:11
  • how to get the root layout? – Kevin Bryan Jun 16 '19 at 16:41
  • Most probably `relativeLayout` is your root layout – Jasurbek Jun 16 '19 at 16:43
  • I removed the relative layout now, I'm only using the linear layout. So it would be basically the same scrollview > linearlayout > imageview. Which does not work for me – Kevin Bryan Jun 16 '19 at 16:47
  • 1
    That all I can do for you, for now, I suggest starting bounty to the question – Jasurbek Jun 16 '19 at 16:53
0

try this:

listView = (ListView) findViewById(R.id.lv_info);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, text);

listView.addHeaderView(creatHeader(getString(R.string.abuot_email),getResources().getString(R.string.email_info)));
listView.addHeaderView(creatHeader(getString(R.string.about_phone),getResources().getString(R.string.admin_phone_num)));
listView.addHeaderView(creatHeader(getString(R.string.about_copyright), getResources().getString(R.string.copyright_info)));
listView.addHeaderView(creatHeader(getString(R.string.about_version),getResources().getString(R.string.version_info)));

listView.setAdapter(adapter);

Method creatHeader:

public View creatHeader(String title, String text) {
    View v = getLayoutInflater().inflate(R.layout.lv_item_header_info, null);
    ((TextView) v.findViewById(R.id.tvTitle)).setText(title);
    ((TextView) v.findViewById(R.id.tvText)).setText(text);
    return v;
}

Layout file for items:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ssss"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"/>


    <TextView
        android:id="@+id/tvText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="sssssssss"
        android:gravity="center_vertical"/>

</LinearLayout>
Mkurbanov
  • 197
  • 3
  • 13