I want to add the default search app widget to a view inside my app. Just like the homescreen but without any dragging and I only want the workspace to be 4x1 big.
How can I do this? I have looked inside the sources for the launcher but its a lot to understand and remove because I want it as simple as possible...
I know that I can get the Component name of the widget like this:
private ComponentName getSearchWidgetProvider() {
SearchManager searchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
ComponentName searchComponent = null;
searchManager.getSearchableInfo(searchComponent);
if (searchComponent == null) return null;
return getProviderInPackage(searchComponent.getPackageName());
}
private ComponentName getProviderInPackage(String packageName) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
if (providers == null) return null;
final int providerCount = providers.size();
for (int i = 0; i < providerCount; i++) {
ComponentName provider = providers.get(i).provider;
if (provider != null && provider.getPackageName().equals(packageName)) {
return provider;
}
}
return null;
}
So how can I add it inside a view (lets say a LinearView) in the easiest way...
Thanks!
Use AppWidgetHost. I actually needed this to store and read the packagename of the widget. See, we both helped out. AppWidgetHost can be implemented to be added to LnearLayout. I woud use RelativeLayout if I was you, since RelativeLayout allows custom areas, per se if you wanted it centered, then you can. Unless you use LinearLayout inside a RelativeLayout.
Here is how it is implemented:
You can edit this and change it to the way you like. The call "appWidgetId", is the int given for each widget. You can store that, then have your app check if the file that is stored exists, read it, convert the text to string, then convert the string to int.
Storing info:
String path="/sdcard/widget.txt";
//You can change the path to whatever you want it to be
File f=new File(path);
try{
FileWriter fw=new FileWriter(f);
fw.write("whatever text");
fw.flush();
fw.close();
}catch(IOException e){
e.printStackTrace();
}
Now to read the file, you must call a StringBuilder, BufferedReader, and String line to get the info.
File f=new File("/sdcard/widget.txt");
//the path must be where you stored the files
StringBuilder sb=new StringBuilder();
try{
BufferedReader br=new BufferedReader(new FileReader(f));
String line;
while((line=br.readLine())!=null){
sb.append(line);
sb.append('\n');
}
br.close();
}catch(IOException e){
e.printStackTrace();
}
Now all you need is get the text into a string:
String widId=sb.toStrng();
And then convert that String to int:
int wid=Integer.parseInt(widId);
Now you impement that ID inside this code:
public void createWidget(Intent data) {
Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(rl.getWidth(), rl.getHeight()/3);
lp.topMargin = numWidgets * (rl.getHeight()/3);
AppWidgetHostView hostView = mAppWidgetHost.createView(getActivity().getApplicationContext(), appWidgetId, appWidgetInfo);
hostView.setAppWidget(widID,appWidgetInfo);
rl.addView(hostView, lp);
hostView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
numWidgets ++;
}
This will add the selected widget to the layout.