Skip to main content

Posts

Showing posts from July, 2018

Permissions

To maintain security for the system and users, Android requires apps to request permission before the apps can use certain system data and features. Depending on how sensitive the area is, the system may grant the permission automatically, or it may ask the user to approve the request. if(Build.VERSION.SDK_VERSION>=23) { checkselfPermission(Manifest.Permission.READ_External_Storage) !=PackageManager.PERMISSION_GRANTED} {String[]S={Manifest.permission.READ_EXTErNAL_STORAGE}; else{ } }else onRequestPermissionResult(int ReqCode,String[]perm,int[]res){ if(res.length>0) { if(res[0]==package.Manager>permission_granted); } proceed()}; else{ }

Rating Bar

A ndroid RatingBar  can be used to get the rating from the user. The Rating returns a floating-point number. It may be 2.0, 3.5, 4.0 etc. Android RatingBar displays the rating in stars. Android RatingBar is the subclass of AbsSeekBar class. The  getRating()  method of android RatingBar class returns the rating number.

Custom listview

After creating simple ListView, android also provides facilities to customize our ListView. As the simple ListView, custom ListView also uses Adapter classes which added the content from data source (such as string array, array, database etc). Adapter bridges data between an AdapterViews and other Views MainActivity.java package com.example.basleenkaur.lvcustom; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView l1 ; ArrayList alplaces ; ArrayList<Integer> image ; second1 ad ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. activity_main ); l1 =(ListView) findViewById(R.id. listview ); alplaces = new ArrayList(); image = new ArrayList(); alplaces .add( "Mysore" ); //a...

Adapter

Adapter An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.The AdapterView really brings together the data and the layout (potentially in a complex collection of views) for each of the rows that make up the AdapterView. At a minimum, you will need to implement four methods.  These four methods are called by Android to build your AdapterView and to return the correct information when one of the items in the AdapterView is selected. getCount( ):  indicates to Android how many items (or rows) are in the data set that will be presented in the AdapterView. getItem(int pos):  get the data item associated with the item (or row) from the AdapterView passed as a parameter to the method.  This method will be used by Android to fetch the appropriate data to build the item/row in the AdapterView. ...

Imageview

An ImageView is simply a view you embed within an XML layout that is used to display an image (or any drawable) on the screen. The ImageView looks like this in res/layout/activity_main.xml: <ImageView android:id=”@+id/image” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:scaleType=”center” android:src=”@drawable/my_image” /> The ImageView handles all the loading and scaling of the image for you. The scaleType attribute which defines how the images will be scaled to fit in your layout.

Spinners

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the  Spinner  object. You should usually do so in your XML layout with a  <Spinner>  element. For example: <Spinner     android:id = "@+id/planets_spinner"     android:layout_width = "fill_parent"     android:layout_height = "wrap_content" /> To populate the spinner with a list of choices, you then need to specify a  SpinnerAdapter  in your  Activity  or  Fragment  source code. Key classes are the following: Spinner SpinnerAdapter AdapterView.OnItemSelectedListener

Intents

Android application components can connect to other Android applications. This connection is based on a task description represented by an Intent object.Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining the components you are targeting. For example, via the startactivity() method you can define that the intent should be used to start an activity. An intent can contain data via a Bundle. This data can be used by the receiving component. Intent i = new Intent(MainActivity.this , ActivityTwo.class); i.putExtra(“Value1″ , ” “); i.putExtra(“Value2″ , ” “); Listview ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that’s placed into the list. An adapter actually bridges between UI components and the data source ...

Android Activity Lifecycle

Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper class. An activity is the single screen in android. It is like window or frame of Java. By the help of activity, you can place all your UI components or widgets in a single screen. Android Activity Lifecycle methods Let’s see the 7 lifecycle methods of android activity. Method Description onCreate called when activity is first created. onStart called when activity is becoming visible to the user. onResume called when activity will start interacting with the user. onPause called when activity is not visible to the user. onStop called when activity is no longer visible to the user. onRestart called after your activity is stopped, prior to start. onDestroy called before the activity is destroyed.

Java Iterator

In Java, Iterator is an interface available in Collection framework in java.util package. It is a Java Cursor used to iterate a collection of objects. It is used to traverse a collection object elements one by one. It is available since Java 1.2 Collection Framework. It is applicable for all Collection classes. So it is also known as Universal Java Cursor. It supports both READ and REMOVE Operations. Compare to Enumeration interface, Iterator method names are simple and easy to use. Java Iterator Methods In this section, we will discuss about Java Iterator methods in-brief. We will explore these methods in-depth with some useful examples in the coming section. boolean hasNext():Returns true if the iteration has more elements. E next(): Returns the next element in the iteration. default void remove(): Removes from the underlying collection the last element returned by this iterator. default void forEachRemaining(Consumer action): Performs the given action for each...

