Beginners question: onClickListener

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Beginners question: onClickListener

Postby echinus » Thu Oct 15, 2009 10:24 pm

Hi,

this is the first time i you an View.onClickListener and i have this problem:
In the app i added a GraphView (its a class i found on the internet) to my LinearLayout:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. layout.addView(graphView);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

Now my problem: in the onClick function of the listener i must have access to the graphView. How do i do this?

And a second question: why does
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Toast.makeText(getBaseContext(), "Hello", Toast.LENGTH_LONG);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
within the onClick function not work?

Thanks in advance!
echinus
Freshman
Freshman
 
Posts: 8
Joined: Fri Oct 09, 2009 3:20 pm

Top

Re: Beginners question: onClickListener

Postby IHeartAndroid » Fri Oct 16, 2009 10:52 am

echinus wrote:
Now my problem: in the onClick function of the listener i must have access to the graphView. How do i do this?


inflate the view before and set a private member of the activity to it.
u can use this private member then in your onclick event

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private View v;
  2.  
  3. ...
  4.  
  5. this.v = (GraphView) findViewbyId(...);
  6.  
  7. onclick(){
  8. // now u can use v
  9. }
  10.  
  11.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
User avatar
IHeartAndroid
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Jul 08, 2009 8:51 am

Postby echinus » Fri Oct 16, 2009 11:45 am

Ok, that seems to be a good idea. But what is the ID of the GraphView? Where/who can i set this!?

Thanks
echinus
Freshman
Freshman
 
Posts: 8
Joined: Fri Oct 09, 2009 3:20 pm

Postby IHeartAndroid » Fri Oct 16, 2009 12:01 pm

look in your res/layout folder to find the right .xml file

If the graphview doesnt have an id yet add a line like this :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. android:id="@+id/whatever"  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
User avatar
IHeartAndroid
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Jul 08, 2009 8:51 am

Postby echinus » Fri Oct 16, 2009 1:30 pm

Unfortunatly it is not possible to define that view in the xml-file because it does not have a function which is neccessay for that. Thats why i added the GraphView via layout.addView(..).
So it is not possible to define the id in the xml file.

Any other idea?

Thanks
echinus
Freshman
Freshman
 
Posts: 8
Joined: Fri Oct 09, 2009 3:20 pm

Postby qlimax » Mon Oct 19, 2009 8:07 pm

to add your custom view on the xml you can do like this

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. .......
  2.  
  3. <com.your.package.GraphView
  4.  
  5. android:id="@+id/myGrahView"
  6.  
  7. />
  8.  
  9. ........
Parsed in 0.000 seconds, using GeSHi 1.0.8.4



cya :run:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Postby echinus » Mon Oct 19, 2009 8:34 pm

Well, i already tried that.
I added
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.         <com.echinus.graphdemo.GraphView android:id="@+id/wert"
  2.                 android:layout_width="wrap_content"
  3.                 android:layout_height="wrap_content"
  4.                 />     
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

...and the result is:
10-19 21:30:20.103: WARN/System.err(893): android.view.InflateException: Binary XML file line #13: Error inflating class com.echinus.graphdemo.GraphView
10-19 21:30:20.113: WARN/System.err(893): at android.view.LayoutInflater.createView(LayoutInflater.java:502)
10-19 21:30:20.123: WARN/System.err(893): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:564)
10-19 21:30:20.123: WARN/System.err(893): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
10-19 21:30:20.123: WARN/System.err(893): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-19 21:30:20.133: WARN/System.err(893): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-19 21:30:20.133: WARN/System.err(893): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-19 21:30:20.143: WARN/System.err(893): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
10-19 21:30:20.143: WARN/System.err(893): at android.app.Activity.setContentView(Activity.java:1626)
10-19 21:30:20.153: WARN/System.err(893): at com.echinus.graphdemo.graphDemo.onCreate(graphDemo.java:28)
10-19 21:30:20.153: WARN/System.err(893): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
10-19 21:30:20.153: WARN/System.err(893): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
10-19 21:30:20.163: WARN/System.err(893): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
10-19 21:30:20.163: WARN/System.err(893): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
10-19 21:30:20.173: WARN/System.err(893): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
10-19 21:30:20.173: WARN/System.err(893): at android.os.Handler.dispatchMessage(Handler.java:99)
(...)

10-19 21:30:20.203: WARN/System.err(893): Caused by: java.lang.NoSuchMethodException: GraphView(Context,AttributeSet)
10-19 21:30:20.213: WARN/System.err(893): at java.lang.Class.getMatchingConstructor(Class.java:669)
10-19 21:30:20.213: WARN/System.err(893): at java.lang.Class.getConstructor(Class.java:484)
10-19 21:30:20.223: WARN/System.err(893): at android.view.LayoutInflater.createView(LayoutInflater.java:474)
10-19 21:30:20.223: WARN/System.err(893): ... 21 more


Any idea now?

Thanks
echinus
Freshman
Freshman
 
Posts: 8
Joined: Fri Oct 09, 2009 3:20 pm

Postby qlimax » Mon Oct 19, 2009 8:37 pm

can you post the GraphView.java code ?
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby echinus » Mon Oct 19, 2009 8:40 pm

Here it is:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.echinus.graphdemo;
  2.  
  3.  
  4.  
  5. import android.content.Context;
  6.  
  7. import android.graphics.Canvas;
  8.  
  9. import android.graphics.Color;
  10.  
  11. import android.graphics.Paint;
  12.  
  13. import android.graphics.Paint.Align;
  14.  
  15. import android.view.View;
  16.  
  17.  
  18.  
  19. /**
  20.  
  21.  * GraphView creates a scaled line or bar graph with x and y axis labels.
  22.  
  23.  * @author Arno den Hond
  24.  
  25.  *
  26.  
  27.  */
  28.  
  29. public class GraphView extends View {
  30.  
  31.  
  32.  
  33.         private Paint paint;
  34.  
  35.         public float[] values;
  36.  
  37.         private String[] horlabels = {"10", "11", "12" };
  38.  
  39.         private String[] verlabels;
  40.  
  41.         private String title;
  42.  
  43.         private float min;
  44.  
  45.         private float max;
  46.  
  47.  
  48.  
  49.  
  50.  
  51.         public GraphView(Context context, float[] values, String title) {
  52.  
  53.                 super(context);
  54.  
  55.                 if (values == null)
  56.  
  57.                         return;
  58.  
  59.                 else
  60.  
  61.                         setValues(values);
  62.  
  63.  
  64.  
  65.                 makeVerLabels();
  66.  
  67.                 this.title = title;
  68.  
  69.  
  70.  
  71.                 paint = new Paint();
  72.  
  73.         }
  74.  
  75.  
  76.  
  77.         public float[] getValues()
  78.  
  79.         {
  80.  
  81.                 return this.values;
  82.  
  83.         }
  84.  
  85.        
  86.  
  87.         public void setValues(float[] values_new)
  88.  
  89.         {
  90.  
  91.                 this.values = values_new;
  92.  
  93.                 float diff = getMax()-getMin();
  94.  
  95.                 max = getMax() + diff/20;
  96.  
  97.                 min = getMin() - diff/20;
  98.  
  99.                 makeVerLabels();
  100.  
  101.         }
  102.  
  103.        
  104.  
  105.         private void makeVerLabels()
  106.  
  107.         {
  108.  
  109.                 int numValues = 6;
  110.  
  111.                 this.verlabels = new String[numValues+1];
  112.  
  113.                 float diff = max - min;
  114.  
  115.                 for(int i=0; i<=numValues; i++)
  116.  
  117.                 {
  118.  
  119.                         this.verlabels[i] = ""+((float)Math.round(10*(max-(float)i/numValues*diff))/10);
  120.  
  121.                         System.out.println(i);
  122.  
  123.                 }
  124.  
  125.         }
  126.  
  127.        
  128.  
  129.         @Override
  130.  
  131.         protected void onDraw(Canvas canvas) {
  132.  
  133.                 float border = 20;
  134.  
  135.                 float horstart = border * 2;
  136.  
  137.                 float height = getHeight();
  138.  
  139.                 float width = getWidth() - 1;
  140.  
  141.                 float diff = max - min;
  142.  
  143.                 float graphheight = height - (2 * border);
  144.  
  145.                 float graphwidth = width - (2 * border);
  146.  
  147.  
  148.  
  149.                 paint.setTextAlign(Align.LEFT);
  150.  
  151.                 int vers = verlabels.length - 1;
  152.  
  153.                 for (int i = 0; i < verlabels.length; i++) {
  154.  
  155.                         paint.setColor(Color.DKGRAY);
  156.  
  157.                         float y = ((graphheight / vers) * i) + border;
  158.  
  159.                         canvas.drawLine(horstart, y, width, y, paint);
  160.  
  161.                         paint.setColor(Color.WHITE);
  162.  
  163.                         canvas.drawText(verlabels[i], 0, y, paint);
  164.  
  165.                 }
  166.  
  167.                 int hors = horlabels.length - 1;
  168.  
  169.                 for (int i = 0; i < horlabels.length; i++) {
  170.  
  171.                         paint.setColor(Color.DKGRAY);
  172.  
  173.                         float x = ((graphwidth / hors) * i) + horstart;
  174.  
  175.                         canvas.drawLine(x, height - border, x, border, paint);
  176.  
  177.                         paint.setTextAlign(Align.CENTER);
  178.  
  179.                         if (i==horlabels.length-1)
  180.  
  181.                                 paint.setTextAlign(Align.RIGHT);
  182.  
  183.                         if (i==0)
  184.  
  185.                                 paint.setTextAlign(Align.LEFT);
  186.  
  187.                         paint.setColor(Color.WHITE);
  188.  
  189.                         canvas.drawText(horlabels[i], x, height - 4, paint);
  190.  
  191.                 }
  192.  
  193.  
  194.  
  195.                 paint.setTextAlign(Align.CENTER);
  196.  
  197.                 canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);
  198.  
  199.  
  200.  
  201.                 if (max != min) {
  202.  
  203.                         paint.setColor(Color.LTGRAY);
  204.  
  205.                         float datalength = values.length;
  206.  
  207.                         float colwidth = (width - (2 * border)) / datalength;
  208.  
  209.                         float halfcol = colwidth / 2;
  210.  
  211.                         float lasth = 0;
  212.  
  213.                         for (int i = 0; i < values.length; i++) {
  214.  
  215.                                 float val = values[i] - min;
  216.  
  217.                                 float rat = val / diff;
  218.  
  219.                                 float h = graphheight * rat;
  220.  
  221.                                 if (i > 0)
  222.  
  223.                                         canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
  224.  
  225.                                 lasth = h;
  226.  
  227.                         }
  228.  
  229.                 }
  230.  
  231.         }
  232.  
  233.  
  234.  
  235.         private float getMax() {
  236.  
  237.                 float largest = Integer.MIN_VALUE;
  238.  
  239.                 for (int i = 0; i < values.length; i++)
  240.  
  241.                         if (values[i] > largest)
  242.  
  243.                                 largest = values[i];
  244.  
  245.                 return largest;
  246.  
  247.         }
  248.  
  249.  
  250.  
  251.         private float getMin() {
  252.  
  253.                 float smallest = Integer.MAX_VALUE;
  254.  
  255.                 for (int i = 0; i < values.length; i++)
  256.  
  257.                         if (values[i] < smallest)
  258.  
  259.                                 smallest = values[i];
  260.  
  261.                 return smallest;
  262.  
  263.         }
  264.  
  265.  
  266.  
  267. }
