Android – How to speed up Android Studio?

Android Studio is great tool to develop Android apps. However, it is really sluggish. But there is a way how to solve this problem. Thanks to Josh and for his solution.

Solution.
Step 1: Open File->Settings->Appearance & Behavior->Appearance->Remove tick from Animate windows->Apply
Step 2: Open File->Settings->System Settings->Remove tick from Synchronize files on frame activation

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)

Android Club – Go to another activity using Intent

ActivityA.java

public class ActivityA extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        // find button
        Button bOpenActivityB = (Button) findViewById(R.id.bOpenActivityB);
        // set on click listener
        // when you click this button, slushatel's code will work
        bOpenActivityB.setOnClickListener(slushatel);

    }

    View.OnClickListener slushatel = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // create intent
            // go from ActivityA to ActvityB
            Intent sel = new Intent(ActivityA.this, ActivityB.class);
            // let's go
            startActivity(sel);
        }
    };

}

activity_a.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"
   tools:context="net.joerichard.helloandroid.ActivityA">

    <Button
       android:id="@+id/bOpenActivityB"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Сим сим откройся"/>

</LinearLayout>

ActivityB.java

public class ActivityB extends ActionBarActivity {

    static int create_counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_b);

    }

}

Android Club – How to show text in TextView?

First of all, let’s open your activity. It starts with this method:

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

    }

This line says that we are going to use activity_a layout as layout for our activity.

setContentView(R.layout.activity_a);

Open res folder, then layout, and open activity_a.xml. Inside it put one TextView. After adding TextView, your layout should look like this:

<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="net.joerichard.helloandroid.ActivityA">

    <TextView
       android:id="@+id/txtCreate"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textColor="#000000" />

</LinearLayout>

Here you can see that we set txtCreate as TextView’s id. Later we will refer this id to get access to this TextView. Now open your Activity class, then refer TextView by its id. Then use method setText to set text.

    private static Integer counter_create = 0;

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        TextView txtCreate = (TextView) findViewById(R.id.txtCreate);
        counter_create++;
        txtCreate.setText(counter_create);
       
    }

Pay attention to that we created static class int variable(counter_create) which we increment each time we call corresponding method(onCreate).

Now your task is to count all callbacks and show in different TextViews. Good luck!