Pages

Saturday, October 27, 2012

Simple Example to move one activity to another in android


First Create your 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"
android:gravity="center">
<EditText
      android:id="@+id/et_data"
      android:hint="EditText"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
</EditText>
<Button
     android:text="Next Screen"
     android:id="@+id/next_screen_btn"
    android:layout_width="wrap_content"
 android:layout_height="wrap_content"
android:layout_gravity="center">
</Button>

</LinearLayout>



Define your first Activity screen


package but.clk;

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

public class clicking extends Activity
{
private Button nextBtn;
private EditText etData;
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    nextBtn=(Button)findViewById(R.id.next_screen_btn);
    etData=(EditText)findViewById(R.id.et_data);
    nextBtn.setOnClickListener(new OnClickListener() {
   
      @Override
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
        String Data=etData.getText().toString();
        Intent intent=new Intent(clicking.this,NextScreen.class);
        intent.putExtra("DATA",Data );
        startActivity(intent);
      }
    });
 
  }

}




Second Activity 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">
<TextView android:text="text"
android:id="@+id/data_text"
android:layout_width="fill_parent"
 android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:textColor="#ff00ff">
</TextView>
</LinearLayout>




Define your next Activity screen

package but.clk;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class NextScreen extends Activity
{
  private TextView tvText;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nextview);

    String Data=getIntent().getExtras().getString("DATA");
    tvText=(TextView)findViewById(R.id.data_text);
    tvText.setText(Data);
  }
}





and don't forget to add second activity in manifest file
<activity android:name=".NextScreen"/>

Your manifest file



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="but.clk"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".clicking"
                  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=".NextScreen"/>
    </application>
</manifest>



















Android Simple Tutorial to Send Data from First Activity to Second Activity



First Create your 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"
android:gravity="center">
<EditText
     android:id="@+id/et_data"
     android:hint="EditText"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
</EditText>
<Button
    android:text="Next Screen"
    android:id="@+id/next_screen_btn"
  android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</Button>

</LinearLayout>



Define your first Activity screen


package but.clk;

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

public class clicking extends Activity
{
private Button nextBtn;
private EditText etData;
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    nextBtn=(Button)findViewById(R.id.next_screen_btn);
    etData=(EditText)findViewById(R.id.et_data);
    nextBtn.setOnClickListener(new OnClickListener() {
   
      @Override
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
        String Data=etData.getText().toString();
        Intent intent=new Intent(clicking.this,NextScreen.class);
        intent.putExtra("DATA",Data );
        startActivity(intent);
      }
    });
 
  }

}




Second Activity 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">
<TextView android:text="text"
android:id="@+id/data_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:textColor="#ff00ff">
</TextView>
</LinearLayout>




Define your next Activity screen

package but.clk;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class NextScreen extends Activity
{
  private TextView tvText;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nextview);

    String Data=getIntent().getExtras().getString("DATA");
    tvText=(TextView)findViewById(R.id.data_text);
    tvText.setText(Data);
  }
}





and don't forget to add second activity in manifest file
<activity android:name=".NextScreen"/>

Your manifest file



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="but.clk"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".clicking"
                  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=".NextScreen"/>
    </application>
</manifest>




















Saturday, September 8, 2012

Android Spinner Example



package sample.spin;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class SpinnerEx extends Activity implements OnItemSelectedListener {
 
String[] listcontent={"c","java","php","c++","jsp","c#","servlet","struts"

,"c","java","php","c++","jsp","c#","servlet","struts"};

TextView tv;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.text);
        Spinner spin=(Spinner)findViewById(R.id.spin_id);
        spin.setOnItemSelectedListener( this);
 ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listcontent);

    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(aa);
    }



public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub

tv.setText(listcontent[position]);

}



public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

tv.setText("");
}
}


main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 
    xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
   
    android:id="@+id/text"
    />
    <Spinner
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spin_id"
    />
</LinearLayout>


Android Flipper Example


Flipper class is used for simply flip the views.

package example.flip;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

public class FlipperEx extends Activity {
   Button b;
   ViewFlipper flip;
   LinearLayout ll;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       ll=(LinearLayout)findViewById(R.id.l_l);
       
        b=(Button)findViewById(R.id.button2);
        flip=(ViewFlipper)findViewById(R.id.viewFlipper1);

    }

    public void ClickHandler(View v)
    {

    flip.showNext();
     //flip.setFlipInterval(1000);
         // flip.setAutoStart(true);
   // flip.startFlipping();
    b.setGravity(Gravity.BOTTOM);
    ll.setGravity(Gravity.CENTER);
    }
    }

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"
    android:gravity="top"
    android:id="@+id/l_l">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

<EditText android:text="EditText"
android:layout_height="wrap_content"
android:id="@+id/editText1"
 android:layout_width="match_parent"
 android:gravity="right">
 </EditText>
<Button android:text="Button"
android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:onClick="ClickHandler"
 android:gravity="top">
 </Button>
<ViewFlipper android:layout_width="match_parent"
 android:id="@+id/viewFlipper1"
  android:layout_height="wrap_content">

<TextView
android:text="first text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="second text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="third text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:text="fourth one is button"
android:id="@+id/button1"
 android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  </Button>


</ViewFlipper>
</LinearLayout>



Android Custom Dialog Box Example



create your 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"
    />
<Button android:text="Button"
android:id="@+id/button1"
android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 </Button>
</LinearLayout>


custom.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
   android:orientation="vertical">
    <ImageView android:layout_height="wrap_content"
    android:id="@+id/image"
     android:src="@drawable/bunch"
      android:layout_width="wrap_content">
      </ImageView>
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/image"
    android:id="@+id/text"
    >
    </TextView>
    <Button android:text="Button"
    android:id="@+id/button"
    android:layout_below="@id/image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    </Button>
</RelativeLayout>


create your Custom Dialog Activity


package example.custom;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DialogExample extends Activity {
    
Context con=this;
Button b1;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
    b1=(Button)findViewById(R.id.button1);
    
    b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(con);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.day);
Button b=(Button)dialog.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
});
    
    }
}