Stack Class in Java

Java provides an inbuilt object type called  Stack . It is a collection that is based on the last in first out (LIFO) principle. On Creation, a stack is empty. It extends  Vector  class with five methods that allow a vector to be treated as a stack. The five methods are: 1.  Object push(Object element)  : Pushes an element on the top of the stack. 2.  Object pop()  : Removes and returns the top element of the stack. An ‘EmptyStackException’ exception is thrown if we call pop() when the invoking stack is empty. 3.  Object peek( )  : Returns the element on the top of the stack, but does not remove it. 4.  boolean empty()  : It returns true if nothing is on the top of the stack. Else, returns false. 5.  int search(Object element)  : It determines whether an object exists in the stack. If the element is found, it returns the position of the element from the top of the stack. Else, it returns -1.

Iterator interface

Iterator interface provides the facility of iterating the elements in forward direction only. Methods of Iterator interface There are only three methods in the Iterator interface. They are: No. Method Description 1 public boolean hasNext() It returns true if iterator has more elements. 2 public Object next() It returns the element and moves the cursor pointer to the next element. 3 public void remove() It removes the last elements returned by the iterator. It is rarely used. Java LinkedList class Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces. The important points about Java LinkedList are: Java LinkedList class can contain duplicate elements. Java LinkedList class maintains insertion order. Java LinkedList class is non synchronized. In Java LinkedList class, manipulation is fast because no shifting needs to be occurr...

Methods of Collection interface

There are many methods declared in the Collection interface. They are as follows: No. Method Description 1 public boolean add(Object element) is used to insert an element in this collection. 2 public boolean addAll(Collection c) is used to insert the specified collection elements in the invoking collection. 3 public boolean remove(Object element) is used to delete an element from this collection. 4 public boolean removeAll(Collection c) is used to delete all the elements of specified collection from the invoking collection. 5 public boolean retainAll(Collection c) is used to delete all the elements of invoking collection except the specified collection. 6 public int size() return the total number of elements in the collection. 7 public void clear() removes the total no of element from the collection. 8 public boolean contains(Object element) is used to search an element. 9 public boolean containsAll(Collection c) is used to search the specified collection in this collect...

Collections

Collections in java  is a framework that provides an architecture to store and manipulate the group of objects. All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections. Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc). What is framework in java provides readymade architecture. represents set of classes and interface. is optional. What is Collection framework Collection framework represents a unified architecture for storing and manipulating group of objects. It has: Interfaces and its implementations i.e. classes Algorithm Hierarchy of Collection Framework Let us see the hierarchy of collection framework.The  java.util  package contains all the classes and ...

ArrayList

ArrayList is a dynamic data structure in which you can add or remove any number of elements and those elements are stored in ordered sequence. It may also contain duplicate values. ArrayList are the implementations of  List  interface. The package you required to import the ArrayList is  import java.util.ArrayList; Syntax of ArrayList: Its very easy to use ArrayList, below is the  syntax  to declare it: ArrayList arrayListDemo = new ArrayListDemo(); String ArrayList Declaration: ArrayList<String> arrayListDemo = new ArrayListDemo<String>(); By using this syntax, an ArrayList is created which you can use to store characters. Here arrayListDemo is the name of this particular ArrayList. ArrayList Methods: ArrayList is a subclass of  AbstractList  class and it implements  List  Interface.  It has various methods that are defined and inherited from its parent class. Below is the list of those methods with...

Core Java Basic Concepts

Prefix Increment and Decrement Operator In prefix form, the operand is incremented or decremented before the value is received for use in the expression. Postfix Increment and Decrement Operator In postfix form, the previous value is obtained for the use in expression, and then the operand is modified. Example Here in the examples, there is no difference between the prefix and the postfix forms. However, when the increment and/or decrement operators are the part of a larger expression, then a subtle, however powerful, difference between these two forms appears. Here is an example: x = 42; y = ++x; Here, y is set to 43 as you would expect, as the increment occurs before x is assigned to y. Therefore, the line y = ++x; is equivalent of these two statements: x = x + 1; y = x;

Android Directory Structure

src  - Java source files associated with your project. This includes the Activity "controller" files as well as your models and helpers. res  - Resource files associated with your project. All graphics, strings, layouts, and other resource files are stored in the resource file hierarchy under the res directory. res/layout  - XML layout files that describe the views and layouts for each activity and for partial views such as list items. res/values  - XML files which store various attribute values. These include  strings.xml , dimens.xml,  styles.xml , colors.xml,  themes.xml , and so on. res/drawable  - Here we store the various density-independent graphic assets used in our application. The most frequently edited files are: res/layout/activity_foo.xml  - This file describes the layout of the activity's UI. This means the placement of every view object on one app screen. src/.../FooActivity.java  - The Activity "controller" t...