Results 1 to 2 of 2
  1. #1

    How to edit objects defined in main.xml via java call in my .java file?

    Hello, this is my first post here. Have done hundreds of posts, mostly answers in linuxquestions.org, but nothing here yet.
    I'm starting to develop using android SDK 1.2, and struggle to understand something.

    I've used a basic source that uses xml to define a UI and gets it displayed using setContentView(R.layout.main);.

    Now in my Java program I'd like to add some code to modify some of the objects I defined in my xml file.

    I understand Java, the object concept, methods, etc ... but I don't know which exact syntax to use, and which id my object actually is.

    My xml code includes:
    Code:
    <EditText android:layout_width="match_parent" android:id="@+id/editText1" android:layout_height="wrap_content" android:text="@string/saisie">
        <requestFocus></requestFocus>
    </EditText>
    my .java includes:
    Code:
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
            setContentView(R.layout.main);
        }
    My question is: how do I modify the .java code to make the EditText object display "hello" instead of what is statically defined in my strings file?

    I know there is not a big practical use in the example code I am doing, but it will help me do lots of other more interesting things.

    Thanks

  2. #2
    Member
    Join Date
    Jul 2011
    Posts
    2

    Original Poster
    I added 2 lines in my onCreate in my .java file. It now is:
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    EditText et = (EditText)findViewById(R.id.editText1);
    et.setText("some text");

    }
    And it works



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •