Android - Add dynamic xml to layout - android

I want to merge between layout file and xml file.
But, my problem, is how can I do it dynamically.
I mean that I have general toolbar file that contains: my app icon, the activity name and linearlayout space for button. I want to merge this toolbar to each activity, so that all activity could put its own buttons in the linearlayout in the toolbar.
I have try to do that, I wrote toolbar file that called toolbar.xml in the layout folder.
And each activity included this toobar like that: <include layout="#layout/toolbar.xml" />, but I do not know how to insert the buttons.
Can I do that with the xml of the activity only?

To add buttons from code (which I guess is what you want to do) you just need to have a container where your buttons shall be inserted into. You can just add <LinearLayout> to your layout XML file (ensure your layout got id, i.e.:
<LinearLayout>
...
android:id="#+id/button_container"`
</LinearLayout>
Them, in your code create your button as any other object:
Button myButton = new Button( mContext );
then find your button container:
LinearLayout buttonContainer = findViewById(R.id.button_container);
and add your button to it:
buttonContainer.addView( myButton );
To find out more see ViewGroup documentation

Related

Include template layout and add components on it

I defined a layout and want to use it as template for my other activitys (same background color, distance to margin etc.). So i`m trying to include it by
<include layout="#layout/template">
That does work. But i want to place buttons on the included layout like this
<include layout="#layout/template"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/applyLeave"
android:layout_alignTop="#+id/upperEdge"
style="#style/buttons"/>
</include>
(e.g. #+id/upperEdge is part of the included layout and i want to set the button below the included component)
Is it possible and how do i do this?
I think it's not possible to add Views by XML on include layout.
You can add views, like buttons, after TAG. Also, if you want to add views dynamically, you can use Java code and add views here.
Here a sample code:
//Get your object layout by ID
LinearLayout myLayout = (LinearLayout)findViewById(R.id.your_include_layout);
//Create a new button
Button mButton = new Button();
mButton.setText("This is a dynamic button");
//Add button to the layout
myLayout.addView(mButton);
I hope this help you.

Android studio - new Activity with Fragment (XML files explanation)

I want to create a new Activity in Android Studio with MainActivity as a parent. Although, when I create a blank activity (ex. NewActivity) with a fragment added I get the two expected classes
(NewActivity and NewActivityFragment) but when I check the layout resources, I can't understand why there are 3 XML files auto-generated and what's their meaning?
What is the exact meaning of each XML-file generated ? The 3 XML files are the following : activity_new.xml, fragment_new.xml and content_new.xml
For example , if i want to add a TextView in the second activity, which XML file shall i modify?
When you create a New activity with a Fragment using the wizard in Android Studio, it will generate two src files :
NewActivity.java
NewActivityFragment.java
and three res files:
activity_new.xml
content_new.xml
fragment_new.xml
The details goes as below:
The activity class NewActivity.java inflates the layout activity_new.xml in onCreate() method as below.
setContentView(R.layout.activity_new);
This layout is a CoordinatorLayout and contains the Appbarlayout, FAB and the container for your main comonents.
activity_new.xml includes another layout using include tag.
content_new.xml is a fragment xml file and contains the attributes to define its layouts.
observer that content_new.xml has an attribute as below
tools:layout="#layout/fragment_new"
fragment_new.xml is the layout which gets inflated in NewActivityFragment onCreateView() method.
inflater.inflate(R.layout.fragment_new, container, false);
fragment_new.xml is the layout file that is the place where you go for adding the components to be shown in the fragment. So as per your requirement of adding a TextView in the fragment you need to add it in fragment_new.xml.
If you check activity_new.xml, you will see an <include> tag calling the content_new.xml.

How can i add a customized footer layout in a activity layout

Hello Community i am new and need your expert opinions.
i have custom footer.xml layout with some buttons on it , i want to add this xml to my activity layout, and i also want to implement click listner for button. Need your guidance.enter image description here
i am able to include footer through
<include layout="#layout/footer" />
it includes the footer layout in activity layout, but there are some buttons i also want to implement action for them.is there any way ?
You can use this on your button definition (inside the footer layout)
android:onclick="clickMethodName"
Just ensure that any class that uses that layout implements 'clickMethodName'.
Alternatively, you can set the onClickListener in your class, after finding the button by id.
See the official documentation for more details and examples, right at the top:
http://developer.android.com/reference/android/widget/Button.html

how to add linearlayout inside of linearlayout when application running

I want to know, there is a linearlayout and use that with setContentView function. Also there is a spinner inside of linearlayout. What I want to do is, create a new layout inside of /res/layout folder and add it into layout that I set with setContentView.
Is there anyway or I need to do that programmatically?
EDIT:
I think I couldn't tell.
I have 2 two layouts(ready). I use the first layout with setContentView.For example, there is a buton and if user click that button, I want to add second layout bottom of first layout when application running.
Easiest you do that with include in the xml of your main layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/second" />
</LinearLayout>
It´s also possible to do it programmatically, but this way I think it is more clear.
Edit:
To do this programmatically, put this code in listener of the first button.
RelativeLayout view = (RelativeLayout) findViewById(R.id.RelativeLayout1);
Button b = new Button(getApplicationContext());
b.setText("Click me too!");
view.addView(b);
Instead of creating a button (or whatever you want) you can also inflate a premade layout.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.second, null);
view.addView(v);
I don't think you can change the res folder programmatically. You need to add any layout programmatically only.
Edited:
Get the 2nd layout's instance using findViewById and use setVisibility method to control the layout's visibility.

Newbie: set content view which consists of two parts

I am developing an Android 2.1 app.
I have defined a LinearLayout class:
public class MyTopBar extends LinearLayout {
...
}
Then, I have a layout xml file (content.xml):
<LinearLayout>
...
</LienarLayout>
I have a RootActivity.java , I would like to set MyTopBar as content in this RootActivity.
Then I have MyActivity which extends RootActivity:
public class MyActivity extends RootActivity{
//set xml layout as content here
}
I would like to set the content.xml as content of MyActivity.
As a whole, I would like to use the above way to achieve the layout that MyTopBar should be located on top of the screen always. The other Activities which extend RootActivity will have its content below MyTopBar. How to achieve this??
1 You could add your custom LinearLayout directly to the xml layout of the MyActivity class like this:
<LinearLayout>
<com.full.package.MyTopBar
attributes here like on any other xml views
/>
...
</LinearLayout>
or you could use the include tag to include the layout with the custom view:
<LinearLayout>
<include layout="#layout/xml_file_containing_mytopbar"
/>
...
</LinearLayout>
2 Use :
setContentView(R.layout.other_content);
Have a Layout vacant for the TopBar and add Your Topbar in it by using layout.addView(topbarObject);
Regarding your second question the setContentView can be called only once, as far as I know. You can however have those two xml files inflated using View.inflate(other_content.xml) and added in the parent xml layout whenever you need it. You can removeView() on parent layout and addView() with the new layout file.
Edit:
For the solution of both the question, you can have a parent Layout for eg. like the following:
//Omitting the obvious tags
//parent.xml
<RelativeLayout
android:id="#+id/parentLayout">
<RelativeLayout
android:id="#+id/topLayout">
</RelativeLayout>
<RelativeLayout
android:id="#+id/contentLayout">
</RelativeLayout>
</RelativeLayout>
Now in your code set the parent layout as content view,make an object of your TopBar layout and add it to the topLayout.
setContentView(R.layout.parent);
MyTopBar topBar=new MyTopBar(this);
RelativeLayout toplayout=(RelativeLayout)findViewByid(R.id.topLayout);
topLayout.addView(topBar); //or you can directly add it to the parentLayout, but it won't work for the first question. So better stick to it.
Now inflate the required xml layout. and add it to contentLayout.
RelativeLayout layout=(RelativeLayout)View.inflate(R.layout.content,null);
contentLayout.addView(layout);//Assuming you've done the findViewById on this.
and when you need to show the other content xml, just call the following code.
contentLayout.removeAllView();
RelativeLayout layout2=(RelativeLayout)View.inflate(R.layout.other_content,null);
contentLayout.addView(layout2);

Categories

Resources