Android, Reusing Not Layouts, But Views via XML - android

I have a button I use a lot in one of my layouts. Or rather, I have like ten buttons all with the same text color, background color, text size, width, and height. I don't want to define all of these parameters for each button. So I want to be able to write something like...
<include view="#layout/standard_button"
android:id="#+id/button-id"
android:text="button-specific-text"/>
But of course, there is no include view="", there is only include layout, and include layout the xml file as a Layout, not a View, so setting the text is not possible and when I findViewById() in my activity, it would refer to a Layout and not a View.
Is there something like <include view="... ?

You can create a layout with your customized style button that you want to use multiple times, and wrap it in a <merge> tag like below sample.
And then you can reuse it over and over again using the include tag in any other layout.
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</merge>
Usage within any other layout, assuming the top layout named as my_button.xml
<include layout="#layout/my_button" />

Related

How do I avoid repetitive TextView code?

I want to include the below layout in a main layout file, at multiple points, but at each usage, I want to change ONLY the "android:text" attribute of the text view inside the relative layout (as seen below). How can I achieve that?
P.S. I know how to include it in the main layout. This includes the relative layout (as seen below), but the main purpose of creating another layout file is because the code (of the textview) is being repeatedly used in the main layout, and the only attribute that differs is "android:text" between these repeated text views.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/order_id_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:fontFamily="sans-serif-light"
android:padding="20dp"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
in your another Layout file you can use this .
<include layout="#layout/main_layout"/>
And From your activity class you can set text by this.
TextView tv = (TextView) findViewById(R.id.order_id_label)
tv.setText("New Text");
This is the only way you can do this .
If all TextView element arguments are the same you could define this component in a separate file using <merge> </merge> directive and then <include layout="" />
Check here how to reuse
But if any of the TextView argument is changing, i.e. android:text attribute, the best way is to separate all other TextView attributes to custom style and reuse this custom style in different xml layout files
Check here how to use styles

ViewGroup - Add XML content into different container within inflated ViewGroup

Is it possible to inflate a LinearLayout from XML that contains some static objects and another LinearLayout and later when "XML" code is used inside the LinearLayout it's content is being added inside the inner LinearLayout.
Explanation with some code removed:
<LinearLayout id="main">
<LinearLayout id="top">
<TextView text="This is always here" />
<ImageView src="#drawable/image_alwayshere" />
</LinearLayout>
<LinearLayout id="bottom">
</LinearLayout>
</LinearLayout>
This is then inflated by my View "CustomLinearLayout" or whatever we call it
and when using the View in another Layout:
<com.my.views.CustomLinearLayout>
<ImageButton id="button1" src="#drawable/button1" />
</com.my.views.CustomLinearLayout>
In this case, the ImageButton should not be added below "bottom" it should be added into it. SO whatever I have in top, stays static and whatever I want to change is added to the Bottom LinearLayout.
Is this possible and if it is how could it be done?
Not sure if it's good or bad practice, if it would work. But if I have a constant layout (top container, middle container and bottom container) and I have 10 different activities and the only one changing content is the middle one, I can easily make one change to the top and bottom container at one place instead of 10 places and have whatever "View" I want to show in my activity be added inside.
Maybe I need to create a whole new ViewGroup for this? But currently working on LinearLayout since it's functionality is pretty much what I need.
If not, then what I'm looking for is where and when a LinearLayout reads the content of the XML and then override that to be added to my inner LinearLayout instead.
It is possible, you can either use include tag to add the other xml layout into the "bottom" layout and make its visibility as "gone" and change it to "visible" when you need it.
Or you can do that dynamically and inflate the xml whenever you want then add it to "bottom" using ViewGroup.addView(View) method.

Android: How do you define buttons and other widgets outside of a layout?

So generally you define an XML layout with a single root node and all the buttons and such contained inside. But if you want to just define buttons somewhere so you can reference their IDs later on to move them around, where/how do you do that?
I'm asking this question because I'm trying to use the "android:foreground" field in the FrameLayout. So I want to point this to the ID of a button (or other widget) that I've defined. But where do I define this button (or other widget)? If I just defined it normally wouldn't it end up appearing somewhere where I don't want it to?
android:foreground adds a drawable over the layout as an overlay. you put the drawable in /res/drawable
As for the buttons, you can always code them in the java
Just define your button in my_button.xml file in res/layout folder with any attributes:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_margin="2dp"
android:layout_gravity="center"
...
style="#style/MyStyle" />
And then inflate it when needed:
getLayoutInflater().inflate(R.layout.my_button, null);

Android layout <include /> tag

i want to use include tag in my xml layout to recycle a common layout part.
Android documentation says i can modify some attribute of imported layout, for example
<include layout="#layout/topbar" />
Include topbar layout without specify width and height.
<include layout="#layout/topbar" android:layout_width="fill_parent" />
Include topbar layout with specific width.
So, in my topbar layout, i've a textview without text, can i set textview text from include tag like below?
<include layout="#layout/topbar" android:text="Hello world" />
Thank you.
No, you'll need to do this in code. In your activity, call findViewById() to find the TextView, then call TextView.setText() to set the text.

Is is possible to view two xml at the same time in Android?

Is is possible to view two xml at the same time in Android? Suppose xml1 contains an ImageView and xml2 contains another ImageView. Likes to view both images at the same time sharing half of the total window size
Try using the include feature: http://developer.android.com/resources/articles/layout-tricks-reuse.html
Here's an example that might work for you (make sure you put these inside of a root layout tag, like LinearLayout, as if they were any other View items):
<include android:id="#+id/image1" layout="#layout/xml1" />
<include android:id="#+id/image2" layout="#layout/xml2" />
The ids can be whatever you want, but the layout must match the name of the separate XML file in your project.
As far as making each one take up exactly half the screen at the same time, you'll want to try setting the weight for each one to the same value. See the example here: Assign width to half available screen width declaratively
So like this:
<include android:id="#+id/image1" layout="#layout/xml1" android:layout_weight="1" />
<include android:id="#+id/image2" layout="#layout/xml2" android:layout_weight="1" />
You might also need to add android:layout_height or layout_width (depending on whether your layout is vertical or horizontal) set to fill_parent to get your desired effect.
we can see both xml files in one window .But at a time we modified only one xml file only,
Use the below poperty in Activity tag in manifest file
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"

Categories

Resources