Android place Relative Layout below Relative Layout programmatically - android

Hi I am having troubles trying to get this Relative Layout placed below another Relative Layout. In my Activity I have a NavBar (RelativeLayout) that is aligned to the top of the activity. I would like to place my TitleBar (RelativeLayout) below this programmatically.
Here is my onCreate method where I allocate both the NavBar and TitleBar and add them to my Activity. The NavBar is correctly aligned at the top of the activity, however, the TitleBar is aligned with the top of the activity as well. I'd like the top of the TitleBar to align with the bottom of the NavBar so it is placed below the NavBar.
super.onCreate(bundle);
Context context = getApplicationContext();
RelativeLayout.LayoutParams params;
//navbar
this.navBar = new BINavBar(context);
relativeLayout.addView(this.navBar);
params = (LayoutParams)this.navBar.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
this.navBar.setLayoutParams(params);
//titleBar
this.titleBar = new BITitleBar(context);
relativeLayout.addView(this.titleBar);
params = (LayoutParams)this.titleBar.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.BINavBar_layout);
this.titleBar.setLayoutParams(params);

I don't think you are actually setting the id of the nav bar. Try the following:
super.onCreate(bundle);
Context context = getApplicationContext();
RelativeLayout.LayoutParams params;
//navbar
this.navBar = new BINavBar(context);
relativeLayout.addView(this.navBar);
params = (LayoutParams)this.navBar.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
this.navBar.setLayoutParams(params);
this.navBar.setId(R.id.BINavBar_layout); // this is the change
//titleBar
this.titleBar = new BITitleBar(context);
relativeLayout.addView(this.titleBar);
params = (LayoutParams)this.titleBar.getLayoutParams();
params.addRule(RelativeLayout.BELOW, this.navBar.getId()); // this should use navBar's id
this.titleBar.setLayoutParams(params);
Note that you may need to set the id yourself. Are you sure that R.id.BINavBar_layout exists? If not, try using the following function: View.generateViewId()

I have a feeling the ID of the view you're creating isn't using the ID you're trying to use. Try this:
//params.addRule(RelativeLayout.BELOW, R.id.BINavBar_layout);
params.addRule(RelativeLayout.BELOW, this.navBar.getId());

Related

Why are my layouts not showing up?

I have a header layout that programmatically needs to be divided into 3 equally sized layouts. I tried weights, but nothing showed up, so I hardcoded the height in there and the root layout (extra header) becomes bigger. So they are working but the background color I have assigned them to doesn't show up, the button that is supposed to show inside the right layout doesn't show up and when I use weights instead of hardcoded numbers, the layouts are also not visible. Am I missing something?
private void SetHeader(LinearLayout extraHeader)
{
ImageView btnSettings = new ImageView(this);
btnSettings.SetBackgroundResource(Resource.Drawable.general_btn_dots_horizontal);
btnSettings.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
LinearLayout lnLeft = new LinearLayout(this);
LinearLayout lnCenter = new LinearLayout(this);
LinearLayout lnRight = new LinearLayout(this);
extraHeader.Orientation = Orientation.Horizontal;
LinearLayout.LayoutParams lpWeights = new LinearLayout.LayoutParams(200, 500);
//lpWeights.Weight = 33;
lnLeft.SetGravity(GravityFlags.Center);
lnCenter.SetGravity(GravityFlags.Center);
lnRight.SetGravity(GravityFlags.Center);
lnLeft.SetBackgroundColor(Android.Graphics.Color.ParseColor("#222222"));
lnCenter.SetBackgroundColor(Android.Graphics.Color.ParseColor("#333333"));
lnRight.SetBackgroundColor(Android.Graphics.Color.ParseColor("#444444"));
lnLeft.LayoutParameters = lpWeights;
lnCenter.LayoutParameters = lpWeights;
lnRight.LayoutParameters = lpWeights;
lnRight.AddView(btnSettings);
extraHeader.AddView(lnLeft);
extraHeader.AddView(lnCenter);
extraHeader.AddView(lnRight);
}
Also: Making the extra header orientation vertical shows my layouts right... only on top of one another and not side by side...
Remember folks: If the layout already has children (i.e.: from the xml) REMOVEALLVIEWS()

