Tuesday, 20 August 2013

App Wigets|Android

Hello Friends,

I am writing here something on Widgets in android. Please make sure that you have basic knowledge of widget before going through this post. I have created this post to make you clear about widget concept and how to implement in android through easy steps. Hope you will like it.

App Widgets
Widgets are nothing but views.

Ex. Text View, Button, Check Box, etc.
Similarly App Widgets are small views of application. They are put into other application. They run in another app i.e. widgets run in another process. This other applications are known as App Widget Host.  Ex, Home Screen.

Note: - You can create your own App Widget Host.

For creating App Widgets, You need 3 things.
1) XML LAYOUT
2) METADATA FILE
3) BROADCAST RECEIVER

XML LAYOUT Resource  :-  Initial Layout of App Widget. It is XML file.  It uses remote views. Remote views are used to define & update view hierarchy in another app process.

METADATA (AppWidgetProvider Info) :-   Metadata of widget.
Ex, what is Widget’s layout? , What is update frequency of widget, Which is App Widget Provider Class (widget’s broadcast receiver).
Metadata is defined in XML.

BROADCAST RECEIVER (AppWidgetProvider Class) :-  It is class that has basic method that allows you to programmatically interface with app widget i.e. to interact with widgets (remote views).
This Provider class basically broadcast receiver which receives broadcasts when app widget is updated, enabled, disabled, deleted. This broadcast receiver content intent filters to listen for broadcast intents requesting for widget updates.

App Widget Size Guide Lines:-  
Please refer this link for details. 
Each widget much defines minWidth & minHeight in metadata. (Minimum height & width for widget)
Home screen offers users grid of available space.
For n cells available size is 70 * n-30.

HOW TO MODIFY WIDGETS(VIEWS):-  To modify views that form  app widget create remote view , modify remote view , apply remote view using AppWidgetManager.
Which type of changes ex, changing view’s visibility, changing text, changing image of imageview.

Steps for modifying Remote Views

1)      Create RemoteView:
Remoteview views= new RemoteViews(context.getpackage(), R.id.widget_layout);
1st parameter: package of widget
2nd parameter : layout file  of widget.

2)      Modify Remoteview:
Views.setviewvisibilty(R.id.textview_widget,,view.unvisible);
Views.setTextViewText(R.id.textview_widget,”invisible”);

3)      Applying RemoteView changes:
to apply changes to widget , we must know its ID. But there may be widget  of same type running on home screen.
AppwidgetManager.updateAppWidget(appwidgetIds,views);
1st parameter: integer array of all appwidgetds
2nd parameter: remoteview   that we created.

There are two places where we can update views
1)      Updating in OnUpdate handler (quite easy)
Step 1: get list of all ids(one of the handlers parameter)

Step 2: iterate through each id.

Step 3: update views for each id using appwidgetmanager(one of the parameter of onupdate handler)

2)      Outside onUpdate i.e in service,activity,broadcast receiver
Step1: get AppWidgetManager Instance

AppWidgetManager apM= AppWidgetManager.getInstance(pass current context)

Step 2: get all ids

Componentname  thisWidget = new ComponentName(current_context,MyAppwidgetProvider class)
Appwidgetmanager.getIds(thisWidget);

Step 3: Updateviews using AppWidgetManager (apM).        

AppWidget inherits  all permission of parent process i.e.(Home Screen) so for security reason interaction with AppWidgets are following
1)Adding click listener to one or more views
2)changing UI Based on selection  
3) transitioning between views within a collection view widget.

Note: there is no technique to directly enter Text. If you need to input text , then add click listener that will display activity and enter in that activity.

How to open activity: onclick view listener,open activity but your view don’t have permission to open activity so pending Intent is the mechanism.

Views.setonClickPendingIntent(R.id.widget_text,pendingIntent)

For changing images on selection use selectors.

Refreshing widgets : your widget should be up to date, as it appears on home screen,
1) using minimum update rates(update rate is mentioned in metadata file)
 
2) using event driven model
In provider(broadcast receiver) register intent filter for all the events(broad casts) when u want to update your widget.


Thanks

Maulik Dhameliya

