Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,198,532 members, 7,968,506 topics. Date: Monday, 07 October 2024 at 08:37 AM

Nollyj2's Posts

Nairaland Forum / Nollyj2's Profile / Nollyj2's Posts

(1) (of 1 pages)

Programming / Re: Embed And Play Youtube Video In Android Webview by nollyj2: 11:32am On Apr 16, 2015
Activity_Main.xml
The main layout of our application will contain a WebView. You can drag and drop the View control to the project activity_main.xml. The xml code snippet for the layout is shown below.


<RelativeLayout xmlns:android=""
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:layout_alignParentStart="true" />

</RelativeLayout>


Since we are going to use internet in this application, we are going to add internet persimmon in our Manifest.xml file. In the MainActivity section, we also enable Hardware accelerated to true. This is in case if it is not already enable in your device. The following is the update code in the Manifest.xml file.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
package="" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:hardwareAccelerated="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


MainActivity.java

In the MainActivity.java file, we will get the instances of our WebView controls. We will convert the iFrame code of our embed YouTube video to a string.

In other to use the WebView to play our video rather than going to the external website, we created a WebView client to replace the default WebView client of the WebView.

With the WebView web settings, we enabled Javascript and load the video iFrame string to the webview instance by using the loaddata() of the WebView.

The final code in MainActvity.java is shown below.


package inducesmile.com.androidembedvideo;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends ActionBarActivity {

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

String frameVideo = "<html><body>Youtube video .. <br> <iframe width=\"320\" height=\"315\" src=\"https://www.youtube.com/embed/lY2H2ZP56K4\" frameborder=\"0\" allowfullscreen></iframe></body></html>";

WebView displayVideo = (WebView)findViewById(R.id.webView);
displayVideo.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = displayVideo.getSettings();
webSettings.setJavaScriptEnabled(true);
displayVideo.loadData(frameVideo, "text/html", "utf-8"wink;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


Save the file and run your project. If everything works for you, the project will appear like this in your device.



You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

You can download the complete source code for this project here http://inducesmile.com/android/embed-and-play-youtube-video-in-android-webview/

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published
Programming / Re: Android Dictionary Application With Search Suggest And Text To Speech Feature by nollyj2: 9:08am On Apr 13, 2015
SQLITE Database
We will start from the backend and build the application to finish. The first thing we will do is to create a folder called databases inside assets folder. Since we created our SQLite database file using SQLite database manager, we will drag and drop the file inside the databases folder and zip.

We are preparing our database file this way since we are going to use a third party library called android sqlite asset helper. This library will help us to create and manage your database file. You can read more about this library here.

The next to do is the import the library to our project. Since the library makes use of gradle, just open your build.gradle file and copy and paste the code below under the dependence section.

compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'

Now you can build your project and the library will be added automatically to your project. If you are using Eclipse for you android development, you can search on how to import library inside android project.

We will go ahead with the SQLite database code now that we have finished setting up our database library.

We will create three different files – DictionaryDatabase.java, DatabaseObject.java and DbBackend.java.

The DictionaryDatabase java class will inherit from SQLiteAssetHelper which is a class from our imported library. We will define our database name and version. The version number is use to control our application database update.

Whenever there is a change in the database file, the version number will be updated. The code sample is shown below. Copy and paste it to your project file.

import android.content.Context;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class DictionaryDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAMES = "quiz";
private static final int DATABASE_VERSION = 5;

public DictionaryDatabase(Context context) {
super(context, DATABASE_NAMES, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
}


We are going to establish a connection for our database and return the instance of our database in DatabaseObject.java file. An instance of the DictionaryDatabase is created and the getReadableDatabase of the class is used to get the connection object. The getter and close methods in the class is used to return and close the database connection. The code for this class is shown below.

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
public class DbObject {
private static DictionaryDatabase dbHelper;
private SQLiteDatabase db;
public DbObject(Context context) {
dbHelper = new DictionaryDatabase(context);
this.db = dbHelper.getReadableDatabase();
}
public SQLiteDatabase getDbConnection(){
return this.db;
}
public void closeDbConnection(){
if(this.db != null){
this.db.close();
}
}
}
Programming / Android Dictionary Application With Search Suggest And Text To Speech Feature by nollyj2: 10:42am On Apr 12, 2015
In this android tutorial we are going to learn how to create an android dictionary application with Search Suggest and Text to Speech feature. The android application can be used in defining terms or words use in a particular field.

You can found an example of Biology Dictionary I built in one of my apps in the Google Play Store.

The app will consist of different words and their meaning. When a word is selected, it will display the definition or meaning of the word with an option to convert it to speech.

The app will also has a search functionality with integrated search suggestion. When a user type in two characters the search box will display all the words that contain the type in characters.

If you still don't understand what search suggest means you can read my post on Android ListView with EditText Search Filter

We will start by designing android SQLITE database that will house our data. There are different ways we can use to create our database. In this tutorial, I am going to use a SQLITE Manager to create and manager the database file.

The database will be create using SQLite Expert Professional Manager which is a paid software but there are many free to use SQLite Manager you can get online and use.

I will go ahead and create a database called dictionary. Now, we have to create a table inside our database called dictionary. The table will have three columns – id, word and meaning. The SQLite code for the table is shown below.

Create Table [dictionary] (
[_id] INTEGER PRIMARY KEY,
[word] TEXT,
[meaning] TEXT
);


Before we start, the first thing I will do is to list the environment and tools I used in this android tutorial but feel free to use whatever environment or tools you are familiar with.

Windows 7
Android Studio
Samsung Galaxy Fame Lite
Min SDK 14
Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu
Click on New menu
Click on Android Application
Enter Project name: AndroidDictionaryApplication
Package: com.inducesmile.androiddictionaryapplication
Keep other default selections.
Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package.

1 Like

Programming / Re: Android Quiz Application With Json Parser, Php And Mysql Database by nollyj2: 5:14pm On Apr 10, 2015
Main Layout

We will start by adding background image to the root layout in our activity_main.xml layout file. The image I used can be found in the project source code below. You can use your own image be changing the above used image.

Add a Button View to the layout as shown in the image above. If you are using Eclipse or Android Studio, you can switch to the design view and drag and drop this View inside your layout file.

You can also copy and paste the following code below into this file if you don’t want to do it yourself.

<RelativeLayout xmlns:android="link missing due to Nairaland bot"
xmlns:tools="link missing due to Nairaland bot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="32dp"
android:layout_marginEnd="32dp"
android:background="@drawable/mbutton"
android:layout_marginBottom="48dp" />
</RelativeLayout>

The text content of our View components are stored in the strings.xml file. The updated contain of the file is shown below

<resources>
<string name="app_name">Android Quiz Application</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="title_activity_quiz">Quiz Questions</string>
<string name="question">Question 1. How is the first president of Nigeria?</string>
<string name="answer_one">Answer One</string>
<string name="answer_two"> Answer Two</string>
<string name="answer_three">Answer Three</string>
<string name="answer_four"> Answer Four</string>
<string name="next_questions"> Next Question</string>
<string name="previous_questions"> Previous Question</string>
</resources>

We will make use of few colors in this application so we have to update our color.xml file to include all these colours. This is important because it lets us have a single place where we can change and set colours for our project. The code is shown below.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#000000</color>
<color name="white">#ffffff</color>
</resources>

MainActivity.java

In the MainActivity.java file, we will get the instance of our button View and attach and event listener to the button View. When the button is click, it will redirect to the QuizActivity page. The code for the MainActivity.java is shown below.

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

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

Button startButton = (Button)findViewById(R.id.button);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, QuizActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

(1) (of 1 pages)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 42
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.