0% found this document useful (0 votes)
131 views

Android 1

The document describes creating an Android application to demonstrate the use of intents, activities, controls, alert dialogs and list views. It provides code for the layout files and Java classes to display different screens and components. The steps include designing layouts, adding controls, writing code for events and displaying data in lists. The programs are executed successfully to showcase the key Android concepts.

Uploaded by

Mary James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Android 1

The document describes creating an Android application to demonstrate the use of intents, activities, controls, alert dialogs and list views. It provides code for the layout files and Java classes to display different screens and components. The steps include designing layouts, adding controls, writing code for events and displaying data in lists. The programs are executed successfully to showcase the key Android concepts.

Uploaded by

Mary James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Intent and Activity

Aim
To create a simple application using intent and Activity.
Algorithm
Step1: Open the android application.
Step2: click file menu and select android application project and give the name of project
Step 3: Select res folder then select the layout and click activity_main.
Step 4: Design the page using button, textview and edit text controls.
Step 5: write the coding for onclick event.
Step 6: Save the file and run the project.
Step 7: Close the project

Program
Activity_main.xml

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


<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button3"
android:layout_width="224dp"
android:layout_height="591dp"
android:onClick="startSecond"
android:text="ok" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="17dp"
android:text="first Activity"
tools:layout_editor_absoluteX="157dp"
tools:layout_editor_absoluteY="152dp" />

</RelativeLayout>
Activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".second">

<TextView
android:id="@+id/textView2"
android:layout_width="395dp"
android:layout_height="wrap_content"
android:text="Second Activity"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="324dp" />
</RelativeLayout>

MainActivity.java
package com.example.progam1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivityextends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startSecond(View v)
{
startActivity(new Intent(MainActivity.this,second.class));
}
}

Result:
Thus the above program has been executed successfully.
Using Controls
Aim
To create a simple application using Controls
Algorithm
Step1: Open the android application.
Step2: click file menu and select android application project and give the name of project
Step 3: Select res folder then select the layout and click activity_main.
Step 4: Design the page using button, textview and edit text controls.
Step 5: write the coding for onclick event and onCreate event.
Step 6: Save the file and run the project.
Step 7: Close the project

Program
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="Name"
android:inputType="text"
android:selectAllOnFocus="true" />

<EditText
android:id="@+id/txtPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password 0 to 9"
android:inputType="numberPassword" />

<EditText
android:id="@+id/txtEmai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />

<EditText
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:ems="10"
android:hint="Date"
android:inputType="date" />

<EditText
android:id="@+id/txtPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Phone Number"
android:inputType="phone"
android:textColorHint="#FE8DAB" />

<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:textSize="16sp"
android:textStyle="normal|bold" />

<TextView
android:id="@+id/resultView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:textSize="15dp" />

</LinearLayout>

MainActivity.java

package com.example.controls;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {


Button btnSubmit;
EditText name, password, email, dob, phoneno;
TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.txtName);
password = (EditText)findViewById(R.id.txtPwd);
email = (EditText)findViewById(R.id.txtEmai);
dob = (EditText)findViewById(R.id.txtDate);
phoneno= (EditText)findViewById(R.id.txtPhone);
btnSubmit = (Button)findViewById(R.id.btnSend);
result = (TextView)findViewById(R.id.resultView);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (name.getText().toString().isEmpty() || password.getText().toString().isEmpty() ||
email.getText().toString().isEmpty() || dob.getText().toString().isEmpty()
|| phoneno.getText().toString().isEmpty()) {
result.setText("Please Fill All the Details");
} else {
result.setText("Name - " + name.getText().toString() + " \n" + "Password - " +
password.getText().toString()
+ " \n" + "E-Mail - " + email.getText().toString() + " \n" + "DOB - " +
dob.getText().toString()
+ " \n" + "Contact - " + phoneno.getText().toString());
}
}
});
}
}

Result:
Thus the above program has been executed successfully.
Alert Dialogs
Aim
To create a simple application with Alert Dialogs.
Algorithm
Step1: Open the android application.
Step2: click file menu and select android application project and give the name of project
Step 3: Select res folder then select the layout and click activity_main.
Step 4: Design the page using button control.
Step 5: Write the coding for invoking alert dialog box.
Step 6: Save the file and run the project.
Step 7: Close the project.

Program

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent
">
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="200dp"
android:text="Show Alert" />
</RelativeLayout>

MainActivity.java

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.getBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder
= new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Login Alert")
.setMessage("Are you sure, you want to continue ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnCli
ckListener() {
@Override
public void onClick(DialogInterface
dialog, int which) {
Toast.makeText(MainActivity.this,"Selected
Option: YES",Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No", new DialogInterface.OnClic
kListener() {
@Override
public void onClick(DialogInterface
dialog, int which) {
Toast.makeText(MainActivity.this,"Selected
Option: No",Toast.LENGTH_SHORT).show();
}
});
//Creating dialog box
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}

Result:
Thus the above program has been executed successfully.
List View
Aim
To create a simple application to view list of items using List View
Algorithm
Step1: Open the android application.
Step2: click file menu and select android application project and give the name of project
Step 3: Select res folder then select the layout and click activity_main.
Step 4: Design the page using List view.
Step 5: write the coding for onCreate event.
Step 6: Save the file and run the project.
Step 7: Close the project.

Program
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:id="@+id/userlist"
android:layout_width="288dp"
android:layout_height="wrap_content">

</ListView>

</LinearLayout>

MainActivity.java
package com.example.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


private ListView mListView;
private ArrayAdapter aAdapter;
private String[] users = { "Suresh Dasari", "Rohini Alavala", "Trishika Dasari",
"Praveen Alavala", "Madav Sai", "Hamsika Yemineni"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.userlist);
aAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, users);
mListView.setAdapter(aAdapter);
}
}

