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.