How to use custom Android ViewGroup in XML? - android

I have a class that extends FrameLayout called NoteView.
It is a top level public class.
It works just fine when I'm creating the UI from java code, but I can't figure out how to use it in XML. I tried inserting the fully qualified class name just like I would for a custom view, but I get and exception ClassCastException: android.view.View cannot be cast to android.view.ViewGroup when activity tries to inflate the XML.
I have only been able to find tutorials on Custom Views, not custom viewgroups. What do I need to do to use a NoteView just like I would a FrameLayout in an XML Layout?
For example: This works
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<test.cjb.sandbox.NoteView
android:id="#+id/noteview"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#FF0000"
android:layout_gravity="right">
</test.cjb.sandbox.NoteView>
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:text="Sibling Button"/>
</LinearLayout>
But this throws the above exception:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<test.cjb.sandbox.NoteView
android:id="#+id/noteview"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#FF0000"
android:layout_gravity="right">
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:text="Child Button"/>
</test.cjb.sandbox.NoteView>
</LinearLayout>

In the future, when complaining about exceptions, please consider including the full stack trace, particularly when people ask for the full stack trace, as the full stack trace may contain information that is relevant to giving you an answer. You can get the full stack trace via adb logcat, DDMS, or the DDMS perspective in Eclipse.
Lacking that, all I can do is point you to this sample project that extends a LinearLayout into a custom widget.

Related

How to add bindings to MvxSpinner inside MvxDropdownItemTemplate and MvxItemTemplate

I pulled this section of code from: https://github.com/slodge/MvvmCross-Tutorials/blob/master/MoreControls/MoreControls.Droid/Resources/Layout/FirstView.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxSpinner
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
local:MvxBind="ItemsSource Shapes; SelectedItem Current" />
<!--
could be customised using
local:MvxDropDownItemTemplate="#layout/spinner_dropdownitem_shape"
local:MvxItemTemplate="#layout/spinner_item_shape"
-->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
local:MvxBind="Text Current" />
<MC.ShapeView
android:layout_width="fill_parent"
android:layout_height="300dp"
local:MvxBind="Shape Current" />
</LinearLayout>
The comment mentions it is possible to customize the look by passing MvxDropDownItemTemplate and MvxItemTemplate a layout. Let's assume we are going to use this simple layout for both templates:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Project.Ui.Droid"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
local:MvxBind="Text ???????" />
</FrameLayout>
The MvxSpinner displays the enumerated name (Circle, Square, Triangle) as the text for each item. Without changing the view model, is it possible to create a binding, in the template, that displays the same thing the MvxSpinner displays by default when no template is used? If so, what would that binding look like? If that's not possible, what is the best way to accomplish this outcome?
in the template, that displays the same thing the MvxSpinner displays by default when no template is used? If so, what would that binding look like? If that's not possible, what is the best way to accomplish this outcome?
The default template uses ToString() on the object.
To achieve the same thing, you can use the Whole Object binding. From the wiki, you should be able to specify using an empty Path or a single Period for the Path. Since we've seen some issues reported recently with empty Path binding, I recommend using a Period:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Project.Ui.Droid"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
local:MvxBind="Text ." />
</FrameLayout>

Start a activity like a "Dialog", by writing java code

I want to make a activity like a "Dialog", and I know two ways so far:
Way 1) In Android ApiDemos, it is implemented by adding the attribute to the activity like
android:theme="#android:style/Theme.Dialog">
The result is: the new Activity appears on top of the existing activity, that is what I want.
Way 2) I try to invoke setTheme(android.R.style.Theme_Dialog) in the Activity.onCreate(Bundle) method, and the new activity also appears, but the background is all black. This is not what I want. Code is as below:
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Dialog);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.selecte_date);
Can anybody tell me how to implement the effect by writing Java code?
The easiest way is to add a second layout in you xml which is a overlay of existing one.
then you can set the overlay to visible in your activity. So then you got multiple views in 1 activity.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="#android:color/transparent">
<ImageView
android:id="#+id/icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout
android:layout_alignParentTop="true"
android:id="#+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/translucent_black"
android:visibility="false" >
</LinearLayout>
</RelativeLayout>

Using Custom AutoCompleteTextView in Android Causes Error

I extended AutoCompleteTextView in android. When I use that new custom AutoCompleteTextView in my XML layout file, Eclipse shows an error saying:
You must specify a valid content view by calling setContentView() before attempting to show the popup. Exception details are logged in Window > Show View > Error Log"`.
Edited XML File is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.mypackage.MyAutoCompleteTextView
android:id="#+id/countries_list_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionHint="Test">
<requestFocus />
</com.mypackage.MyAutoCompleteTextView>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
By simply taking out my custom view from the XML, the layout shows just fine.
Any help will be greatly appreciated, thanks.

Android - Layout error

I'm trying to create a button. I've done something like this before, but now I got an error and I' can't find out what is it. I think i've got the problem in the xml file, but can't find it :S
So there is my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="115dp"
android:layout_marginTop="80dp"
android:src="#drawable/login_button" />
</RelativeLayout>
I'm trying to implemet some kind of facebook login. I'm using the easyfacebook sdk. So here is the java code where i trying to implement the login button:
http://pastebin.com/brHmJKs1
The messages i get is the following:
W/webcore(29941): Can't get the viewWidth after the first layout
E/SkLayout_wtle(29941): layout error:106 width:0 ellipsizedWidth:0
W/InputManagerService(1171): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#405c4e68
First close your first LinearLayout in last line.Secondly remove RelativeLayout, that is of no use and then try i hope you will get success.

Accessing Android views on nested layouts

I have trouble accessing Views from a layout that is included in another layout.
Please take a look at this picture:
http://dl.dropbox.com/u/3473245/layout_includes.png
How do I access the 4 text views programmatically?
Its probably something really simple that I'm missing.
Thank you very much!
You can do as follows:
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include android:id="#+id/item_base_lang" layout="#layout/dictionary_list_item" />
<include android:id="#+id/item_learn_lang" layout="#layout/dictionary_list_item" />
</LinearLayout>
dictionary_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/dictionary_list_item_text_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/dictionary_list_item_text_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
To set the text programmatically:
((TextView)findViewById(R.id.item_base_lang).findViewById(R.id.dictionary_list_item_text_header)).setText("item_base_lang_header");
((TextView)findViewById(R.id.item_base_lang).findViewById(R.id.dictionary_list_item_text_content)).setText("item_base_lang_content");
((TextView)findViewById(R.id.item_learn_lang).findViewById(R.id.dictionary_list_item_text_header)).setText("item_learn_lang_header");
((TextView)findViewById(R.id.item_learn_lang).findViewById(R.id.dictionary_list_item_text_content)).setText("item_learn_lang_content");
This Android wiki page shows how to use reusable UI components with XML layouts, but it doesn't show how to access nested reusable components from code.
Although it is fairly straightforward, it might be not so clear for those who are pretty new to Android Views.
The following two lines should help you get the languageHeader of both includes. You can do the same for languageText
findViewByid(R.id.activityBaseLangView).findViewById(R.id.languageHeader)
findViewByid(R.id.activityLearnLangView).findViewById(R.id.languageHeader)

Categories

Resources