Arbitrary positioning icon on a View

by Eric » Tue, 24 Nov 2009 10:32:54 GMT


Sponsored Links
 Hey all,

I'm trying to figure out how to mimic the "badge" functionality of an
iPhone app...in particular, I've got a gridview that has several
icons, and based on the result of an AsyncTask, I'd like to position a
"new" badge hovering over one of the icons.  I've got the icon showing
up using Activity.addContentView but there don't seem to be any
ViewGroup.LayoutParams attributes to control an x/y position where the
new view should be displayed.  The options as I see them:

1) FrameLayout (also no support for offset that I can see)
2) RelativeLayout (can't figure out how to dynamically add a new
ImageView)
3) Create a large, mostly transparent png to handle the offset non-
programmatically
4) Use AbsoluteLayout to specify pixel offsets.

Any other options I'm missing?  I'd really like to avoid using #3
(clunky) or #4 (deprecated)...any way to get #1 or #2 working?  Or is
there another option I'm missing?

--



Arbitrary positioning icon on a View

by Nithin » Tue, 24 Nov 2009 14:21:06 GMT


 Hi Eric,

Try offsetLeftAndRight() and offsetTopAndBottom() on the imageView, to
position it accordingly in the layout. But it is temporary only. Both
these offset will reset in the next layout pass.

Thanks





--


Sponsored Links


Arbitrary positioning icon on a View

by appforce.org » Tue, 24 Nov 2009 15:44:48 GMT


 Hi Eric,

Have you considered changing your adapter to provide the ImageView-s
nested in RelativeLayout-s?

This will let you do getParent().addView() on you icons.

--



Arbitrary positioning icon on a View

by Eric » Wed, 25 Nov 2009 07:58:52 GMT


 Thanks for the replies, guys.  I ended up using View.setPadding (the
temporary nature of the offset* methods was a bit too transitory for
my needs) to set a left/top padding on the ImageView, and it works
like a charm.




--



Arbitrary positioning icon on a View

by Dianne Hackborn » Wed, 25 Nov 2009 08:16:35 GMT


 



Given that you don't know when a layout pass will be needed, this is
probably a bad solution.

There are lots of options with the layout managers -- using relative layout
and specifying the appropriate parameters to position the badge view in the
corner, using a FrameLayout to layer two children on top of each other, etc.

-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--



Other Threads

1. Problem when coming back to activity after changing font in Galaxy S

Hi All,
 I am going through a strange behaviour. I have Two activities in my
application - ActivityOne and ActivityTwo. Both are very simple
activities and have only basic lifecycle methods overrridden.

Steps to reproduce this are as follows:
1. I am launching ActivityTwo by clicking on a button on ActivityOne.
For this I am using startActivityForResult() method.
2. Now when I am on ActivityTwo, I long press home key and go to
Settings > Display > Font Style. and change fonts.
3. Now I go back to ActivityTwo by pressing back button.
4. On ActivityTwo I press again back key.

Ideally while pressing back on ActivityTwo, It should call
AcitivityOne's onActivityResult() method.
But Here I saw, that first iActivityOne's onDestroy(), then onCreate()
and then onActivityResult() is called.
Because onCreate() is getting called it again launches ActivityTwo and
hence it feels to user that ActivityTwo is coming twice while pressing
back and user has to press back button twice for going to
ActivityOne.

This is a very strange behavoiur.

On digging more i found out that when font are getting changed, they
are restarting all packages. Hence all the activities are getting
created(instantiated) again. Because of this the first instance of
ActivityOne is getting killed and a new instance is getting created.
Because of this onCreate method is called and it again launches
ActivityTwo.

Here my problem is that, I can not make my activities singleinstance
or singletask. This is a requirement.

I also found that the same situation occurs in many of the pre-
installed applications or any of the newly created method.

My questions are:
1. Is there any config attribute for font changes? I tried to find out
in android docs and in google also but no luck yet.
2. If there is no config attribute, then please tell me the ways to
handle this situation. Its very weired situation for the user

ActivityOne Code is

public class ActivityOne extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) findViewById(R.id.btn_launch);
        button.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                                Log.d("ActivityOne", "<SURESH> 
setOnClickListener()");
                                Intent intent = new Intent();
                                intent.setComponent(new 
ComponentName("com.demo.backpress",
"com.demo.backpress.ActivityTwo"));
                                startActivityForResult(intent, 1);
                        }
                });
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
                super.onActivityResult(requestCode, resultCode, data);

                if(resultCode == RESULT_CANCELED) {
                        Toast.makeText(this, "First activity cancelled",
Toast.LENGTH_LONG);
                }
                Log.d("ActivityOne", "<SURESH> onActivityResult() resultCode 
==> " +
resultCode + " -:- requestCode ==> " + requestCode + " -:- data ==> "
+ data);
        }
}

ActivityTwo Code is :

public class ActivityTwo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("ActivityTwo", "<SURESH> onCreate()");
        setContentView(R.layout.main_two);
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                Log.d("ActivityTwo", "<SURESH> onActivityResult() resultCode 
==> " +
resultCode + " -:- requestCode ==> " + requestCode + " -:- data ==> "
+ data);
        }
}

-- 

2. Syntax error: Unterminated quoted string

I'm following the basic instructions for setting up an Android
application with Eclipse and just after creating a new project,
without modifying any sources, I get the following error when I try to
run it in the emulator:

[2011-05-20 00:02:13 - bla] Device API version is 10 (Android 2.3.3)
[2011-05-20 00:02:13 - bla] Uploading bla.apk onto device
'emulator-5554'
[2011-05-20 00:02:13 - bla] Installing bla.apk...
[2011-05-20 00:02:13 - bla] Success!
[2011-05-20 00:02:13 - bla] Starting activity com.bla.bla.bla on
device emulator-5554
[2011-05-20 00:02:13 - bla] ActivityManager: am: 1: Syntax error:
Unterminated quoted string

I fail to see any problem in any of the sources, and my attempts to
find that unterminated string anywhere in the generated sources have
failed so far. This happens to me with the following version:

Eclipse Eclipse IDE for Java Developers
Version: Helios Service Release 2
Build id: 20110218-0911

SDK Platform Android 2.3.3, API 10, revision 1
Android SDK Tools, revision 11

Mac OS X 10.6.7

Can anyone explain that error? If you need more information, please
let me know.

And please Cc: my on answers.

Thanks,
Daniel

-- 

3. Java Guru needed -- compiler bug?

4. Specific drawable not working?????

5. Devs with paid apps -- check your enabled countries

6. I connect Acer iconia a500 with ddms

7. Orientation change and setRender