Wednesday, 3 December 2014

Sending data from one activity to another in Android


Step1: Using Intent Object for sending data from one activity:

public class MainActivity extends Activity implements OnClickListener {
  
  EditText etFName;
  EditText etLName;
  
  Button btnSubmit;
  
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        etFName = (EditText) findViewById(R.id.etFName);
        etLName = (EditText) findViewById(R.id.etLName);
        
        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(this);
        
    }


  @Override
  public void onClick(View v) {
    Intent intent = new Intent(this, ViewActivity.class); 
    
 intent.putExtra("fname", etFName.getText().toString());
    intent.putExtra("lname", etLName.getText().toString());
 
     OR
     
 Bundle b = new Bundle();
    //Inserts a String value into the mapping of this Bundle
    b.putString("fname", etFName.getText().toString());
 b.putString("lname", etLName.getText().toString());
 intent.putExtras(b);
    
 startActivity(intent);
  } 
}

Step2: Using Intent Object for receiving data in another activity

public class AnotherActivity extends Activity {
  
  TextView tvView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);
    
    tvView = (TextView) findViewById(R.id.tvView);
    
    Intent intent = getIntent();
    String fName = intent.getStringExtra("fname");
    String lName = intent.getStringExtra("lname");
 
     OR
 Bundle b = getIntent().getExtras();
 String fName = b.getCharSequence("fname");
    String lName = b.getCharSequence("lname");
    
    tvView.setText("Your name is: " + fName + " " + lName);
  }
} 

Next Article:


For any questions, suggestions and feedback, Please write to us. Thanks for Reading :)

Providing UP Navigation in Android


Step1: specifying parentActivityName attibute in an activity

Declare which activity is the appropriate parent for each activity. You can do it by specifying parentActivityName attibute in an activity.

<activity
 android:name="com.example.test.MainActivity2"
 android:label="@string/title_activity_main_activity2"
 android:parentActivityName="com.example.test.MainActivity" >
</activity>

Step2: Call setDisplayHomeAsUpEnabled() method of getActionBar() in the onCreate()

Call setDisplayHomeAsUpEnabled() method of getActionBar() in the onCreate method of the activity to enable the back button in the top action bar

@Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_activity2);
      getActionBar().setDisplayHomeAsUpEnabled(true);
   }

Step3: Override onOptionsItemSelected()

When the user presses it, your activity receives a call to onOptionsItemSelected(). The ID for the action is android.R.id.home.

public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
  case android.R.id.home:
  NavUtils.navigateUpFromSameTask(this);
  return true;
 } 
}

Step4: Handling device back button

Overrid onBackPressed() and then calling moveTaskToBack() and finish() method.

@Override
public void onBackPressed() {
   moveTaskToBack(true); 
   MainActivity2.this.finish();
}

Complete Code:

<application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity
         android:name="com.example.test.MainActivity"
         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="com.example.test.MainActivity2"
         android:label="@string/title_activity_main_activity2"
         android:parentActivityName="com.example.test.MainActivity" >
      </activity>
   </application>

public class MainActivity2 extends Activity {

   @SuppressLint("NewApi")
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_activity2);
      getActionBar().setDisplayHomeAsUpEnabled(true);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main_activity2, menu);
      return true;
   }  
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
         // Respond to the action bar's Up/Home button
         case android.R.id.home:
         NavUtils.navigateUpFromSameTask(this);
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
   @Override
   public void onBackPressed() {
      moveTaskToBack(true); 
      MainActivity2.this.finish();
   }

}

Next Article: Android - Sending data from one activity to another


For any questions, suggestions and feedback, Please write to us. Thanks for Reading :)

Adding EditBox in LisView Header in Android


Create a separate layout header.xml and add below code:

<TableLayout   
    android:layout_weight="0"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content">  

    <TableRow>  
        <EditText android:id="@+id/newmessagecontent"  
            android:layout_height="wrap_content"   
            android:singleLine="false"  
            android:gravity="top"  
            android:layout_width="250dp"  
            android:layout_alignParentTop="true"  
              />  

        <Button android:layout_height="wrap_content"   
            android:id="@+id/sendmessage"   
            android:text="Send"   
            android:layout_width="wrap_content"  
            />  
    </Table> 
      </TableLayout>

Update the main activity java code with below code

ListView lv = getListView(); 
LayoutInflater inflater = getLayoutInflater(); 
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false); //header is above XML code
lv.addHeaderView(header, null, false);

Next Article: Android - Providing UP Navigation


For any questions, suggestions and feedback, Please write to us. Thanks for Reading :)

Tuesday, 2 December 2014

Different ways to create Android View Separator


Example:1 Using View control to create View Separator

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:orientation="horizontal">
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="Yes"
        android:layout_weight="1"
        android:id="@+id/button1"
        android:textColor="#00b0e4" />
    <View android:layout_height="fill_parent"
        android:layout_width="1px"
        android:background="#90909090"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:id="@+id/separator1" />
  
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="No"
        android:layout_weight="1"
        android:id="@+id/button2"
        android:textColor="#00b0e4" />
    
</LinearLayout>

Example:2 Using android:divider attribute in LinearLayout

Create a new file called separator.xml in the drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <size android:width="1dp" />
 <solid android:color="#90909090" />
</shape>


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:divider="@drawable/separator"
    android:showDividers="middle"
    android:orientation="horizontal">
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="Yes"
        android:layout_weight="1"
        android:id="@+id/button1"
        android:textColor="#00b0e4" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/buttonBarButtonStyle"
        android:text="No"
        android:layout_weight="1"
        android:id="@+id/button2"
        android:textColor="#00b0e4" />
  
