Wednesday 2 November 2022

Android XML

 

XML in Android: Basics And Different XML Files Used 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:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">


 <Button
 android:id="@+id/buton1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Button"/>

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="sample Text"
 android:layout_marginTop="15dp"
 android:textSize="30dp"/>

 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <EditText
 android:id="@+id/editTextName"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:hint="First Name"
 />

 <EditText
 android:id="@+id/editTextLastName"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:hint="Last Name"/>

 </RelativeLayout>
</LinearLayout>



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.

<!--  RelativeLayout in which we set green color for the background -->
<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:background="@color/greenColor"
tools:context=".MainActivity">

<TextView
android:id="@+id/firstTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:padding="10dp"
android:text="First Text View"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
<!-- second TextView -->
<TextView
android:id="@+id/secondTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/firstTextView"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:padding="10dp"
android:text="Second Text View"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />

</RelativeLayout>

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.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.abhiandroid.MyApplication">     <!-- application package name -->

<uses-permission android:name="ANDROID.PERMISSION.INTERNET" />
<!-- define Internet Permission -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<!-- add your Activities, Receivers, Services Names Here -->
<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>
</activity>
</application>

</manifest>

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.

<resources>
<string name="app_name">My Application</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="login">User Login</string>
<!-- define your strings here -->
</resources>

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.

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>

</resources>

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.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- define start, center and end color for gradient -->
<gradient
android:centerColor="#0f0"
android:endColor="#00f"
android:startColor="#f00" />
</shape>

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.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- define your colors Here -->
<color name="greenColor">#0f0</color>
<color name="white">#fff</color>
</resources>

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.

<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen><dimen name="btnheight">50dp</dimen>
</resources>


Android Programming with Eclipse IDE

Prerequisites For JAVA:

Eclipse:

Being a JAVA programmer you will need some tools or software to code and run it. Lots of tools are available over the web but we recommend you to use Eclipse for learning JAVA since it is the most common tool used for Android Development alongwith Android Studio. So getting habitual with Eclipse and its shortcuts will be bonus in your Android journey. Other then Eclipse you can also prefer IntelliJ or netbeans for learning JAVA.

Other Tools: IntelliJ and Netbeans

Android Studio? Are you thinking of using Android Studio for learning JAVA? Then Android Studio is solely meant for Android programming. IntelliJ, Eclipse and Netbeans are for real Java codings.

Android Studio is the official IDE (integrated development environment) for developing Android Apps by Google. It is based on  JetBrains’ IntelliJ IDEA software and has lots of amazing features which helps developer in creating Android App.

Android Studio is available for free download on Windows, Mac OS X and Linux.

Here we are going to share lots of guide, tips, tricks and shortcut of Android Studio to help developer easily understand the tool.

Eclipse Vs Android Studio

Android App Development is mostly done in two IDE i.e. Eclipse and Android Studio. Earlier Eclipse was the popular IDE but now Android Studio has taken over it. This is because Google has ended the support for Eclipse and now only focused on Android Studio. Google also recommended developer to import their Android projects and use Android Studio.

Prerequisites For Learning Android Studio:

There are two prerequisites for learning Android Studio tips:

System Requirement – First your system OS must be either Windows, Max OS X or Linux with below requirement:

  • Microsoft Windows 10/8.1/8/7/Vista/2003/XP (32 or 64 bit)
  • Mac OS X 10.8.5 or higher, up to 10.10 to up 10.10.2 up 10.10.3 on 10.10.5 (Yosemite)
  • GNOME or KDE or Unity desktop on Ubuntu or Fedora or GNU/Linux Debian
  • Minimum RAM: 2GB
  • Recommended RAM: 4GB
  • Disk Space: 500 MB disk space
  • Android SDK Space Reqirement: At least 1 GB for Android SDK, emulator system images, and caches
  • JDK: Java Development Kit (JDK) 7 or higher
  • Screen Resolution: 1280×800 minimum screen resolution
  • Prefer faster processor according to your budget

Hello World program in Java using Eclipse IDE in in

 As we all know Windows and Ubuntu are different Operating System. For installing in Windows just we need to click on Eclipse setup package and it will start installation but for Ubuntu it's based on commands.

This post describes how to create a "Hello World" java program using Eclipse IDE. This program will print "Hello World" in the console.

