Feb 15, 2011

Menu,Spinner,RadioButtons,AutoCompleteTextView

main page :
// mainActivity name : MyViews.java
/*
his View is specifically helpful at making the most of the
limited space available to the Android main screen.
 */
package com.MyViews;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MyViews extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   // to calling main.xml class
       
        }
    // adding buttons to main menu
    /** Select statement to handle calls
    to specific menu items */

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
      super.onCreateOptionsMenu(menu);
      // adding each button to menu with some prioritywise
      menu.add(0, 0, 0, "AutoComplete");
      menu.add(0, 1, 0, "Button");
      menu.add(0, 2, 0, "CheckBox");
      menu.add(0, 3, 0, "EditText");
      menu.add(0, 4, 0, "RadioGroup");
      menu.add(0, 5, 0, "Spinner");
      // menuobject.add(zero,menuindex,0)
     // menu.add(0, 6, 0, "SRavan");
      // if u add more than 6 simple it displays some arrow mark to get all details
     
      return true;
    }
   
    // selecting options from main menu
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        //option selected method will be fired when u selected each menu item simple it returns menu index values
        switch(item.getItemId()){
        //int getItemId() : it returns menu id values which u selected
       case 0:
            showAutoComplete();  
            return true;
        case 1:
            showButton();
            return true;
        case 2:
            showCheckBox();
            return true;
        case 3:
            showEditText();
            return true;
        case 4:
            showRadioGroup();
            return true;
        case 5:
            showSpinner();
            return true;
       
        }
        return false;
    }
    // calling different views what  you selected
   public void showAutoComplete(){
   Intent autocomplete=new Intent(this,AutoComplete.class);
   startActivity(autocomplete);
    }
    public void showButton(){
              Intent button=new Intent(this,testButton.class);
               startActivity(button);
            }
    public void showCheckBox(){
        Intent checkbox=new Intent(this,testCheckBox.class);
        startActivity(checkbox);
    }
    public void showEditText(){
        Intent edittext=new Intent(this,testEditText.class);
        startActivity(edittext);
    }
    public void showRadioGroup(){
        Intent radiogroup=new Intent(this,testRadioGroup.class);
        startActivity(radiogroup);
    }
    public void showSpinner(){
              Intent spinner=new Intent(this,testspinner.class);
               startActivity(spinner);
            }
//    private int getItemId() {
//        // TODO Auto-generated method stub
//        return 0;
//    }
   
 }

// after running this project simply press menu button to see  your menu options
--------------------------------------------------
main XML file :( It Contains only one image )

***************


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

// AutoCompelete.java

package com.MyViews;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
public class AutoComplete extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.autocomplete); // calling autocomplete Xml file
//        n the first line, you are taking the string array you created and assigning it to an
//        ArrayAdapter named monthArray. Next, you instantiate your AutoCompleteTextView
//        by locating it in the .xml layout file. Finally, you use the setAdapter( ) method to assign
//        the monthArray ArrayAdapter to the AutoCompleteTextView.

        ArrayAdapter<String> monthArray = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Months);
        final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.testAutoComplete);
        // adding array adapter class to AutoCompleteTextView
        textView.setAdapter(monthArray);
        final Button changeButton = (Button) findViewById(R.id.autoCompleteButton);
        changeButton.setOnClickListener(new Button.OnClickListener() {
              public void onClick(View v){
                  changeOption(textView);
              }
        });
        final Button changeButton2 = (Button) findViewById(R.id.textColorButton);
        changeButton2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v){
                  changeOption2(textView);
            }
     });
 }
   
//    AutoCompleteTextView so that it contains a list
//    of the months of the year. When a user types into the box, it will anticipate which month
//    they are trying to enter. Given that the AutoCompleteTextView will contain a list of the
//    months, you need to create a list that can be assigned to the AutoCompleteTextView.

     static final String[] Months = new String[]{
            "January","February","March","April","May","June","July","August","September","October","November","December"};
     public void changeOption(AutoCompleteTextView text){
            if (text.getHeight()==200){
               text.setHeight(30);// changing text field height from 200 to 30
          }
            else{
              text.setHeight(200); // changing text field height from 30 to 200
          }
}
    public void changeOption2(AutoCompleteTextView text){
          text.setTextColor(Color.CYAN);  // changing text colour from default colour to cyan
        }
}

