Java Question AttributeSet

Semen Matusovskiy

Well-Known Member
Licensed User
Hi, guys --

The constructor requires AttributeSet as parameter.
Due to clear reasons, I don't want to use xml. To describe methods in attributeset constructor is very lazy (hundreds lines).

Does somebody try to convert "xml"-string with attributes to Attributeset ? For example, using XmlPullParser ?

Just now I try to create an analog of following xml:
B4X:
<android.inputmethodservice.KeyboardView
             android:id="@+id/keyboardview"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
             android:layout_centerHorizontal="true"
             android:focusable="true"
             android:focusableInTouchMode="true"
             android:visibility="gone" 
         />

I assembled some fragments, found in Internet --

B4X:
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance ();
    factory.setNamespaceAware(true);     
    XmlPullParser parser = factory.newPullParser ();
    parser.setInput (new StringReader ("<attribute xmlns:android=\"http://schemas.android.com/apk/res/android\" " + 
                                       "android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\" " + 
                                       "android:layout_alignParentBottom=\"true\" " +
                                       "android:layout_centerHorizontal=\"true\" " +
                                       "android:focusable=\"true\" " +
                                       "android:focusableInTouchMode=\"true\" " +
                                       "android:visibility=\"gone\" " +
                                       "/>"));
                     
    int eventType = parser.getEventType();
   
    while (eventType != XmlPullParser.END_DOCUMENT)
        {
        if (eventType == XmlPullParser.START_DOCUMENT)
            {
            BA.Log ("Start document");
              }
        else if (eventType == XmlPullParser.START_TAG)
            {
              BA.Log ("Start tag " + parser.getName ());
              }
        else if (eventType == XmlPullParser.END_TAG)
            {
            BA.Log ("End tag " + parser.getName ());
              }
        else if(eventType == XmlPullParser.TEXT)
            {
            BA.Log ("Text " + parser.getText ());
            }
         eventType = parser.next ();
         }
    BA.Log ("End document");
           
    AttributeSet attributes = Xml.asAttributeSet (parser);

This "works", but when I attempt to execute
B4X:
mKeyboardView = new KeyboardView (this, attributes);

I get an error
Caused by: java.lang.ClassCastException: android.util.XmlPullAttributes cannot be cast to android.content.res.XmlBlock$Parser

Guess, I made a lot of mistakes in Java code. First of all, it's not clear, what exactly I need to pass to StringReader
 
Last edited:
Top