Android: Align a Button to the Right in LinearLayout

I have a piece of code which I use to create a new LinearLayout. Within the layout I wish to add a TextView which contains both a label and a value. Then next to it on the right I want to display the button. I want the button to be located toward the end of the screen, without stretching the button. I am happy with the button width and height as WARP_CONTENT.
How can I achieve this in code? I have barely any XML so using XML is not an option. I am trying to make the app as dynamic as possible, so I decided to steer clear of XML.
Please see the code below:
// Build a button
final Button addButton = new Button(task.getParent());
addButton.setText("Add New");
addButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Open a file picker here to let the user pick a file
}
});
// Build a new layout to hold all the elements
LinearLayout verticalLayout = new LinearLayout(task.getParent());
verticalLayout.setOrientation(LinearLayout.HORIZONTAL);
verticalLayout.addView(sizeTextView);
verticalLayout.addView(addButton);
Thank you guys in advance.
Try this: Add Space (View) between TextView & Button.
// View space = new View(parent_context);
View space = new View(task.getParent());
// Width:0dp, Height:1 & Weight: 1.0
LinearLayout.LayoutParams spaceLP = new LinearLayout.LayoutParams(0, 1, 1.0f);
space.setLayoutParams(spaceLP);
verticalLayout.addView(sizeTextView);
verticalLayout.addView(space);
verticalLayout.addView(addButton);
Add textview with size and gravity,like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setLayoutDirection(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
verticalLayout.addView(sizeTextView,params);
To achieve this you should use Relative layout and RelativeLayout.LayoutParams. By using LayoutParams you can set the rule to align your views as per your requirements.
for example
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);

Removing layout exits my application

I have this code for initial/default layout
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Retrieve the shared preferences
mGameSettings = getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
glView = new GLSurfaceView(this);
glView.setRenderer(this);
glView.setZOrderMediaOverlay(false);
glView.setId(123); // set id
layout = new RelativeLayout(this);
// create a fake view with zero size and place it to center of RelativeLayout
fakeView = new View(this);
fakeView.setId(24736);
fakeparams = new RelativeLayout.LayoutParams(0, 0);
fakeparams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ABOVE, fakeView.getId()); // set position is above fakeView
layout.addView(glView, params);
setContentView(layout);
Say we are in main menu and in some menu when user click it, i'll inflate new layout which I prepared in res/layout folder
myStoryView = getLayoutInflater().inflate(R.layout.story2, layout, false);
layout.addView(myStoryView);
This goes well, I get the result, new layout appear as it should.
later I call this code when user want to close story screen and return to main menu:
layout.removeView(myStoryView);
But what happened is I lose all my layout. It exits entire app, though in my galaxy tab when I hold center button, I can see my app is there, maybe it's not entirely closed. I only have no layout to display.
sorry, i just need to
setContentView(layout)

How to get RelativeLayout to change height accordingly upon adding more Layouts to it's parent?

