How can I preview a custom android component layout? - android

I've created a custom view which extends RelativeLayout, and I want to preview how it looks in the designer.
The java is something like this:
// The view for a snap in the search/browse fragment.
public class MyView extends RelativeLayout
{
public MyView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.the_layout, this);
}
public void setData(String text)
{
mText.setText(text);
}
// *** Views ***
private TextView mText;
#Override
protected void onFinishInflate()
{
super.onFinishInflate();
mText = (TextView)findViewById(R.id.text);
}
}
And the XML is like this:
<?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/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- (And lots more views) -->
</RelativeLayout>
There are a few problems with this however:
This actually creates a RelativeLayout within a RelativeLayout which is pointless. It can be solved by changing the XML <RelativeLayout> to a <merge> but then I can't preview the layout at all!
I want to use the isInEditor() function (or whatever it is called) in the java to add some sample data for previewing purposes, but I can't find a way to tell the XML editor that it should display a preview of my class instead of the actual XML.
One unsatisfying solution I can think of is to create an empty preview.xml file with only <com.foo.bar.MyView/> in it... But that seems kind of silly. Is there a better way? (I don't really care about editing as much as previewing, since - let's face it - Eclipse/ADT are way too slow and flaky to make graphical layout editing usable.)

If I understand your problem correctly I would say that the solution is to just replace the "RelativeLayout" tags (or any other tag for that matter) in your xml layout with "your.packagename.MyView" as in:
<your.packagename.MyView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- (And lots more views) -->
</your.packagename.MyView>
If you'll get any exceptions regarding MyView class while running your app, add all the missing super/parent constructors.
I do this for almost all of my custom xml layouts. Extending your class with RelativeLayout, LinearLayout or any other GUI class also gives you great controll over how your GUI should behave (because you can also override parent methods etc.).

Related

How to make a custom and compound objectin android?

After a lot search I found a great article that describes how to make compound objects. The recipe works great, Eclipse shows it in the custom made widgets. Fantastic.
http://mobile.cs.fsu.edu/creating-a-simple-compound-component-in-android/
Just one problem, I need a few of them. When I added a few into a layout, the resource id for the enclosing layer/object bumps as expected; that is, the new object receives new ids auomagically. The issue is the components inside the main layout do not; so if I need to use finViewById to get the view for internal components, they all have the same id. Does anyone know how to automagically assign different id for the internal components of the complex widget? I would try to assign them manually, but Eclipse does not show the internal structure of a compound object; so no help there.
-- in response to Xaver answer
Yes, at least I think I did. If I follow the example in the link I have something alike; now I see what you are saying, I got the views as just by adding findviewbyId to custom class, as this
public class HorizontalNumberPicker extends LinearLayout {
public HorizontalNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.horizontal_number_picker, this);
View v = findViewbyID(R.id.btn_minus); //
}
}
What I cannot understand and I most likely have wrong is how to hook my code to two objects, that is following the example in the link
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<example.example.HorizontalNumberPicker
android:id ="#+id/horizontal_number_picker"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content" />
<example.example.HorizontalNumberPicker2
android:id ="#+id/horizontal_number_picker"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content" />
</FrameLayout>
How does HorizontalNumberPicker construtor know is linked to object horizontal_number_picker or horizontal_number_picker2?
if in in my activity I add
HorizontalNumberPicker obj1 = HorizontalNumberPicker (this,null);
what links to horizontal_number_picker or horizontal_number_picker2?
By the way how do I pass the Attributeset?

How to inflate xml as Custom View without a parent

