Tuesday, December 27, 2011

Set Validation error for edittext in android

Set Validation error for edittext in android

Prob:

suppose user not fill the edittext filed  at the time we can show alert\error msg .

Solution:

we can solve this issue using setError().

ex code:
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn =(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                 EditText edittext =(EditText)findViewById(R.id.editText1);
                if(edittext.getText().length()==0){
                    edittext.setError("please enter the value");
                }

            }
        });
    }

screen shot:


Wednesday, December 21, 2011

Android TextToSpeech

Android TextToSpeech:

this example for text convert speech. ex: what you are passing value into texttospeech it convert speech.
android text to speech example code ;

Emulator this is working fine but mobile you have install external library speech synthesis..... form android android market.

Goto->mobile->settings->voice input and output->text-to-speech settings->install voice data.


public class TextToSpeachActivity extends Activity implements OnInitListener {
    /** Called when the activity is first created. */
    private TextToSpeech    textToSpeech;;
    EditText edt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         edt=(EditText)findViewById(R.id.edittext);
        Button btn =(Button)findViewById(R.id.button1);
        textToSpeech=new TextToSpeech(this, this);
      //  textToSpeech.setLanguage(Locale.US);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                textToSpeech.speak(edt.getText().toString(), TextToSpeech.SUCCESS, null);
            }
        });
    }
    @Override
    public void onDestroy() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onDestroy();
    }
    public void onInit(int status) {
    }
}

Monday, December 19, 2011

Android Tab Navigation Bar

Android Tab Navigation Bar  Example;

android tab host navigation bar  example  look like iphone navigation bar.

it's very usefull for page navigation very easyly and you can inside tabview GroupActivity.
adding tab to tab host;

  Drawable d = getResources().getDrawable(R.drawable.setting_onclick);
        ImageView img1 = new ImageView(this);
        img1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,    LayoutParams.FILL_PARENT));
        img1.setPadding(20, 10, 20, 10);
        img1.setImageDrawable(d);
        homeTabSpec.setIndicator(img1).setContent(mainActivity);
       
        d = getResources().getDrawable(R.drawable.setting_onclick);
        img1 = new ImageView(this);
        img1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        img1.setImageDrawable(d);
        img1.setPadding(20, 10, 20, 10);
        signinTabSpec.setIndicator(img1).setContent(mainActivity1);

       tabHost.addTab(homeTabSpec);
        tabHost.addTab(signinTabSpec);

style for onpress tab.
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_selected="false" android:drawable="@drawable/settingstrans"/>
   <item android:state_selected="true" android:drawable="@drawable/settings_onclick"  />
</selector>



dowload source code here

Android Date Picker Example

Android Date Picker Example

android sample and simple example for date picker.

creating date picker dialog.

  @Override
     protected Dialog onCreateDialog(int id) {
         switch (id) {
         case DATE_DIALOG_ID:
             return new DatePickerDialog(this,
                         mDateSetListener,
                         mYear, mMonth, mDay);
         }
         return null;
     }

date set in date picker
private DatePickerDialog.OnDateSetListener mDateSetListener =
     new DatePickerDialog.OnDateSetListener() {

         public void onDateSet(DatePicker view, int year,
                               int monthOfYear, int dayOfMonth) {
             mYear = year;
             mMonth = monthOfYear;
             mDay = dayOfMonth;
             updateDisplay();
         }
     };

updated date in text view;

private void updateDisplay() {
        tx.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mYear).append("-")
                    .append(mMonth + 1).append("-")
                    .append(mDay).append(" "));
    }

calling date picker dialog in button onclick method

datePick.setOnClickListener(new OnClickListener() {
       
        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
           
        }
    });

sample screen


full activity code;

public class DatePickerExampleActivity extends Activity {
    /** Called when the activity is first created. */
    TextView tx;
   
    private int mYear;
    private int mMonth;
    private int mDay;
    static final int DATE_DIALOG_ID = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
      tx=(TextView)findViewById(R.id.date);
      Button datePick=(Button)findViewById(R.id.button1);
      final Calendar c = Calendar.getInstance();
      mYear = c.get(Calendar.YEAR);
      mMonth = c.get(Calendar.MONTH);
      mDay = c.get(Calendar.DAY_OF_MONTH);
      updateDisplay();
     
      datePick.setOnClickListener(new OnClickListener() {
       
        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
           
        }
    });
    }
   
      private void updateDisplay() {
        tx.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mYear).append("-")
                    .append(mMonth + 1).append("-")
                    .append(mDay).append(" "));
    }

 private DatePickerDialog.OnDateSetListener mDateSetListener =
     new DatePickerDialog.OnDateSetListener() {

         public void onDateSet(DatePicker view, int year,
                               int monthOfYear, int dayOfMonth) {
             mYear = year;
             mMonth = monthOfYear;
             mDay = dayOfMonth;
             updateDisplay();
         }
     };
    
     @Override
     protected Dialog onCreateDialog(int id) {
         switch (id) {
         case DATE_DIALOG_ID:
             return new DatePickerDialog(this,
                         mDateSetListener,
                         mYear, mMonth, mDay);
         }
         return null;
     }
}
you can download sample source code here

