How to add a layout into a layout in android - android

I don't know how to write code to add a layout into a layout in android, because when I read the reference, I saw it only have "AddView" method, when I try to use "AddView" method to add a layout, it don't work. Can you suggest me some solutions, I don't use XML file to add layout?

Here's a snippet that creates a new layout (newL), adds an image to it, and then adds newL to a layout (layout) that was defined in the XML
ViewGroup.LayoutParams lp_fullWidth =
new ViewGroup.LayoutParams(lWidth, lHeight);
ViewGroup.LayoutParams lp_wrap =
new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, lHeight);
LinearLayout layout = (LinearLayout) findViewById(R.id.viewNowPlaying);
LinearLayout newL = new LinearLayout(context); // create layout
ImageView arrowRT = new ImageView(context); // create image
arrowRT.setImageResource(R.drawable.arrowrt); // set source file
newL.addView(arrowRT, lp_wrap); // add image to newL
layout.addView(newL,lp_fullWidth); // add newL to layout

Related

Add UI elements dynamically in android

I want to add some ui elements to my android app, a Button for example!
But I can't find a complete tutorial! I found this code after a lot of searches:
LinearLayout ll = (LinearLayout)findViewById(R.id.layout);
Button btn = new Button(this);
btn.setText("Manual Add");
btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
ll.addView(btn);
My first problem is first line! Can you explain it for me please? What is R.id.layout? I know R is an object for resources but I don't know what is layout!
Second problem is line 3, What is LayoutParams?
Thank you all!
You can create views using default constructors for example
Button button = new Button(context);
After that you should determine to which type of parent view you are going to attach it, and create corresponding layot params. Every parent view LayoutParams type has uniqe customize methods, for example rules of RelativeLayout.LayoutParams.
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height)
//customize params here
button.setLayoutParams(params)
Attach view to your parent view
frameLayout.addView(button)
That is it.
LinearLayout ll = (LinearLayout)findViewById(R.id.layout);// you are getting a refrence to your layout
Button btn = new Button(this); // creating a new object of button type
btn.setText("Manual Add"); //setting the button text
btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT)); //setting the width and height of the element
ll.addView(btn);//adding the button to your layout
R.id.layout is the name of your activity layout
It is the parent view which you are going to add your views. In your example it is a LinearLayout called layout.

Confusion with findViewById()

I am trying to programmatically add LinearLayouts inside an existing RelativeLayout. Each LinearLayout will contain some buttons and I want to be able to toggle the visibility of each set of buttons by setting the visibility of the container LinearLayout.
// We iterate over a list and call the following to create the new
// layout assigning an index from a int counter
LinearLayout LL = new LinearLayout(MainActivity.this);
LL.setOrientation(LinearLayout.VERTICAL);
LL.setId(nextId);
LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
LL.setWeightSum(6f);
LL.setLayoutParams(LLParams);
LinearLayout mll=((LinearLayout) findViewById(R.id.menuLayout));
mll.addView(LL);
My problem comes when I try to retrieve these layouts later, for instance to be able to toggle their visibility on/off. I thought I would be able to use
LinearLayout ll = (LinearLayout) findViewById(layoutIndex);
But findViewById() gives me an error when I try to supply an int, it wants a resource ID. Is there an easy way I can convert the ints that I have assigned as the Ids for these layouts to R.id.XXX ids?
Thanks,
Andy
findViewById(id) looks up elements that were included as part of the XML defining a layout.
You will probably have better luck with getChildAt(index), which returns the View at the passed index.
Yes! Find all LinearLayout in a container without using ID.
LinearLayout mll= (LinearLayout) findViewById(R.id.menuLayout);//container
for(int i=0; i<mll.getChildCount(); ++i) {
View nextChild = mll.getChildAt(i);
if (nextChild instanceof LinearLayout ) {
//TODO add your code here nextChild is a LL that you wanna find
}
}

How to add view created programatically on to relativelayout in xml and below a specific component

I am having a RelativLayout in xml file with several components. Now one view created programatically should be added below specific component in activity view.
To be precise,
XML having RelativeLayout with one LinearLayout.
Programatically created view in java code needs to be added below this LinearLayout. How to achieve this requirement.Thanks in advance.
create another linearlayout in xml below first layout. and add your component in that layout
Please try this, may be help you
I use imageView but you add any other view that you required.
LinearLayout view = (LinearLayout) findViewById(R.id.layout);
ImageView myImage = new ImageView(this);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(105, 105);
parms.setMargins(5, 5, 5, 5);
myImage.setLayoutParams(parms);
myImage.setBackgroundColor(Color.TRANSPARENT);
myImage.setImageBitmap(bitmap);
view.addView(myImage);
When you add the new view, you can add a new rule to it
View foo = new View();
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.your_view);
foo.setLayoutParams(p);

Android place Relative Layout below Relative Layout programmatically

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());

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