how to create a buttonbar with a sliding control in android? - android

The footprints and clock app on a HTC sense phones use a slider control on the bottom buttonbar (see this movie for an example: https://docs.google.com/leaf?id=0B9hmEy_z2CRzOTJmYTVlNDktYjkwMS00YTM5LTk3ODQtYmIzODU4Yzk1YjA0&hl=en&authkey=CMWwwegG).
From the hierarchy viewer I can see that they use a custom control for this. I'm looking for a way to achieve the same effect. I want a buttonbar on the bottom of my app with a movable selector which I can drag from left to right and back, thereby selecting the different tabs.
Can anyone give some tips on how I might achieve this? Which layout controls could I use for this purpose? I know how to create the tabs and the button bar, but I'm a bit puzzled on the sliding selector.
Thanks and kind regards,
Ivo

To create a view, you have two main options.
One is to define it in XML, in a layout file your Activity will use.
<LinearLayout ... > <!-- or any other layout, see layout documentation in the SDK -->
<ImageView ... <!-- or another view as fits your needs -->
android:id="#+id/myview"
android:src="#drawable/myimage" ... />
</LinearLayout>
You can then reference your view in your code, after you called setContentView, with findViewById(R.id.myview). (of course, if you said, say, "#+foo/bar" in the XML file, you'd use R.foo.bar).
The other way is to create it dynamically and attach it to the view hierarchy.
ImageView v = new ImageView();
v.setImageResource(R.drawable.myimage);
... // initialize your view here
parent.addChild(v);
...Or so.
To get events, you have several options again.
One of them is to subclass View (or ImageView for that matter) and implement onTouchEvent. You'd need to adapt the code above to create an instance of your new class instead of an ImageView (obvious in Java code, for XML you'd write as tag name and make sure you implement the constructor that takes (Context, AttributeSet) to call its superclass' constructor).
Or, you could write a TouchDelegate and use setTouchDelegate on your ImageView, which is probably simpler in your case.
Another approach would be to create a GestureDetector and to match touches to your view using its hitTest method. For the task you are describing it's overkill, but it might come in handy when your application becomes more complicated.

I would extend the TabWidget class to do my own rendering. You'd have the benefits of hot-swappability with other tab systems and uniform interface, while still being able to draw your tab bar the way you want.
You'd start your animation in setCurrentTab.

Related

Android - how to draw in a multiple view layout

i have to draw a ball inside a view (or anything else that is good for this task).
I have followed some tutorial about the matter, but every tutorial that i have found
uses only one view (that is shown on the screen without the use of a layout).
But in my Activity i use a layout, that is composed by many views, and i want to draw
only on one of them.
here a little mockup!
Anybody knows a way to do it?
Is the view the wrong container to do it?
Thanks is advance!
Bye
...
You should extend a view that inherits from ViewGroup. this kind of view lets you handle a view that contains other views. with that, you can control the drawing, measures and layout of each of it's children separately
Your mockup can be made with a LinearLayout, which will contain some TextView's and you own View (the one which is containing the ball).
I'm surprised by this question, because there are many examples over the net explaining how to build a layout containing multiple views.

Android, custom component design

In my application I need to have something like image below. I have experience of creating custom components and I know how to do it. However, I have no idea how to create this kind of component. Do I need to do something like when we are designing custom rating bar?
Anyway, their so many way you can achieve this.
One way i am seeing using radio group and radio button, vertical lines and text view you can easily achieve this. Put everything inside a layout of your choice and then just inflate the layout where ever you need. After that you just need to set the radio button like this radio1.setChecked(true).This is a simple solution but may not a be a optimal one.
Alternatively you can create a custom view by extending view class or any of its subclass, whatever you feel suitable for your work then override its onDraw method do the entire thing manually.

Graphical Layout shows first view of ViewFlipper, how to see others?

I am using Eclipse and a ViewFlipper. In Graphical Layout, I want to see the second, third, and forth layouts of my views - right now, I can only see the first view. Any suggestions?
If I'm understanding you correctly, you want to see each view in the 'Graphical layout' tool? The way I do this, is instead of having all the layout work done in one xml (where your viewflipper is) I make each view a new layout xml. And then have each view (xml file) included into the view flipper by using this....
<include
layout="#layout/layout_media"
android:id="#+id/flipper_media" />
Hope this helps.
just put each layout in relative layout or linear what ever you are working with then with each layout you will work with the first one in the order and etc.. then at the end put each layout in the order you want later
I had to subclass the ViewSwitcher class to display an indeterminate ProgressBar until data is ready to display in the second view. I used isInEditMode() to determine whether I was actually running the app or just previewing in AS.
You should be able to add a custom attribute to choose which child to display. This might look a bit overkill, but if you happen to already have to subclass your ViewSwitcher or ViewFlipper, i think it is not a big deal.
I will try to put an example later.

custom imageview in larger layout

Having a bit of trouble here. I've created a custom ImageView by subclassing ImageView and I'm able to get it to display if I simply instantiate my view and set the entire contentView of my activity to my imageview via setContentView(myCustomImageView)
But what if I want to just use my custom imageview as part of a larger layout? E.g., I want a button, a textview, etc and then my ImageView somewhere in there...
I tried doing something like programatically creating a linearlayout and then instantiating my special imageview and adding it via mLinearLayout.addView(myCustomImageView) but it crashes when I do this.
Am I missing something basic here?
EDIT: apparently you just use the full namespace of your custom view in the XML.
Why don't you put your layout in the XML?
You can add you custom View like this:
<view
class="com.your_package.myCustomImageView"
id="#+id/customView"
/>
You can find more useful resources on the Android SDK documentation site
Edit: Yes, you must use the entire namespace for your self defined views. For standard views you can omit com.android.widget because the SDK knows where to find them, but for your own stuff he doesn't.

Effective way to handle redundant UI elements in Android?

I have numerous activites in my Android app., and most should contain the same, relatively complex set of UI widgets on the screen's top area (lets say that its a kind of toolbar that you can find on most screens).
Right now, every screen's layout contains the markup for this toolbar, along with its logic inside every Activity's source, so it's damn redundant. Could you recommend a more efficient / less redunant way to do this?
I would take advantage of the <include> tag in the layout's xml. This will let you reuse that toolbar very easily and effectively. As for the code I would subclass Activity and place the logic in there, then each of you activities can subclass your custom Activity class.
There are 3 very useful articles on the dev site about this topic. Here is the first one link
I would create a custom View object (subclass View) and then include it in all of your layout xml. You can actually pass parameters, etc. just like built in views. Then define XML for that view that will always be used when that view is drawn on the screen. Also, this allows you to change the view and have that change populated across all of your Activities without having to manually modify all of the code.

Categories

Resources