Result:
Thus the above program has been executed successfully.
Options Menu
Aim
To create a simple application using Option Menu
Algorithm
Step1: Open the android application.
Step2: click file menu and select android application project and give the name of project
Step 3: Select layout folder then select the Menu folder and create options_menu.xml.
Step 4: write the coding for Menu items.
Step 5: Save the file and run the project.
Step 6: Close the project.

Program

options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/search_item"
android:title="Search" />
<item android:id="@+id/upload_item"
android:title="Upload" />
<item android:id="@+id/copy_item"
android:title="Copy" />
<item android:id="@+id/print_item"
android:title="Print" />
<item android:id="@+id/share_item"
android:title="Share" />
<item android:id="@+id/bookmark_item"
android:title="BookMark" />
</menu>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(),
Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.search_item:

return true;
case R.id.upload_item:

return true;
case R.id.copy_item:

return true;
case R.id.print_item:

return true;
case R.id.share_item:

return true;
case R.id.bookmark_item:

return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

Result:
Thus the above program has been executed successfully.
Seek Bars

Aim:
To create an application using progress bar.

Algorithm:
Step1: Open the android application.
Step2: Click file menu and select android application project and give the name of project
Step 3: Select res folder then select the layout and click activity_main.
Step 4: Design the page using progressbar and text view controls.
Step 5: Write the coding for progressbar current status.
Step 6: Save the file and run the project.
Step 7: Close the project.

Program

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent
">
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="200dp"
android:max="100"
android:indeterminate="false"
android:progress="0" />
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/seekBar1"
android:layout_below="@+id/seekBar1"
android:layout_marginTop="40dp"
android:layout_marginLeft="130dp"
android:textSize="20dp"
android:textStyle="bold"/>
</RelativeLayout>
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


private SeekBar sBar;
private TextView tView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sBar = (SeekBar) findViewById(R.id.seekBar1);
tView = (TextView) findViewById(R.id.textview1);
tView.setText(sBar.getProgress() + "/" + sBar.getMax());
sBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener
() {
int pval = 0;
@Override
public
void onProgressChanged(SeekBarseekBar, int progress, boolean fromUser) {
pval = progress;
}
@Override
public void onStartTrackingTouch(SeekBarseekBar) {
//write custom code to on start progress
}
@Override
public void onStopTrackingTouch(SeekBarseekBar) {
tView.setText(pval + "/" + seekBar.getMax());
}
});
}
}

Result:
Thus the above program has been executed successfully.
Shared Preferences
Aim:
To create an application using Shared Preferences

Algorithm:
Step1: Open the android application.
Step2: Click file menu and select android application project and give the name of project
Step 3: Select res folder then select the layout and click activity_main.
Step 4:Select res folder layout folder  Go to New  select Layout Resource File and give
name as details.xml.

Step 5: Write the coding on MainActivity.java for onclickListener.


Step 6:Create another activity file DetailsActivity.java
Step 7: Save the file and run the project.
Step 8: Close the project.

Program

activity_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="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="UserName" />
<EditText
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>
<TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtPwd"
android:inputType="textPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Login" />
</LinearLayout>

details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resultView"
android:layout_gravity="center"
android:layout_marginTop="170dp"
android:textSize="20dp"/>
<Button
android:id="@+id/btnLogOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Log Out" />
</LinearLayout>

MainActivity.java
package com.tutlane.sharedpreferencesexample;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText uname, pwd;
Button loginBtn;
SharedPreferences pref;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname = (EditText)findViewById(R.id.txtName);
pwd = (EditText)findViewById(R.id.txtPwd);
loginBtn = (Button)findViewById(R.id.btnLogin);
pref = getSharedPreferences("user_details",MODE_PRIVATE);
intent = new Intent(MainActivity.this,DetailsActivity.class);
if(pref.contains("username") && pref.contains("password")){
startActivity(intent);
}
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = uname.getText().toString();
String password = pwd.getText().toString();
if(username.equals("suresh") &&password.equals("dasari")){
SharedPreferences.Editor editor = pref.edit();
editor.putString("username",username);
editor.putString("password",password);
editor.commit();
Toast.makeText(getApplicationContext(), "Login
Successful",Toast.LENGTH_SHORT).show();
startActivity(intent);
}
else
{
Toast.makeText(getApplicationContext(),"Credentials are
not valid",Toast.LENGTH_SHORT).show();
}
}
});
}
}

DetailsActivity.java
package com.tutlane.sharedpreferencesexample;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
* Created by tutlane on 03-01-2018.
*/
public class DetailsActivity extends AppCompatActivity {
SharedPreferences prf;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
TextView result = (TextView)findViewById(R.id.resultView);
Button btnLogOut = (Button)findViewById(R.id.btnLogOut);
prf = getSharedPreferences("user_details",MODE_PRIVATE);
intent = new Intent(DetailsActivity.this,MainActivity.class);
result.setText("Hello, "+prf.getString("username",null));
btnLogOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = prf.edit();
editor.clear();
editor.commit();
startActivity(intent);
}
});
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.sharedpreferencesexample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /
>
</intent-filter>
</activity>
<activity android:name=".DetailsActivity" android:label="Shared
Preferences - Details"></activity>
</application>
</manifest>

Result:
Thus the above program has been executed successfully.

You might also like