how to add default text inside text field

edited November 2013 in Android Mode

Hi to all for my app I would like to add default text when I dispalyed a text field. For create a text field I used this library http://code.google.com/p/apwidgets/ and for insert I try to use setHint command but doesn`t work.

import apwidgets.*;
import android.widget.TextView

APWidgetContainer widgetContainer; 
APEditText textField;

void setup(){

  widgetContainer = new APWidgetContainer(this); //create new container for widgets
  textField = new APEditText(20, 100, 150, 50); //create a textfield from x- and y-pos., width and height
  widgetContainer.addWidget(textField); //place textField in container
  textField.setText("text");//this is not a default text
  textField.setHint("default");//this is going to be a default text but doesn`t work
  //taken from http://developer.android.com/reference/android/widget/TextView.html#attr_android:hint
}

void draw(){

  background(0); //black background

  text(textField.getText(), 10, 10); //display the text in the text field
}

The error that give to me is cannot find symbol...can anyone help me? thanks in advance

Answers

  • edited November 2013

    Reformatted code.

    "cannot find symbol"
    Which one? Copy & paste error here.

    ...

    Obvious error:

    importandroid.widget.TextView

    Probably needs to be:

    import android.widget.TextView;
    

    or something like that.

  • A side note... why do you import Android's TextView? If anything, this will only cause confusion... unless the APWidgets library reqires that you access native methods instead of those that it provides...

  • So for the import android.widget.TextView I simply made a mistake when I copy my code in the forum...and this is the entire message BUILD FAILED E:\adt-bundle-windows-x86_64-20130917\sdk\tools\ant\build.xml:720: The following error occurred while executing this line: E:\adt-bundle-windows-x86_64-20130917\sdk\tools\ant\build.xml:734: Compile failed; see the compiler error output for details.

  • By any change, are you using Android API level 19? If so, this is a common problem and you need to revert back to API level 18 (and uninstall 19...). You may also have to downgrade Processing.

  • I checked and I have API level 18...is it possible that this function (setHint()) was not implemented on the APwidgwet library?

  • Does removing (or commenting out) this line change the error? If not, then this particular function isn't causing your error. Do you only get the error when trying to run APWidgets sketches? Are you able to run normal sketches?

  • yes without the line setHint() the sketch will run in a good way, as I want

  • TextView.setHint() expects a native-Android resource String id... which is provided as an int, not a String. I don't think there's a way around this without going native. If you really want to, then here's how to implement it (untested):


    Open the sketch folder. Create a folder called res. Inside this folder, create a folder called values. Inside this folder, create a text (XML) file called strings.xml.

    The XML file's content should look something like this (you can change where it says Default Text to whatever you want):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="text_field_default_text">Default Text</string>
    </resources.
    

    You have created a native-Android resource. To access this from within the application, use the following (it is vital that the text text_field_default_text remain the same in both the XML file and in the sketch):

    textField.setText(R.string.text_field_default_text);
    

    That should do it. If you run into any problems, it may be something that I have missed... or it may be something lacking in Processing's implementation. I have read through Android mode's pre-compiler source code recently, and think that it should work. This same approach can be used elsewhere in native Android methods that require int values.

  • First of all thank you for the help.....I try to use your method but processing tell me that setText(java.lang.String) in apwidgets.APTextView cannot be applied to (int)

  • You should use a String for setText(), but the int method that I described for setHint().

  • Yes but if I use setHint() the error that I obtain is the same that I report at the begin of this discussion

  • You receive the error "cannot find symbol"? If this is the case, then it probably isn't possible in Processing. You could move the project to Eclipse... but this is likely beyond the scope of what you are looking to achieve.

  • I think so....What I want to do is to clear the text that is set using the command setText() when I click on the text field...So I can also do it if there is a callback function that manage the click on the text field....I try to use onClickWidget() but enter on this function only when I close the text field and not when I click on It...If you now that there are any other solution it will be appreciated...Thank you for all...

  • That would be what setHint() is for... but as you have demonstrated, this function isn't compatible with APWidgets / Processing. To implement similar functionality, you would need something rather hacky such as what you have described.

  • But you have any solution in order to receive my purpose?

  • edited November 2013

    It's going to be hacky... keep that in mind. Something like this (assuming it works with APWidgets, and assuming that I didn't make any silly errors - this is untested):

    //Global
    boolean usingHint = true; //Might need to change this depending on your situation
    final String hintText = "default"; //This is the hint text, customize as necessary
    
    //setup(), after textField is initialized
    textField.setOnFocusChangedListener(new android.view.View.OnFocusChangeListener() {
      public void onFocusChange(View view, boolean hasFocus) {
        APEditText editText = (APEditText) view;
        String curText = editText.getText().toString();
    
        if(hasFocus) {
          if(usingHint) {
            editText.setText("");
            usingHint = false;
          }
        } else {
          if(curText.length() == 0) {
            editText.setText(hintText);
            usingHint = true;
          }
        }
    }});
    
  • edited November 2013

    Maybe you could do whatever you want to do without apwidgets? But it would work in eclipse.

  • edited November 2013

    @mcspud, mixing native UI components with Processing's PApplet Activity implementation is a bad idea. APWidgets was created to serve as a gateway between these two worlds, but apparently its implementation isn't complete.

  • Yea, but APWidgets does work well for sound.

  • Hi I use your code but the error is always the same because OnFocusChangeListener() is not implemented...maybe I`ll missing to include some library?

  • The package name should be android.view.View.OnFocusChangeListener, but this was included in the code. What error message do you get, exactly? Do you mean that APWidgets doesn't include this functionality? If that is the case, then this... isn't possible.

  • edited December 2013

    I try but the error is always the same..I also try to create a new edit text uising this code

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnKeyListener;
    import android.widget.EditText;
    import android.widget.Toast;
    
    EditText edittext;
    //EditText editext;
    
    
    
    
    // @ Override
     public void onCreate(Bundle savedInstanceState) {
       println("//////");
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      edittext = (EditText) findViewById(R.id.edit_message);
      //edittext.setVisibility(View.INVISIBLE);
      //edittext.setVisibility(View.VISIBLE);
     }
    
    
    void setup() {
    println("test");
    }
    
    void draw() {
    println("test");
    }
    

    and this strings file (xml)

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">My First App</string>
        <string name="edit_message">Enter a message</string>
        <string name="button_send">Send</string>
        <string name="action_settings">Settings</string>
        <string name="title_activity_main">MainActivity</string>
        <string name="title_activity_display_message">My Message</string>
    </resources>
    

    and main file (xml)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <EditText android:id="@+id/edit_message"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/edit_message" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send" />
    </LinearLayout>
    

    but the problem is that create the text field as I want but don't enter on the setup and draw function.

    How can I solve this alternative problem?

    thank you

  • What error do you get, exactly? Is there a stack trace associated with the error? My guess is that, again, APWidgets hasn't implemented this functionality and you have encountered a dead end.

  • I think so...but why If I using the code that I posted yesterday program doesn`t enter in the function setup() end draw()?

  • I'm afraid that I don't exactly understand what you mean. Perhaps there is an error in the XML files that is causing the app to not load. Do you get a stack trace? If so, what is it?

  • I mean that the code posted above create a text field but over write the default layout of processing and don`t do the function void setup() and void loop() and all the stuff that are inside of it.

  • It looks like you aren't using APWidgets anymore. Remember that Processing and native components aren't compatible.

    As a side note, loop() hasn't been in use for a while now (but is still used in Arduino). You probably meant draw()...

  • yes sorry i mean draw....after that at the end i solve my problem apporting some little modify to apwidgets library but now I dont now how to include the new library in a jar file...you know how can I do that?

  • In what way have you modified the APWidgets library? If you use Eclipse library template, you should be able to run the ANT build script on it. Perhaps you can contact the author of APWidgets with your modifications if you think that they would improve the project.

  • Hi sorry for the late...I try to contact the author weeks ago but they didn`t answer to me...IWhat I modify is the source file adding code for use hint...but now I have to include all the source file in my sketch folder...I f i post all the new source file you can create the new jar file?... thank you in advance

  • Wait... is the library source stored as Java files (.java) or Processing files (.pde)? If you have Java files, then you can compile them in Eclipse. In fact, you can even compile them from the command line using the JDK tools. Sending them to someone else to build them would be overkill...

Sign In or Register to comment.