Pages

Sunday, 10 February 2013

  Android Blink Text :


Main Activity :

import android.app.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        blinkText();
     
    }
 
    private void blinkText(){
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            public void run() {
            int timeToBlink = 1000;  
            try{
            Thread.sleep(timeToBlink);
        }catch (Exception e) {
       
        }
            handler.post(new Runnable() {
                public void run() {
                    TextView txt = (TextView) findViewById(R.id.tv);
                    if(txt.getVisibility() == View.VISIBLE){
                        txt.setVisibility(View.INVISIBLE);
                    }else{
                        txt.setVisibility(View.VISIBLE);
                    }
                    blinkText();
                }
                });
            }}).start();
   }
 
    public void blinkText2(){
    TextView myText = (TextView) findViewById(R.id.tv );

    Animation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(50);
    anim.setStartOffset(20);
    anim.setRepeatMode(Animation.REVERSE);
    anim.setRepeatCount(Animation.INFINITE);
    myText.startAnimation(anim);
    }
 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
 
 
}

main.xlm :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".MainActivity"
        android:id="@+id/tv" />
    
</RelativeLayout>




Android Blink Animation as LED Bulb :


 Create  a new folder in res  as anim-->

 tween.xml

<set >
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="100"
    android:repeatMode="reverse"
    android:repeatCount="infinite" />
</set>

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="80dp" >

        <FrameLayout
            android:id="@+id/rightIcon"
            android:layout_width="30.0dip"
            android:layout_height="30.0dip" >

            <ImageView
                android:id="@+id/bsecond"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_red"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/afirst"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_yellow"
                android:visibility="visible" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/rightIcon"
            android:layout_width="30.0dip"
            android:layout_height="30.0dip" >

            <ImageView
                android:id="@+id/bfourth"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_green"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/athird"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_red"
                android:visibility="visible" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/rightIcon"
            android:layout_width="30.0dip"
            android:layout_height="30.0dip" >

            <ImageView
                android:id="@+id/bsix"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_red"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/afifth"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_green"
                android:visibility="visible" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/rightIcon"
            android:layout_width="30.0dip"
            android:layout_height="30.0dip" >

            <ImageView
                android:id="@+id/bsecond"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_red"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/cfirst"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_yellow"
                android:visibility="visible" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/rightIcon"
            android:layout_width="30.0dip"
            android:layout_height="30.0dip">

            <ImageView
                android:id="@+id/bsix"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_red"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/cfifth"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_green"
                android:visibility="visible" />
        </FrameLayout>

  </TableRow>

</RelativeLayout>


MainActivity


import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends Activity {

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

private void animate() {

ImageView myImageView4 = (ImageView) findViewById(R.id.afirst);
ImageView myImageView5 = (ImageView) findViewById(R.id.athird);
ImageView myImageView6 = (ImageView) findViewById(R.id.afifth);
ImageView myImageView7 = (ImageView) findViewById(R.id.cfirst);
ImageView myImageView9 = (ImageView) findViewById(R.id.cfifth);


Animation myFadeInAnimation = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.tween);

myImageView4.startAnimation(myFadeInAnimation);
myImageView5.startAnimation(myFadeInAnimation);
myImageView6.startAnimation(myFadeInAnimation);
myImageView7.startAnimation(myFadeInAnimation);
myImageView9.startAnimation(myFadeInAnimation);

}

}

OutPut :


     





Monday, 21 January 2013

Android Simple ToggleButton Saving the ON/OFF Example


I use two Activity ,in first used  button in second activity ToggleButton.

........................Main.Xml..............


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginRight="75dp"
        android:layout_marginTop="120dp"
        android:text="Button" />

</RelativeLayout>

...........................Second.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" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" />

</LinearLayout>


..............................MainActivity.java.....................


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), second.class));
}
});
}
}


.............................second.java........................................






import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class second extends Activity {

ToggleButton button;
SharedPreferences app_preferences;
SharedPreferences.Editor editor;
private static final String TOGGLE = "abc";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
button = (ToggleButton) findViewById(R.id.toggleButton1);
app_preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
editor = app_preferences.edit();

if (app_preferences.getBoolean(TOGGLE, true)) {
button.setChecked(true);
} else {
button.setChecked(false);
}
// TODO Auto-generated method stub

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (button.isChecked()) {
button.setChecked(true);
editor.putBoolean(TOGGLE, true);
} else {
button.setChecked(false);
editor.putBoolean(TOGGLE, false);
}
editor.commit();
}
});

}

}
Android Simple ToggleButton Example

