Python Tutorial

Friday, December 16, 2011

Android multiple screen same layout

It is very import use common layout for multiple screen if is possible. Now I show you a simple example of use single layout in multiple screen. And also manipulate them.



Screen A
Name:
Email:

Screen B
Address:
Location:



Let we need to construct this two screen. Here two screen is very similar, except some text. Let's do it by one layout




main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
>
<TextView android:id ="@+id/labelA"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"
   android:text="Name:"
   android:layout_x="0px"
   android:layout_y="5px"
/>

<EditText android:id="@+id/edtInput"
   android:layout_x="0px"
      android:layout_y="40px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
/>

<TextView android:id ="@+id/labelB"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"
   android:text="Email:"
   android:layout_x="0px"
   android:layout_y="110px"
/>

<EditText android:id="@+id/edtInputA"
   android:layout_x="0px"
      android:layout_y="150px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
/>

<Button android:id ="@+id/btnClick"
   android:layout_width="180px"
   android:layout_height="70px"
   android:text="Open New Screen"
   android:textSize="14px"
   android:layout_x="0px"
      android:layout_y="235px"
/>

</AbsoluteLayout>



MultipleScreenSameLayout.java
package com.example.MultipleScreenSameLayout;

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

public class MultipleScreenSameLayout extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button b = (Button) findViewById(R.id.btnClick);
  b.setOnClickListener(new View.OnClickListener() {

   public void onClick(View arg0) {
    // here i call new screen;
    Intent i = new Intent(MultipleScreenSameLayout.this,
      NewScreen.class);
    startActivity(i);
   }
  });
    }
}



NewScreen.java
package com.example.MultipleScreenSameLayout;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class NewScreen extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView tvB=(TextView) findViewById(R.id.labelB);
        tvB.setText("Address:");
        
        TextView tvA=(TextView) findViewById(R.id.labelA);
        tvA.setText("Location:");
        
        Button b = (Button) findViewById(R.id.btnClick);
        b.setText("Back");
  
  b.setOnClickListener(new View.OnClickListener() {
   public void onClick(View arg0) {
    setResult(RESULT_OK);
    finish();
   }
  });
    }
}


Don't fotget add both activity on manifest file



Output Screen


Screen A



Screen B


Android multiple screen passing data through intent

For passing data one screen to another screen on android you need to pass it through the intent. This example very similar to my previous example, only extra feature is passing data one screen to another screen. Let's go to the example:

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="You are in the first Screen"
/>
<Button android:id ="@+id/btnClick"
   android:layout_width="180px"
   android:layout_height="70px"
   android:text="Open New Screen"
   android:textSize="14px"
/>

</LinearLayout>



AndroidMultipleScreenEasy.java
package com.example.AndroidMultipleScreenEasy;

import java.util.ArrayList;

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

public class AndroidMultipleScreenEasy extends Activity {
 MyNewScreen obB = new MyNewScreen();

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

  Button b = (Button) findViewById(R.id.btnClick);
  b.setOnClickListener(new View.OnClickListener() {

   public void onClick(View arg0) {
    
    ArrayList tdata = new ArrayList();
    tdata.add("This text from first screen");
    // here i call new screen, and pass the data;
    Intent i = new Intent(AndroidMultipleScreenEasy.this,
      MyNewScreen.class);
    i.putExtra("Screen1Data", tdata);
    
    startActivity(i);
   }
  });
 }
}



my_new_screen.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="You are in the New Screen, press Back for back to previous screen" />
 <Button android:id="@+id/btnClick2" android:layout_width="100px"
  android:layout_height="50px" android:text="Back" />
 <TextView android:id="@+id/myTextViev" android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
</LinearLayout>




MyNewScreen.java
package com.example.AndroidMultipleScreenEasy;

import java.util.ArrayList;

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

public class MyNewScreen extends Activity {
 AndroidMultipleScreenEasy ob;
 TextView tv;
 
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.my_new_screen);
  
  tv = (TextView) findViewById(R.id.myTextViev);
  Intent sender = getIntent();
  
  ArrayList tdata = (ArrayList) sender.getExtras().getCharSequenceArrayList("Screen1Data");
  tv.setText(tdata.get(0));
  
  Button b = (Button) findViewById(R.id.btnClick2);
  b.setOnClickListener(new View.OnClickListener() {
   public void onClick(View arg0) {
    setResult(RESULT_OK);
    finish();
   }
  });
 }
}



Output Screen:


First screen


Second screen



Saturday, January 15, 2011

Android multiple screen example

Let we want constract an application which have two different screen and we want to move one screen to another scrren.



At first we need to constract this two screen individually, both screen should extends Activity.

After build two screen you add your new screen to AndroidManifest.xml file (first screen which extends Activity, it's automatically added to AndroidManifest.xml, but all new screen should be added individually).



//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="You are in the first Screen" /> <Button android:id ="@+id/btnClick" android:layout_width="150px" android:layout_height="50px" android:text="Open New Screen" /> </LinearLayout>

//AndroidMultipleScreenEasy.java

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

public class AndroidMultipleScreenEasy extends Activity
{
 MyNewScreen obB=new MyNewScreen();
   public void onCreate(Bundle icicle)
   {
      super.onCreate(icicle);
      setContentView(R.layout.main);
      
      Button b = (Button) findViewById(R.id.btnClick);
      b.setOnClickListener(new View.OnClickListener() {
      
         public void onClick(View arg0) {
         // here i call new screen;
         Intent i = new Intent(AndroidMultipleScreenEasy.this, MyNewScreen.class);
         startActivity(i);
         } 
      });
   }
}

//my_new_screen.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="You are in the New Screen, press close for back to previous screen" /> <Button android:id="@+id/btnClick2" android:layout_width="100px" android:layout_height="50px" android:text="Close" /> </LinearLayout>

//MyNewScreen.java

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

public class MyNewScreen extends Activity
{
 AndroidMultipleScreenEasy ob;
   public void onCreate(Bundle icicle)
   {
      super.onCreate(icicle);
      setContentView(R.layout.my_new_screen);
      Button b = (Button) findViewById(R.id.btnClick2);
      b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View arg0) {
   setResult(RESULT_OK);
         finish();
         } 
      });
   }
   
   public void setOb( AndroidMultipleScreenEasy obA){
    this.ob=obA;
   }
}



Add this line to your AndroidManifest.xml file.



//AndroidManifest.xml
<activity android:name=".MyNewScreen" android:label="MyNewScreenLabel"> </activity>


output:



Screen 1


Now you can goto new screen by clicking Open New Screen button.


Image not found


Screen 2


Now you can goto previous screen by clicking Close button.


Image not found

Friday, January 14, 2011

Android spinner example

Android Spinner help user to select an item from desired items. Here i implements OnItemSelectedListener geting the Spinner selected item. I also set Spinner selected item to a text view (code: selection.setText(items[position]);
). I used LinearLayout here, if you need better positioning you can use AbsoluteLayout.


//android_spinner.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:id="@+id/selection" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="true" /> </LinearLayout>

//AndroidSpinner.java

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 AndroidSpinner extends Activity implements OnItemSelectedListener {

 TextView selection;
 Spinner spin;
 String[] items = { "bangladesh", "bangla", "bd", "australia", "japan",
   "china", "indiaA", "indiaC" };

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

  selection = (TextView) findViewById(R.id.selection);

  Spinner spin = (Spinner) findViewById(R.id.spinner);
  spin.setOnItemSelectedListener(this);

  ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, items);

  spin.setAdapter(aa);
 }

 @Override
 public void onItemSelected(AdapterView<?> parent, View v, int position,
   long id) {
  // TODO Auto-generated method stub
  selection.setText(items[position]);

 }

 @Override
 public void onNothingSelected(AdapterView<?> arg0) {
  // TODO Auto-generated method stub
  selection.setText("");

 }
}

output:



Image not found

Android autocomplete

Android autocompete is very easy. You need to create a AutoCompleteTextView. In AutoCompleteTextView you can set completionThreshold from which character you want to get autocompletion help (here i set 1, means after pressig ist character you will get help for autocomplete).


//main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/selection" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <AutoCompleteTextView android:id="@+id/edit" android:layout_width="150px" android:layout_height="wrap_content" android:completionThreshold="1" /> <TextView android:id="@+id/myTextViev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="5px" android:layout_y="110px" /> <Button android:id="@+id/myButton" android:layout_width="100px" android:layout_height="50px" android:text="Submit" android:layout_x="5px" android:layout_y="60px" /> </AbsoluteLayout>

//AutoComplete.java

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

public class AutoComplete extends Activity implements TextWatcher,
  OnClickListener {
 TextView selection;
 AutoCompleteTextView edit;
 String[] items = { "bangladesh", "india", "pakistan", "australia", "japan",
   "china", "indiaA", "indiaC" };
 Button btn;
 TextView tv;

 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);
  selection = (TextView) findViewById(R.id.selection);
  edit = (AutoCompleteTextView) findViewById(R.id.edit);
  edit.addTextChangedListener(this);

  btn = (Button) findViewById(R.id.myButton);
  btn.setOnClickListener(this);
  tv = (TextView) findViewById(R.id.myTextViev);

  edit.setAdapter(new ArrayAdapter(this,
    android.R.layout.select_dialog_singlechoice, items));
 }

 public void onTextChanged(CharSequence s, int start, int before, int count) {
  // selection.setText(edit.getText());
 }

 public void beforeTextChanged(CharSequence s, int start, int count,
   int after) {
  // needed for interface, but not used
 }

 public void afterTextChanged(Editable s) {
  // needed for interface, but not used

 }

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if (v.getId() == R.id.myButton) {
   tv.setText("get: "+edit.getText());
  }

 }
}