Saturday, December 17, 2011

Android Version History

Android Version History
1.0 A-stro
1.1 B-ender
1.5 C-upcake
1.6 D-onut
2.1 E-clair
2.2 F-royo
2.3.x G-ingerbread
3.x.x H-oneycomb
4.0 I-ce Cream Sandwich

next May be Jelly bean......

 

Friday, December 16, 2011

Android NavigationBar Example

Android NavigationBar  Example
simple   android navigationbar look like iphone navigationbar.

layout code.
layout/actnavbar.xml.
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
>
    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="@drawable/navbar_background"
    >
        <RadioButton
            android:id="@+id/btnAll"
            style="@style/navbar_button"
            android:drawableTop="@drawable/navbar_allselector"
            android:text="All"
        />
        <RadioButton
            android:id="@+id/btnPicture"
            style="@style/navbar_button"
            android:drawableTop="@drawable/navbar_pictureselector"
            android:text="Pictures"
            android:layout_marginLeft="5dp"
        />
        <RadioButton
            android:id="@+id/btnVideo"
            style="@style/navbar_button"
            android:drawableTop="@drawable/navbar_videoselector"
            android:text="Videos"
            android:layout_marginLeft="5dp"
        />
         <RadioButton
            android:id="@+id/btnFile"
            style="@style/navbar_button"
            android:drawableTop="@drawable/navbar_fileselector"
            android:text="Files"
            android:layout_marginLeft="5dp"
        />
        <RadioButton
            android:id="@+id/btnMore"
            style="@style/navbar_button"
            android:drawableTop="@drawable/navbar_moreselector"
            android:text="More"
            android:layout_marginLeft="5dp"
        />
    </RadioGroup>
   
   
    <LinearLayout
        android:id="@+id/floatingmenu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:background="@drawable/laysemitransparentwithborders"
        android:orientation="vertical"
        android:layout_marginBottom="-4dp"
        android:visibility="gone"
    >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            android:text="Contacts"
            android:textColor="#ffffff"
            android:textSize="16dp"
        />
        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#ff999999"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingTop="15dp"
            android:paddingBottom="15dp"
            android:text="Calendar"
            android:textColor="#ffffff"
            android:textSize="16dp"
        />
    </LinearLayout>
</RelativeLayout>

values/style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="navbar_button">
 <item name="android:layout_width">0dp</item>
 <item name="android:layout_height">wrap_content</item>
 <item name="android:button">@null</item>
 <item name="android:background">@drawable/navbar_backgroundselector</item>
 <item name="android:gravity">center_horizontal</item>
 <item name="android:layout_weight">1</item>
 <item name="android:textSize">12dp</item>
  </style>
</resources>

Activity code


public class NavbarActivity  extends Activity {

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

        RadioButton radioButton;
        radioButton = (RadioButton) findViewById(R.id.btnAll);
        radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
        radioButton = (RadioButton) findViewById(R.id.btnPicture);
        radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
        radioButton = (RadioButton) findViewById(R.id.btnVideo);
        radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
        radioButton = (RadioButton) findViewById(R.id.btnFile);
        radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
        radioButton = (RadioButton) findViewById(R.id.btnMore);
        radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
    }

    private CompoundButton.OnCheckedChangeListener btnNavBarOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                Toast.makeText(NavbarActivity.this,"Clicked Button ! "+ buttonView.getText(), Toast.LENGTH_SHORT).show();
            }
        }
    };
}

Sample Screen Shot:

naviigation bar for android look like iphone navigation bar. download source code here.

Tuesday, December 13, 2011

Animation between one activity to another activity in android

Animation between one activity to another activity in android.
Animation between Activity A to Activity B.
anim/mainfadein.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:fromAlpha="0.0"
            android:toAlpha="1.0"
            android:duration="1000" />
anim/splashfadeout.xml
<?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
            android:interpolator="@android:anim/decelerate_interpolator"
            android:zAdjustment="top"
            android:fromAlpha="1.0"
            android:toAlpha="0.0"
            android:duration="1000" />
just u have to this code in onCreate();
overridePendingTransition(R.anim.mainfadein, R.anim.splashfadeout);                                
                             or
calling another activity.
Intent in=new Intent(this,ActivityB.class);
startActivity();
overridePendingTransition(R.anim.mainfadein, R.anim.splashfadeout);


Check out this may be help you

Related Posts Plugin for WordPress, Blogger...