Sunday, 20 January 2013

Working with Toggle Button

Screen Shots:








1.main.xml:

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

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="150dp"
        android:textOff="OFF"
        android:textOn="ON"
        android:textSize="20dp" />

</LinearLayout>

2.Activity:

package com.venkool.togglebutton;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class ToggleButtonActivity extends Activity {
     /** Called when the activity is first created. */

     ToggleButton toggleButton;

     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);

          // Get the Reference for ToggleButton
    toggleButton = (ToggleButton)findViewById(R.id.toggleButton);

          // set Listener for ToggleButton (CheckedListener)
    toggleButton.setOnCheckedChangeListener(new                                                       OnCheckedChangeListener() {

         public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                   // TODO Auto-generated method stub

                   if (isChecked) {
         Toast.makeText(getApplicationContext(),
                 "ToggleButton Staus ==> ON", Toast.LENGTH_LONG)
                                  .show();
                   } else {
         Toast.makeText(getApplicationContext(),
                 "ToggleButton Staus ==> OFF", Toast.LENGTH_LONG)
                                  .show();
                   }
              }
          });
     }
}

Saturday, 19 January 2013

Working with EditText

Demo:



1)main.xml: 
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="Enter your Name"
        android:textColor="#ffffff"
        android:textSize="25dp" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="#000000" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="Show Entered Name"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textColor="#ffffff"
        android:textSize="25dp" />

</LinearLayout>


2.Activity:
 
package com.venkool.edittext;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EditTextActivity extends Activity {
      /** Called when the activity is first created. */

      TextView textview;
      EditText edittext;
      Button button;

      String enteredName;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            edittext = (EditText) findViewById(R.id.edittext);

            // 2nd TextView Reference
            textview = (TextView) findViewById(R.id.textview);

            // set Listener for Button
            button = (Button) findViewById(R.id.button);

            button.setOnClickListener(new OnClickListener() {

                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        // Getting entered data in EditText
                        enteredName = edittext.getText().toString();

                        // Set the EditText data to TextView
                        textview.setText(enteredName);
                  }
            });

      }
}


3)Download this Project Click Here