Saturday, March 30, 2019

Login form in android app | Android Studio | 2019

Login Form


How to make a login form in android.

Let's create a login app in just a few simple steps. Making a login form in the Android app is very simple. We are using two EditText components as username and password. Button as login button.

Follow the steps below:-
Step1:- Create an empty activity project and named it as LoginApp.
Step2:- Open up your activity_main.xml layout file.
Step3:- Copy and paste the below code as given.

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

    <RelativeLayout        
        android:layout_marginTop="150dp"        
        android:padding="10dp"        
        android:layout_width="match_parent"        
        android:layout_height="wrap_content">

        <EditText            
           android:inputType="text"            
           android:textColorHint="@color/gray"            
           android:textColor="@color/white"            
           android:padding="10dp"            
           android:background="@color/colorAccent"            
           android:hint="@string/user"            
           android:id="@+id/user"            
           android:layout_width="match_parent"            
           android:layout_height="wrap_content" />

        <EditText            
           android:inputType="textPassword"            
           android:layout_marginTop="10dp"            
           android:background="@color/colorAccent"            
           android:textColorHint="@color/gray"            
           android:textColor="@color/white"            
           android:padding="10dp"            
           android:layout_below="@id/user"            
           android:hint="@string/user"            
           android:id="@+id/pass"            
           android:layout_width="match_parent"            
           android:layout_height="wrap_content" />

        <Button            
           android:textColor="@color/white"            
           android:background="@color/colorPrimaryDark"            
           android:layout_marginTop="5dp"            
           android:layout_centerInParent="true"            
           android:layout_below="@id/pass"            
           android:onClick="login"            
           android:text="@string/login_btn"            
           android:id="@+id/loginbtn"            
           android:layout_width="wrap_content"            
           android:layout_height="wrap_content" />

    </RelativeLayout>

</LinearLayout>
Step4:- Open your MainActivity.java java file.
Step5:-Copy and paste the below code:-

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
       EditText user,pass;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        user = findViewById(R.id.user);
        pass = findViewById(R.id.pass);

    }

   public void login(View v){
       String id = user.getText().toString();
       String pwd = pass.getText().toString();
        if (!id.isEmpty() && !pwd.isEmpty()){
            Toast.makeText(getApplicationContext(),"username :"+id+"\n password:"+pwd,Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(),"enter user and pass.!",Toast.LENGTH_SHORT).show();
        }
   }

}
Step6:-Now run your app.
Without any input text.

With some input text


As above in MainActivity.java file we have created a method as login() and passed an argument View to implement the method in our design layout.

After that, we check both the EditText value is empty or not, for checking empty value in android we got a method isEmpty which will return a boolean value true or false.

If username or password is empty then the empty method will return true else it will return false.
For the else part we have toasted a message as "enter user and pass.!". If we got both text boxes values then we toast username and password as given above in MainActivity.java file.

So, as you can see, it was very simple to make a login app but for more functionality. we will can insert a value in the SQLite database and check the value from our local database also.

Thank you for your support, Subscribe to youtube channel  Android Buddy.

Tuesday, March 26, 2019

How to display image in android app.

How to display image in android app.

Images in android using ImageView.
Displaying images in android app is the most amazing thing to do. In this documentation, we are going to display some images using ImageView component.

Step1:- Create a new project and name it.
Step2:- Open up your XML layout file as in my case my main work is done in activity_main.xml.
Step3:- Copy and paste the below code as given.

<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="horizontal"    
tools:context=".MainActivity">

 <RelativeLayout    
    android:layout_width="wrap_content"    
    android:layout_height="wrap_content">
    <ImageView        
        android:id="@+id/image1"        
        android:layout_width="fill_parent"        
        android:src="@drawable/logo2"        
        android:layout_height="wrap_content" />

    <ImageView        
      android:id="@+id/img2"        
      android:layout_width="fill_parent"        
      android:layout_marginTop="5dp"        
      android:src="@drawable/logo6"        
      android:layout_below="@id/image1"        
      android:layout_height="wrap_content" />
 
 </RelativeLayout>
</LinearLayout>


Step4:- Open up your JAVA file as named MainActivity.java.
Step5:- Copy and paste the below code as given.


import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

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

        ImageView img1 = (ImageView)findViewById(R.id.image1);




    img1.setPadding(15,15,15,15);
    img1.setBackgroundColor(Color.DKGRAY);
    img1.setScaleType(ImageView.ScaleType.FIT_XY);


        ImageView img2 = (ImageView)findViewById(R.id.img2);

        img2.setPadding(15,15,15,15);
        img2.setBackgroundColor(Color.DKGRAY);
        img2.setScaleType(ImageView.ScaleType.FIT_XY);



    }
}

