Pages

Tuesday, September 20, 2016

Android Example- Simple compount control (Clear Text of EditText programmatically)


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/Edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Clear" />

</LinearLayout>

MainActivity.java
package in.kantapp.example31;

import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new Clearable_Edit_Text(this));
        Log.v("oncreate()", "finished");
    }



}

Clearable_Edit_Text.java
package in.kantapp.example31;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class Clearable_Edit_Text extends LinearLayout {

    // Define the elements or the child controls of our compound view
    private EditText edittext;
    private Button clear_buuton;

    public Clearable_Edit_Text(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

        String inflatorservice = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li;

        li = (LayoutInflater) getContext().getSystemService(inflatorservice);
        /*
         * inflate the resource R.layout.clearable_edit_text in the context of
         * this view and automatically attach to this view(true is to
         * automatically attach to this view)
         */
        li.inflate(R.layout.activity_main, this, true);

        edittext = (EditText) findViewById(R.id.Edittext);
        clear_buuton = (Button) findViewById(R.id.button1);

        hookbutton();
    }

    private void hookbutton(){

        clear_buuton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                edittext.setText("");
                Log.v("clearButton", "clicked");

            }
        });
    }
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example31">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

No comments:

Post a Comment