1

Here is my code :

  rl=(RelativeLayout)findViewById(R.id.ScoreLayout);


    rtb=new RatingBar(this);
    rl.addView(rtb);
    rtb.setNumStars(5);
    rtb_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    rtb_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    rtb_params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    rtb.setLayoutParams(rtb_params);


    txt1=new TextView(this);
    rl.addView(txt1);
    txt1.setSingleLine();
    txt1.setTextSize(TypedValue.COMPLEX_UNIT_PX,15);
    txt1.setText("Best Score: ");
    txt1_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    txt1_params.addRule(RelativeLayout.ALIGN_TOP, rtb.getId());
    txt1_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    txt1.setLayoutParams(txt1_params);


    txt2=new TextView(this);
    rl.addView(txt2);
    txt2.setSingleLine();
    txt2.setTextSize(TypedValue.COMPLEX_UNIT_PX,30);
    txt2.setText(String.valueOf(DataBase.HighScore));
    txt2_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    txt2_params.addRule(RelativeLayout.ALIGN_TOP, txt1.getId());
    txt2_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    txt2.setLayoutParams(txt1_params);


    txt3=new TextView(this);
    rl.addView(txt3);
    txt3.setSingleLine();
    txt3.setTextSize(TypedValue.COMPLEX_UNIT_PX,15);
    txt2.setText("Current Score: ");
    txt3_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    txt3_params.addRule(RelativeLayout.ALIGN_TOP, txt2.getId());
    txt3_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    txt3.setLayoutParams(txt1_params);


    txt4=new TextView(this);
    rl.addView(txt4);
    txt4.setSingleLine();
    txt4.setTextSize(TypedValue.COMPLEX_UNIT_PX,30);
    txt4.setText(String.valueOf(DataBase.score));
    txt4_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    txt4_params.addRule(RelativeLayout.ALIGN_TOP, txt3.getId());
    txt4_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    txt4.setLayoutParams(txt1_params);


    btn1=new Button(this);
    rl.addView(btn1);
    btn1_params=new RelativeLayout.LayoutParams(150,40);
    btn1_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    btn1_params.topMargin=DataBase.Layout_Height-80-80;//first 80 for the advertisment Bar and second for the two 40-height-buttons
    btn1.setLayoutParams(btn1_params);


    btn2=new Button(this);
    rl.addView(btn2);
    btn2_params=new RelativeLayout.LayoutParams(150,40);
    btn2_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    btn2_params.addRule(RelativeLayout.ALIGN_TOP, btn1.getId());
    btn2.setLayoutParams(btn2_params);

and while these views should be centered horizontally and tactical they appear like that :

http://postimg.org/image/peqhzw0uh/

I want each view to be under another view and all views should be horizontally in the center ! RatingBar(rtb) should be in the top and under that should be

txt1-->txt2-->txt3-->txt4-->btn1-->btn2

I have spent to much time quering this issue and i cant find the answer why this is happening thank you for your help :)

sciritai
  • 3,688
  • 1
  • 17
  • 22
MajorDanger
  • 275
  • 1
  • 3
  • 9

2 Answers2

3

You need to setId() some identifiers to your views so that the ALIGN_TOP, view.getId() rules actually do something.

Though ALIGN_TOP only aligns the top of the view, making the views draw on top of each other. Use LAYOUT_BELOW rule or a LinearLayout instead.

For a view id, any integer > 0 will do. Just make sure the id's are distinct so the RelativeLayout can find the correct views you're referring to.

For Android Studio's "Expected resource of type Id" error, use @IdRes annotation to annotate your integers as resource identifiers. For example,

@android.support.annotation.IdRes int id = 1;
view.setId(id); // no error

id++; // next id
view2.setId(id); // and so on

... though in this case a LinearLayout would avoid the need of generating ids in the first place.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • but i add all my views programmatically ! How am i supposed to setid() ? thank you :) – MajorDanger Aug 05 '14 at 11:32
  • The id you set can be any number > 0. Just make sure the `RelativeLayout` can refer to the view you want when given an id. – laalto Aug 05 '14 at 11:34
  • no i cant android studio says : Expected resource of type Id – MajorDanger Aug 05 '14 at 11:53
  • You can use `@IdRes` annotation to get around that. http://stackoverflow.com/questions/24716385/android-studios-expected-resource-of-type-checks – laalto Aug 05 '14 at 11:58
0

Since you are using RelativeLayout use txt1_params.addRule(RelativeLayout.BELOW, rtb.getId()); This should do the trick..

Sample Code for txt1 below rtb

txt1_params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
txt1_params.addRule(RelativeLayout.BELOW, rtb.getId());
txt1_params.addRule(RelativeLayout.CENTER_HORIZONTAL);
txt1.setLayoutParams(txt1_params);

You can use the same code for rest of the views.

Assign id via code (programmatically)

  • Manually set ids using someView.setId(int);
  • The int must be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.)
  • For example, if creating and numbering several views representing items, you could use their item number.
Femil Shajin
  • 1,768
  • 6
  • 24
  • 38
  • well i have already tried that and the result is the same ! It might be that i dont set the id of each view , but android studio doesnt let me :/ – MajorDanger Aug 05 '14 at 11:55
  • Check the edited answer and let me know.. Every time when you add a view increase the int value which the id for every view.. – Femil Shajin Aug 05 '14 at 11:57