In any programming, A variable is “a named space in the memory” that we use to store some values. In other words, it acts a box or a container filled with some values in it. Variable names are also called identifiers, all dart data types Variables in Dart are bit same as that of javascript language.
Here are some Rules we need to follow while creating an identifier/dart variables.
A Variable should not be keywords
Variable can contain alphabet’s and numbers.
A Variable cannot contain any space or special character, except underscore( _ ) and Dollar symbol ( $ )
Variable name should not begin with a number.
Note: All the data types in dart are Objects. It means everything in Dart is objects and it inherits object properties. This means the default value of all these data types is null.
Syntax of Variable in dart
Dart makes use of the var keyword to declare the same. The syntax for declaring a dart variable is as given below:-
var name;
When we declare a variable without giving it any value, we essentially have created a dynamic type of variables, which means that variable can store any kind of values whether it may be an int or a string.
Eg: dart variable declared as a string
var Name = 'proto coders point';
Here is the explanation.
var is the Data Type
Name is the Variable name
Proto Coders Point is the Value stored in the variable
In the above-given example, we are declaring a variable name with the value ” proto coders point “ which means that this variable can only store Strings. when we set any value in single or double inverted commas it will automatically initialize itself a string, in other cases number can directly set to a variable. as shown below:-
void main() {
var number = 123;
print(number);
}
output
123
Dart Programming Language Different data-types variable.
Here is an example where are will implement all kinds of data types variables in dart programming language.
void main() {
var x = 'Maths pi'; // string variable
var y = 3.14; // int variable
print(x); // Maths pi
print(y); // 3.14
}
Output
Maths pi
3.14
Example 2: In Dart, we can store different kinds of data in the same variable and change it during run time. Here is an example of flutter math.pi
//this will work know more about this below
void main() {
var x ;
x = 'Math pi';
print(x); // Maths pi
x = 3.14;
print(x); // 3.14
}
Output
Math pi
3.14
This is valid Example because we are not initilizating any value to var x so we can easily change its data type at runtime.
Example 3
In this Example 3 we will try to mix both example 1 &2.
Now I am going to initialize the variable at the time if declaration and trying to store different data type in the same variable. I Now This will sure give me some error because this is not valid in Dart, as it is valid in javascript.
// This is invalid Example this will work
void main() {
var x = 'Math pi';
print(x); // Maths pi
x = 3.14; // Compilation failed error because
// you are trying to assign double to String variable.
print(x);
}
Here in above Example 3 we will get compilation Failed Error Because we are trying to assign double to String Variable which is not possible in Dart.
dynamic type keyword variable in dart
There is the dynamic keyword for variable declaration in Dart which is equivalent to JavaScript var keyword.
If we had used dynamic instead of var in Dart code we will not got any error because in dart programming language dynamic is same as Var in JavaScript.
dynamic name = 'My Name Is Proto'; // dynamic keyword
dynamic id = 101;
name= 123; // dynamic variable can also even store numbers
As in above lines of code we have declared a dynamic variable which is currently storing string values, but as code reach line num 3 it will change its datatype to integer.
if you need the flexibility of a dynamic language. The dynamic type itself is static, but can contain any date type at runtime. Of course, that removes many of the benefits of a type-safe language for that variable.
Example of Dynamic Variable in dart :
void main() {
dynamic n ='Its a Dynamic Value : String';
print(n);
n = 5;
print("its a Dynamic Value : Integer = $n ");
}
OutPut
Its a Dynamic Value : String
its a Dynamic Value : Integer = 5
Final and Const
The final and const keyword are used to declare constants. Here Dart Programming Language allow prevents modifying the values of a variable declared using the final or const keyword.
here final and const keyword variable will work into similar way.
Syntax of const keyword:
const variable_name = value; //if value is string it should have inverted commans
Syntax of final keyword:
final variable_name = value; // is value is string it should have inverted commas.
Here final or const variable in dart must be initialized at the time of declaration.
Example of final or const constants in dart programming language.
void main() {
final abc='xyz';
print("final data : "+abc);
const xyz=123;
print("const data : $xyz");
}
output
final variable : xyz
const variable : 123
let us check if final or const data can be changed or no
void main() {
final abc='xyz';
print("final variable : "+abc);
abc="abc"; // this will give me error becz it final which iss constant in nature
const xyz=123;
print("const variable : $xyz");
}
// above Code will give me compilation error because once a datatype is declared as final or const with cannot we changed during runtime.
All in one Dart Programming Language Data type Variables Example
void main() {
String name = 'Smith'; //string data type
print("Name : "+name);
int num = 10; //integer datatype
print(" Number : $num");
dynamic x = "tom"; //is initialized as string but later we can change it to double or integer
print("dynamic string : "+x); // dynamic print string
x=123; //dynamic datatype value string gets changed into integer
print("dynamic integer : $x");
final abc='xyz';
print("final constants"+abc);
const number=123;
print("constants number : $number");
}
Here name is normal string datatype , num is normal integer datatype , x is a dynamic datatype which can even change its datatype at runtime, final abc is a constants datatype string which cannot be changed at runtime.
In this Tutorial, we will implement recyclerview with cardview in android studio.
As it is usually recommended now to make use of simple ListView to show a large set of data sets. If you have a huge set of data sets that you want to show to UI, then you should use RecyclerView, Because RecyclerView is much more customizable than ListView and in terms of performance it is also very far ahead.
Here is a demo of how the Final Recyclerview with cardview UI looks like.
Before We start Implementing Recyclerview with CardView android we will know the basics.
Android RecyclerView
Android RecyclerView is one of the more advanced, powerful, and flexible versions of the ListView Android.
RecyclerView is very useful to set the Layout Managers dynamically at runtime, unlike the ListView That was able to show the list in only one format is the vertical list. RecyclerView allows us to set the following types of Layouts at runtime.
LinearLayoutManager: This helps users to set views in both vertical wells as horizontal listviews.
StaggeredLayoutManager: it supports staggered lists
Apps often need to display data in similarly styled containers. These containers are often used in lists to hold each item’s information. The system provides the CardView API is an easy way for you to show information inside cards that have a consistent look across the platform. CardView is mostly used for good-looking UI with RecyclerView.
CardView is usually an XML UI Design to show data within the list using the recycler view. Android CardView UI component shows information inside cards. This component is generally used to show contact information.
In above Android CardView cards_layout.xml code it holds a ImageView that displayed drawable resources along with two TextViews in a Nested Linear Layout.
The menu_main.xml contains a single item to add back the cards removed.
package protocoderspoint.com.cardviewrecycleviewandroid;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
private static ArrayList<DataModel> data;
static View.OnClickListener myOnClickListener;
private static ArrayList<Integer> removedItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myOnClickListener = new MyOnClickListener(this);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
data = new ArrayList<DataModel>();
for (int i = 0; i < MyData.nameArray.length; i++) {
data.add(new DataModel(
MyData.nameArray[i],
MyData.versionArray[i],
MyData.id_[i],
MyData.drawableArray[i]
));
}
removedItems = new ArrayList<Integer>();
adapter = new CustomAdapter(data);
recyclerView.setAdapter(adapter);
}
private static class MyOnClickListener implements View.OnClickListener {
private final Context context;
private MyOnClickListener(Context context) {
this.context = context;
}
@Override
public void onClick(View v) {
removeItem(v);
}
private void removeItem(View v) {
int selectedItemPosition = recyclerView.getChildPosition(v);
RecyclerView.ViewHolder viewHolder
= recyclerView.findViewHolderForPosition(selectedItemPosition);
TextView textViewName
= (TextView) viewHolder.itemView.findViewById(R.id.textViewName);
String selectedName = (String) textViewName.getText();
int selectedItemId = -1;
for (int i = 0; i < MyData.nameArray.length; i++) {
if (selectedName.equals(MyData.nameArray[i])) {
selectedItemId = MyData.id_[i];
}
}
removedItems.add(selectedItemId);
data.remove(selectedItemPosition);
adapter.notifyItemRemoved(selectedItemPosition);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getItemId() == R.id.add_item) {
//check if any items to add
if (removedItems.size() != 0) {
addRemovedItemToList();
} else {
Toast.makeText(this, "Nothing to add", Toast.LENGTH_SHORT).show();
}
}
return true;
}
private void addRemovedItemToList() {
int addItemAtListPosition = 3;
data.add(addItemAtListPosition, new DataModel(
MyData.nameArray[removedItems.get(0)],
MyData.versionArray[removedItems.get(0)],
MyData.id_[removedItems.get(0)],
MyData.drawableArray[removedItems.get(0)]
));
adapter.notifyItemInserted(addItemAtListPosition);
removedItems.remove(0);
}
}
In the above lines of code the removeItems() method is invoked from the listener method to remove the CardView with every card is been clicked. ts respective id is stored in an array to retrieve later.
The CustomeAdapter.javaclass is defined below :
package protocoderspoint.com.cardviewrecycleviewandroid;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private ArrayList<DataModel> dataSet;
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewName;
TextView textViewVersion;
ImageView imageViewIcon;
public MyViewHolder(View itemView) {
super(itemView);
this.textViewName = (TextView) itemView.findViewById(R.id.textViewName);
this.textViewVersion = (TextView) itemView.findViewById(R.id.textViewVersion);
this.imageViewIcon = (ImageView) itemView.findViewById(R.id.imageView);
}
}
public CustomAdapter(ArrayList<DataModel> data) {
this.dataSet = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cards_layout, parent, false);
view.setOnClickListener(MainActivity.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
TextView textViewName = holder.textViewName;
TextView textViewVersion = holder.textViewVersion;
ImageView imageView = holder.imageViewIcon;
textViewName.setText(dataSet.get(listPosition).getName());
textViewVersion.setText(dataSet.get(listPosition).getVersion());
imageView.setImageResource(dataSet.get(listPosition).getImage());
}
@Override
public int getItemCount() {
return dataSet.size();
}
}
In the CustomAdopter.java code, we have implemented our own ViewHolder by extending RecyclerView.ViewHolder. The view that inflate the cards_layout.xml.
An ArrayList stores all the data in the form of a DataModel class object in an ArrayList and adds them to the respective cards in a form of list.
Here i have made user of data model class that contains specific data for this application,
DataModel.java
package protocoderspoint.com.cardviewrecycleviewandroid;
public class DataModel {
String name;
String version;
int id_;
int image;
public DataModel(String name, String version, int id_, int image) {
this.name = name;
this.version = version;
this.id_ = id_;
this.image=image;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
public int getImage() {
return image;
}
public int getId() {
return id_;
}
}
In the above DataModel.java we have defined a list if variables that stored and fetch data using getters mehods.
In above code i m getting data like name, id, image; that help us in showing data in cardview.
JAVA is a programming language that is used in many software developments like Android App Development and much more. It is class-based and object-oriented programming whose syntax is influenced by C++. The primary goals of JAVA are to be simple, object-oriented, robust, secure, and high level. JAVA application runs on JVM (JAVA Virtual Machine).
Learn about variables and how they are created for storing information in memory. A variable is a container that holds the value while the java program is executed. In Java, there are 3 types of variables namely Local Variable, Static Variable, Instance Variable.
Data Types
Learn about Data Types (i.e. byte, int, char, etc) which is basically a type of information we want to store in the variable. Weather data is number, Character, or something else like boolean value.
String
The string is nothing but a character array for example “protocoderspoint” is a string of 16 characters as shown.
Keywords
Keywords in JAVA are a predefined list of keywords which has a specific meaning and cannot be used in the Java programming language as an identifier, such as the name of a variable, method, class, function, or label.
Operators
In computer, a programming Operator is a symbol that tells the compiler to perform specific action which can be mathematical or logical like addition, subtraction, multiplication, etc.
Class And Objects
The concept of class comes into the role when we see certain types of objects or things around us and the common idea or a blueprint behind this type of object is called Class. Object is an instance of the class.
Method
A method is a self-defined block of code that performs a specific task.
Picasso android library is – A powerful image loading, image downloading, and image caching library for Android.
This Picasso library allows for hassle-free image loading in your application—often in one line of code!
Picasso dependency is an open-source Android library that is developed and maintained by Square. This library hides all the complexity as it will deal with back-on threading, loading images from the internet, and even caching. Picasso makes memory-efficient to resize and transform images. Picasso Library makes it quite easier for displaying images from remote locations.
Final Output of this Picasso android tutorial.
1. Feature of this Universal image loader library
Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
.fetch() –this is an asynchronously load the image into a background thread. This method only saves the image into the disk or memory cache. It’s neither going to load the image into image view nor it is going to return any Bitmap. If you want to reduce the reduce image loading times and if you know that the image is needed by you shortly after then it could be used to fill the image cache in the background.
.get() – It returns a Bitmap object and loads an image synchronously. But it will freeze the UI if you will call it from the UI thread.
into(ImageView targetImageView) – It is provided by Picasso where you can Specify your target Imageview in which the image is supposed to get displayed.
In this tutorial, you will learn How to use Picasso android library to load images from the locally drawable drive and even how to load images from a URL.
How to add Picasso library in android studio Picasso dependency?
Navigate to your project file at the left side of your android studio. You will see “Gradle Scripts” open the drop down list, you will see a build.gradle (Module:app) open it.
Before using Picasso we have to add its dependency in build.gradle file in module level : app.
Picasso supports both download and error placeholders as optional features.
We can specify an image as a place holder until the image is being loaded, placeholder are commenly used with we face any technical issue like network failed to load the picasso image, in such a case we make use of placeholder in picasso so that we can show a default image in place of real image.We can also give an image as an error handler if any error occurs while loading the image.
Now let us begin by creating a project in android studio to implement Picasso universal image loader library.
Picasso Android Example
In the Example, i m loading an image from local and from URL when button is clicked OK.
Create a project with and of your wish package name i m using “protocoderspoint.com.picassoandroidimageloader” and add following code in respective files
Welcome To Proto Coders Point, In this tutorial, we will learn how to create a profile page UI Design app using Dart and Flutter. A quick sample app on how to create a simple profile screen page in flutter.
The Final UI of Flutter profile page will look something like below screenshot.
Video Tutorial play here
Open android Studio and create a new flutter project.
File > New > New Flutter Project
Give a name you your flutter project and set a path to store you flutter project.
Now you will a see a default counter code generated, that simply counts the number of times the button is pressed and display counting on the screeen.
You can just remove default code of flutter and shown in below Image.
So let’s begin with the actual code for Flutter profile page example UI Design.
before we start implement, we need the resources such as images and fonts to be set/copy in our project directory, For that we need to create 2 directory.
Directory
images : to store all our project image resources.
fonts : to store all our fonts style ttf extension files.
Here is how to create directory in android-studio ( flutter )
Google Flutter is one of a open source user interface(UI) Software Development Toolkit that is been created/ developed by Google, for Software developer which makes programmer to design UI natively compiled applications for mobile, web, and desktop from a single codebase (Google Flutter).
The First version of Flutter Application was named as “SKY”,That was smoothly running on any of the android operating system. Fluttter was released on 2015 by a developer “Summit”. Google announced Flutter Release Preview 2 which is the last big release before Flutter 1.0.
After somedays of testing the final version was released on December 4th of 2018 as flutter 1.0 at it live event, which was he first “stable” version of the Framework.
Which Language Google Flutter prefers to use?
Google Flutter are written in the Dart programming language and make use of many of the language’s more advanced features.
Flutter framework for crafting high-quality native interfaces on iOS and Android in record time. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source. Watch this Video
Widgets in Flutter UI
UI design in Flutter development involves create “Widgets” from other Widgets. Flutter Application is basically a groups of widgets build together to form a beautiful UI/ UX experience.
A widget is an immutable description of part of a user interface.” A human being will tell you it’s a Blueprint, which is a much easier way to think about it.
The MyApp Widget contains all the other Widgets, which can contain even smaller Widgets, and together they make up your app.
Flutter is very much fast in development, With Statefull Flutter’s hot reload feature helps you quickly and easily experiment, build UIs, add features, and fix bugs within a seconds.
Expressive and Flexiable UI
Quickly ship features with a focus on native end-user experiences. Layered architecture allows for full customization, which results in incredibly fast rendering and expressive and flexible designs.[1]
Native Performation
Flutter’s widgets incorporate all critical platform differences such as scrolling, navigation, icons and fonts to provide full native performance on both iOS and Android. Here are some examples of application that are build using flutter
How to install flutter plugin in android studio?
Here i have demonstrated installation process in ubuntu android-studio same applied to Windows OS.