Wednesday, 31 December 2014

Explicit Intent Example

There are two types of Intent navigation between activities
1. Explicit Intent Navigation
2. Implicit Intent Navigation

Following is an example of explicit Intent Navigation

step1: create new android application projects
step2: create new activity<SecondActivity.java>
step3: create new xml file<second_page.xml> under layout folder
step4: register the activity in manifest.xml file



activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/next_page" />

</RelativeLayout>

MainActicity.java
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;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(i);
finish();
}
});
}
}

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Exlicit Intent</string>
    <string name="title_activity_second">SecondActivity</string>
    <string name="second_activity">This is second Activity</string>
    <string name="next_page">Go to second Activity</string>
</resources>

second_page.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="42dp"
        android:text="@string/second_activity"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

SecondActivity.java
import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_page);
}
}

Note: add activity registration in manifest.xml as follows

AndroidManifest.xml
<application>
-
-
-
-
        <activity
            android:name="com.example.exlicitnavigationexample.SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
</application>

Tuesday, 30 December 2014

Multiple Buttons

Here is an example for multiple buttons, even we can handle multiple buttons with single method(onClick(View)) or with unanimous object of OnClickListener interface,





activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_save" />

    <Button
        android:id="@+id/button_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_cancel" />

</LinearLayout>

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Multiple Buttons Example</string>
    <string name="button_save">Save</string>
    <string name="button_cancel">Cancel</string>
</resources>

MainActivity.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.Toast;
import com.swamy.multiplebuttonsexample.R;

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_save = (Button) findViewById(R.id.button_save);
Button button_cancel = (Button) findViewById(R.id.button_cancel);

button_save.setOnClickListener(this);
button_cancel.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_save:
Toast.makeText(getApplicationContext(),
"You have clicked on Save button", Toast.LENGTH_SHORT)
.show();
break;
case R.id.button_cancel:
Toast.makeText(getApplicationContext(),
"You have clicked on Cancel button", Toast.LENGTH_SHORT)
.show();
break;
}
}
}


Button Example3

Here one approach for button click event handling, in this approach we need to write individual methods for each and every button, it is complicated not recommended to use.



activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/click_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:onClick="button_clicked" />

</LinearLayout>

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.click_button);
}

public void button_clicked(View view) {
Toast.makeText(getApplicationContext(), "You have Clicked on Button",
Toast.LENGTH_SHORT).show();
}
}

Button Example2

here is one more approach to handle button click event using unanimous class as follows




activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/click_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

</LinearLayout>

MainActivity.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.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.click_button);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"You have clicked on Button", Toast.LENGTH_SHORT)
.show();
}
});
}

}

Button Example1

Create new project, go to activity_main.xml drag and drop  a button or add the following code.




activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/click_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

</LinearLayout>

MainActivity.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.Toast;

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.click_button);
b.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You have clicked on button",
Toast.LENGTH_SHORT).show();
}

}