Step6:- Now run and test your app.

As you can see, we have used two images, one image resource is added in design mode were as another image resource is added programmatically.

Remember to have two image file in the drawable folder before adding.
In the above code in the MainActivity.java file, we have a method setPadding(), this method is used to provide some padding to the image as TOP, BOTTOM, LEFT and RIGHT.
Method setBackgroundColor() , provides a background color to the image.
Method setScaleType(),this method provide to scale your image as FIT_XY.

Thank you for your support if you have any doubt then give a comment below and also do not forget to subscribe to channel Android Buddy

Sunday, March 24, 2019

Making a custom toast message in android app

Custom toast message.



In this example, we are going to make some custom toast message while clicking a button.
Making a custom toast message is very simple and easy. just follow the below steps.

As we made a simple toast message earlier, we have used a toast class object and calling the method
makeText so this method is used to generate toast text messages. If we want to show up any custom view like any layout resource or an image toast, then we have to instantiate toast class for classing a method setView.

Step1:- Open up your MainActivity.java file, copy and paste the below code.

ImageView imageView;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

       imageView = new ImageView(this);
       imageView.setImageResource(R.drawable.images);

    Button mMsgBtn = findViewById(R.id.mbtn);
    mMsgBtn.setOnClickListener(new View.OnClickListener() {
        @Override        public void onClick(View v) {
            Toast toast = new Toast(getApplicationContext());
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.setView(imageView);
            toast.show();
        }
    });
}
Step2:- Open up your activity_main.xml layout file, copy and paste it.

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

    <Button        android:layout_gravity="center"        android:text="Show message"        android:id="@+id/mbtn"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />

</LinearLayout>

As you can see we made an ImageView object for showing an image toast message.ImageView or any view in android must have a constructor with argument context. So, we passed the current application context inside ImageView constructor.

For setting any image in ImageView there is a method called setImageResource.
You need to have an image inside your app > res > drawable folder. Just copy the image anywhere from your pc and paste inside this drawable folder. Remember to have an image name in lowercase because android studio only supports the files named in lowercase.

Toast object is using the setGravity method to set the toast position as the center you can change it as
Gravity.BOTTOM, Gravity.TOP or Gravity.LEFT and Gravity.RIGHT.

Android Studio | Toast Messages

Toast Message





How to build toast messages in android.

Firstly create a project named as MyToastApp or you can name as you want.
Next > Finish project.

After creating our project we have two main files named as:-
  1. MainActivity.java.
  2. activity_main.xml.
Now Open up activity_main.xml file, copy and paste this below code:-

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

    <Button android:layout_gravity="center" android:text="Show message" android:id="@+id/mbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" />

</LinearLayout>

Open up MainActivity.java file and change the code as givien below:-

public class MainActivity extends AppCompatActivity {

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


        Button mMsgBtn = findViewById(R.id.mbtn);

        mMsgBtn.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
             Toast.makeText(MainActivity.this, "I m Tony stark",Toast.LENGTH_LONG).show();
            }
        });
    }
}
Here, In android View is parent class of each component in android and Button is a subclass of the view parent class.

We have created a button class object as named mMsgBtn.

For click action, we need to set onClickListener to show a toast message.

mMsBtn.setOnClickListener(); //setting up the button listener.

Here, toast is class having its static methods (makeText()); and it has some of its method arguments like ( makeText(contex,"String message",Toast.Length_Short); ).
Context is the current application context on which message will be shown.

"String message" is some message what we wanted to display.

Toast.Length_Short and Toast.Length_Long is Time Duration how long we want to display the message on a screen, they both are the Toast class static variables.

Thank you for reading.. please subscribe to the channel or you can comment below :D.

Getting started with Android.


Getting started with Android.

Before moving ahead.Lets setup android studio.

  • Step:1:- Download the android setup from this Download.
  • Step:2:-Install SDK while the step is in progress and do not forget to have a backup of the SDK folder.
  • Step:3:- Now you are ready to create an android project.
  • Step:4:- Go to File > Create a project and named the project as you wish.
  • Step:5:- Remember while creating a project the Main work is done in MainActivity.java file and all design is done in activity_layout.xml, You can change the MainActivity as any name you want.
  • Step:6:- Congratulations, Finally you know how to setup android studio IDE.

Now, next, we are going to work with MainActivity.