Blog purpose for android basic example for android app developer. any query please my contact

Monday 29 July 2024

Android studio to Create and use a Compound Control

Android studio to Create and use a Compound Control 


In this Tutorial Today Learn Android studio to Create and use a Compound Control 

Follow This Example.


1.Main Activity.java:

import android.os.Bundle;

import android.app.Activity;

import android.util.Log;

import android.view.Menu;

 

public class MainActivity extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(new Clearable_Edit_Text(this));

        Log.v("oncreate()", "finished");

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

 

}



2.Clearable_Edit_Text.java:


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.clerable_edit_text, 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");

 

            }

        });

    }

}


3.ClearableEdittext.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>


No comments:

Post a Comment