</LinearLayout>

Example:3 Using ButtonBarStyle attribute in LinearLayout style attribute

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    style="?android:buttonBarStyle"
    android:dividerPadding="15dp"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:id="@+id/button1"
        android:layout_gravity="center_vertical" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:id="@+id/button2"
android:layout_gravity="center_vertical"/>
     
</LinearLayout>

Next Article: Android - Adding EditBox in LisView Header


For any questions, suggestions and feedback, Please write to us. Thanks for Reading :)

Android Tutorials - User Interface



All You Need To Know About User Interface in Android.

This article will walk you through with the basics of User Interface in Android.

Each topic includes basic programming tutorial and related interview question.


Android UI Layout

What is View and View group? How to define or create View in android?

Types Layouts in android. Layout Type attributes with description.

Example of Linear, Relative, Table, Frame and Absolute, ListView, GridView Layout.

continue reading...


Android UI Controls

continue reading...


Android Event Handling

continue reading...


Android Custom Component

continue reading...


Android Styles & Themes

continue reading...


Wednesday, 5 November 2014

Web Services Tutorial(SOAP, REST)


All You Need To Know About Web Services in Java

This article will walk you through with the basics of WebServices in Java.

Each topic includes basic programming tutorial and related interview question.


Web Services Overview

This article will guide you through the basics of Web Services.

What are Web Services?. How does Web Services work? What are Web Services Components?

What are Implementation of Web Services? What are the Types of Web Services?

Brief description of UUDI and WSDL. WSDL Elements mapping to Java.

continue reading...


RESTful Web Services

This article will guide you through the basic fundamentals of RESTful Web Services.

What is REST? What are RESTful Web Services? What are RESTful Web Services Architectural constraints?

What is JAX-RS? What are the Implementations of JAX-RS?

What are annotations provided by JAX-RS?

continue reading...


Tuesday, 7 October 2014

PL-SQL Tutorial and Interview Question


All You Need To Know About PL-SQL Programming

This article will walk you through with the basics of Pl-SQL programming.

Each topic includes basic programming tutorial and related interview question.


PL-SQL Introduction

This article explains its advantages and briefly describes its main features and its architecture.

PL-SQL architecture with flow diagram.

PL-SQL Engine, PL-SQL Unit and PL-SQL Block structure with example.

How to write and execute the PL-SQL block.

continue reading...


PL-SQL Fundamentals

This article will guide you through the basic fundamentals of PL-SQL programming.

Character Sets and Lexical Units (Delimiters,Identifiers, Literals and Comments).

Declaration of identifiers with example. Using DEFAULT and NOT NULL.

Using the %TYPE and %ROWTYPE Attribute. Aggregate Assignment. Naming Convention, Scope and Visibility.

CASE Expression and Handling NULL values.

continue reading...


PL/SQL Datatypes

This article explains the basic, frequently used predefined PL/SQL data types and subtypes, how to define and use your own PL/SQL subtypes, and PL/SQL data type conversion

Predefined PL/SQL Datatypes. Subtypes of predefined Datatypes.

What is Subtypes? User defined subtypes with example.

Datatype conversions.

continue reading...


PL/SQL Control Statement

This article shows you how to structure the flow of control through a PL/SQL program. PL/SQL provides conditional tests, loops, and branches that let you produce well-structured programs.

IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF. CASE Statement and Searched CASE Statement.

Basic Loop Statement. WHILE and FOR statement. Usage of CONTINUE and EXIT in Loop statement

Sequential Control (GOTO and NULL Statements)

continue reading...


PL/SQL Collections and Records

This article explains how to create and use PL/SQL collection and record variables.

What is Collections and Records? How to declare and define Collection and Records

Types of PL/SQL Collection: Associative array (or index-by table), Variable-Size Arrays (Varrays), Nested Tables

Difference between Nested Tables and Associative Arrays? Difference between Nested Tables and Varrays?

Important Collection Methods and their usage.

continue reading...


PL-SQL Subprograms (Procedure and Function)

This article explains how to turn sets of statements into reusable subprograms. Subprograms are the building blocks of modular, maintainable applications

What is Subprogram? How to create Subprogram?

What is Procedure and Function? How to create Procedure and Function?

Nested Subprograms. Overloading Subprograms

continue reading...


Declaring and Passing Subprogram Parameters

This article explains hot to declare and pass parameteres in Subprograms.

What are the types Parameter? What are the modes of Parameter?

Passing Actual parameter as Positional, Named, or Mixed Notation with example.

Passing Default value in Formal parameter with example.

continue reading...


PL/SQL Packages

This article explains how to bundle related PL/SQL code and data into a package.

What is PL-SQL Packages? How to create PL-SQL Package Specs and Body?

What Goes in a PL/SQL Package? Private and Public Items in PL/SQL Packages.

Overview of Product-Specific PL/SQL Packages

continue reading...


PL/SQL Triggers

This article will guide you through the basics of PL-SQL Triggers.

What is PL-SQL Triggers? Types of Triggers?

What is Trigger States? How to Create Triggers? When Does the Trigger Fire?

continue reading...


PL/SQL Exception Handling

This article will guide you through the basics of PL-SQL Exception Handling.

What is PL/SQL Exception or Errors? What is PL/SQL Exception Handling?

User Defined Exception? Declaring & Invoking PL/SQL Exceptions. Scope Rules for PL/SQL Exceptions.

Usage of RAISE_APPLICATION_ERROR, SQLCODE, SQLERRM and EXCEPTION_INIT Pragma.

continue reading...