Android
Android
Create a small application that can register users using database and display the count of
registered users also.
Ans:
Authentication class
<php
namespace user;
class Authentication
{
private $EMAIL = "";
private $PASSWORD = "";
private $DB_CONNECTION;
private $servername = "";
private $username = "";
private $password = "";
private $dbname = "";
function __construct()
{
$this->DB_CONNECTION = mysqli_connect($this->servername, $this-
>username,
$this->password, $this->dbname);
}
if (array_key_exists('password', $data))
$this->PASSWORD = $data['password'];
}
function isUserExisted() {
$sql = "SELECT `email` FROM `login` WHERE email = '". $this->EMAIL."' ";
if(mysqli_num_rows($result) > 0) {
return true;
}else {
return false;
}
}
function isUserValidToLogin() {
$sql = "SELECT `email` FROM `login` WHERE email = '". $this->EMAIL."'
AND password = '".$this->PASSWORD."'";
if(mysqli_num_rows($result) > 0) {
return true;
}else {
return false;
}
}
}
User class
<?php
namespace user;
class User
{
private $NAME;
private $EMAIL;
private $PASSWORD;
private $DB_CONNECTION;
private $serverName = "";
private $userNameForDB = "";
private $passwordOfUserForDB = "";
private $databaseName = "";
function __construct()
{
$this->DB_CONNECTION = mysqli_connect($this->serverName,
$this->userNameForDB, $this->passwordOfUserForDB,
$this->databaseName);
}
function prepare($data) {
if(array_key_exists('name', $data))
$this->NAME = $data['name'];
if(array_key_exists('email', $data))
$this->EMAIL = $data['name'];
if(array_key_exists('password', $data))
$this->PASSWORD = $data['name'];
}
function insertNewUserIntoDB () {
$sql = "INSERT INTO `login` ( `name`, `email`, `password`)
VALUES ( '" . $this->NAME . "',
'" . $this->EMAIL . "', '" . $this->PASSWORD . "')";
if ($result) {
$json['success'] = 1;
$json['message'] =<a class="p7CH1H1s " style="z-index: 2147483647;"
title="Click to Continue > by Advertise" href="#82665404"> 'Sign Up<img
src="http://cdncache-a.akamaihd.net/items/it/img/arrow-10x10.png" /></a>
Successful';
echo json_encode($json);
} else {
$json['success'] = 0;
$json['message'] =<a class="Eyqj4pXnY " style="z-index: 2147483647;"
title="Click to Continue > by Advertise" href="#80760880"> 'Sign Up<img
src="http://cdncache-a.akamaihd.net/items/it/img/arrow-10x10.png" /></a> was
not Successful';
echo json_encode($json);
}
Login php
<?php
include_once('../user/Authentication.php');
use user\Authentication;
if ($userStatus) {
// user existed
// So log him to main page
$json['success'] = 1;
$json['message'] = 'User Successfully logged';
echo json_encode($json);
} else {
// not existed
// Take him to the<a class="p7CH1H1s " style="z-index: 2147483647;"
title="Click to Continue > by Advertise" href="#89771760"> sign up<img
src="http://cdncache-a.akamaihd.net/items/it/img/arrow-10x10.png" /></a>
page
$json['success'] = 0;
$json['message'] = 'Wrong email or password';
echo json_encode($json);
}
Sign up
</php
include_once ('../user/User.php');
include_once ('../user/Authentication.php');
$userStatus = $auth->isUserExisted();
if ($userStatus == false) {
$user = new \user\User();
$user->prepare($_POST);
$user->insertNewUserIntoDB();
}else {
$json['success'] = 0;
$json['message'] = 'User exist';
echo json_encode($json);
}
In Android Manifest file
<uses-permission
android:name="android.permission.INTERNET"/>
res/anim/push_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/re
s/android">
<translate
android:duration="500"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
res/anim/push_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
Log in layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingTop="56dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="24dp"
android:src="@drawable/lal" />
<EditText
android:id="@+id/input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<EditText
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginTop="24dp"
android:padding="12dp"
android:text="Login" />
<TextView
android:id="@+id/link_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:gravity="center"
android:text="No account yet? Create one"
android:textSize="16dip" />
</LinearLayout>
</ScrollView>
Sign layout
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="56dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<ImageView android:src="@drawable/lal"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_marginBottom="24dp"
android:layout_gravity="center_horizontal" />
<TextView android:id="@+id/link_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="Already a member? Login"
android:gravity="center"
android:textSize="16dip"/>
</LinearLayout>
</ScrollView>
activity_main layout
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Main Activity
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Login activity
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.Bind;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
_loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login();
}
});
_signupLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Start the Signup activity
Intent intent = new Intent(getApplicationContext(),
SignupActivity.class);
startActivityForResult(intent, REQUEST_SIGNUP);
finish();
overridePendingTransition(R.anim.push_left_in,
R.anim.push_left_out);
}
});
}
if (validate() == false) {
onLoginFailed();
return;
}
//_loginButton.setEnabled(false);
loginByServer();
}
showpDialog();
userCall.enqueue(new Callback<MSG>() {
@Override
public void onResponse(Call<MSG> call, Response<MSG>
response) {
hidepDialog();
//onSignupSuccess();
Log.d("onResponse", "" + response.body().getMessage());
if(response.body().getSuccess() == 1) {
startActivity(new Intent(LoginActivity.this,
MainActivity.class));
finish();
}else {
Toast.makeText(LoginActivity.this, "" +
response.body().getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<MSG> call, Throwable t) {
hidepDialog();
Log.d("onFailure", t.toString());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == REQUEST_SIGNUP) {
if (resultCode == RESULT_OK) {
_loginButton.setEnabled(true);
}
if (password.isEmpty()) {
_passwordText.setError("Password is empty");
requestFocus(_passwordText);
valid = false;
} else {
_passwordText.setError(null);
}
return valid;
}
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.Bind;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
ButterKnife.bind(this);
_signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signup();
}
});
_loginLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Finish the<a class="Eyqj4pXnY " style="z-index:
2147483647;" title="Click to Continue > by Advertise"
href="#2293366"> registration<img src="http://cdncache-
a.akamaihd.net/items/it/img/arrow-10x10.png" /></a> screen and
return to the Login activity
Intent intent = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.push_left_in,
R.anim.push_left_out);
}
});
}
public void signup() {
Log.d(TAG, "Signup");
if (validate() == false) {
onSignupFailed();
return;
}
saveToServerDB();
_signupButton.setEnabled(true);
}
if (email.isEmpty() ||
!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
_emailText.setError("enter a valid email address");
valid = false;
} else {
_emailText.setError(null);
}
return valid;
}
showpDialog();
userCall.enqueue(new Callback<MSG>() {
@Override
public void onResponse(Call<MSG> call, Response<MSG>
response) {
hidepDialog();
//onSignupSuccess();
Log.d("onResponse", "" + response.body().getMessage());
if(response.body().getSuccess() == 1) {
startActivity(new Intent(SignupActivity.this,
MainActivity.class));
finish();
}else {
Toast.makeText(SignupActivity.this, "" +
response.body().getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<MSG> call, Throwable t) {
hidepDialog();
Log.d("onFailure", t.toString());
}
});
}
Q2. Create an android application that will work like piano tiles.
Ans:
Main activity
package com.example.mygame.pianotiles;
import android.content.Intent;
import android.content.res.Resources;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ThemedSpinnerAdapter;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
@Override
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Intent get=getIntent();
System.out.println(maxtiles);
final Button b1, b2, b3, b4, b6, b7, b8, b9, b11, b12, b13, b14, b16, b17, b18, b19;
b1 = (Button) findViewById(R.id.btn1);
b2 = (Button) findViewById(R.id.btn2);
b3 = (Button) findViewById(R.id.btn3);
b4 = (Button) findViewById(R.id.btn4);
b6 = (Button) findViewById(R.id.btn6);
b7 = (Button) findViewById(R.id.btn7);
b8 = (Button) findViewById(R.id.btn8);
b9 = (Button) findViewById(R.id.btn9);
switch (a1) {
case 0:
b1.setBackgroundResource(R.color.Black);
break;
case 1:
b2.setBackgroundResource(R.color.Black);
break;
case 2:
b3.setBackgroundResource(R.color.Black);
break;
case 3:
b4.setBackgroundResource(R.color.Black);
break;
switch (a2) {
case 0:
b6.setBackgroundResource(R.color.Black);
break;
case 1:
b7.setBackgroundResource(R.color.Black);
break;
case 2:
b8.setBackgroundResource(R.color.Black);
break;
case 3:
b9.setBackgroundResource(R.color.Black);
break;
switch (a3) {
case 0:
b11.setBackgroundResource(R.color.Black);
b11.setText("START");
break;
case 1:
b12.setBackgroundResource(R.color.Black);
b12.setText("START");
break;
case 2:
b13.setBackgroundResource(R.color.Black);
b13.setText("START");
break;
case 3:
b14.setBackgroundResource(R.color.Black);
b14.setText("START");
break;
Double score;
int tiles=-1;
double startTime=0;
@Override
if (tiles == -1) {
((Button) v).setText("");
startTime = SystemClock.uptimeMillis();
}
score=(SystemClock.uptimeMillis()-startTime)/1000;
tiles++;
if(tiles==maxtiles)
i.putExtra("SCORE",String.valueOf(score));
i.putExtra(MAXTILES,String.valueOf(maxtiles));
startActivity(i);
switch (v.getId()) {
case R.id.btn11:
switch (c3) {
case 0:
v.setBackgroundResource(R.color.Grey);
c4 = c3;
c3 = c2;
c2 = c1;
if(tiles<=maxtiles-2) {
c1 = r.nextInt((int) SystemClock.uptimeMillis()) % 4;
else
c1=4;
}
switch (c1) {
case 0:
b1.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 1:
b2.setBackgroundResource(R.color.Black);
b1.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 2:
b3.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 3:
b4.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
break;
case 4:
b4.setBackgroundResource(R.color.White);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
break;
switch (c2) {
case 0:
b6.setBackgroundResource(R.color.Black);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
b9.setBackgroundResource(R.color.White);
break;
case 1:
b7.setBackgroundResource(R.color.Black);
b8.setBackgroundResource(R.color.White);
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
break;
case 2:
b8.setBackgroundResource(R.color.Black);
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
break;
case 3:
b9.setBackgroundResource(R.color.Black);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
break;
case 4:
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
break;
switch (c3) {
case 0:
b11.setBackgroundResource(R.color.Black);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
case 1:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.Black);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
case 2:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.Black);
b14.setBackgroundResource(R.color.White);
break;
case 3:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.Black);
break;
case 4:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
}
switch (c4) {
case 0:
b16.setBackgroundResource(R.color.Grey);
b17.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 1:
b17.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 2:
b18.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b17.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 3:
b19.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b17.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
break;
case 4:
b19.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b17.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
break;
break;
default:
break;
case 1:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
case 2:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
case 3:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
}
break;
case R.id.btn12:
switch (c3) {
case 1:
c4 = c3;
c3 = c2;
c2 = c1;
if(tiles<=maxtiles-2) {
c1 = r.nextInt((int) SystemClock.uptimeMillis()) % 4;
else
c1=4;
switch (c1) {
case 0:
b1.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 1:
b2.setBackgroundResource(R.color.Black);
b1.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 2:
b3.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 3:
b4.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
break;
case 4:
b4.setBackgroundResource(R.color.White);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
break;
switch (c2) {
case 0:
b6.setBackgroundResource(R.color.Black);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
b9.setBackgroundResource(R.color.White);
break;
case 1:
b7.setBackgroundResource(R.color.Black);
b8.setBackgroundResource(R.color.White);
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
break;
case 2:
b8.setBackgroundResource(R.color.Black);
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
break;
case 3:
b9.setBackgroundResource(R.color.Black);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
break;
case 4:
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
break;
switch (c3) {
case 0:
b11.setBackgroundResource(R.color.Black);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
case 1:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.Black);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
case 2:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.Black);
b14.setBackgroundResource(R.color.White);
break;
case 3:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.Black);
break;
case 4:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
switch (c4) {
case 0:
b16.setBackgroundResource(R.color.Grey);
b17.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 1:
b17.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 2:
b18.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b17.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 3:
b19.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b17.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
break;
break;
default:
break;
case 0:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
case 2:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
case 3:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
break;
case R.id.btn13:
switch (c3) {
case 2:
c4 = c3;
c3 = c2;
c2 = c1;
if(tiles<=maxtiles-2) {
c1 = r.nextInt((int) SystemClock.uptimeMillis()) % 4;
else
c1=4;
switch (c1) {
case 0:
b1.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 1:
b2.setBackgroundResource(R.color.Black);
b1.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 2:
b3.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 3:
b4.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
break;
case 4:
b4.setBackgroundResource(R.color.White);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
break;
switch (c2) {
case 0:
b6.setBackgroundResource(R.color.Black);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
b9.setBackgroundResource(R.color.White);
break;
case 1:
b7.setBackgroundResource(R.color.Black);
b8.setBackgroundResource(R.color.White);
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
break;
case 2:
b8.setBackgroundResource(R.color.Black);
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
break;
case 3:
b9.setBackgroundResource(R.color.Black);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
break;
case 4:
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
break;
switch (c3) {
case 0:
b11.setBackgroundResource(R.color.Black);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
case 1:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.Black);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
case 2:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.Black);
b14.setBackgroundResource(R.color.White);
break;
case 3:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.Black);
break;
case 4:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
switch (c4) {
case 0:
b16.setBackgroundResource(R.color.Grey);
b17.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 1:
b17.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 2:
b18.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b17.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 3:
b19.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b17.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
break;
}
break;
default:
break;
case 1:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
case 0:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
case 3:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
break;
case R.id.btn14:
switch (c3) {
case 3:
c4 = c3;
c3 = c2;
c2 = c1;
if(tiles<=maxtiles-2) {
c1 = r.nextInt((int) SystemClock.uptimeMillis()) % 4;
else
c1=4;
switch (c1) {
case 0:
b1.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 1:
b2.setBackgroundResource(R.color.Black);
b1.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 2:
b3.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
b4.setBackgroundResource(R.color.White);
break;
case 3:
b4.setBackgroundResource(R.color.Black);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
break;
case 4:
b4.setBackgroundResource(R.color.White);
b2.setBackgroundResource(R.color.White);
b3.setBackgroundResource(R.color.White);
b1.setBackgroundResource(R.color.White);
break;
switch (c2) {
case 0:
b6.setBackgroundResource(R.color.Black);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
b9.setBackgroundResource(R.color.White);
break;
case 1:
b7.setBackgroundResource(R.color.Black);
b8.setBackgroundResource(R.color.White);
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
break;
case 2:
b8.setBackgroundResource(R.color.Black);
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
break;
case 3:
b9.setBackgroundResource(R.color.Black);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
break;
case 4:
b9.setBackgroundResource(R.color.White);
b6.setBackgroundResource(R.color.White);
b7.setBackgroundResource(R.color.White);
b8.setBackgroundResource(R.color.White);
break;
default:
break;
}
switch (c3) {
case 0:
b11.setBackgroundResource(R.color.Black);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
case 1:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.Black);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
case 2:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.Black);
b14.setBackgroundResource(R.color.White);
break;
case 3:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.Black);
break;
case 4:
b11.setBackgroundResource(R.color.White);
b12.setBackgroundResource(R.color.White);
b13.setBackgroundResource(R.color.White);
b14.setBackgroundResource(R.color.White);
break;
default:
break;
switch (c4) {
case 0:
b16.setBackgroundResource(R.color.Grey);
b17.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 1:
b17.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 2:
b18.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b17.setBackgroundResource(R.color.White);
b19.setBackgroundResource(R.color.White);
break;
case 3:
b19.setBackgroundResource(R.color.Grey);
b16.setBackgroundResource(R.color.White);
b17.setBackgroundResource(R.color.White);
b18.setBackgroundResource(R.color.White);
break;
break;
default:
break;
case 1:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
case 2:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
case 0:
i.putExtra("SCORE", "FAILED");
startActivity(i);
break;
break;
};
b11.setOnClickListener(clickListener);
b12.setOnClickListener(clickListener);
b13.setOnClickListener(clickListener);
b14.setOnClickListener(clickListener);
Mainactivity2.java
package com.example.mygame.pianotiles;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
Intent i=null;
Button b1,b2,b3;
@Override
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
i=new Intent(this,MainActivity.class);
TextView tvs=(TextView)findViewById(R.id.tvs);
b1=(Button)findViewById(R.id.btnnwgme);
b2=(Button)findViewById(R.id.btnqt);
b3=(Button)findViewById(R.id.btnshare);
if(score.equals("FAILED"))
((TextView)findViewById(R.id.tvs)).setText(score);
((LinearLayout)findViewById(R.id.linear2)).setBackgroundResource(R.color.Red);
b1.setBackgroundResource(R.color.Red);
b2.setBackgroundResource(R.color.Red);
b3.setBackgroundResource(R.color.Red);
else
((TextView)findViewById(R.id.tvscore)).setText(score + "s");
b1.setOnClickListener(new View.OnClickListener() {
@Override
startActivity(i2);
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
onBackPressed();
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
if(score.equals("FAILED"))
else
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT,"My Score!!");
startActivity(Intent.createChooser(intent,"Share..."));
});
}
@Override
i2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i2.putExtra("EXIT", true);
startActivity(i2);
// super.onBackPressed();
Mainacitvity3.java
package com.example.mygame.pianotiles;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main3);
Button b=(Button)findViewById(R.id.btnplay);
b.setOnClickListener(new View.OnClickListener() {
@Override
startActivity(i);
});
Mainacivity4.java
package com.example.mygame.pianotiles;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
@Override
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main4);
tilelist.add("50 TILES");
tilelist.add("100 TILES");
tilelist.add("150 TILES");
tilelist.add("200 TILES");
tilelist.add("250 TILES");
tilelist.add("300 TILES");
tilelist.add("350 TILES");
tilelist.add("400 TILES");
tilelist.add("450 TILES");
tilelist.add("500 TILES");
ArrayAdapter<String>adapter=new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,tilelist);
ListView lv=(ListView)findViewById(R.id.lv1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
intent.putExtra(TILES,(position + 1)*50);
startActivity(intent);
});
Main5activity.java
package com.example.mygame.pianotiles;
import android.content.Intent;
import android.content.res.Resources;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
@Override
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main5);
i2 = getIntent();
myTask.setOnPreExecuteCalledListener(new Task.OnPreExecuteCalledListener() {
@Override
if (i2 != null) {
if (i2.getBooleanExtra("EXIT", false)) {
myTask.cancel(true);
finish();
});
myTask.execute(1);
myTask.setOnPostExecuteDone(new Task.OnPostExecuteDone() {
@Override
startActivity(i);
});
Task.java
package com.example.mygame.pianotiles;
import android.os.AsyncTask;
import android.os.SystemClock;
void onDone();
void onpreExecutecalled();
private OnPostExecuteDone o;
void Task()
void setOnPreExecuteCalledListener(OnPreExecuteCalledListener
onPreExecuteCalledListener)
opcl=onPreExecuteCalledListener;
o=op;
@Override
opcl.onpreExecutecalled();
super.onPreExecute();
@Override
while(SystemClock.uptimeMillis()-startTime<5000)
return null;
@Override
o.onDone();
Activity_main.xml
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.mygame.pianotiles.MainActivity"
android:background="@color/Grey">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn1"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn2"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn3"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn4"
android:background="@color/White"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn6"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn7"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn8"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn9"
android:background="@color/White"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn11"
android:textStyle="bold"
android:textColor="@color/White"
android:textSize="27dp"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn12"
android:textStyle="bold"
android:textColor="@color/White"
android:textSize="27dp"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn13"
android:textStyle="bold"
android:textColor="@color/White"
android:textSize="27dp"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn14"
android:textStyle="bold"
android:textColor="@color/White"
android:textSize="27dp"
android:background="@color/White"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn16"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn17"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn18"
android:background="@color/White"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="93dp"
android:layout_height="175dp"
android:id="@+id/btn19"
android:background="@color/White"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Acivity_main2.xml
<?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="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="60dp"
tools:context="com.example.mygame.pianotiles.Main2Activity"
android:background="@color/Black"
android:orientation="vertical"
android:id="@+id/linear2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/White"
android:textAlignment="center"
android:textSize="85dp"
android:textStyle="bold"
android:text="GAME OVER"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/White"
android:textStyle="bold"
android:textSize="49dp"
android:text="YOUR SCORE"
android:id="@+id/tvs"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/White"
android:textStyle="bold"
android:textSize="59dp"
android:id="@+id/tvscore"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center">
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="90dp"
android:layout_height="95dp"
android:background="@color/Black"
android:textSize="30dp"
android:textColor="@color/White"
android:text="New Game"
android:id="@+id/btnnwgme"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_marginLeft="20dp"
android:layout_marginTop="18dp"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="@color/Black"
android:textColor="@color/White"
android:textSize="30dp"
android:text="QUIT"
android:id="@+id/btnqt"/>
<Button
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_marginLeft="20dp"
android:layout_marginTop="18dp"
android:layout_width="105dp"
android:layout_height="90dp"
android:background="@color/Black"
android:textColor="@color/White"
android:textSize="30dp"
android:text="SHARE"
android:id="@+id/btnshare"/>
</LinearLayout>
</LinearLayout>
Activity_main3.xml
<?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="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="60dp"
tools:context="com.example.mygame.pianotiles.Main3Activity"
android:orientation="vertical"
android:background="@color/Black"
android:textAlignment="center"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/White"
android:textStyle="bold"
android:textSize="89dp"
android:text="Piano Tiles"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/White"
android:textSize="28dp"
android:textAlignment="center"
<Button
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Black"
android:textColor="@color/White"
android:textAlignment="center"
android:textSize="70dp"
android:text="PLAY"
android:id="@+id/btnplay"
/>
</LinearLayout>
Activity_main4.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.mygame.pianotiles.Main4Activity"
android:paddingTop="60dp">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv1"></ListView>
</RelativeLayout>
Acitvity_main5.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="30dp"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="60dp"
tools:context="com.example.mygame.pianotiles.Main5Activity"
android:background="@color/Black2"
android:id="@+id/mainl">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:layout_marginLeft="20dp"
android:src="@drawable/gamelogo"/>
</RelativeLayout>
Q3. Create an android application that can show all the contents of memory card?
Ans:
Mainactivity.java
package com.example.readfilefromexternalresource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.textView);
if (!(state.equals(Environment.MEDIA_MOUNTED))) {
} else {
try {
String line;
textBuilder.append(line);
textBuilder.append("\n");
textView.setText(textBuilder);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Mainactivity.xml
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Q4. Explain the steps of adding the application on PlayStore along with screenshot
of each step.
Ans:
Step 1: First generate signed apk of your Android App to publish it on Play Store.
Step 2: Now you will need to sign up for Google Play Console to publish and
manage your Android App.
Step 3: Login with your Gmail account that you want to use for publishing App on
Play Store.
Step 4: Now there are 4 steps to complete the registration for Google play store
console. You have already completed two.
Step 5: After reading the Google play store developer distribution agreement
agree to their terms by clicking on check box
Step 6: Now you will need to pay one time ‘Developer Registration Fee’ of $25 to
Google. Please fill your credit card details to make the payment.
Step 7: Complete your account details for Google developer account. For example
see the below image:
Step 8: Now click on Create Application
Step 13: Now scroll down and fill other details which include application type,
category, website, email and phone no.
After this check privacy policy because now we are not submitting and then click
on save draft. If your App require user permission then it is mandatory to put
privacy url.
Click on Save Draft to save your work so far.
Step 14: After saving data on draft now go to app release and click on manage
production.
Step 15: Now you will see create release now click on it.
Step 16: After click on create release you will see browse files click on it and
upload your signed APK.
Step 17: Once the upload is successful then scroll down and click on review to
check.
Step 23: Now scroll down and see mandatory things with * you need to select
After this click on save draft .
Step 24: Now Click on ready on publish along with save draft and click on Manage
release.
Step 28: After review click on Start Rollout to production. Now you need to
confirm. After confirm you will need to wait for one or six hour for approval.
Q5. Add a ListView component to your activity and design an Activity using that
ListView which can display different color names.
Ans. package com.example.ListDisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<resources>
<string name="app_name">ListDisplay</string>
<string name="action_settings">Settings</string>
</resources>
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>