dhameliya blog






                                                                                                                                                    


Monday, 11 March 2013

How to reconnect to emulator when it is disconnected.

Hello Friends ,

When you are working with emulator , you may get error that emulator is disconnected or adb server is down. Its very irritating . But don't worry. Its very easy to reconnect the emulator.

Just follow the steps given below.

1)open cmd
2)change current directory to  ...\Android\android-sdk\platform-tools (path to adb.exe)
3)type following command
   adb kill-server.

Now check whether the process is stopped or not. Check by task manager -->process . And if it is there. End the process.

4)adb start-server

Follow the snapshots given below.






Thanks

How to get absolute path of drawable images ?


Hello Everyone , After lots of searching on internet I am able to get absolute path of images that are stored in drawable folder of android application.

There is no any straight forward way to get the absolute path. What I have done is that I have stored images on device’s internal memory/sdcard.

If your application is going to be installed on sdcard then you can store it in sdcard.

Make sure that depending on size of images , you decide where to store.


To store Image on sdcard.


Bitmap bitMap = BitmapFactory.decodeResource(getResources(),R.id.img1);

File mFile1 = Environment.getExternalStorageDirectory();

String fileName =”img1.jpg”;

File mFile2 = new File(mFile1,fileName);
try {
       FileOutputStream outStream;

       outStream = new FileOutputStream(mFile2);

       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

       outStream.flush();

       outStream.close();

       } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
                         }

   String    sdPath = mFile1.getAbsolutePath().toString()+"/"+fileName;

Log.i(“MAULIK”, “Your IMAGE ABSOLUTE PATH:-”+sdPath); 

       File temp=new File(sdPath);

       if(!temp.exists()){
              Log.e("file","no image file at location :"+sdPath);
       }
                          
Store Image in internal Memory


Bitmap bitMap = BitmapFactory.decodeResource(getResources(),R.id.img1);

String fileName =”img1.jpg”;

try {
                                 
FileOutputStream out1=openFileOutput(fileName, Context.MODE_PRIVATE);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out1);

out1.flush();

out1.close();

File f=getFileStreamPath(fileName);

String mPath=f.getAbsolutePath();

       } catch (FileNotFoundException e1) {
                                  // TODO Auto-generated catch block
                                  e1.printStackTrace();
                           } catch (IOException e) {
                                  // TODO Auto-generated catch block
                                  e.printStackTrace();
                           }

      
Log.i(“MAULIK”, “Your IMAGE ABSOLUTE PATH:-”+mPath);   

      

I am sure this will be helpful to many. If you have any problem kindly contact me.

If you have better approach than this, kindly inform me.

Thank You.  

Saturday, 3 March 2012

Principle of locality of reference

In computer science, locality of reference, also known as the principle of locality, is the phenomenon of the same value or related storage locations being frequently accessed. There are two basic types of reference locality. Temporal locality refers to the reuse of specific data and/or resources within relatively small time durations. Spatial locality refers to the use of data elements within relatively close storage locations.

1)    Temporal locality: if at one point in time a particular memory location is referenced, then it is likely that the same location will be referenced again in the near future. There is a temporal proximity between the adjacent references to the same memory location. In this case it is common to make efforts to store a copy of the referenced data in special memory storage, which can be accessed faster.

2)    Spatial locality: if a particular memory location is referenced at a particular time, then it is likely that nearby memory locations will be referenced in the near future. In this case it is common to attempt to guess the size and shape of the area around the current reference for which it is worthwhile to prepare faster access.

Reasons for Locality:-
1)Predictability: In fact, locality is merely one type of predictable behavior in computer systems. Luckily, many of the practical problems are decidable and hence the corresponding program can behave predictably, if it is well written.
2) Structure of the program: Locality occurs often because of the way in which computer programs are created, for handling decidable problems. Generally, related data is stored in nearby locations in storage. One common pattern in computing involves the processing of several items, one at a time. This means that if a lot of processing is done; the single item will be accessed more than once, thus leading to temporal locality of reference. Furthermore, moving to the next item implies that the next item will be read, hence spatial locality of reference, since memory locations are typically read in batches.
3) Linear data structures: Locality often occurs because code contains loops that tend to reference arrays or other data structures by indices. Sequential locality, a special case of spatial locality, occurs when relevant data elements are arranged and accessed linearly. For example, the simple traversal of elements in a one-dimensional array, from the base address to the highest element would exploit the sequential locality of the array in memory. 
Use of Locality in general :
    If most of the time the substantial    portion of the references aggregate into clusters, and if the shape of this system of clusters can be well predicted, then it  can be used for speed optimization





