Reading Android attributes on my custom view - android

I have created a custom layout class (extends RelativeLayout) and have a TextView as part of the layout.
I want to apply the properties declared in XML to my TextView, is there anyway I can read the android attributes (not my custom attributes, that part is already taken care of).
For example in my XML I'll have this:
<my.custom.MyLayout
android:layout_width="100dp"
android:layout_height="20dp"
android:text="SomeText" />
I want to read the text attribute and apply it to my TextView (currently it is being applied to the RelativeLayout) instead of creating my own attribute and reading it.
My custom layout is something like this :
public class MyLayout extends RelativeLayout {
private TextView textView;
public void MyLayout(final Context context, final AttributeSet attrs) {
/**Read android attributes and apply it to TextView **/
??
}
My current solution is creating custom attributes and reading them, but I feel that is not a good solution as I'll be duplicating every attribute declared to TextView.
More info about my current solution.
I have a custom attribute called myText which I use to apply the text declared in XML to my TextView.
In my layout XML :
myNameSpace:myText="SomeText"
And read it in my Java class :
String text= a.getString(R.styleable.MyStyleable_myText);
textView.setText(text);
I'm looking to get rid of my custom attributes and read "android:" attributes.

First of all I am not sure if setting android:text will be possible for view extending RelativeLayout. If it is possible do it like in TextView implementation:
final Resources.Theme theme = context.getTheme();
a = theme.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView, defStyleAttr, defStyleRes);
text = a.getText(attr);
So basically it's the same way you get your custom properties but with different styleable.
If it's not working I would consider another approach. I did it for one of my custom views. Since you have a TextView in your custom view you could create it in XML and then get a reference to that child inside your custom view.
<YourCustomView>
<TextView android:text="someText"/>
<YourCustomView/>

Related

How to render custom view in designer?

I have created a simple custom TextView. The layout designer however, won't let me preview those custom TextViews, only MockView-Blocks are being rendered.
public class MainTextView : TextView
{
public Typeface typeface;
public MainTextView (Context context) : base(context, null)
{
InitializeView(context);
}
...
}
Is how my class goes. I would like to preview my custom TextViews, but how can I do that?
If you are in the layout file, you can use:
<(insert your namespace for MainTextView here).MainTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world" />
That should work, you just have to put the namespace that your textview is a part of.
I would also like to note that I've had issues with Visual Studio failing to render my views no matter what I do, so in those situations there's not much you can do.

Apply style to every child view

I have the following pseudo layout:
<RelativeLayout>
<ACustomView />
<AnotherCustomView />
</RelativeLayout>
I have TextViews in both of the custom views (inflated from XML). How can i set
style properties (eg. textColor) for every TextView in the first custom view? For example i want every TextView to be red which are in the first custom view.
I don't think that this is possible.
If you don't want to much typing you can define your own style and add it to each textview or you can create your own textview class where style attributes are set.
For an example use this question/answer:
Setting global styles for Views in Android
I hope i could help you
Cu
JackZ

What's the "Android way" to dynamically create Views with special styles/properties?

As mentioned in topic, I have some Views, e.g. a TableRow with always the same background used as topic, or a special TableRow containing a TextView with some special styles/properties. These Views are set dynamically, so it's problematic to use a XML for this. As I read it's not possible to set styles programmatically too. So what's the best way to solve that?
Possibility 1:
I use and instance derived Views, like this:
public class TopicTableRow extends TableRow {
public TopicTableRow(Context context) {
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setBackgroundColor(Color.parseColor("#777777"));
setClickable(false);
}
}
Possibility 2:
I could create a valid xml template with a special layout I never use in the application, containing the needed Views which have already all assigned styles. Afterward I access the needed Views by R.id....
But this method seems to be very dilettante to me.
I don't think that those 2 possibilities are the "real" Android way to do this, so how is this usually done?
If you want to set specific styles for groups of elements, you can use the themes and styles concepts in android.
You can read up on them here: http://developer.android.com/guide/topics/ui/themes.html
It is not possible though to change the style attribute of a view programatically.
Therefore the android way is probably to create the Views you need in XML and use a LayoutInflater to get create an 'java' version of the xml view. This allows you to reuse the component and fill it with apropriate data for as many rows as you would like.
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.textViewFromWeb, null);
I hope this will be of use to you!

Connection between View in the xml file and extened View class

Is there a way to link inherit class to xml file.
I am trying to connect extended class to widget in the xml file.
Is it possible ?
Thanks in advance.
You must have noticed that all the nodes that we specify in the layout XMLs are actually either View classes(for e.g: TextView, EditView) or view containers/layout managers(e.g: LinearLayout, RelativeLayout etc.). Android allows you to create custom views and containers by extending the View class and one of the layout managers, respectively. You can then choose to inflate such views directly from code or specify them as nodes in your layout XMLs.
For instance, assuming you create a View class such as:
public class com.views.MyView extends View{}
then you can include this class directly in you layout XML by saying:
<LinearLayout ..>
<com.views.MyView .. />
</LinearLayout>
Note that when you specify your View class directly in XML there are a few important subtleties to consider such as:
When inflating the custom view, the framework will call different constructor of the view. The arguments would be a context object and AttributeSet(containing attributes you set in the XML).
For more details refer this

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