Ads

Android Tutorial Day 3

Learning Objective:┬áHope you have succesfully run Hello World Application. Now Its time to understand it’s code structure and update code. Also we will make simple app with some code.


Question – 1 : How to find out Which Screen / Activity / Fragment is setup as Startup Screen?

Answer: First you need to check out your App > Manifests > AndroidManifest.xml file and look for Activity with <intent-filter> code and look for Launcher category options.

Check out that Activity Java Code > XML Code > You will find “nav_graph”

res > navigation > nav_graph.xml : here startup Fragment is setup


Question – 2: How to Add new Fragment?

Answer: Right Click on App > New > Fragment > Blank Fragment. It will create create 2 new files. 1 Java File and 1 XML File.


Question – 3: In Fragment Java file I want to add code to move from one fragment to another fragment, But where should I add my code?

Answer: In Fragment Java File you need to use OnCreateView method. Sample code is here:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	View view = inflater.inflate(R.layout.fragment_splash, container, false);

	final Handler handler = new Handler();
	handler.postDelayed(new Runnable() {
		@Override
		public void run() {

			NavHostFragment.findNavController(SplashFragment.this)
					.navigate(R.id.action_Splash_to_Login);
		}
	}, 5000);

	return view;
}

Now Let’s make one simple app with few screens and few layout options.

We need following:

SplashFragment

LoginFragment

Splash Fragment Layout

<?xml version="1.0" encoding="utf-8"?>
<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:background="#03A9F4"
    tools:context=".SplashFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:textColor="#FFFFFF"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Welcome to Arth Technology" />

</LinearLayout>

Splash Fragment Java Code

package com.example.myapplication;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import android.os.CountDownTimer;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SplashFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SplashFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public SplashFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SplashFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SplashFragment newInstance(String param1, String param2) {
        SplashFragment fragment = new SplashFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_splash, container, false);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                NavHostFragment.findNavController(SplashFragment.this)
                        .navigate(R.id.action_Splash_to_Login);
            }
        }, 5000);

        return view;
    }
}

Login Fragment Layout Code

<?xml version="1.0" encoding="utf-8"?>
<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:background="#48D6D5D1"
    tools:context=".LoginFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login" />

    <EditText
        android:id="@+id/etusername"
        android:hint="Username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/etpassword"
        android:hint="Password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_gravity="center_horizontal"
        android:id="@+id/btnlogin"
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tverrorlog"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="" />


</LinearLayout>

Login Fragment Java Code

package com.example.myapplication;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link LoginFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class LoginFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public LoginFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment LoginFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static LoginFragment newInstance(String param1, String param2) {
        LoginFragment fragment = new LoginFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_login, container, false);

        TextView tverrorlog = (TextView) view.findViewById(R.id.tverrorlog);
        EditText et1 = (EditText) view.findViewById(R.id.etusername);
        EditText et2 = (EditText) view.findViewById(R.id.etpassword);
        Button btn1 = (Button) view.findViewById(R.id.btnlogin);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(et1.getText().toString().equals("admin") && et2.getText().toString().equals("admin"))
                {
                    tverrorlog.setText("Success");
                }
                else
                {
                    tverrorlog.setText("Invalid Username or Password");
                }
            }
        });


        // Inflate the layout for this fragment
        return view;
    }
}
Ads