I have a simple xml which I want to inflate as a java view object.
I know how to inflate a view:
view = LayoutInflater.from(context).inflate(R.layout.alarm_handling, this);
But then I have a view which is a child of the parent (this). And then the messy problem starts with setting layoutparameters and having an extra layout which I do not need. These are much easier to do in xml.
With an Activity one can just call: setContentView() but with a View that is not possible.
In the end I would like to have a Java class (extends ViewSomething) which I can refer to in an other xml. I have looked at ViewStub, which almost is the answer, except that it is final :(
public class AlarmView extends ViewStub{ //could work if ViewStub wasn't final
public AlarmView (Context context, AttributeSet attrs) {
super(context);
//using methods from ViewStub:
setLayoutResource(R.layout.alarm_handling);
inflate();
}
}
So how to to this? What to extend to be able to just call setContentView() or setLayoutResource()?
I looked at many SO answers but none of them fit my question.
as far as I understood the trick that you want to apply it's a bit different than what you trying to.
No ViewStub are not the solution as ViewStub have a very different way of handling everything.
Let's say for the sake of the example your XML layout is something like this (incomplete, just to show the idea):
<FrameLayout match_parent, match_parent>
<ImageView src="Myimage", wrap_content, Gravity.LEFT/>
<TextView text="hello world", wrap_content, Gravity.CENTER_HORIZONTAL/>
</FrameLayout>
then you don't want to extend FrameLayout and inflate this XML inside it, because then you'll have two FrameLayouts (one inside the other) and that's just a stupid waste of memory and processing time. I agree, it is.
But then the trick is to use the merge on your XML.
<merge match_parent, match_parent>
<ImageView src="Myimage", wrap_content, Gravity.LEFT/>
<TextView text="hello world", wrap_content, Gravity.CENTER_HORIZONTAL/>
</merge>
and inflate as normal on your widget that extends FrameLayout
public class MyWidget extends FrameLayout
// and then on your initialisation / construction
LayoutInflater.from(context).inflate(R.layout.alarm_handling, this);
and your final layout on screen will only have 1 FrameLayout.
happy coding!

Custom ImageView crashes program

I am making a small app for Android, where I have a RelativeLayout, which amongst other things contains a custom ImageView. In my Java code I have this class:
package com.example.android.helloactivity;
class ArrowImageView extends ImageView {
public ArrowImageView(Context context) {
super(context);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(10,10,10,null);
}
}
Then in my RelativeLayout xml I have the following:
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFF"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button ...... />
<TextView ......./>
<com.example.android.helloactivity.ArrowImageView
android:id="#+id/hello_activity_bearingarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
When I run my main class (not shown here) then my program crashes. If I omit the xml reference to ArrowImageView, then it does not crash.
Am I referring to my custom class te wrong way, or what is going on?
When extending the View widgets, if you plan to use them in XML layouts you need to also override the constructors that take the AttributeSet argument.
First don't extend ImageView but just View, unless there is something special about ImageView that you know and want to use.
Second, as Leffel said, you need to state attribute set like this
public ArrowImageView(Context context, AttributeSet set) {
super(context, set);
}
Also you may need to give some size to the custom view by setting witdh and height to something like 100dp. I am not sure what a custom View can "wrap" when there is no other views inside.
Thanks for the answers. I found a bug in the onPaint method itself which still made things crash. After implementing your suggestions and changing the onPaint into:
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.MAGENTA);
canvas.drawCircle(10,10,10,paint);
}
everything worked!
Thank you very much for your help :-)

Android -- SDK/IDE Layout Bug (w/Custom Layouts)?

Can someone tell me if this is a bug in the SDK/IDE:
Any custom or extended layout I add to my layout XML causes the IDE to ignore the fact that there are any child views of that layout (they just disappear from the outline view/window), thus making them uneditable via the properties view/window. (I need to extend a layout to make onSetAlpha() public)
FYI: I'm developing for Android 1.5 and up, using all the latest plug-ins/updates in Eclipse
Here is a simple example of a layout XML and the extended Layout that causes this error.
[Extended Layout]
package com.test;
public class CustomLinearLayout extends LinearLayout
{
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLinearLayout(Context context) {
super(context);
}
}
[Simple layout XML]
<?xml version="1.0" encoding="utf-8"?>
<com.test.CustomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
</com.test.CustomLinearLayout>
ImageView01 is not visible or editable in the properties or outline views/windows.
Thanks.
When you want to edit the properties of view just replace your com.test.CustomLinearLayout
with LinearLayout.
Then you can see the view in properties and can edit the properties. After you finish editing then revert the tag to original one that is from LinearLayout to com.test.CustomLinearLayout

Wrapping a LinearLayout from xml

I'm trying to make some Android view classes (which are just wrappers around layouts defined in an XML file). Is this correct:
public class MyViewWrapper extends LinearLayout {
private TextView mTextView;
public MyViewWrapper(Context context) {
super(context);
}
public constructUI() {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.myview, this);
mTextView = (TextView)findViewById(R.id.myview_textview);
}
}
so the idea is just that I can construct my views like that, and they have logic inside for modifying their child views etc. The layout looks like:
<LinearLayout>
<TextView />
</LinearLayout>
It just looks like I'm going to get an extra unnecessary LinearLayout. The wrapper class is itself a LinearLayout, and then it will attach the inner LinearLayout from the xml file.
Is that ok?
Thanks
You can try replacing the <LinearLayout> in your layout file with <merge>. I have not tried that recently, and I think I ran into problems when I last tried it, but in theory it should serve the purpose. <merge> basically means "take all my children and put them directly into whatever container I'm being inflated into".

Categories

Resources