Pages

Tuesday, February 11, 2014

Android ViewPager Example


This is simple example for a viewpager in android.First add "android-support-v4.jar"  library in your libs folder.than create an activity and add this code.


public class MainActivity extends Activity implements OnClickListener {

private ViewPager viewPager;
private TextSwipping pagerAdapter;
private ArrayList<String> alText = new ArrayList<String>();
private Context context;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.welcomepage);

alText.add("Hi");
alText.add("This");

alText.add("is");

alText.add("Manu");

viewPager = (ViewPager) findViewById(R.id.welcome_text_pager);
pagerAdapter = new TextSwipping(alText, this);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrollStateChanged(int pos) {
// TODO Auto-generated method stub

if (viewPager.getCurrentItem() == 0) {
//                           set your conditions here
} else if (viewPager.getCurrentItem() == 1) {
//                           set your conditions here
} else if (viewPager.getCurrentItem() == 2) {
//                           set your conditions here
} else if (viewPager.getCurrentItem() == 3) {
//                           set your conditions here
}

}
});

}

}



add this viewpager in your welcomepage.xml  file



<android.support.v4.view.ViewPager
            android:id="@+id/welcome_text_pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />





and create Adapter for the text displaying in your viewpager.



public class TextSwipping extends PagerAdapter {

ArrayList<String> alText;
Context context;

public TextSwipping(ArrayList<String> alText, Context context) {
this.alText = alText;
this.context = context;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// View view= inflater.inflate(R.layout.textswitcher, null);


// I am using a   dynamic  text view  you could use
//a layout using inflater or use a fragment as your necessity.
TextView tvTextSwitcher = new TextView(context);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tvTextSwitcher.setTextColor(Color.parseColor("#ff78ff"));
tvTextSwitcher.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
tvTextSwitcher.setLayoutParams(lp);
tvTextSwitcher.setGravity(Gravity.CENTER_HORIZONTAL);

tvTextSwitcher.setText("  " + alText.get(position));
tvTextSwitcher.setTypeface(WidgetProperties
.setTextTypefaceRegular(context));

((ViewPager) container).addView(tvTextSwitcher);

return tvTextSwitcher;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
return null;
}
}

Saturday, October 27, 2012

Android Simple Button click Example with String Resources



Your Activity
package pro2.pro3;

import android.app.Activity;

import android.app.AlertDialog;
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;

import android.widget.Toast;

public class ButtonExample extends Activity implements OnClickListener
{

  private Button hiButton;
  private Button helloButton;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    hiButton = (Button) findViewById(R.id.hi_button);
    hiButton.setOnClickListener(this);
    helloButton = (Button) findViewById(R.id.hello_button);
    helloButton.setOnClickListener(this);
  }

  public void onClick(View v)
  {

    EditText nameField = (EditText) findViewById(R.id.name_field);
    String name = nameField.getText().toString();
    if (name.length() == 0)
    {
      new AlertDialog.Builder(this).setMessage(R.string.error_name_missing)
          .setNeutralButton(R.string.error_ok, null).show();
      return;

    }
    if (v == hiButton || v == helloButton)
    {
      int resourceId = v == hiButton ? R.string.hi_greeting
          : R.string.hello_greeting;
      String greeting = getResources().getString(resourceId, name);
      Toast.makeText(this, greeting, Toast.LENGTH_LONG).show();
      TextView greetingField = (TextView) findViewById(R.id.greeting_field);
      greetingField.setText(greeting);

    }
  }
}


Create 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:id="@+id/greeting_field"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_width="match_parent"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:text="@string/enter_your_name"
android:textColor="#ff00ff"/>
<EditText
   android:id="@+id/name_field"
   android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
   android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2">

<Button
    android:id="@+id/hi_button"
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
android:text="@string/hi_button"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#ff00ff"/>

<Button
   android:id="@+id/hello_button"
   android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_button"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#ff00ff"/>

</LinearLayout>
</LinearLayout>



Define main.xml  strings in your strings.xml file

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <string name="hello">Hello World, manu!</string>
    <string name="app_name">pro1</string>
    <string name="enter_your_name">Enter your name:</string>
    
    <string name="hi_button">Say Hi!</string>
<string name="hello_button">Say Hello!</string>

<string name="error_name_missing">Please enter your name</string>
<string name="error_ok">OK</string>

<string name="hi_greeting">Hi %s!</string>
<string name="hello_greeting">Hello %s!</string>
</resources>

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>