Wednesday, 10 October 2018

XML in Android


XML stands for Extensible Markup Language. XML is a markup language much like HTML used to describe data.  XML tags are not predefined in XML. We must define our own Tags. Xml as itself is well readable both by human and machine. Also, it is scalable and simple to develop. In Android we use xml for designing our layouts because xml is lightweight language so it doesn’t make our layout heavy.
In this article we will go through the basic concepts of xml in Android and different XML files used for different purpose in Android. This will help you in writing a UI code to design your desired user interface.
Basics Of User Interface:
The whole concept of Android User Interface is defined using the hierarchy of View and ViewGroup objects. A ViewGroup is an invisible container that organizes child views. These child views are other widgets which are used to make the different parts of UI. One ViewGroup can have another ViewGroup as an child element as shown in the figure given below:
Here in above Diagram ViewGroup (Linear Layout) contains one ViewGroup (i.e. Relative Layout)and two View(Button and TextView). Further two more View (i.e. 2 EditText ) are nested inside Relative Layout ViewGroup. It is important to note that one layout can be nested in another layout.
The below code snippet will explain the above image in better way. Paste it in activity_main.xml:
Every Android application screen has some components like button, Text or images. These are contained inside the ViewGroup. Layouts are the best examples for ViewGroups. The different types of layout in android are Linear Layout, Relative Layout, Absolute Layout, Table Layout and Frame Layout.
Different XML Files Used in Android:
In Android there are several xml files used for several different purposes. Below we define each and every one.
1. Layout XML Files: Layout xml files are used to define the actual UI(User interface) of our application. It holds all the elements(views) or the tools that we want to use in our application. Like the TextView’s, Button’s and other UI elements.
Location in Android Studio:
You will find out this file inside the res folder and inside it there is another folder named layout where you will get all the layout files for their respective activities or fragments.
Basic Layout XML Code:
Below we show activity_main.xml file in which we have two TextView’s.
2. Manifest xml File(Mainfest.xml): This xml is used to define all the components of our application. It includes the names of our application packages, our Activities, receivers, services  and the permissions that our application needs. For Example – Suppose we need to use internet in our app then we need to define Internet permission in this file.
Location in Android Studio:
It is located inside app > manifests folder
Defining Internet Permission in AndroidManifest.xml
Below we show the AndroidManifest.xml file and define the Internet Permission in that file.
3. Strings xml File(strings.xml): This xml file is used to replace the Hard-coded strings with a single string. We define all the strings in this xml file and then access them in our app(Activity or in  Layout XML files) from this file. This file enhance the reusability of the code.
Location in Android Studio:
 Below we show strings.xml file and define a string in the file.

4. Styles xml File(styles.xml): This xml is used to define different styles and looks for the UI(User Interface) of application. We define our custom themes and styles in this file.
Location in Android Studio:
Below we show the style.xml file.
 5. Drawable xml Files: These are those xml files that are used to provide various graphics to the elements or views of application. When we need to create a custom UI we use drawable xml files. Suppose if we need to define a gradient color in the background of Button or any custom shape for a view then we create a Drawable xml file and set it in the background of View.
Below we show custom_drawable.xml file and create a gradient background color using style attribute.
6. Color xml File (colors.xml): This file is used to define the color codes that we used in our app. We simply define the color’s in this file and used them in our app from this file.
Location in Android Studio
Below we show the colors.xml file in which we define green and white color.
 7. Dimension xml File(dimens.xml): This xml file is used to define the dimensions of the View’s. Suppose we need a Button with 50dp(density pixel) height then we define the value 50dp in dimens.xml file and then use it in our app from this file.
Location in Android Studio:
Below we show the dimens.xml file in which we define 50dp  dimension for Button height.

Data Management : Repeats in R Languages

Repeats
  • Command rep is used to replicates the values in a vector.
  • Syntax rep (x) replicates the values in a vector x.
  • rep (x, times=n )  # Repeat x as a whole n times
  • rep (x, each=n )  # Repeat each cell n times
Help for the command rep

> help  ("rep")



rep { base }

      Replicate Element of Vectors and Lists

Description

rep replicates the values in x. It is generic function, and the (internal) default method is described here.
rep.int and rep-len are faster simplified versions for two common cases.
They are not generic.

Usage

rep (x, ......)
rep.int (x,  times)
rep_len (x, length.out)

The command rep

Repeat an object n-times:

> rep (3.5,  times = 10)

[1]     3.5    3.5    3.5   3.5    3.5    3.5   3.5    3.5    3.5     3.5

> rep ( 1 :4,  2)
   [1]   1   2    3    4     1    2    3    4




Repeat an object n-times

rep (x, times  = n)

Repeat each cell n-times:

rep (x, each = n)

> x <- 1 : 3
> x
 [1]   1  2  3

>  rep (x,  times = 3)
  [1]  1   2   3  1    2   3   1   2   3

> rep (x, each = 3 )
   [1]  1    1   1    2    2   2   3     3    3

Every object is repeated several times successively:

> rep (1 : 4,   each  =  2)
   [1]   1    1   2   2   3   3   4   4

> rep ( 1 : 4,  each = 2 ,  times = 3)
 [1]   1  1   2   2    3    3   4   4   1   1   2   2    3   3   4   4   1   1   2   2    3    3   4    4


Every object is repeated a different number of times:

> rep  (1 : 4,  2 :  5)
  [1]   1   1   2   2    2    3    3   3   3   4    4    4   4    4




Monday, 8 October 2018

Tic Tac Toe Game Coding

 
 Activity_main.xml File
  
<?xml version="1.0" encoding="utf-8"?> 
<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" 
xmlns:ads="http://schemas.android.com/apk/res-auto"     
android:layout_width="match_parent"     
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".MainActivity">


   <RelativeLayout         
