SuperCoolDude1337
SuperCoolDude1337

Reputation: 95

Java Android Dev

So this might be a stupid question but I'm new to Android Dev and can't really understand what this is. I mean, it runs just fine without that one line. But is it like a holding variable then? If someone could maybe give me an example about why this is needed or an explanation that would be great.

android:id="@+id/edit_message"

My entire code is here:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myfirst.MainActivity" >



     <EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        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>

Upvotes: 1

Views: 94

Answers (1)

Rawa
Rawa

Reputation: 13906

The id is an identifier to help you get hold of your view from you code (activities and classes)

android:id="@+id/edit_message"

In an activity you can grab hold of this textview (if it is shown) with the following code:

TextView editMessage = (TextView) findByView(R.id.edit_message);

When you got hold of the TextView you can change is the values of it from your code e.g.

editMessage.setText("new text"); 

So why do we use XML when you can create all the views in your code? It is way simpler to program and it is easy to read. You can also create new XML field which dynamically adapts to the language of the system for example.

Upvotes: 1

Related Questions