autocompleteview.xml
-------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <AutoCompleteTextView android:id="@+id/testAutoComplete"
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
                          android:text="sravan"
                          />
    <Button android:id="@+id/layoutButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="dis is the test btn"/>
    <Button android:id="@+id/autoCompleteButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Change Layout"/>
    <Button android:id="@+id/textColorButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Change Text Color"/>
</LinearLayout>

-------------------------------------------------------------------
package com.MyViews;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class testButton extends Activity{
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.button);
         //  final Button button=(Button)findViewById(R.id.testButton);
        final Button changeButton=(Button)findViewById(R.id.layoutButton);
         changeButton.setOnClickListener(new OnClickListener() {
   
            @Override
            public void onClick(View arg0) {
                changeOption(changeButton);
            }
        });
        final Button changeButton2=(Button) findViewById(R.id.textColorButton);
        changeButton2.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                changeOption2(changeButton2);
            }
        });
    }
    public void changeOption(Button button){
        if(button.getHeight()==100){
            button.setHeight(30);
        }
        else{
            button.setHeight(100);  // changing button height
        }
    }
    public void changeOption2(Button Button){
        Button.setTextColor(Color.RED);  // changing text colour
    }
}

button.xml
-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<Button android:id="@+id/testButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the test Button"/>
<Button android:id="@+id/layoutButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Layout"/>
<Button android:id="@+id/textColorButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Text Color"/>
</LinearLayout>
    
**********************************
package com.MyViews;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class testCheckBox extends Activity{
      @Override
      public void onCreate(Bundle icicle){
          super.onCreate(icicle);
          setContentView(R.layout.checkbox);
          final CheckBox checkbox=(CheckBox) findViewById(R.id.testCheckBox);  // checkbox
          final Button changeButton=(Button) findViewById(R.id.layoutButton);
          changeButton.setOnClickListener(new Button.OnClickListener(){
             public void onClick(View v){
                 changeOption(checkbox);
             }
          });
          final Button changeButton2=(Button) findViewById(R.id.textColorButton);
          changeButton2.setOnClickListener(new Button.OnClickListener(){
              public void onClick(View v){
                  changeOption2(checkbox);
              }
          });
         
      }
      public void changeOption(CheckBox checkbox){
          if(checkbox.getHeight()==100){
              checkbox.setHeight(30);
          }
          else{
              checkbox.setHeight(100);
          }
      }
     
      public void changeOption2(CheckBox checkbox){
          checkbox.setTextColor(Color.RED);
      }
}
---------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<CheckBox android:id="@+id/testCheckBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the test CheckBox"/>
<Button android:id="@+id/layoutButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Layout"/>
<Button android:id="@+id/textColorButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Text Color"/>
</LinearLayout>
************************************************************
package com.MyViews;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class testEditText extends Activity{
     @Override
     public void onCreate(Bundle icicle){
         super.onCreate(icicle);
         setContentView(R.layout.edittext);
     final EditText edittext=(EditText) findViewById(R.id.testEditText);
     final Button changeButton=(Button) findViewById(R.id.layoutButton);
     changeButton.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            changeOption(edittext);
        }
     });
     final Button changeButton2=(Button) findViewById(R.id.textColorButton);
     changeButton2.setOnClickListener(new Button.OnClickListener(){
         public void onClick(View v){
             changeOption2(edittext);
         }
      });   
    }
     public void changeOption(EditText edittext){
         if(edittext.getHeight()==100){
             edittext.setHeight(30);
         }
         else{
             edittext.setHeight(100);
         }
     }
     public void changeOption2(EditText edittext){
         edittext.setTextColor(Color.RED);
     }
}
----------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    >
        <EditText android:id="@+id/testEditText"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  />
        <Button android:id="@+id/layoutButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Change Layout"/>
        <Button android:id="@+id/textColorButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Change Text Color"/>
