Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

September 22, 2016

EditText Floating Label - Android


The EditText is the standard text entry widget in Android apps. If the user needs to enter text into an app, this is the primary way for them to do that. Traditionally, the EditText hides the hint message after the user starts typing. In addition, any validation error messages had to be managed manually by the developer.

Starting with Android M and the design support library, the TextInputLayout can be used to setup a floating label to display hints and error messages. First, wrap the EditText in a TextInputLayout:
<android.support.design.widget.TextInputLayout
    android:id="@+id/username_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/etUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:hint="Username" />

</android.support.design.widget.TextInputLayout>

We can also use the TextInputLayout to display error messages using the setError and setErrorEnabled properties in the activity at runtime:
private void setupFloatingLabelError() {
    final TextInputLayout floatingUsernameLabel = 
(TextInputLayout) findViewById(R.id.username_text_input_layout);
    floatingUsernameLabel.getEditText().addTextChangedListener(new TextWatcher() {
        // ...
        @Override
        public void onTextChanged(CharSequence text, int start, int count, int after) {
            if (text.length() > 0 && text.length() <= 4) {
                floatingUsernameLabel.setError(getString(R.string.username_required));
                floatingUsernameLabel.setErrorEnabled(true);
            } else {
                floatingUsernameLabel.setErrorEnabled(false);
            }
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, 
                                      int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

Here we use the addTextChangedListener to watch as the value changes to determine when to display the error message or revert to the hint.

September 21, 2016

Android Splash Screen Tutorial



I assume you have one or more applications on your phone that uses splash screen. Splash screen simply refers to the first activity that causes a delay for some seconds while in most cases the logo of the application is displayed or something similar to that is done before taking you to the main activity where you begin to make use of the functionalities of that app.

Some persons find it annoying while others don't. So I suggest you shouldn't over use them in your applications. As the saying goes, "too much of everything is bad."

In this tutorial, I will be teaching you how you can create splash screen in your android application. Feel free to ask questions in the comment section below and I will be glad to answer them.

I don't advice potential programmers or developers to copy and paste codes. I suggest you type everything yourself. It gives you a better understanding.

Open Android Studio, create New Project and give the Application Name ‘SplashScreen’.

Leave the activity name as ‘MainActivity’ and hit the finish button to build the project.

Now create new Java Class under Java folder by right clicking on your package name > New > Java Class. We can give name ‘ SplashScreen.java’ to it.

SplashScreen.java
package com.technology.airwaves.splashscreen;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * Created by Joe Gee on 9/9/2016.
 */

public class SplashScreen extends Activity {

    private boolean backBtnPressed;
    private static final int SPLASH_SCREEN_DURATION = 3000;
    //private Handler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        Thread timerThread = new Thread(){
          public void run(){
              try{
                  sleep(SPLASH_SCREEN_DURATION);
              }catch(InterruptedException e){
                    e.printStackTrace();
              }finally {
       Intent intent = new Intent(getApplicationContext(),MainActivity.class);
       startActivity(intent);
              }
          }
        };
timerThread.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

}


Create XML file under res folder by right clicking on layout > New > Layout resource file. We'll give it the name ‘splash.xml’.

splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#013498"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/applogo"
        android:layout_centerVertical="true"
        android:src="@drawable/app_logo"
        android:layout_centerHorizontal="true"
        android:background="#013498"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/loading"
        android:layout_below="@id/applogo"
        android:text="Loading"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="#fff"
        android:textSize="30sp"
        />
</RelativeLayout>

Make sure you have the image used in the ImageView in your drawable folder and the name should correspond to the name used

In AndroidManifest.xml, we have to add both activities SplashScreen.java & splash.xml by which our splash screen would be appear before MainActivity (Home Screen). After adding some codes, AndroidManifest.xml will be,

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" />
    </application>

</manifest>

What we did in the AndroidManifest.xml file is to change the launcher category. By default, the main activity which is always known as MainActivity.java should be launched first when the application is launched. But now we have changed it in a manner that the SplashScreen.java activity will execute first and cause a delay of 3 seconds before launching the second activity known as MainActivity.java.

Moving from one activity to the other is done using Intent. I discussed more on Intent in one of my tutorials. Read more on Intent if you are not clear on it.

As it is, that's how you can create a splash screen activity in your Android application. Thank you.

September 20, 2016

Android Intent - The man in-between



What is an Intent you may ask.

According Android Developers, an intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components,  and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background service.

It could also be referred to as a messaging object you can use to request an action from another app component.

Basically there are two types of intent. Explicit intent and implicit intent. Whereas explicit intent specifies the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, start a new activity in response to a user action or start a service to download a file in the background. Implicit intent on the other hand do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.


With that said, lets see how we can use intent in an android application.

Go to File in your IDE, click and choose New to create new application. (I assume you know how to create a new application so I will not proceed further on that). Give your application a name of your choice. For the benefit of this tutorial, am naming my application as ExplicitIntent and the launcher activity as  ActivityA and click finish.

Without wasting time, lets create another activity. This time I give it the name ActivityB to distinguish it from the first. ActivityB therefore happens to be a sub-activity of ActivityA. See snapshot below

STEP 1

STEP 2
With these two activities in place, lets go ahead and code.

First of all, lets set the XML files in order. In your activity_a.xml or whatever name you'd used to represent the first activity, here is the XML style or components on the file.

Activity A XML file

<?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="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="technology.airwaves.com.explicitintent.ActivityA">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:text="Launch Activity B"
        android:onClick="startActivityB"
        android:id="@+id/btn1"/>
</RelativeLayout>

In XML file above, we're using xml's onclick event handler to call the method startActivityB when the button is pressed.

Activity B XML file

<?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="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="technology.airwaves.com.explicitintent.ActivityB">
    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="center"
    android:id="@+id/imageView1"
    android:src="@drawable/coffee_cup"
    />
</RelativeLayout>



Here comes the code for the first activity (ActivityA).
package technology.airwaves.com.explicitintent;

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

public class ActivityA extends AppCompatActivity {
Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        btn = (Button)findViewById(R.id.btn1);


    }

    public void startActivityB(View v){
        Intent intent = new Intent(this,ActivityB.class);
        startActivity(intent);
    }
}


And the second activity (ActivityB)
package technology.airwaves.com.explicitintent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class ActivityB extends AppCompatActivity {

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

    }
}

As simple and straight forward as that. When the button is clicked in activity A, the method startActivityB() is called. Inside this method, we have defined it to start an explicit intent and call ActivityB who happens to be our target or interest. Intent takes two parameters, the application context and the name of the class to be called with the .class extension.

If you have any question or doubt, leave a comment below and I will be glad to answer you. If you like this, you can comment as well and recommend it to your friends. Thank you.







Rounded Layout Android Tutorial with images and Code



Graphical User Interface is what makes all the difference when it comes to Android app development or iOS. You may have a wonderful application but if the UI is poor, users may not find it interesting or kin to use it.

In this tutorial, I will be taking you through on how to create or make your own rounded effect on layout of any view (LinearLayout, RelativeLayout, ImageView, Button, etc) in Android.

We'll first create a rounded layout on all sides.
 STEPS


  1. Right click on your drawable folder and choose Drawable resource file
  2. Give it a name. For the sake of this tutorial. I will give it the name all_rounded_layout.xml

In your newly launched file, change the selector tag to shape

to



Then add the following CODE between 
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
and
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#8bb32e"></solid>
    <stroke android:width="1dp"
        android:color="#dddddd"></stroke>
    <padding android:bottom="5dp"
        android:top="5dp"
        android:left="5dp"
        android:right="5dp"></padding>
    <corners android:radius="10dp"></corners>
</shape>
Lets proceed to our View (LinearLayout,RelativeLayout,ImageView, etc). The view that you want it to have rounded effect. Now we'll use all_rounded_layout as background in our intended view.
<LinearLayout android:layout_width="20dp"
        android:layout_height="20dp"
        android:background="@drawable/all_rounded_layout"
        android:layout_centerInParent="true">
         
         <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ImageView1"
            android:padding="5dp"
            android:background="@drawable/event"
            android:src="@drawable/ic_talktojoegee"
            android:elevation="4dp"/>
         
    </LinearLayout>


NOTE:

The LinearLayout where we've used our all_rounded_layout as backround has a HEIGHT of 20dp and WIDTH of 20dp. Therefore, our corners tag has a radius of 10dp so that we can get a perfect rounded circle.

If you would like to have rounded effect on maybe extreme right or left or bottomLeft or bottomRight, use any of the attributes below in the corners tag.
 <corners
        android:bottomRightRadius="10dp"
        android:topRightRadius="10dp"
        android:bottomLeftRadius="10dp"
        android:topLeftRadius="10dp"></corners>





If you have any question or not clear, let a comment below. Thank you