SOURCE CODE [main.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" >

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
    
<ToggleButton android:id="@+id/togglebutton"
android:layout_width="150px"
android:layout_height="50px"
android:textOn="ON"
android:textOff="OFF" />

</LinearLayout>

SOURCE CODE [ToggleButtonExample.java] is

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class ToggleButtonExample extends Activity
{

ToggleButton tb;

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

                                tb = (ToggleButton) findViewById(R.id.togglebutton);
                                tb.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getBaseContext(),
"Button is "+tb.getText().toString(),
Toast.LENGTH_LONG).show();
                                                }
                                });
                }
}




The OUTPUT










Saturday, 21 April 2012

Custom Spinners







Refer to the exercise "HelloAndroid with Spinner", it's a basic spinner with default format to display simple text in the spinner. Current exercise is modified to have custom display with icon in the spinner, actually it is Spinner version of another exercise "ListView, with icon".






-------------------------------------------------------------Code------------------------------------------------------------------------



package com.kiran;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class CustomSpinnerDemoActivity extends Activity {
String[] strings = {"CoderzHeaven","Google",
"Microsoft", "Apple", "Yahoo","Samsung"};

String[] subs = {"Heaven of all working codes ","Google sub",
"Microsoft sub", "Apple sub", "Yahoo sub","Samsung sub"};

int arr_images[] = { R.drawable.coderzheaven,
R.drawable.google, R.drawable.microsoft,
R.drawable.apple, R.drawable.yahoo, R.drawable.samsung};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner mySpinner = (Spinner)findViewById(R.id.spinner1);
        mySpinner.setAdapter(new MyAdapter(CustomSpinnerDemoActivity.this, R.layout.row, strings));
    }

    public class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int textViewResourceId, String[] objects) {
    super(context, textViewResourceId, objects);
    }

    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
    return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater=getLayoutInflater();
    View row=inflater.inflate(R.layout.row, parent, false);
    TextView label=(TextView)row.findViewById(R.id.company);
    label.setText(strings[position]);

    TextView sub=(TextView)row.findViewById(R.id.sub);
    sub.setText(subs[position]);

    ImageView icon=(ImageView)row.findViewById(R.id.image);
    icon.setImageResource(arr_images[position]);

    return row;
    }
    }
   }

---------------------------------------------------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:orientation="vertical" >


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>


-------------------------------------------------------------Row 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="wrap_content"
    android:orientation="vertical"
    android:padding="3dip" >


    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <TextView
        android:id="@+id/company"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="2dip"
        android:layout_toRightOf="@+id/image"
        android:padding="3dip"
        android:text="CoderzHeaven"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/company"
        android:layout_marginLeft="5dip"
        android:layout_toRightOf="@+id/image"
        android:padding="2dip"
        android:text="Heaven of all working codes" />


</RelativeLayout>








Friday, 20 April 2012

Shared Preferences

how data can be stored in SQLDB. However, many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs. 

Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity (which is not discussed here). 

The context object lets you retrieve SharedPreferences through the methodContext.getSharedPreferences(). 


------------------------------------------CODE------------------------------------------------------

package com.kiran;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SharedpreActivity extends Activity {



Button save, show;
EditText enter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button save = (Button) findViewById(R.id.button1);
Button show = (Button) findViewById(R.id.button2);


        final EditText enter = (EditText) findViewById(R.id.editText1);
       
  //-----------------------------SAVE---------------------------------------------------------//    
save.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(SharedpreActivity.this);
SharedPreferences.Editor editor = app_preferences.edit();
String text = enter.getText().toString();
editor.putString("key", text);
editor.commit();

}

});
//----------------------------------SHOW-------------------------------------------------//
show.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
TextView textView = (TextView) findViewById(R.id.textView1);
SharedPreferences app_Preferences = PreferenceManager
.getDefaultSharedPreferences(SharedpreActivity.this);
String text = app_Preferences.getString("key", "null");
            textView.setText(text);

}
});
}

}
---------------------------------------------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="@drawable/back"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="TextView" android:textColor="@color/black"/>
   
   

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="14dp"
        android:text="SHOW" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="43dp" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/editText1"
        android:text="SAVE" />



    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/fish"
        android:orientation="vertical" >

    </LinearLayout>

</RelativeLayout>