Can I create a statich part of view - android

I`m wondering about one thing. If I decide to have a part of my View same in every Intent. For example 2 buttons at the bottom of screen and for example I have 3 diffrent views, List, Detail and a third one :) Do I need to put the buttons on every xml schem for each view or can I create other xml and attach it in each activity with all listener etc.
If I can attach in activity how can I do that ?

Create a XML buttons.xml with the common elements
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="#+id/myButton"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
<ImageButton
android:id="#+id/myButton2"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
</merge>
Include it in another xml:
<include layout="#layout/buttons"/>
where buttons is the name of the xml file to be included

You can use the <include /> tag in your XML files.
See Layout Tricks for an example.

Create a separate layout for your buttons then in the layout you wish to display them use the include tag.
This will allow you to reuse the same layout in multiple parent layouts.

The include works as stated above you could also consider using fragments
Fragments
You can use these in older versions of Android by including the compatibility library in your application. It's definitely more work than a simple include but if you need some reusable UI for more sophisticated features than simple buttons you might want to look at that as well
How to use compatibility API

Related

Can you set a view as a property of another view in an XML layout file?

We have a custom GridView which has headerView and footerView properties. I'm wondering if in Android, it's possible to set those properties from within a layout file.
XAML in Windows lets you do this easily since you can specify properties either via attributes (for things like strings, numbers or other simple types), or via nested elements (for any object type) with a ControlType:PropertyName syntax.
Here's a pseudo-version of what this would look like if Android supported something similar:
<MyCustomGrid
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- This would set the 'headerView' property
on 'MyCustomGrid' to a TextView -->
<MyCustomGrid:headerView>
<TextView android:text="I'm the Header TextView" />
</MyCustomGrid:headerView>
</MyCustomGrid>
Obviously the above is not valid. But is it possible to do something similar in Android, or do I have to do it in the code-behind in the Activity/Fragment?
Yes, you can.
You can create a Custom View by extending the GridView class and add some logic through attributes. This will not work as an attached property from XAML (Like Grid.Column or Grid.Row from XAML UWP) but you can do something like this:
<com.teste.widget.MarqueIVGridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:my_header="#layout/my_header"
/>
Don't forget to add the namespace at the root of your layout:
xmlns:app="http://schemas.android.com/apk/res-auto"
Google has this sample:
HeaderGridView
It uses a different approach, if you copy this class you will just need to use this "HeaderGridView" and call addHeaderView method from it sending your header view inflated.
Feel free to ask any question and It will be a pleasure to answer.

How to group multiple Views without using nested Layouts

Consider this situation:
<LinearLayout style="#style/Basic_W_mp_H_wc_Vertical">
<!-- IMAGE-1 -->
<TextView style="#style/Copyright_TextView_1" />
<ImageView style="#style/Copyright_Image_1" />
<include layout="#layout/layout_copyright_info"/>
<View style="#style/HorizontalSeparatorGray"/>
<!-- IMAGE-2 -->
<TextView style="#style/Copyright_TextView_2" />
<ImageView style="#style/Copyright_Image_2" />
<include layout="#layout/layout_copyright_info"/>
<View style="#style/HorizontalSeparatorGray"/>
<!-- IMAGE-X -->
[...]
</LinearLayout>
Now I want to hide copyright information for IMAGE-1. I have two options:
Create a wrap-layout (LinearLayout) with an Id, so I can find and hide it
Give Id for every View find and hide them.
The first option will work, but I don't want to create an extra nested layout for this.
The second option will also work, but my layout will get unreadable if every view has an Id.
I know there is a third memory-optimized option to group multiple views, but I can't remember how it works and google search gives me always ViewGroup...
Any idea?
You could try to use what's called the merge tag and pair it with the include tag. What this does is it will allow you to use the same layouts in different files (since you seem to have multiple groups together). The <merge/> tag will merge the elements in to the top-level layout (in this case a LinearLayout). I have not used it with this scenario, so I imagine what will happen is you will not be able to retrieve them in a group. It is worth a shot though.
I think option 1 is not a bad choice either for these reasons:
It's easier to code. If you don't group them together than you'll have an extra lines and remember extra IDs. With just one Layout wrapped around it, then you can just hide one layout.
It's easier to maintain. What happens if you change the copyright look? What happens if you want to change the rest of the layout? With one wrapped, it's easier to just fiddle around with one view and everything inside stays intact.
The performance hit isn't that bad. Overall, this looks like the top-most root view. Inflating this Layout won't be as bad as if you were say, inflating a ListView item. This is a one-and-done operation and that's it. You don't want to fall in to the trap of doing premature optimization where you sacrifice code maintainability in favor of optimizations without any benefit.

Change several layouts for different densities at the same time

If I have three different layouts for a same view (lets say, one for phone, one for tablet and one for tablet in landscape) and I want to add/change a property that the three of them share (lets say, the android:background), is there a way of adding/changing it in one place and have it automatically in the other two layouts? Is there an alternative to copy-and-paste it?
Unfortunately not really. You could have separate layouts for each set of elements and use include to include them in each subsequent layout, then just updating the single layout would update all of them. But this would mean having a lot of stand alone layouts. A good example of this is using a seperate Toolbar layout like so:
Toolbar layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
style="#style/Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Then have this line in all layouts which you want to use Toolbar.
<include layout="#layout/toolbar" />
This technique could be applied to anything you like, and would allow you to in future only edit each separate layout once and have it applied everywhere you use it. You can also edit dimensions inside these Layouts under the include, or in code.
The only other way to achieve what you want would be to handle it all explicitly in code but I wouldn't recommend this.
You can apply style for the widget(TextView or LinearLayout for example) that uses background property and determine background property in that style.
Changing background property in style will affect all the widgets this style was previously applied to.
http://developer.android.com/guide/topics/ui/themes.html

List event handlers for an Android control

I am new to Android development and Eclipse. I have been coding on ASP.Net and MS Visual Web Developer for years. In VWD, when you add a control to the design view, double clicking on it will automatically bring you to code view for the OnClick function of the control you have just created. You can also see the list of possible event handlers for a control from the design view.
But I can't seem to find this feature in Eclipse. Is there such a thing? I did a search on Google and the best I found is this (same question but without an answer).
http://www.techrepublic.com/forum/questions/101-341077/event-handlers-in-eclipse
Anyone to advice please?
Thanks!
What you're talking doesn't quite exist in Eclipse. You'll have to manually open your java class and add the method to the corresponding java activity there.
For example, if you set the android:onClick XML attribute to "myAwesomeMethod" in your layout XML file, in the corresponding Activity that uses that layout, you'll need to make sure you have a "myAwesomeMethod" method defined.
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myAwesomeMethod" />
<!-- even more layout elements -->
In your java Activity class:
public void myAwesomeMethod(View v) {
// does something very awesome
}
Note: you can also do this programmatically, which is what I generally do. However, defining the android:onClick method will save you a few lines of code.
For more information, check out this post. It gives a lot more detail on how to assign onClick handlers to a button and the two ways you can do so.
No, that is not how Eclipse works. You add the control in the xml file, then in the activity that you are going to load that layout in you add the onClickListener on the element you want to respond to clicks for,

How to add header to Activity in Android

How to add a header to an existing android activity? All the examples that I found with a search engine are showing how to add a header to the list view.
I don't know what you are looking for..might be this answer is useful to you.
You have to add a view the xml that you're using for your layout. So for example, say your Activity is using a linear layout. You'd do:
<LinearLayout android:orientation="vertical" ....>
<LinearLayout....>
<!---- Header Stuff Goes Here ---->
</LinearLayout>
<!--- Rest of the layout for your activity goes here ---->
</LinearLayout>
What you are looking for is a ActionBar, it is included on 3.0 api but if you want to use with backward compatibility you can use different libraries, my preferit is GreenDroid you can also see an example downloading the GreenDroid Catalog App from Market.
I hope this helped you :)

Categories

Resources