1

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:

<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:

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.

2 Answers 2

3

In your onCreate() or another function, add:

 EditText et = (EditText)findViewById(R.id.editText1);
 et.setText("some text");
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. I added what you mention after setContentView(R.layout.main); it works now.
0

Change to this (delete the android:text=@string/..)

<EditText 
 android:layout_width="match_parent" 
 android:id="@+id/editText1" 
 android:layout_height="wrap_content" 
 <requestFocus></requestFocus>
</EditText>

And add the following in onCreate()

 EditText text = (EditText)findViewById(R.id.editText1);
 text.setText("some text");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.