Tuesday, 3 September 2013

CustomView (accessing attrs.xml)

package com.example.customviewsecondexampkle;

import android.os.Bundle;

import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


package com.example.customviewsecondexampkle;

import android.content.Context;

import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;

public class ViewV extends TextView
{
    public ViewV(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);

        TypedArray array = ctx.obtainStyledAttributes(attrs, R.styleable.CustomAttrs);
        String text = array.getString(R.styleable.CustomAttrs_xattr);
       
        if(text != null)
        {
          
        }
        array.recycle();
    }

}

activity_main.xml

<LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res/com.example.customviewsecondexampkle"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >

    <com.example.customviewsecondexampkle.ViewV
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           app:xattr="testing"  />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomAttrs">
        <attr name="xattr" format="string" />
         <attr name="labelColor" format="color"/>
   
    </declare-styleable>
</resources>

No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...