Parsed in 0.052 seconds, using GeSHi 1.0.8.4


I did not write the whole class by myself. I found it on the internet and changed a couple of things. But the same error occured in the "original" version.

Thanks
echinus
Freshman
Freshman
 
Posts: 8
Joined: Fri Oct 09, 2009 3:20 pm

Postby qlimax » Mon Oct 19, 2009 9:10 pm

OK :) you miss a constructor for the GraphView.

that's work so:

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.  
  8.  
  9. <ch.example.db.GraphView
  10. android:id="@+id/myGraphView"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"/>
  13.  
  14.  
  15. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package ch.example.db;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6.  
  7.  
  8. public class Example extends Activity {
  9.     /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.              
  15.         GraphView gw=(GraphView)this.findViewById(R.id.myGraphView);
  16.         gw.setValues(new float[]{0.1f,0.2f,0.3f,0.4f});
  17.        
  18.     }
  19. }
  20.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package ch.example.db;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.Paint.Align;
  9. import android.util.AttributeSet;
  10. import android.view.View;
  11.  
  12. /**
  13.  * GraphView creates a scaled line or bar graph with x and y axis labels.
  14.  * @author Arno den Hond
  15.  *
  16.  */
  17. public class GraphView extends View {
  18.  
  19.  
  20.  
  21.         private Paint paint;
  22.      public float[] values;
  23.      private String[] horlabels = {"10", "11", "12" };
  24.      private String[] verlabels;
  25.      private String title;
  26.      private float min;
  27.      private float max;
  28.  
  29.      
  30.      public GraphView(Context context, AttributeSet attrs) {
  31.                 super(context, attrs);
  32.                 // TODO Auto-generated constructor stub
  33.                
  34.                 setValues(new float[]{0.1f,0.2f});
  35.                 makeVerLabels();
  36.                
  37.                 paint=new Paint();
  38.                 this.title="Hello Graph";
  39.         }
  40.      
  41.  
  42.      public GraphView(Context context, float[] values, String title) {
  43.           super(context);
  44.           if (values == null)
  45.                return;
  46.           else
  47.               setValues(values);
  48.  
  49.           makeVerLabels();
  50.           this.title = title;
  51.  
  52.           paint = new Paint();
  53.      }
  54.  
  55.      public float[] getValues()
  56.      {
  57.           return this.values;
  58.      }
  59.      
  60.      public void setValues(float[] values_new)
  61.      {
  62.           this.values = values_new;
  63.           float diff = getMax()-getMin();
  64.           max = getMax() + diff/20;
  65.           min = getMin() - diff/20;
  66.           makeVerLabels();
  67.      }
  68.      
  69.      private void makeVerLabels()
  70.      {
  71.           int numValues = 6;
  72.           this.verlabels = new String[numValues+1];
  73.           float diff = max - min;
  74.           for(int i=0; i<=numValues; i++)
  75.           {
  76.                this.verlabels[i] = ""+((float)Math.round(10*(max-(float)i/numValues*diff))/10);
  77.                //System.out.println(i);
  78.           }
  79.      }
  80.      
  81.      @Override
  82.      protected void onDraw(Canvas canvas) {
  83.           float border = 20;
  84.           float horstart = border * 2;
  85.           float height = getHeight();
  86.           float width = getWidth() - 1;
  87.           float diff = max - min;
  88.           float graphheight = height - (2 * border);
  89.           float graphwidth = width - (2 * border);
  90.  
  91.           paint.setTextAlign(Align.LEFT);
  92.           int vers = verlabels.length - 1;
  93.           for (int i = 0; i < verlabels.length; i++) {
  94.                paint.setColor(Color.DKGRAY);
  95.                float y = ((graphheight / vers) * i) + border;
  96.                canvas.drawLine(horstart, y, width, y, paint);
  97.                paint.setColor(Color.WHITE);
  98.                canvas.drawText(verlabels[i], 0, y, paint);
  99.           }
  100.           int hors = horlabels.length - 1;
  101.           for (int i = 0; i < horlabels.length; i++) {
  102.                paint.setColor(Color.DKGRAY);
  103.                float x = ((graphwidth / hors) * i) + horstart;
  104.                canvas.drawLine(x, height - border, x, border, paint);
  105.                paint.setTextAlign(Align.CENTER);
  106.                if (i==horlabels.length-1)
  107.                     paint.setTextAlign(Align.RIGHT);
  108.                if (i==0)
  109.                     paint.setTextAlign(Align.LEFT);
  110.                paint.setColor(Color.WHITE);
  111.                canvas.drawText(horlabels[i], x, height - 4, paint);
  112.           }
  113.  
  114.           paint.setTextAlign(Align.CENTER);
  115.           canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);
  116.  
  117.           if (max != min) {
  118.                paint.setColor(Color.LTGRAY);
  119.                float datalength = values.length;
  120.                float colwidth = (width - (2 * border)) / datalength;
  121.                float halfcol = colwidth / 2;
  122.                float lasth = 0;
  123.                for (int i = 0; i < values.length; i++) {
  124.                     float val = values[i] - min;
  125.                     float rat = val / diff;
  126.                     float h = graphheight * rat;
  127.                     if (i > 0)
  128.                          canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
  129.                     lasth = h;
  130.                }
  131.           }
  132.      }
  133.  
  134.      private float getMax() {
  135.           float largest = Integer.MIN_VALUE;
  136.           for (int i = 0; i < values.length; i++)
  137.                if (values[i] > largest)
  138.                     largest = values[i];
  139.           return largest;
  140.      }
  141.  
  142.      private float getMin() {
  143.           float smallest = Integer.MAX_VALUE;
  144.           for (int i = 0; i < values.length; i++)
  145.                if (values[i] < smallest)
  146.                     smallest = values[i];
  147.           return smallest;
  148.      }
  149.  
  150. }
Parsed in 0.056 seconds, using GeSHi 1.0.8.4


Image


CYA :run:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby echinus » Mon Oct 19, 2009 9:13 pm

Great, thanks!
echinus
Freshman
Freshman
 
Posts: 8
Joined: Fri Oct 09, 2009 3:20 pm

Postby qlimax » Mon Oct 19, 2009 9:18 pm

cheers :xmas:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google Feedfetcher and 4 guests