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