Technologies used in this article :

  1. JDK 1.6
  2. Eclipse 3.7
If JDK or Java is not installed in your computer, then follow the below step by step guidelines


If Eclipse is not installed in your computer, then follow the below step by step guidelines


1. Create Java Project

Select from the menu File --> New --> Java Project.


Enter "HelloWorld" as the project name. Keep rest of the settings as it is as shown in the following screenshot.


Click "Finish" button and Eclipse IDE will generate the java project automatically.

2. Create Java Package

Right click on 'src' folder and select from context menu New --> Package.


Type 'com.srccodes.example' in the 'Name' field and click "Finish" button.


3. Create Java Class

Right click on 'com.srccodes.example' package and select from context menu New --> Class.



Write "HelloWorld" in the 'Name' field and select the check-box for 'public static void main(String[] args)'.

Click "Finish" button. Eclipse will generate a java class and open the same in the java editor as shown below.


4. Write Java Code

Edit the generated 'HelloWorld' java class as per the following code.

File: HelloWorld.java

package com.srccodes.example;

public class HelloWorld {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("Hello World");
    }

}

5. Run Your Code

Right click on 'HelloWorld.java' and select from context menu 'Run As' --> 'Java Application'.



6. Console Output

Your code will print 'Hello World' in the eclipse console.





Wednesday 23 February 2022

Installing and Running Lex or Flex Program in Ubuntu

FLEX (Fast Lexical Analyzer Generator)

This post will show you how to run Lex and Yacc programs easily on Ubuntu 12.04 18.04 and 21.04

Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lex in the C programming language. l, or if the file is named -, lex reads the description from standard input (standard input). It produces a set of tables that, together with additional prototype code from /etc/yylex. c, constitute a lexical analyzer to scan those expressions.
 
I have assumed that you have a working Ubuntu 12.04 LTS version installed on your system and the computer is connected to the network i.e, internet.

Ubuntu does not come installed with a lex and yacc compiler to do so install it first by
1. Opening the terminal- Short cut Ctrl + Alt + T here T is Terminal
2. sudo apt-get install flex
3. enter your ubuntu password

After installation of flex is completed then

4. sudo apt-get install bison
5. Enter your password.

Successfully  Lex and Yacc have been installed on your system.

Running a Lex and Yacc program        
        
1. Write the lex program in a file and save it as file.l (where file is the name of the file).
2. Open the terminal and navigate to the directory where you have saved the file.l  (e.x cd Desktop)
3. Type  lex filename.l
4. Then type  cc lex.yy.c -lfl
5. Finally type  ./a.out
 
Your lex progam will be running now (comment should be correct).
or compiling lex and yacc together
 
1. write lex program in a file file.l and yacc in a file file.y
2. open the terminal and navigate to the directory where you have saved the files.
3. Type lex filename.l
4. Type yacc filename.y
5. Type cc lex.yy.c y.tab.h -lfl

6. Finally Type ./a.out

 
 
Simple Program
%%

"+" {printf("PLUS ");}
%%
int main(int argc, int argv)
{
yylex();
}

 
 
Step 1: Open Terminal Type cd Desktop
Start Program by typing gedit filename.l here it's gedit xyz.l
Write the program 
 
Step 2: For compiling use flex filename.l here l is lex code its is understood by the machine-->compiler
Here it's $flex xyz.l 
 
Step 3: After compiling  use this $cc lex.yy.c -lfl 
Here cc is C Compiler, lex.yy is The lex command stores the yylex function in a file named lex. yy. c. You can use the yylex function alone to recognize simple one-word input, or you can use it with other C language programs to perform more difficult input analysis functions.
  1. Use the lex program to change the specification file into a C language program. The resulting program is in the lex.yy.c file.
  2. Use the cc command with the -ll flag to compile and link the program with a library of lex subroutines. The resulting executable program is in the a.out file.
  3. lfl: First, FLEX reads a specification of a scanner either from an input file *.lex, or from standard input, and it generates as output a C source file lex.yy.c. Then, lex.yy.c is compiled and linked with the "-lfl" library to produce an executable a.out. Finally, a.out analyzes its input stream and transforms it into a sequence of tokens.
  4. Background Process:   
  1. After it is compiled through flex filename.l internally it generates a file called lex.yy.x the after we type $ cc lex.yy.c -lfl it generates a.out.