output:



Let you want to type bangladesh or bangla or bd, after pressed b you will get all autocomplete help


Image not found

Android radion button,Android AbsoluteLayout

For android radion button you need to create a RadioGroup. From each group
maximum one item can be selected.
Here you also find how get selected radio button.
In this example i used AbsoluteLayout for better positioning.


//main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/myTextViev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choose Dictionary:" android:layout_x="20px" android:layout_y="250px" /> <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Rock" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Scissors" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Paper" /> </RadioGroup> <Button android:id="@+id/myButton" android:layout_width="100px" android:layout_height="50px" android:text="Submit" android:layout_x="5px" android:layout_y="60px" /> </AbsoluteLayout>

//AndroidRadioButton.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class AndroidRadioButton extends Activity implements OnClickListener{
    
 TextView tv;
 Button btn;
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btn = (Button) findViewById(R.id.myButton);
        btn.setOnClickListener(this);
        
        tv = (TextView) findViewById(R.id.myTextViev);
        tv.setText("This is sample text");
        
        
    }

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  RadioButton ra=(RadioButton)findViewById(R.id.radio1);
  RadioButton rb=(RadioButton)findViewById(R.id.radio2);
  RadioButton rc=(RadioButton)findViewById(R.id.radio3);
  if (v.getId() == R.id.myButton) {
   if(ra.isChecked()){
    tv.setText("Rock selected");
   }
   if(rb.isChecked()){
    tv.setText("Scissors selected");
   }
   if(rc.isChecked()){
    tv.setText("Paper selected");
   }
  }
 }
}

output:

select any item of radio button and then click submit button (let i select Scissors radio button),
now output will be


Image not found

Android button/Android button action/AbsoluteLayout/Positioning

This example is very similar to my previous example on button action.

Here main difference is positioning your sereen UI component using AbsoluteLayout.
By using android AbsoluteLayout we can easily make positioning.We can positioning using xml or from code.
In this example i showed both.


//my_button_interface.xml

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myTextViev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="20px" android:layout_y="200px" /> <Button android:id="@+id/myButton" android:layout_width="100px" android:layout_height="50px" android:text="Submit" /> </AbsoluteLayout>

//HelloWorldAndroid.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;

public class AndroidButton extends Activity implements OnClickListener {
 /** Called when the activity is first created. */
  
 Button btn;
 TextView tv;
  
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.my_button_interface);
     
  btn = (Button) findViewById(R.id.myButton);
  tv = (TextView) findViewById(R.id.myTextViev);
  
  AbsoluteLayout.LayoutParams layoutParams= new
                AbsoluteLayout.LayoutParams(100,50, 200, 200);
  btn.setLayoutParams(layoutParams);
     
  tv.setText("This is sample text");
  btn.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if (v.getId() == R.id.myButton) {
   tv.setText("welcome to AbsoluteLayout");
  }
 }
}

output:



Before click button


Image not found


After click button


Image not found

Android button/Android button action

UI design in android is very faster using xml

Here i am using linearLayout.
From this example we will know how to create a button and then give this button action.Lets see

Xml file description:
     At first i declared here a textview wich will changed after button action performed.
Each elements need a ID, for example textview id is myTextViev. we also define each content layout size and position (AbsoluteLayout).
Then i declared a button (id=myButton,name=Submit) and set button width height


Now you need to create a file named 'my_button_interface.xml' then paste the code below

//my_button_interface.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:id="@+id/myTextViev" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/myButton" android:layout_width="100px" android:layout_height="50px" android:text="Submit" /> </LinearLayout>

Code description:

First you need to set layout using setContentView(R.layout.my_button_interface).Now you get all elements of this layout.
If we want to get declare text view code is tv = (TextView) findViewById(R.id.myTextViev),now you
can set text in this textview.
For button action you need to implements OnClickListener,then everything is easy....

//AndroidButton.java

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class AndroidButton extends Activity implements OnClickListener { /** Called when the activity is first created. */ Button btn; TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_button_interface); btn = (Button) findViewById(R.id.myButton); tv = (TextView) findViewById(R.id.myTextViev); tv.setText("This is old text"); btn.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v.getId() == R.id.myButton) { tv.setText("This is new text"); } } }
output:


Before click button


Image not found
After click button


Image not found

Wednesday, January 12, 2011

Installing android

Installing android:

Android Hello World

Android hello world sample code:
    Create a android project
    Create a class named 'HelloWorldAndroid.java' and then
    paste this code

//HelloWorldAndroid.java


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

public class HelloWorldAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       TextView tv = new TextView(this);
       tv.setText("Hello, Android");
       setContentView(tv);
   }
}

output:

Image not found