1

I am trying to create a contentView programmatically and i cannot understand why the following code dows not work as i want. Here is the code:

public class UserProfile extends AppCompatActivity {
    ScrollView scrollView;
    LinearLayout contentnView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        ServerRequest serverRequest = new ServerRequest(this);
        final String userid = intent.getStringExtra(PostPopUp.EXTRA_USER_PROFILE_ID);
        serverRequest.getUserPostsInBackground(userid, new GetCallback() {
            @Override
            public void done(ArrayList<Document> docs) {
                contentnView = new LinearLayout(UserProfile.this);
                contentnView.setOrientation(LinearLayout.VERTICAL);
                setContentView(contentnView);
                RelativeLayout relativeLayoutWithView = new RelativeLayout(UserProfile.this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                relativeLayoutWithView.setLayoutParams(params);
                relativeLayoutWithView.setPadding(16, 16, 16, 16);
                relativeLayoutWithView.setBackgroundColor(Color.WHITE);
                FacebookSdk.sdkInitialize(getApplicationContext());
                ProfilePictureView profilePictureView = new ProfilePictureView(UserProfile.this);
                profilePictureView.setProfileId(userid);
                profilePictureView.setId(View.generateViewId());
                final HashMap<Integer, View> lines = new HashMap<>();

                int i = 0;

                for (Document post : docs) {
                    lines.put(i, new View(UserProfile.this));
                    i++;
                }
                RelativeLayout.LayoutParams paramsProfileImg = new RelativeLayout.LayoutParams(170, 170);
                paramsProfileImg.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);              
                relativeLayoutWithView.addView(profilePictureView, paramsProfileImg);
                for (int j = 0; j < textViews.size(); j++) {
                    RelativeLayout.LayoutParams paramsLine = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
                    if (j == 0) {
                        paramsLine.addRule(RelativeLayout.BELOW, profilePictureView.getId());
                    } else {
                        paramsLine.addRule(RelativeLayout.BELOW, lines.get(j - 1).getId());                   
                    }

                    paramsLine.setMargins(0, 16, 0, 0);
                    lines.get(j).setLayoutParams(paramsLine);
                    relativeLayoutWithView.addView(lines.get(j), paramsLine);
                }
                scrollView = new ScrollView(UserProfile.this);
                scrollView.addView(relativeLayoutWithView);
                contentnView.addView(scrollView);
            }
        });



    }
}

What i want is the first view (which is just a line like hr btw) to align below profile picture view (which it does) and the other views to align one below the other. What does happen is that the first view aligns below profile picture view and all the others are stack to start of the screen.

Any ideas?

.. serverRequest.getUserPostsInBackground is just an AsynkTask

Iraklis Bekiaris
  • 1,163
  • 15
  • 43

1 Answers1

1

Since you're using a relativelayout you should align every new view you are adding under the view added beforehand.
I think you're doing that I'm not sure though becuase I dont know what is the lines field used for (a list of views?), in case it is - try and explicitly set the id for every new view you create, I suspect they all added to the top since the id field is 0 or something similar so the layout parames you're using are useless (i.e. they are all aligned under view with an id -1 or 0 since they only get there id after you complete the action of creating the entire view)

crazyPixel
  • 2,301
  • 5
  • 24
  • 48
  • i just found out that i was forgetting to setIDs to hashmap values... Thanks anyway for your answer that was the problem indeed! : ) – Iraklis Bekiaris Mar 29 '16 at 20:17
  • sometimes copypasting your own code could pe a pain in the ass! – Iraklis Bekiaris Mar 29 '16 at 20:18
  • I am getting documents from mongo db where a document represents a post with fields like post body, title, icon etc... So i am putting all this values in a hashmap where hashmap value are (...)View like ImageView, TextView etc.. In this way i have what i need in hashmaps and then i layout them in content view. If you want i can post the whole class to see it and tell me if i am doing it the wrong/hard way – Iraklis Bekiaris Mar 29 '16 at 20:25
  • Actually after you're putting this way it sounds great, I just thought you're using a hashmap to keep the last view (which is a HUGE overkill...) – crazyPixel Mar 29 '16 at 20:26
  • Glad we figured that out :) Thanks for your interest! – Iraklis Bekiaris Mar 29 '16 at 20:27