Advertisements

Aatul Palandurkar

Archive for the ‘Android Source Codes’ Category

Objective :

  • Passing data between activities in Android
  • Passing multiple data between activities in Android
  • Passing array between activities in Android
  • Passing ArrayList between activities in Android
  • Passing ArrayList to another activity in Android
  • Sending data via Intent in Android
  • Sending multiple data via Intent in Android
  • Sending array between activities in Android
  • Sending ArrayList between activities in Android
  • Sending ArrayList to another activity in Android
  • Sending data via Intent and Bundle in Android
  • Sending multiple data via Intent and Bundle in Android
  • Passing array between activities in Android using Intent and Bundle
  • How to pass array to another activity in Android?
  • How to pass array between activities in Android?
  • How to pass data to another activity in Android?
  • How to pass data to activity in Android?
  • How to pass data to between activities in Android?
  • How to pass ArrayList to another activity in Android?
  • How to pass ArrayList between activities in Android?

 

Code for passing data between activities in Android :

ActivityOne.java


String value = "Hello!";
Intent in = new Intent(this,ActivityTwo.class);
in = putExtra("Key", value);
startActivity(in);

ActivityTwo.java


Bundle bundle = getIntent().getExtras();
String valueReceived = bundle .getString("Key");

 

Code for passing multiple data or values between activities in Android :

Method 1 : Using Intent to pass data and Bundle to extract data between activities in Android

ActivityOne.java


String value1 = "Hello!";
String value2 = "Hi!";
Intent in = new Intent(this,ActivityTwo.class); 
in = putExtra("Key1", value1); startActivity(in); 
in = putExtra("Key2", value2); startActivity(in); 

ActivityTwo.java

Bundle bundle = getIntent().getExtras();
String valueReceived1 = bundle .getString("Key1");
String valueReceived2 = bundle .getString("Key2");

 

Method 2 : Using Bundle to pass and to extract data between activities in Android

ActivityOne.java


String value1 = "Hello!";
String value2 = "Hi!";
Intent in = new Intent(this,ActivityTwo.class); 
Bundle bundle = new Bundle();
bundle.putString("Key1", value1);
bundle.putString("Key2", value2);
in = putExtras(bundle); 
startActivity(in); 

ActivityTwo.java

Bundle bundle = getIntent().getExtras();
String valueReceived1 = bundle .getString("Key1");
String valueReceived2 = bundle .getString("Key2");

 

Code for passing array between activities in Android :

ActivityOne.java

String[] array = new String[]{"Item1", "Item2", "item3", "Item4", "item5"};
Intent in = new Intent(this,ActivityTwo.class); 
Bundle bundle = new Bundle();
bundle.putStringArray("MyArray", array);
in = putExtras(bundle); 
startActivity(in); 

ActivityTwo.java

Bundle bundle = getIntent().getExtras();
String arrayReceived[] = bundle.getStringArray("MyArray");

 

Code for passing ArrayList between activities in Android :

ActivityOne.java

ArrayList<String> array = new ArrayList<String>();
array.add("Hello");
array.add("Hi");
array.add("Bye");
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("array_list", array);
startActivity(intent);

ActivityTwo.java

Bundle bundle = getIntent().getExtras();
ArrayList<String> array = (ArrayList<String>) bundle.getStringArrayList("array_list");

		
Advertisements
Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Objectives :
– How to use TextView in Android?
– How to scroll Text in Android?
– How to implement Marquee Text in Android?
– How to make text scrollable in Android?

Getting Started :
1. Create a New Android Application titled as “AutoscrollTextviewDemo” with blank activity.
2. Update the layout xml with code given below.

How to do it?
Here is the XML code for Auto Scroll TextView, update your layout xml file with code below.

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AutoScroll TextView" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="This is small example for auto scroll text view simply using XML, no need to code Java. Hope you like it! Try it on your own, it is very easy to perform" />

</LinearLayout>

Download Source Code
Output :

Output in Potrait mode

Output in Potrait mode

Output in Landscape mode

Output in Landscape mode

This is small example for auto scroll text view simply using XML, no need to code Java. Hope you like it! Try it on your own, it is very easy to perform.

Click here to download source code.

Objectives :

  • How to take input in Android?
  • How to take text input in Android?
  • How to use EditText in Android?
  • How to take multiline input in Android?

If you wish to take text input in Android, you can use <EditText> to take the text input in Android.

Here is the small example showing different uses of EditText. EditText can be also used to take multiline input as that of textarea.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
android:text="First Name" />

<EditText
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:inputType="text"
 android:id="@+id/EditTextFirstName"
 android:hint="First Name" />

<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Cell No" />

<EditText
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:inputType="number"
 android:hint="Cell No." />

 <TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Email ID" />

<EditText
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:inputType="textEmailAddress"
 android:hint="Email Name" />

 <TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Message" />

<EditText
 android:id="@+id/editText1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:inputType="textMultiLine"
 android:lines="5" />

