Android: membuat TextView diatas EditText sebagai text label


Android: membuat TextLabel diatas EditText

Kita akan memanfaatkan FrameLayout untuk menumpuk TextView diatas EdiText.

<FrameLayout
    android:id=”@+id/frmUsernameLogin”
    android:layout_width=”fill_parent”
    android:layout_height=”wrap_content” >
    <EditText
        android:id=”@+id/editUsernameLogin”
        android:text=””
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content” />
    <TextView
        android:id=”@+id/lblUsernameLogin”
        android:text=”Username”
        android:layout_gravity=”center_vertical”
        android:textSize=”20dp”
        android:layout_marginLeft=”5dp”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content” />

</FrameLayout>

<FrameLayout
    android:id=”@+id/frmPasswordLogin”
    android:layout_below=”@+id/frmUsernameLogin”
    android:layout_width=”fill_parent”
    android:layout_height=”wrap_content” >
    <EditText
        android:id=”@+id/editPasswordLogin”
        android:text=””
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content” />
    <TextView
        android:id=”@+id/lblPasswordLogin”
        android:text=”Password”
        android:layout_gravity=”center_vertical”
        android:textSize=”20dp”
        android:layout_marginLeft=”5dp”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content” />
</FrameLayout>

Untuk mengeset kode nya supaya Text Label nya Show dan Hide:

   EditText editUsernameLogin = (EditText) findViewById(R.id.editUsernameLogin);
   EditText editPasswordLogin = (EditText) findViewById(R.id.editPasswordLogin);
        
  TextView  lblUsernameLogin = (TextView) findViewById(R.id.lblUsernameLogin);
  TextView  lblPasswordLogin = (TextView) findViewById(R.id.lblPasswordLogin);
        
        editUsernameLogin.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View arg0, boolean arg1) {
                if (arg1) {
                    //Log.i(TAGS, “editUsernameLogin get Focus”);
                    lblUsernameLogin.setVisibility(View.GONE);
                } else {
                    //Log.i(TAGS, “editUsernameLogin lost Focus”);
                    lblUsernameLogin.setVisibility(View.VISIBLE);
                }
            }
        });
        
        editPasswordLogin.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View arg0, boolean arg1) {
                if (arg1) {
                    //Log.i(TAGS, “editPasswordLogin get Focus”);
                    lblPasswordLogin.setVisibility(View.GONE);
                } else {
                    //Log.i(TAGS, “editPasswordLogin lost Focus”);
                    lblPasswordLogin.setVisibility(View.VISIBLE);
                }
            }
        });

Smoga Bermanfaat.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s