Is custom views context always an Activity? - android

I have a custom View that will be inflated from xml and has some custom xml attributes.
This view would setup some things in the ActionBar, if a xml attribute has been set to true.
Therefore I need a reference to the activities action bar.
My question is:
Can I assume that the context passed in the constructor of
class MyView extends View {
public MyView(Context context, AttributeSet attrs, int defStyle){
Activity a = (Activity) context;
}
}
I have tested that with different devices and different android versions, and it seems t that the context is an Activity.
Does anybody know that for sure?

No. If you theme the layout containing your view you will end up with a ContextThemeWrapper.

Related

Can't display custom View in Android Studio Layout Editor

I am struggling to get my Custom View to be displayed in Android Studio's Layout Editor.
My view is a simple subclass of RecyclerView with custom parameters. I use View.isInEditMode() to use the given attrs only when actually running the app:
public class CustomRecyclerView extends RecyclerView {
public CustomRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
if (isInEditMode()) {
return;
}
final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomRecyclerView, 0, 0);
/* ... */
}
However, previewing a layout containing this View results in an empty Preview pane (not even the phone's frame):
If I replace my View by a RecyclerView, the Preview works as expected:
I don't see what I should do here: if the RecyclerView is correctly displayed and all I do in my subclass's constructor is call super() and return if isInEditMode is true, then what could explain my View not being displayed?
What can I do to get my custom View displayed in Android Studio's Layout Editor?
As of today, opening the same project with Android Studio 2.1.2 does successfully display the UI in the Preview Pane. I can only guess this was a bug in the IDE and has been fixed since I asked this question.
For solving this problem use lower
API levels like 26
then rebuild the project.

Reading Android attributes on my custom view

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/>

Android Custom Controls with textviews inside them

I am making a Custom control and I want to add TextViews inside it. How Can I do that ?
Any ideas?
The custom controls will be used to display electronic programming guide(EPG). My application is for google tv and will be used for channels listing and playback and EPG, in EPG screen i will show the time intervals and programs in each intervals , I want to use Textviews for Program names and to give them custom fonts and style.
Any kind of help will be appreciated,
Many thanks,
Here is screen shot of my custo control ? to add the texts styles i want to use textviews. I hope its clear now?
Try to build further upon this example code:
public class CustomView extends LinearLayout {
public CustomView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public CustomView(Context context) {
super(context);
}
public void addTextView(String text) {
TextView tv = new TextView(getContext());
tv.setText(text);
this.addView(tv);
this.invalidate(); //I think this is called implicitly, but just in case.
}
}
However, you have to do thing like margins, scrolling, layout and so on yourself, as your context does not give many clues.
EDIT: with the addition of some context, I suggest you use a TableLayout. Still, you can build that with the given example code.

How can I preview a custom android component layout?

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.).

Compound Controls forwarding State

Does anyone know what the appropriate mechanism is when creating a custom compound control to apply state changes from the container down to all the children? It would seem to me there should be a straightforward way to set up a ViewGroup to forward all state changes (pressed, enabled, etc.) to each child.
For example, if I create a custom widget along the lines of:
public class MyWidget extends RelativeLayout {
private TextView mTitleView, mValueView;
private ImageView mImageView;
public ValueButton(Context context) {
this(context, null);
}
public ValueButton(Context context, AttributeSet attrs) {
super(context, attrs);
//View setup, etc.
}
}
In many cases, there are drawable or color state lists attached to the children that I want to toggle when changes apply to the overall widget as a whole. What do I need to add to a widget such as this so that when, for example, I call MyWidget.setEnabled() or when MyWidget is pressed those state changes filter down the hierarchy?
Thanks!
Add android:duplicateParentState="true" to each child (perhaps iterating through the childs using getChildAt(int index))
http://developer.android.com/reference/android/view/View.html#attr_android:duplicateParentState
Edit: you will need to set it up in the xml
Note: in the current implementation, setting this property to true after the view was added to a ViewGroup might have no effect at all. This property should always be used from XML or set to true before adding this view to a ViewGroup.
Not sure if I'm going to be much help here, but it's an interesting problem for me to think about because I think I'm going to want to do something quite similar.
In terms of cascading state information down to child Views, could one possible approach be to only contain that state information in the parent ViewGroup, and in the child Views make use of getParent() to access that information?

Categories

Resources