Include template layout and add components on it - android

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.

Related

Better way to design layout for similar content in android

I have designed the red highlighted layout like the picture:
What I have used is LinearLayout with orientation. Here an ImageView and Two Textviews are repeated in four times. But as far my knowledge I designed it using LinearLayout. So I have to write every time the same design code for four times.
Is there any better way to design it so that I have to write it one time instead of four times.
My code for the highlighted portion is [here](https://pastebin.com/C9ZHDaZV).
Create one layout called for example weather_layout.xml and place your ImageView and Two Textviews inside, then just use include four times in your final LinearLayout like this:
<LinearLayout
... >
<include
id="+#/top_left"
layout="#layout/weather_layout"
... />
<LinearLayout/>
And then you can access like this:
LinearLayout topLeft = (LinearLayout) mainView.findViewbyId(R.id.top_left)
ImageView v = (ImageView) topLeft.findViewbyId(R.id.imageView)
Use Grid Layout. That way you won't need to handle the spacing and partitioning yourself. One line of of code will suffice.

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.

Android - Add dynamic xml to layout

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

Displaying popup images on button click

Please refer the image given in the url
http://docs.google.com/Doc?docid=0AQhgDtGvE2HgZGZ6cmtua185M2RneG5nYmNm&hl=en
My query is, How can I display the messages corresponding to the rounded buttons and the table row , when I click on the rounded button with question mark.
I know, I have to use listener for the ? button , but what should I do in listener exactly, such that when I click, it shows those alerts(images) and when I click again, it disappears.
For this UI, I have used Relative layout as was suggested here -- Aligning components at desired positions -- and it worked perfect for me.
So, do I need to change my base layout altogether for accomplishing this?
You can use a FrameLayout as the base for your ui layout and then add an ImageView overlay. For example:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainFrame"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Put your normal layout stuff here -->
</FrameLayout>
Then in your code you can create the ImageView and add it to the MainFrame and it will overlay your UI, like this:
FrameLayout mainFrame = (FrameLayout)findViewById(R.id.MainFrame);
ImageView overlay = new ImageView(this);
overlay.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.overlay));
mainFrame.addView(overlay);
Then later you can call:
mainFrame.removeView(overlay);
to have it go away.

Categories

Resources