Android Club – How to set border for TextView?

Create resource file inside res/drawable. Name it shape_border.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#ffffff" />
   <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>

In your TextView, use this shape:

<TextView android:text="Some text" android:background="@drawable/back"/>

Android Club – Change ActionBarDrawerToggle for fragments: hamburger/back icon

This is not tutorial. This is example code how to implement one fragment as Home Fragment, others as children. If you are on home parent fragment – app shows hamburger icon, if you are on child fragment, it will show back button. If you want to know more about how to implement Material Design Navigation Drawer, you can open this post here

Continue reading Android Club – Change ActionBarDrawerToggle for fragments: hamburger/back icon

Android Club – ViewPager tutorial

In this tutorial, I will show how to use ViewPager. First of all, let’s create Android Application project. Open activity_main.xml. Put ViewPager there:

<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=".MainActivity">

    <android.support.v4.view.ViewPager
       android:id="@+id/pager"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

</RelativeLayout>

Now, you can create fragment Right button of your mouse on your package->New->Fragment->Blank. This will create new Fragment. So clear Fragment code, so after that you should have only onCreateView method inside your fragment.

public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }
}

Now go to fragment_first layout and change background color and text view text.

<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="net.joerichard.viewpagertutorial.FirstFragment"
   android:background="#FF0000">

    <!-- TODO: Update blank fragment layout -->
    <TextView android:layout_width="match_parent" android:layout_height="match_parent"
       android:text="First fragment"/>

</FrameLayout>

Create fragments like this as much as you want. I created 3 of them. Next step is to create adapter for ViewPager

public class MyViewPagerAdapter extends FragmentStatePagerAdapter {

    public MyViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new FirstFragment();
            case 1:
                return new SecondFragment();
            case 2:
                return new ThirdFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

I called my adapter – MyViewPagerAdapter. getCount method returns how many fragments I will have. getItem method returns respective fragment for page by its position. Now reference our ViewPager and set our ViewPagerAdapter(inside MainActivity’s onCreate method):

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);

SourceCode
APK file

That’s it now we create ViewPager which holds fragments.

Android Club – How to create Anroid Browser application?

In this tutorial, I will show how to create browser application for Android OS. This browser will open www.google.com by default. Moreover, we will add such function to this browser, which help open URL from another application. So let’s start! First of all, create new Android Application Project in Android Studio. By default, your project will have MainActivity. Open AndroidManifest.xml in manifests folder. Add new intent filter:

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"/>
            </intent-filter>

Add new permission to connect to INTERNET.

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

After changing AndroidManifest.xml, it should look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="net.joerichard.joebrowser" >

    <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:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Now open your layout file – activity_main.xml. Add new WebView:

<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=".MainActivity">

    <WebView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/wvBrowser"></WebView>

</RelativeLayout>

Now open MainActivity.java. Find button and enable JavaScript:

        final WebView wvBrowser = (WebView) findViewById(R.id.wvBrowser);
        // let's enable javascript in WebView
        wvBrowser.getSettings().setJavaScriptEnabled(true);

Next step is to set WebViewClient. You can ask why we need this client? I answer: this client helps us to handle opening new links. Without this, our browser will try to open link in another browser. In that case, what is the point to create browser application, if we open link in another browser.

        wvBrowser.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                wvBrowser.loadUrl(url);
                return true;
            }
        });

Next step is to check application opened from home screen or from another application. If intent’s data, which we are supposed to receive from another application, is null, it means that we opened from home screen. In this case we open www.google.com by default. If data is not null we can get URL. In this case we open this URL.

        Intent intent = getIntent();
        if(intent.getData()!=null)
        {
            String schema = intent.getData().getScheme();
            String host = intent.getData().getHost();
            String path = intent.getData().getPath();
            String url = schema + "://www."+host+path;
            wvBrowser.loadUrl(url);
        } else {
            // data is null
            // user opened application from home screen
            // let's open default url: google.com
            wvBrowser.loadUrl("http://www.google.com");
        }

When we get URL(http://joerichard.net/ac/android-club-android-presentations/) from another application, we get it in different paramenters. For example, schema is intent’s schema: http. host is domain of url: joerichard.net. path is value which comes after domain : /ac/android-club-android-presentations/

After all, MainActivity.java should look like this:

package net.joerichard.joebrowser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity {

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

        final WebView wvBrowser = (WebView) findViewById(R.id.wvBrowser);
        // let's enable javascript in WebView
        wvBrowser.getSettings().setJavaScriptEnabled(true);

        wvBrowser.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                wvBrowser.loadUrl(url);
                return true;
            }
        });

        Intent intent = getIntent();
        if(intent.getData()!=null)
        {
            String schema = intent.getData().getScheme();
            String host = intent.getData().getHost();
            String path = intent.getData().getPath();
            String url = schema + "://www."+host+path;
            wvBrowser.loadUrl(url);
        } else {
            // data is null
            // user opened application from home screen
            // let's open default url: google.com
            wvBrowser.loadUrl("http://www.google.com");
        }

    }

}

Screenshot:
Screenshot_2015-07-11-10-55-11
Download source code here
Download apk here(for security reasons I could not upload apk, so I changed it to doc. Before you install it, change filetype from doc to apk)