Tuesday, 21 February 2012

How to remove unwanted entry from windows 7 boot manager

Hello Friends , If you want to remove unwanted entries from your boot manager in windows 7 then i have the solution ...




Please Follow it carefully....




1) open cmd ( run as administrator)




2) write command bcdedit




3)now after writing this command you will find list of many entries that are in your list when you open your pc..




4) now for example , if your boot loader as entry like earlier version of windows or ubuntu(though it is not exiting) you would like to remove this unwanted entries ...




5) for this write the following command...




bcdedit /delete {the key you want to remove} /cleanup

Note:- please keep space as shown above ...







here key= value that you will find after step 1 ; this value will be the value of identifier













TAKE CARE WHILE PERFORMING STEP 5....

Tuesday, 14 February 2012

Personalize Your Windows Login Screen in windows 7

Hello Friends , Are you bored with your login screen ? All the time you login on your computer you have same background image ...

So, do you wanna change that image and set your favorite image as login screen , then follow this procedure ...

1) open windows start menu and type regedit and open it

2)go to following path

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background


3) right click on Background and select new -> DWORD(32-bit)Value

4)give name OEMBackground to newly created key

5) right click and select modify


6) set value equal to 1

7) go to following path-

C:\Windows\System32\oobe

8) go to info folder and open backgrounds folder

9) put your image(<256kb) in this folder and give name backgrounddefault.jpg

* it must be jpg file

10)your task is done ... log off your screen and again login .....



IF YOU LIKE THIS POST DO FOLLOW THIS BLOG...

FOR ANY PROBLEM PUT YOUR COMMENTS ....
















Saturday, 4 February 2012

C++11 Standards

C++11, also formerly known as C++0x, is the name of the most recent iteration of the C++ programming language, approved by ISO as of 12 August 2011, replacing C++03. The name is derived from the tradition of naming language versions by the date of the specification's publication.

c++11 includes several additions to the core language and extends the C++ standard library, incorporating most of the C++ Technical Report 1 (TR1) libraries — with the exception of the library of mathematical special functions. C++11 was published as ISO/IEC 14882:2011 in September 2011 and is available for a fee. The most recent working draft freely available is N3242, dated 28 February 2011.



Changes from the previous version of the standard:-

  1. The modifications for C++ involve both the core language and the standard library. 
  2. Prefer introduction of new features through the standard library, rather than extending the core language. 
  3. Prefer changes that can evolve programming technique. 
  4. Improve C++ to facilitate systems and library design, rather than to introduce new features only useful to specific applications. 
  5. Increase type safety by providing safer alternatives to earlier unsafe techniques. 
  6. Increase performance and the ability to work directly with hardware. 
  7. Provide proper solutions for real-world problems. 
  8. Implement “zero-overhead” principle (additional support required by some utilities must be used only if the utility is used). 
  9. Make C++ easy to teach and to learn without removing any utility needed by expert programmers. 



Attention to beginners is considered important, because they will always compose the majority of computer programmers, and because many beginners would not intend to extend their knowledge of C++, limiting themselves to operate in the aspects of the language in which they are specialized.Additionally, considering the vastness of C++ and its usage (including areas of application and programming styles), even the most experienced programmers can become beginners in a new programming paradigm.



Extension to c++ core language:-


One function of the C++ committee is the development of the language core. Areas of the core language that were significantly improved include multi-threading support, generic programming support, uniform initialization, and performance enhancements.

For the purposes of this article, core language features and changes are grouped into four general sections: run-time performance enhancements, build-time performance enhancements, usability enhancements, and new functionality. Some features could fall into multiple groups, but they are only mentioned in the group which primarily represents that feature.