Could some help me with Layouts? I'm having a problem getting it to display the way I'd like.
I have two relativelayouts in a linearlayout. RelativeLayout 1 is used to accomodate a fragment, RelativeLayout 2 contains the 'main' layout that should fill the screen when there is no fragment, but resize when the fragment is added.
I create the layouts dynamically like so:
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setLayoutDirection(LinearLayout.VERTICAL);
unityPlayerLayout = new RelativeLayout(this);
youtubeLayout = new RelativeLayout(this);
LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
mainLayout.setLayoutParams(mainParams);
RelativeLayout.LayoutParams youtubeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,600);
RelativeLayout.LayoutParams unityPlayerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT);
unityPlayerLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mainLayout.addView(youtubeLayout,0,youtubeLayoutParams);
mainLayout.addView(unityPlayerLayout);
unityPlayerLayout.addView(playerView,0,unityPlayerLayoutParams);
Upon adding the fragment, unityPlayerLayout does not resize and aligned to the bottom though. It get's pushed to the right, I can see a sliver of a couple of pixels, which is weird, since youtubeLayout and mainLayout should match the screen.
So, to summarize: Upon adding a fragment to youtubeLayout, I need unityPlayerLayout to resize it's height and drop to the bottom, but in practice unityPlayerLayout gets pushed to the right, and does not resize it's height.
Anyone any idea? Much appreciated!
You nee to set Layout orientation for the main LinearLayout, not direction
Change
mainLayout.setLayoutDirection(LinearLayout.VERTICAL);
to
mainLayout.setOrientation(LinearLayout.VERTICAL);
// try this way,hope this will help you...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mainLayout = new LinearLayout(this);
RelativeLayout unityPlayerLayout = new RelativeLayout(this);
unityPlayerLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
RelativeLayout youtubeLayout = new RelativeLayout(this);
youtubeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
mainLayout.setOrientation(LinearLayout.VERTICAL);
mainLayout.addView(youtubeLayout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,600));
mainLayout.addView(unityPlayerLayout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
setContentView(mainLayout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
}

How to align a dynamically create UserInterface using RelativeLayout in Android

I want to create a relative Layout dynamically through code with 2 Textviews one below the other.How to implement android:layout_below property through code in Android.
can anyone help me in sorting out this issue.
Thanks in Advance,
final TextView upperTxt = (...)
upperTxt.setId(12345);
final TextView lowerTxt = (...);
final RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams(this, null);
params.addRule(RelativeLayout.BELOW, 12345);
lowerTxt.setLayoutParams(params);
Here is my solution for my special Problem.
In case the username wouldn't be found in the db i had to create a RelativeLayout that looks like the xml-generated one.
// text view appears on top of the edit text
enterNameRequest = new TextView(mainActivity.getApplicationContext());
// fill the view with a string from strings.xml
enterNameRequest.setText(mainActivity.getResources().getString(R.string.enterNameRequest));
// edit text appears below text view and above button
enterName = new EditText(mainActivity.getApplicationContext());
enterName.setId(667);
// button appears at the bottom of the relative layout
saveUserName = new Button(mainActivity.getApplicationContext());
saveUserName.setText(mainActivity.getResources().getString(R.string.useUserName));
saveUserName.setId(666);
// generate the relative layout
RelativeLayout layout = new RelativeLayout(mainActivity.getApplicationContext());
layout.setId(668);
// set a background graphic by its id
layout.setBackgroundDrawable(mainActivity.getApplicationContext().getResources().getDrawable(R.drawable.background_head_neutral));
// runtime told me that i MUST use width and height parameters!
LayoutParams params2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ABOVE, 666);
enterName.setLayoutParams(params2);
LayoutParams params3 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ABOVE, 667);
enterNameRequest.setLayoutParams(params3);
LayoutParams params4 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 668);
saveUserName.setLayoutParams(params4);
// add views
layout.addView(enterNameRequest);
layout.addView(enterName);
layout.addView(saveUserName);
/* todo: set button action */
mainActivity.setContentView(layout);
What i found out additionally:
It is not so good to manipulate the layout manually from within java!
You should better use a new Activity and set a new layout in it.
This way, the application-code is readable a lot better!
I even tried to set several layouts (not manually, but wit setContentView) in one activity, and it turned out that i didn't know where what was accessing what else... Also, i had a great problem in adding onClickListeners... so you better use -- android:onClick="myButtonMethod" -- in your button tag in the xml and have a method in your according activity, which uses the layout, like this:
public void myButtonMethod(View v){
// do stuff
}
This improves performance because you are not using additional Listeners - but you use the already available Listener that is bound to your activity in every case.
u can try this
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);``
leftMarginParams.leftMargin = 50;
Button btn1 = new Button(this);
btn1.setText("Button1");
linLayout.addView(btn1, leftMarginParams)

Categories

Resources