</LinearLayout>
-------------------------------------------------------------------------------
testRadioGroup.java
********************
package com.MyViews;

import android.app.Activity;

public class testRadioGroup extends Activity{
     @Override
     public void onCreate(Bundle icicle){
         super.onCreate(icicle);
         setContentView(R.layout.testradiogroup);
         final RadioGroup radiogroup=(RadioGroup) findViewById(R.id.testRadioGroup);
         final Button changeButton=(Button) findViewById(R.id.enableButton);
         changeButton.setOnClickListener(new Button.OnClickListener(){
             public void onClick(View v){
                 changeOption(radiogroup);
             }
         });
         final Button changeButton2    =(Button) findViewById(R.id.backgroundColorButton);
         changeButton2.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                changeOption2(radiogroup);
            }
         });
     }
     public void changeOption(RadioGroup radiogroup){
         if(radiogroup.isEnabled()){
             radiogroup.setEnabled(false);
         }
         else{
             radiogroup.setEnabled(true);
         }
     }
     public void changeOption2(RadioGroup radiogroup){
         radiogroup.setBackgroundColor(Color.RED);
     }
}

testradiogrou.xml
----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
         <RadioGroup android:id="@+id/testRadioGroup"
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content"
                     >
                     <RadioButton android:text="radio 1"
                                  android:id="@+id/radio1" />
                     <RadioButton android:text="radio 2"
                                  android:id="@+id/radio2" />
         </RadioGroup>
         <Button android:id="@+id/enableButton"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="set is enabled"
                 />
         <Button android:id="@+id/backgroundColorButton"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="change background color"
                 />
</LinearLayout>

--------------------------------------------------------------------------
testspinner.java
package com.MyViews;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class testspinner extends Activity{
     @Override
     public void onCreate(Bundle icicle){
         super.onCreate(icicle);
              setContentView(R.layout.testspinner);

         final Spinner spinner=(Spinner)findViewById(R.id.testSpineer);
       
         //TextView tv=(TextView)findViewById(R.id.tv);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                    this,R.array.Months, android.R.layout.simple_spinner_item);

       
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
         spinner.setAdapter(adapter);
       
         final Button changeButton= (Button) findViewById(R.id.enableButton);
   
         changeButton.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
               
                changeOption(spinner);
           
            }
        });
         final Button changeButton2= (Button) findViewById(R.id.backgroundColorButton);
         changeButton2.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                changeOption2(spinner);
            }
         });
     }
//     static final String[] Months=new String[] {"January","February","March","April","May","June","July","August",
//                                  "September","October","November","December"};
     public void changeOption(Spinner spinner){
         Log.w(" sp7","sp7");
         if(spinner.isEnabled()){
                spinner.setEnabled(false);
            }
         else{
             spinner.setEnabled(true);
         }
     }
     public void changeOption2(Spinner spinner){
         spinner.setBackgroundColor(Color.RED);
     }
}

--------
testspinner.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
         <Spinner android:id="@+id/testSpineer"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  />
         <Button android:id="@+id/enableButton"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="set is enabled"
                 />
         <Button android:id="@+id/backgroundColorButton"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="change background color"
                 />
                 <TextView
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:id="@+id/tv"
                 />
</LinearLayout>





*******************
The main important is modifying manifset.xml  file
code look like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.MyViews"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyViews"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
          <activity android:name=".AutoComplete" android:label="AutoComplete">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".testButton" android:label="TestButton">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".testCheckBox" android:label="TestCheckBox">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".testEditText" android:label="TestEditText">
            <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <action android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".testRadioGroup" android:label="TestRadioGroup">
            <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <action android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".testspinner" android:label="TestSpinner">
            <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <action android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>



Note :
after running this project successfully first it displays  one image you should press menu button in android key board
main page






   



No comments:

Post a Comment