android:layout_width="match_parent"         
android:layout_height="wrap_content"
 
<TextView             
android:id ="@+id/text_view_p1" 
 android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:text = "Player 1:  0" 
 android:textSize="30sp" 
 android:textColor="#FF00FF"            />

        <TextView             
android:id ="@+id/text_view_p2"             
android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:layout_below="@+id/text_view_p1"             
android:text = "Player 2:  0" 
 android:textSize="30sp" 
 android:textColor="#FF00FF"            />

        <Button 
 android:id="@+id/button_reset" 
 android:layout_width="wrap_content"             
android:layout_height="wrap_content" 
 android:layout_alignParentEnd="true" 
 android:layout_centerVertical="true" 
 android:layout_marginEnd="33dp" 
 android:text="reset" 
 android:textColor="#DC143C"            />

    </RelativeLayout>

<LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="0dp"     
android:layout_weight="1"    >

    <Button        android:id="@+id/button_00" 
 android:layout_width="0dp"         
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"        />
    <Button         
android:id="@+id/button_01"         
android:layout_width="0dp"         
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"        />
    <Button 
 android:id="@+id/button_02" 
 android:layout_width="0dp" 
 android:layout_height="match_parent"         
android:layout_weight="1" 
 android:textSize="60sp"        />
</LinearLayout>

    <LinearLayout         
android:layout_width="match_parent" 
 android:layout_height="0dp"         
android:layout_weight="1"        >

        <Button 
 android:id="@+id/button_10" 
 android:layout_width="0dp"             
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button 
 android:id="@+id/button_11" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button 
 android:id="@+id/button_12" 
 android:layout_width="0dp"             
android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
    </LinearLayout>
    <LinearLayout 
 android:layout_width="match_parent"         
android:layout_height="0dp" 
 android:layout_weight="1"        >

        <Button             
android:id="@+id/button_20" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button 
 android:id="@+id/button_21" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
        <Button             
android:id="@+id/button_22" 
 android:layout_width="0dp" 
 android:layout_height="match_parent" 
 android:layout_weight="1" 
 android:textSize="60sp"            />
    </LinearLayout>
 
 <com.google.android.gms.ads.AdView         
android:id="@+id/adView" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_centerHorizontal="true" 
 android:layout_alignParentBottom="true" 
 ads:adSize="BANNER"         
ads:adUnitId="ca-app-pub-5864813702378205/9734846806">
    </com.google.android.gms.ads.AdView>

</LinearLayout




MainActivity.Java File

package com.example.irawen.tictactoe;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.widget.Button;
import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private AdView mAdView;

    private Button[][] buttons = new Button[3][3];
    private boolean player1turn = true;
    private int roundCount;
    private int player1Points;
    private int player2Points;



    private TextView textViewPlayer1;
    private TextView textViewPlayer2;


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

        textViewPlayer1 = findViewById(R.id.text_view_p1);
        textViewPlayer2 = findViewById(R.id.text_view_p2);

 for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
       String buttonID = "button_" + i + j;
        int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
         buttons[i][j] = findViewById(resID);
          buttons[i][j].setOnClickListener(this);

            }
        }
        Button buttonReset = findViewById(R.id.button_reset);
        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                resetGame();

            }
        });

            mAdView = findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
    }

    @Override    public void onClick(View v) {
        if (!((Button) v).getText().toString().equals("")) {
            return;
        }
        if (player1turn) {
            ((Button) v).setText("X");
        } else {
            ((Button) v).setText("O");
        }
        roundCount++;
         if (checkForWin()){
             if (player1turn){
                 player1Wins();
             }else{
                 player2Wins();
             }
         }else if(roundCount == 9){
             draw();

         }else{
             player1turn = !player1turn;
         }
    }

    private boolean checkForWin() {
        String[][] field = new String[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                field[i][j] = buttons[i][j].getText().toString();
            }
        }
        for (int i = 0; i < 3; i++) {
            if ((field[i][0].equals(field[i][1])
                    && field[i][0].equals(field[i][2])
                    && ! field[i][0].equals(""))){
                return true;
            }
        }
        for (int i = 0; i < 3; i++) {
            if ((field[0][i].equals(field[1][i])
                    && field[0][i].equals(field[2][i])
                    && ! field[0][i].equals(""))){
                return true;
            }
        }
        if ((field[0][0].equals(field[1][1])
                && field[0][0].equals(field[2][2])
                && ! field[0][0].equals(""))){
            return true;
        }
        if ((field[0][2].equals(field[1][1])
                && field[0][2].equals(field[2][0])
                && ! field[0][2].equals(""))){
            return true;
        }
        return false;
    }
    private void player1Wins() {
        player1Points++;
        Toast.makeText(this,  "Player 1 wins!",Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }


    private void player2Wins() {
        player2Points++;
        Toast.makeText(this,  "Player 2 wins!",Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }

    private void draw() {
        Toast.makeText(this, "Draw!",Toast.LENGTH_SHORT).show();
        resetBoard();
    }
    private void updatePointsText(){
        textViewPlayer1.setText("Player 1: " +player1Points);
        textViewPlayer2.setText("Player 2: " +player2Points);
    }
    private void resetBoard() {
        for ( int i = 0; i < 3; i++){
            for ( int j = 0; j < 3; j++){
                buttons[i][j].setText("");
            }
        }
        roundCount = 0;
        player1turn = true;

    }
    private void resetGame(){
        player1Points = 0;
        player2Points = 0;
        updatePointsText();
        resetBoard();
    }
}
 
Preview
 


Popular Posts

Categories

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (353) Python Quiz (22) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses