its possible to add button to extended view class? I have new class which extends View and want to add new button. How I could solve it?
Ok, I get it. One more question, Its possible to mix activity with onDraw method? I want to create signature activity and add button to save it.
Your custom View needs to extend ViewGroup or another class that extends ViewGroup, such as LinearLayout and RelativeLayout.
One possibilty would be getting the View and adding an Element with
this.addView(.../*new View*/)
Be sure, that the pointer this is the created View.
Is that what you mean?
Related
So this one is probably very simple, but I'm having a bit of trouble figuring it out.
So I have a custom View, which is now running in an activity along with a standard TextView widget.
What I want to know is whether there is a way to update the TextView from my custom View class
Define a listener interface in the custom view. Define a setXxxListener() method in the view. Override the listener in the activity class. From the custom view, call the listener when needed. In the activity's listener implementation, update the TextView.
Such is the Java way.
In general, view classes should not make assumptions about other views running along. The object that manages both views - in your case the activity - should coordinate the data exchange between them.
I've got an activity populated by TwoLineListItems, and I'd like to be able to click them to go to another activity and show a more in-depth view of the information associated with the TwoLineListItem. Do I just use a standard OnClickListener and declare each TwoLineItemListener as clickable?
TwoLineListItems are intended for use in ListViews. In order to implement a clickHandler for a listView you would override the onListItemClick method.
If you are not using a ListView, and your TwoLineListItem is a child of a different layout, I would recommend using a simple Linear or RelativeLayout instead.
I am newbie to android (on mac using eclipse). Please let me know how to create a view dynamically and add it to main view.
Context:
Actually I want to show a view of processing on the main screen when user has performed any action.
Assuming that your main view is a ViewGroup and you already have an instance of it the code would look something like this:
TextView textView = new TextView(getContext());
textView.setText("Processing...");
mainView.addView(textView);
I have an android question:
I've successfully created a countdown kitchen timer activity, however my goal is to have an activity that has 3 timers on it that all work independently. I created a separate layout just for the timer itself and moved the timer code into a class and I've used layoutinflater to create the views and then added them into the linear layout for the activity. I get the layouts fine, however there's no functionality. There doesn't seem to be anything that ties the class code to the activity.
How should I approach this? Can anyone point me to some working example code?
thanks in advance!
Assuming you have inflated a view from an XML layout you could call findViewById(Int) on
the inflated view to get a hold of any view in that layout. Then you could do the wireing manually, for instance adding a onClick listener to some button:
inflatedView.findViewById(ID_OF_SOME_BUTTON_IN_THE_INFLATED_VIEW).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// DO SOMETHING WHEN BUTTON IS CLICKED
}
});
This was simply a case of not knowing enough. What I did in the end was to extend relativelayout in my class and fix my constructors to inflate the view in the class with the context of the activity. Then the view was added into the correct layout in the activity.
Thanks for all the good suggestions
I am building a Android app and I am a bit struggling with custom Views.
I would like to have a reusable View that consist of a few standard layout elements. Let's say a relativelayout with some buttons in it.
How should I proceed. Should I create a custom view class that extends RelativeLayout and programmaticly add those buttons? I would think that's a bit overkill?
What's the way to do it properly in Android?
Here are some rough steps regarding one way to create a custom aggregate view:
extend RelativeLayout
Provide a constructor in your new class that accepts Context and AttributeSet, making sure to call the superclass first. Do no add anything at this point. Wait until the next step.
override the onFinishInflate method, where you can add your contents through Java code or inflating an XML resource
Add any event handlers, etc
Optionally create a resources file if your widget will require attributes to be set.