Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Convert your website into an Android App

 Converting a website or blog into ab Android Application is one of the way to promote your website and reach out to more people. It also helps you to make people get updated with the changes and updates of your websites. If you convert your website or blog into an Android App, you can upload it on Google Playstore and a lot of people will see and possibly download it. For your blog or website to display well, it must be mobile friendly and responsive. Let’s convert this blog into an Android Application If you have read my previous posts, you will be familiar with most of the things I will be saying. If you are not familiar, you can spare some little time to go through my posts. Create a new project on Android Studio Since we want a full screen Application, we will remove the Action bar. Navigate to styles.xml in your res folder and edit the style Just change the DarkActionBar to NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

Navigate to the manifest.xml and add the internet permission, if you don’t add this permission you App won’t display because it can’t access the internet.
Add this above the <application></application>

<uses-permission android:name="android.permission.INTERNET"/>

Navigate to the activity_main.xml in the layout folder and add this following lines of code to display the website or blog

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<!--This view is responsible for the display fo your website or blog-->
    <WebView
        android:id="@+id/webby"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</FrameLayout>

Navigate to the MainActivity.java and add this following lines of code

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    //Strore the URL of the website inside a String variable called      //url
    private String url = "https://wwww.rexandroid.blogspot.com";
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initialize the webview by calling findViewById
        webView = findViewById(R.id.webby);
        //get the web setiimgs
        //set for javascript containing site
        webView.getSettings().setJavaScriptEnabled(true);
        //responsible for opening links inside the app
        webView.setWebViewClient(new WebViewClient());
        //load the url of the blog you stored inside @url
        webView.loadUrl(url);
    }
//This prevent the App from closing when you press the back key to //go to the previous page
    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

If you wanna add some other functionality to your App like progress bar etc., feel free to do so.
If you need any help, feel free to drop your comment in the comment box.

What are Android Intents?

Intents
Intents are basically messages that are passed between components like activities, services, broadcast receivers and content providers. In a nutshell, intent is a messaging object you can use to request or an action or information from another app component. Intents can be used to start a new activity, pick a photo from you gallery, send an email message, launch a map, launch a webpage etc.


They are two types of intents in Android; they include implicit intents and explicit intents.
Explicit intents are used to invoke the activity class. In a nutshell, to link several activities together, you will have to use explicit intents. Apart from linking activities togetther, you can also send data or information from one activity to the other

Linking two Activities together (Link MainActivity with Main2Activity together)
Head on to your AndroidManifest.xml, your AndroidManifest.xml should look like this after creating the new Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.rexben.blogspotdemo">

    <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=".Main2Activity"></activity>
    </application>

</manifest>

In your MainActivity.java
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //The method launches the new Activity
    //MainActivity.this is the Activity we are declaring the intent from
    //Main2Activity.class is the Activity that will be launched when this
    //method is trigerred
    public void nextActivity(View view){
        
        Intent newActivity = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(newActivity);
    }
}

In your activity_main.xml

Passing data or information from one activity to another. I will show you how to pass texts (String) from the first Activity to the next Activity. Displaying the passed data in the next Activity - it's one thing to send a datum and it is another thing to display the datum.
Create another activity apart from your MainActivity and name it Main2Activity
In your MainActivity.java
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //The method launches the new Activity
    //MainActivity.this is the Activity we are declaring the intent from
    //Main2Activity.class is the Activity that will be launched when this
    //method is trigerred
    public void nextActivity(View view){
        EditText editText = (EditText) findViewById(R.id.best_color);
        String colorName = editText.getText().toString();
        Intent newActivity = new Intent(MainActivity.this, Main2Activity.class);
//Passing the entered text to the next activity
        newActivity.putExtra(Intent.EXTRA_TEXT, colorName);
        startActivity(newActivity);
    }
}

In your 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="com.example.rexben.sunshineapps.Main2Activity">

    <EditText
        android:id="@+id/best_color"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="casual"
        android:hint="Enter your best color"
        android:textSize="22sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="nextActivity" />
</LinearLayout>


In your Main2Activity.java
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        TextView textView = (TextView) findViewById(R.id.display_Color);
//Getting the data passed from the MainActivity
        Intent intent = getIntent();

        if (intent.hasExtra(Intent.EXTRA_TEXT)){
            String displayColor = intent.getStringExtra(Intent.EXTRA_TEXT);
            textView.setText("My best color is: " + displayColor);
        }
    }
}

In your 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context="com.android.rexben.blogspotdemo.Main2Activity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:id="@+id/display_Color"/>
</LinearLayout>

Implicit Intents are intents used to invoke the system's components. Unlike explicit intents where you know the Android components that will handle the task (Activity), you don't know the component because there might be one or more Application that can handle that task. For instance, when you want to allow users to send email messages from your App, you don't know the definite App that will handle the request. If the user has one email messaging App, it launches it instantly, if the users has more than one, the user is prompted to pick one.
I will be showing you how to use explicit intent to send text messages, email messages, launch a map, launch a web page
How to send a message
In your MainActivity.java
package com.android.rexben.blogspotdemo;

import android.os.Bundle;
import android.support.v4.app.ShareCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void sendMessage(View view){
        String type = "text/plain";
        String title = "Choose an App to complete this action";
        String message = "Visit www.rexandroid.blogspot.com to get started on" +
                "Android App developmenr. Thanks";

        ShareCompat.IntentBuilder.from(this)
                .setType(type).setChooserTitle(title).setText(message)
                .startChooser();
    }
}

In your activity_main.xml
<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"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:textSize="22sp"
        android:text="Click on the button to send a message"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:onClick="sendMessage"
        android:text="Send a message"/>
</LinearLayout>

To Launch a web page
In your MainActivity.xml
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private String myBlog = "http://www.rexandroid.blogspot.com";

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

    public void launchWebpage(View view) {
        Uri blogUri = Uri.parse(myBlog);
        Intent intent = new Intent(Intent.ACTION_VIEW, blogUri);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
}

In your activity_main.xml
<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"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:textSize="22sp"
        android:text="Click on the button to launch a web page"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:onClick="launchWebpage"
        android:text="Launch the web page"/>
</LinearLayout>

To launch a map
In your AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.rexben.blogspotdemo">
    <!--Add this permission in order to access internet -->
<uses-permission android:name="android.permission.INTERNET"/>
    <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=".Main2Activity"></activity>
    </application>

</manifest>

In your MainActivity.java
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private String mapAddress = "1600 Amphitheatre Parkway, CA";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void launchMap(View view) {
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("geo").path("0,0").query(mapAddress);
        Uri uri = builder.build();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(uri);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Toast.makeText(MainActivity.this, "You don't have the App to perform
this task",
                    Toast.LENGTH_LONG).show();
        }
    }
}

In your activity_main.xml
<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"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:textSize="22sp"
        android:text="Click on the button to launch a map"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:onClick="launchMap"
        android:text="Launch a map"/>
</LinearLayout>

To dial a number
In your AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.rexben.blogspotdemo">
    <!--Add this permission in order to access internet -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <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=".Main2Activity"></activity>
    </application>

</manifest>

In your MainActivity.java
package com.android.rexben.blogspotdemo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;


public class MainActivity extends AppCompatActivity {

    private String number = "tel:2347032716134";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void dialNumber(View view) {
        Uri uriDial = Uri.parse(number);
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(uriDial);
        startActivity(intent);
    }
}

In your activity_main.xml
<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"
    android:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:textSize="22sp"
        android:text="Click on the button to dial my number"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:onClick="dialNumber"
        android:text="Dial my number"/>
</LinearLayout>