</LinearLayout>

Multiline Input in Android

If you need, you can use the same <EditText> to take multiline input as per the code snippet given below :

<EditText
 android:id="@+id/editText1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:inputType="textMultiLine"
 android:lines="5" />

Here, android:inputType=”textMultiLine” will convert the single line text input into multi line input and android:lines=”5″ will give 5 (five) visible lines input.

Objectives :

  • Which layouts does Android have?
  • How to implement a layout in Android?
  • How to use different layouts in Android?
  • How to use Linear Layout in Android?
  • How to use Relative Layout in Android?
  • How to use Absolute Layout in Android?
  • How to use Frame Layout in Android?
  • How to use Table Layout in Android?
  • How to use nested layouts in Android?
  • How to use a layout inside another layout in Android?

In Android, there are main five (5) layouts which are as follows;

  1. Linear Layout
  2. Relative Layout
  3. Absolute Layout
  4. Table Layout
  5. Frame Layout

Here I have used the same form controls/components in form to show how it looks in different layouts except Frame Layout.

Linear Layout in Android

Here I have used nested linear layout.

XML Code for Linear Layout :


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

<LinearLayout
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">

<TextView
 android:text="User Name"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

<EditText
 android:width="100px"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">

<TextView
 android:text="Password"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

<EditText
 android:width="100px"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

</LinearLayout>

</LinearLayout>

Here android:orientation=”vertical” will render controls in vertical manner and android:orientation=”horizontal” will render controls in horizontal manner.

Relative Layout in Android

Here in Relative layout we don’t need to nest layouts generally.

XML Code for Relative Layout :


<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
 android:id="@+id/userName"
 android:text="First Name"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

<EditText
 android:id="@+id/editUserName"
 android:width="100px"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@id/userName"
 android:layout_below="@id/userName"/>

<EditText
 android:id="@+id/editpassword"
 android:width="100px"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/editUserName"
 android:layout_alignLeft="@id/editUserName"/>

<TextView
 android:id="@+id/password"
 android:text="Password"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toLeftOf="@id/editpassword"
 android:layout_below="@id/editUserName" />

</RelativeLayout>

Here we need to specify android:layout_toLeftOf=”@id/controlId”, android:layout_toRightOf=”@id/controlId”, android:layout_below=”@id/controlId”, android:layout_alignLeft=”@id/controlId”, etc. parameters so as to render the control at particular place.

Absolute Layout in Android

Here in Absolute Layout we need to specify exact pixel location or (x,y) coordinate where we need to render our controls.

XML Code for Absolute Layout :

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
 android:layout_x="10px"
 android:layout_y="110px"
 android:text="User Name"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

<EditText
 android:layout_x="150px"
 android:layout_y="100px"
 android:width="100px"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

<TextView
 android:layout_x="10px"
 android:layout_y="160px"
 android:text="Password"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

<EditText
 android:layout_x="150px"
 android:layout_y="150px"
 android:width="100px"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

</AbsoluteLayout>

Table Layout in Android

Whenever you need to manage data in tabular manner, you can use Table Layout. Table Layout will help you to show data in rows and columns.
Here is the sample code showing how to use Table Layout :

<TableLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">

<TableRow>

<TextView
 android:text="User Name"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_column="1" />

<EditText
 android:width="100px"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

</TableRow>

<TableRow>

<TextView
 android:text="Password"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_column="1" />

<EditText
 android:width="100px"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

</TableRow>

</TableLayout>

Here in Table Layout, we use <TableRow> tag to add contents to a row same as that of HTML.

Frame Layout in Android

Frame Layout can be used to display various contents such as text, images, videos, image galleries, etc.
Sample code for Frame Layout is as follows :

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:text="http://aatul.me"
android:textSize="24sp"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>

</FrameLayout>

Objectives :

  • How to set TextView in Android?
  • How to add TextView in Android?
  • How to add TextView in Android via Java program?
  • How to set Layout parameters from Java code in Android?

TextView is a one of the Views available in Android. There are two (2) ways to add TextView in your Android application as follows :

  1. Using XML code
  2. Using Java code

XML Code example – Adding to a TextView in a layout xml file

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

 <TextView
  android:id="@+id/textView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Hello World" />

</LinearLayout>

 

Java Code example – Creating and adding a new TextView to a Layout

// Creates object of TextView
TextView textView = new TextView(this);
// Set text to display in TextView on Screen
textView.setText(“Hello World”);
// Add TextView to Linear Layout with layout parameters
((LinearLayout)findViewById(R.id.mainLayout))
 .addView(textView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

Above Java code can be written in onCreate() of your Activity.

Here LayoutParams.FILL_PARENT will set layout height to FILL_PARENT, LayoutParams.WRAP_CONTENT will set layout width to WRAP_CONTENT.

Posted from WordPress for Android


Advertisements

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 8,341 other followers

Tweets

%d bloggers like this: