IconTextMenu Challenge ;-)

Put your problem here if it does not fit any of the other categories.

IconTextMenu Challenge ;-)

Postby sommeralex » Wed Jan 30, 2008 8:03 pm

I am still working on my IconTextMenu, maybe some could help me again.. it doesnt work, but i dont know why, there is no

Log.i("iconTextElement", "clicked");

output in my LogCat Console but also - no error!


main xml:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <com.airwriting.android.tests.IconTextMenuView id="@+id/icontextmenu1"
  8.    android:layout_width="fill_parent"
  9.    android:layout_height="wrap_content"
  10.    />
  11. </LinearLayout>
  12.  
  13.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


my starter class text

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.airwriting.android.tests;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11.  
  12.  
  13. public class Test extends Activity {
  14.     /** Called when the activity is first created. */
  15.     @Override
  16.     public void onCreate(Bundle icicle) {
  17.         super.onCreate(icicle);
  18.         setContentView(R.layout.main);
  19.        
  20.        
  21.        IconTextMenuView iconTextMenuView = (IconTextMenuView) findViewById(R.id.icontextmenu1);
  22.        
  23.        iconTextMenuView.setOnClickListener(clickListener);
  24.      
  25.        
  26.        
  27.                
  28.     }
  29.    
  30.         OnClickListener clickListener = new OnClickListener() {
  31.                 public void onClick(View v) {
  32.                         Log.i("iconTextElement", "clicked");
  33.                        
  34.                        
  35.                 }
  36.                
  37.         };             
  38. }
  39.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


and now the iconTextMenuView

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.airwriting.android.tests;
  3.  
  4.  
  5. import java.util.Map;
  6.  
  7. import android.content.Context;
  8.  
  9. import android.graphics.Typeface;
  10. import android.util.AttributeSet;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.widget.ImageView;
  14. import android.widget.LinearLayout;
  15. import android.widget.TextView;
  16.  
  17.  
  18. /**
  19.  * adapted from plusminus by sommeralex <img src="http://www.anddev.org/images/smilies/wink.png" alt=";-)" title="Wink" />
  20.  */
  21. public class IconTextMenuView extends LinearLayout implements android.view.View.OnClickListener          {
  22.  
  23.         // ===========================================================
  24.         // Fields
  25.         // ===========================================================
  26.  
  27.        
  28.         ;
  29.         private ImageView menuIcon = null;
  30.         private TextView menuText = null;
  31.  
  32.         // ===========================================================
  33.         // Constructors
  34.         // ===========================================================
  35.  
  36.         public IconTextMenuView(Context context) {
  37.                 super(context);
  38.  
  39.         }
  40.  
  41.         public IconTextMenuView(Context context, AttributeSet attrs,
  42.                         Map inflateParams) {
  43.                 super(context, attrs, inflateParams);
  44.                
  45.                
  46.                 Log.i("Wf", "icon created");
  47.                
  48.                 this.setOnClickListener(this);
  49.                
  50.                 /* Setup the ImageView that will show weather-icon. */
  51.                 this.menuIcon = new ImageView(context);
  52.                 this.menuIcon.setImageDrawable(getResources().getDrawable(
  53.                                 R.drawable.dunno));
  54.  
  55.                 /* Setup the textView that will show the temperature. */
  56.                 this.menuText = new TextView(context);
  57.                 this.menuText.setText("menuText");
  58.                 this.menuText.setTextSize(8);
  59.                 this.menuText.setTypeface(Typeface
  60.                                 .create("Tahoma", Typeface.BOLD));
  61.  
  62.                 /* Add child views to this object. */
  63.                 this.addView(this.menuIcon, new LinearLayout.LayoutParams(
  64.                                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  65.                 this.addView(this.menuText, new LinearLayout.LayoutParams(
  66.                                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  67.         }
  68.  
  69.  
  70.         public void onClick(View arg0) {
  71.                 // TODO Auto-generated method stub     
  72.                 Log.i("InnerListenerOfofIconTextMenu", "clicked");
  73.                
  74.         }
  75. }
  76.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


nor the InnerListenerOfIconTextMenu "clicked" info comes when i click on it, nor the outer listener i set in the test activity (Log.i("iconTextElement", "clicked"); )

:cry:
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Top

Postby Ishtar » Thu Jan 31, 2008 12:32 pm

I think the problem is that your IconTextMenuView extends LinearLayout. Let me try to fix it...

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby Ishtar » Thu Jan 31, 2008 1:38 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class IconTextMenuView extends LinearLayout {
  3.  
  4. public IconTextMenuView(Context context, AttributeSet attrs,
  5.  
  6.               Map inflateParams) {
  7.  
  8.    // blablabla
  9.  
  10.          setFocusable(true); //not sure if you need this..
  11.  
  12.          requestFocus(); //or this..
  13.  
  14. }
  15.  
  16.  
  17.  
  18.     @Override
  19.  
  20.     public boolean onMotionEvent(MotionEvent e)
  21.  
  22.     {
  23.  
  24.         // do we need to check if this event actually happend in our View??
  25.  
  26.         if (e.getAction()==MotionEvent.ACTION_DOWN)
  27.  
  28.         {
  29.  
  30.                 performClick();
  31.  
  32.                 return true;
  33.  
  34.         }
  35.  
  36.         return super.onMotionEvent(e);
  37.  
  38.     }
  39.  
  40. }
  41.  
  42.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


This worked for me. Took me a while to figure this one out :x
If its still not working,, or if you get problems when you put more views in your layout. Please post :)

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby sommeralex » Thu Jan 31, 2008 2:08 pm

thx, i did something similiar already, after hours of experimentating
i just overwrote the onMotion (and this works) - but i did not mention to perform an OnClick

- so at least i can see that my approach is good - because its the same as yours ;-)

thank you very much!
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Postby Ishtar » Thu Jan 31, 2008 2:21 pm

performClick will call the clicklistener that was set, so that would be the preferred way to do it. Keeps your view to follow the android API :wink:

You're welcome!
Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby sommeralex » Thu Jan 31, 2008 2:33 pm

so, then it seems i still cant or wont follow the android-api. why is there an onClickListener, when i have to call onMotion first?

normally (lets say at J2ME, or simple just swing), if you have a button listener, you also dont have to overwrite an "onMotion" method which fires the onClick Listener then..

and WHERE can i find in the api this information? ("please use onMotion to perform the onClick action, even if you want to use the onClickListener..")

hm..
Last edited by sommeralex on Thu Jan 31, 2008 3:01 pm, edited 1 time in total.
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Top

Postby Ishtar » Thu Jan 31, 2008 2:46 pm

Look, an motionevent just occurs, it gets then send to (all/one??) view. This view can decide what to do with it. A button would call performClick (if its a click-motion-event), but a linearlayout would do nothing. Also if you press the "OK" button on your phone while a button has focus, it will receive an key-event, and will call performClick, again the linearlayout would do nothing with it. There is no way for android to know if it was a "click", the view itself has to decide that. Maybe a bit different from swing (didnt use it that much). I assume swing works in the same way, a button will get (something like) a mousevent, and then fire a clickevent. If your IconTextMenuView would extend button, or any other widget that can be clicked, there would be no problem. But you extended a view that does not know what to do with clicks, so you got to implement it. (don't get me wrong, its a good to extend the linear layout :wink: )

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby sommeralex » Thu Jan 31, 2008 3:08 pm

(thx again for you answer..but;)

i know that normally it (the linear layout) would do nothing, for that reason i did:

1. public class IconTextMenuView extends LinearLayout implements android.view.View.OnClickListener
2. this.setOnClickListener(this);
3. and last but not least, i implemented the onClick-method.
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Postby Ishtar » Thu Jan 31, 2008 3:32 pm

I see... I don't plan on starting to explain all what has to be done to implement a GUI system thing. Cuz i don't know it all :wink:

With these 3 things. Still something, some class, has to call the clicklistener with onClick. Since only your view knows what the clicklistener is, theres a method in your view, that fires the clicklistener. The performClick() method does just that. I guess you expect the Android API to call performClick() when the user clicked on it. The problem is, android doesn't know its a "click", maybe its a drag, and besides clicks can come in other ways, like press the big-middle button, on a focussed view. Android can't call performClick, cuz it doesn't know if/when it's a click. So android sends the event (the motionevent) to the view, and let it handle it. All clickable widgets will call their performclick, all other views, will ignore it. If you know that event is a click, you have to call performClick.

Might be difference with swing (if it works different as you say) that in android we get more "control" but at the price that we to have to implement more.

That a view gets touched (it is a touchscreen) is not the same as being clicked. most likely it is, but the api can not and should not know that. (Designing apis isn't easy matter...)

One remark:
if you set, onClickListener(this); You better override performClick...

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby sommeralex » Thu Jan 31, 2008 3:52 pm

so i think i got the idea, the difference to swing is, that listeners dont have to be fired (by performs) explicitly, they are, if implemented, "active". They are: listening.

My "baseListener" now is (and stays) the onMotion Listener. And from it as a base, i forward motion-activities to myListener(s).

thank you again.. 8)
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Postby Ishtar » Thu Jan 31, 2008 4:05 pm

Well, every api is different. Too bad the androids api is so soo badly documented, insights like this came from trial and error, instead of reading a lot of pages. Be careful though, phones without a touchscreen, won't be able to trigger the motionlistener.... Thats why i still think its better to stick with the onclicklistener, and fire that event "manually".

No problem 8)
Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby sommeralex » Thu Jan 31, 2008 4:40 pm

at least, i understood this:

8)
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests