Problem when updating a Widget by an activity - Android Software Development

Hello all together,
i've got a little problem updating my widget.
I wanted to add a widget to an existing app (Static IP Toggle).
This app changes between DHCP and static IP configuration when started =>
app started -> DHCP
app started again -> static IP, and vice versa.
I got a request from a user of this app, that it would be nice if there would be a widget included, showing which mode is active (either DHCP or static IP), so I added the widget as follows:
Added lines, concerning the former version without widget, are highlighted in blue.
First I declared the Widget in AndroidManifest.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chemdroid.staticiptoggle"
android:installLocation="internalOnly" android:versionName="1.2" android:versionCode="3">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".StaticIPToggle"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
[COLOR="Blue"]<receiver android:name=".Widget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>[/COLOR]
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
</manifest>
The AppWidgetProvider was declared in /xml/widget.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget_dhcp">
</appwidget-provider>
I created 2 layouts, one for DHCP, one for static IP. Included was an ImageButton for showing either DHCP or static IP.
Widget.java was created, for setting the specified layout and starting the activity for changing the settings:
Code:
package com.chemdroid.staticiptoggle;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.widget.RemoteViews;
public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int use_static_ip;
RemoteViews remoteViews;
try {
use_static_ip = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP);
if (use_static_ip == 0) { //DHCP
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_dhcp);
} else { //static IP
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_static);
}
Intent call_activity = new Intent(context, StaticIPToggle.class);
PendingIntent pending_call_activity = PendingIntent.getActivity(context, 0, call_activity, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_icon, pending_call_activity);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
At last, the activity was changed to update the widget, after IP settings have been changed:
Code:
package com.chemdroid.staticiptoggle;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings;
import android.widget.Toast;
public class StaticIPToggle extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int use_static_ip;
try { --> Code für Änderung der Config
[COLOR="Blue"]AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName componentName = new ComponentName(getApplicationContext(), Widget.class);
int[] ids = appWidgetManager.getAppWidgetIds(componentName);
Intent update_widget = new Intent();
update_widget.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
update_widget.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
getApplicationContext().sendBroadcast(update_widget);[/COLOR]
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
}
}
But now I have this bug:
If the widget is onClicked, it shows the energy-settings-widget for a short period (~0,5sec-1sec) before the desired widget is shown. A screenshot is attached.
First there's the widget as in the left picture, then changes to the middle picture and either stays there or changes to the right picture after ~0,5-1sec.
Unfortunately I can't see any mistake in the code, do you?
Greetz Oli

Nobody here who can help me? :-/

I really can't see any mistake in my code, but what's the problem there?

Solved the problem by sending an own intent that is received from the widget -> onReceive: call onUpdate

Related

webview help

I am having trouble creating a webview app for my already mobile ready site.
I keep getting this error in the emulator:
The application window cleaning forums (process com.windowcleaningforums) has stopped unexpectadly
The app never actualy loads just goes straight to this.
Basically i already have my sites mobile ready and browsing to them on your mobile works fine, but would like to put these into apps.
With a back, forward and refresh button when hitting menu button on phone.
(I am not sure what i need to add these yet but any advice would be great)
My project is set as bellow
Application name: Window Cleaning Forums
Package name: com.windowcleaningforums
Create activity: windowcleaningforums
Mini SDK version: 4
windowcleaningforums.java
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity {
/** Called when the activity is first created. */
//@Override
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.windowcleaningforums.co.uk");
mWebView.setWebViewClient(new HelloWebViewClient());
}
}
Main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<webView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Manifest
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.windowcleaningforums"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".windowcleaningforums"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
I am very new and im not quite sure what is causing it any help would be great. Thanks
I get this if that is any help i am not sure what it means though.
[2011-01-12 22:35:07 - DeviceMonitor]Sending jdwp tracking request failed!
This is the log if that helps also i really could do with some help peeps, nobody seems to want to. I know i am new and prob asking stupid questions but how am i supposed to learn if i dont ask questions.
Hi cyberpedz,
Probably just a typo but in your main.xml the WebView tag should have a capital "W" like so:
<WebView xmlns:andro...
You did the right thing looking in the log. That's what helped to figure this one out: there was an exception stack trace in the log. (Keep an eye out for the "AndroidRuntime" tag)
Thanks i have changed that but no difference.
I do have a red dot over a hellowebview class though
When viewing windowcleaningforums.java and looking at the right in outline i have a read dot over hellowebviewclient
Could it be something to do with this bit of code?
Code:
public class windowcleaningforums extends Activity {
/** Called when the activity is first created. */
//@Override
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Try moving the
Code:
//@Override
just before onCreate and uncomment it.
Also Java classes should start with a capital (public class Windowcleaningforum). Make sure you modify the Manifest accordingly.
Ok finally i have it working, it was me being blind i missed a webView now changed to Webview and all works thanks so much.
Now i am trying to get a loading progress bar or even better spinning circle.
What code would i need for this and where abouts in my java bellow would i fit it in?
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity {
/** Called when the activity is first created. */
//@Override
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.WebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.windowcleaningforums.co.uk");
mWebView.setWebViewClient(new HelloWebViewClient());
}
}
Anyone know how to get the spinning circle while pages loads? and how to imput into my code above?
Would be a great help
Ok finally i have a running webview in the market what i would like to do now is add a soft menu for a back, refresh and forward button what code do i need and where would i put it in my java below?
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity
{
final Activity activity = this;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.WebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle(" Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.windowcleaningforums.co.uk");
}
}
So why is it no body wants to help is it the way i ask? is it that developers think they are above the noob? or you really cant b bothered!!
I have even offered to pay for help in the past yet nobody is interested maybe it is that no one really knows the answers.
cyberpedz said:
So why is it no body wants to help is it the way i ask? is it that developers think they are above the noob? or you really cant b bothered!!
I have even offered to pay for help in the past yet nobody is interested maybe it is that no one really knows the answers.
Click to expand...
Click to collapse
It's not that developers can't be bothered, but generally they will have other things to do than help out others. The thing is, you're asking on help for pretty basic stuff. This is something that you should know already, and if don't I suggest you should read through Android's developer website one more time and look at the API examples/demos too.
Hints for your problem; Add the buttons as a merge layout in your XML and then link them with your web view, or add them as menu options. Googling for both will surely give you enough results to get you on your way, these are pretty basic things you want to do after all.

[Question] Having trouble setting up wallpaper chooser/picker in eclipse

Doing a theme for ADW and want to include a wallpaper chooser so the user can browse through my wallpaper set...
I am working in eclipse.
I have successfully got setup in wallpaper menu (when you press and hold homescreen, select wallpaper, Then you see the option for wallpaper i have setup)
BUT when click i get a force close...
I have 10 wallpapers in my drawable-hdpi folder. they are named
wallpaper1
wallapper2
etc...
I also have my wallpaper1_small, etc... set up too..
This is contents of my wallpaper_chooser.xml in layout
Code:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@id/wallpaper" android:layout_width="fill_parent" android:layout_height="0.0dip" android:scaleType="fitCenter" android:layout_weight="1.0" />
<Gallery android:id="@id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<Button android:layout_gravity="center_horizontal" android:id="@id/set" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set wallpaper" />
</LinearLayout>
This is my wallpaper_item.xml in layout
Code:
<?xml version="1.0" encoding="UTF-8"?>
<ImageView android:background="?android:galleryItemBackground" android:focusable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitXY"
xmlns:android="http://schemas.android.com/apk/res/android" />
this is my arrays.xml in values-hdpi folder
Code:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string-array name="wallpapers">
<item>wallpaper1</item>
<item>wallpaper2</item>
<item>wallpaper3</item>
<item>wallpaper4</item>
<item>wallpaper5</item>
<item>wallpaper6</item>
<item>wallpaper7</item>
<item>wallpaper8</item>
<item>wallpaper9</item>
<item>theme_wallpaper</item>
</string-array>
</resources>
Any Idea's??
okay, figured out i needed a wallpaper.java reference...
Added the following
Code:
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bigdx.adw.crystalx;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;
import java.io.IOException;
import java.io.InputStream;
/**
* Wallpaper picker for the Home application. User can choose from
* a gallery of stock photos.
*/
public class wallpaper extends Activity implements
AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
private static final String LOG_TAG = "Home";
private static final Integer[] THUMB_IDS = {
R.drawable.wallpaper1_small,
R.drawable.wallpaper2_small,
R.drawable.wallpaper3_small,
};
private static final Integer[] IMAGE_IDS = {
R.drawable.wallpaper1,
R.drawable.wallpaper2,
R.drawable.wallpaper3,
};
private Gallery mGallery;
private boolean mIsWallpaperSet;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.wallpaper_chooser);
mGallery = (Gallery) findViewById(R.id.gallery);
mGallery.setAdapter(new ImageAdapter(this));
mGallery.setOnItemSelectedListener(this);
mGallery.setOnItemClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
mIsWallpaperSet = false;
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
getWindow().setBackgroundDrawableResource(IMAGE_IDS[position]);
}
public void onItemClick(AdapterView parent, View v, int position, long id) {
selectWallpaper(position);
}
/*
* When using touch if you tap an image it triggers both the onItemClick and
* the onTouchEvent causing the wallpaper to be set twice. Synchronize this
* method and ensure we only set the wallpaper once.
*/
private synchronized void selectWallpaper(int position) {
if (mIsWallpaperSet) {
return;
}
mIsWallpaperSet = true;
try {
InputStream stream = getResources().openRawResource(IMAGE_IDS[position]);
setWallpaper(stream);
setResult(RESULT_OK);
finish();
} catch (IOException e) {
Log.e(LOG_TAG, "Failed to set wallpaper " + e);
}
}
public void onNothingSelected(AdapterView parent) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
selectWallpaper(mGallery.getSelectedItemPosition());
return true;
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return THUMB_IDS.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(THUMB_IDS[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(android.R.drawable.picture_frame);
return i;
}
}
}
It goes into the wallpaper picker without getting FC but after sliding the wallpapers on bottom it goes away all of a sudden...
One note is that the following code was originally R.layout.wallpaper, BUT get error...
changed to (setContentView(R.layout.wallpaper_chooser)
Is this that difficult?
or just not that common?
or maybe i should be more familiar with how apk's work?
Any help is appreciated...
bignadad said:
..One note is that the following code was originally R.layout.wallpaper, BUT get error...
changed to (setContentView(R.layout.wallpaper_chooser)
Click to expand...
Click to collapse
what error?
rori~ said:
what error?
Click to expand...
Click to collapse
wallpaper cannot be resolved or is not a field...
thanks for the reply..

[Q] Choose a string in spinner

Hey guys!
I am working on my first app, cut me some slack for asking please .
So I'm stuck on a little problem:
I have a reader activity and I put a spinner on top of the screen to choose the string where the text comes from:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:id="@+id/kapitelspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/kapitel_prompt" />
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/kapitel1"
/>
</ScrollView>
</LinearLayout>
The TextView in the ScrollView is supposed to be the selected text from the spinner.
Code:
package com.asm.reader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class BookActivity extends Activity {
static final private int CHOOSE_KAPITEL = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_activity);
Spinner spinner = (Spinner) findViewById(R.id.kapitelspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.kapitel_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
}
Code:
package com.asm.reader;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
I'm trying to make the spinner choose the string of the textview but i have no idea how to do it. (Already did the toast part)
If anyone could help me it would be greatly appreciated!
Thanks !
bump. the last problem im stuck on
The way I understand what you're trying to do is select an item from the spinner, and set the TextView according to the selected spinner item.
If that is the case you will, for starters, need to set an id attribute for the TextView in the layout xml file.
Then within your onItemSelected() method you need to create a reference to the TextView the same way you did it with the spinner; using the findViewById() method.
Then using that reference you can set the text of the TextView using the same string you got when you created the Toast notification.
Hope that helps!

[Q] Beginning Programmer, need help with personal app

I'm creating an app to keep track of all the messier objects in the sky. I have made a list view in the main activity that holds all the objects, when clicked it should open my other activity and display the objects info.
The program now goes to my second activity no matter what item i choose but doesn't change any of the TextView text without foreclosing.
also how can I recognize what object was clicked and change the information displayed for each object
The main Activity Java looks like this:
Code:
package com.kabluey.messier;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MessierCatalog extends ListActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set string array
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, objects));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// When clicked, change text in object info to clicked object name
TextView objText = (TextView) findViewById(R.id.txtObjName);
objText.setText(((TextView) view).getText());
Intent i = new Intent(MessierCatalog.this,Objectinfo.class);
startActivity(i);
}
});
}
static final String[] objects = new String[]
{
"m1", "m2","m3","m4","m5", "m6","m7","m8","m9","m10","m11","m12","m13","m14"
,"m15","m16","m17","m18","m19","m19","m20","m21","m22","m23","m24","m25","m26","m27","m28","m29","m30","m31","m32","m33"
,"m34","m35","m36","m37","m38","m39","m40","m41","m42","m43","m44","m45","m46","m47","m48","m49","m50","m51","m52","m53"
,"m54","m55","m56","m57","m58","m59","m60","m61","m62","m63","m64","m65","m66","m67","m68","m69","m70","m71","m72","m73"
,"m74","m75","m76","m77","m78","m79","m80","m81","m82","m83","m84","m85","m86","m87","m88","m89","m90","m91","m92","m93"
,"m94","m95","m96","m97","m98","m99","m100","m101","m102","m103","m104","m105","m106","m107","m108","m109","m110"
};
}
The second activity has no code in it now and is just an xml layout:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="@drawable/crab"
/>
<TableLayout
android:layout_width="fill_parent"
android:id="@+id/tableLayout1"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="Name of Object:"
android:padding="3dip"/>
<TextView
android:id="@+id/txtObjName"
android:text="Object name here"
android:padding="3dip"
android:paddingLeft="5dip" />
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>
<TableRow>
<TextView
android:text="Apparent Magnitude:"
android:padding="3dip"/>
<TextView
android:id="@+id/txtMagnitude"
android:paddingLeft="5dip"
android:text="9.0"/>
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>
</TableLayout>
</LinearLayout>
I think it may have to do with your casting of "view" to type "TextView" in you onItemClick override. I don't think the "View" passed to this override is a "TextView" is it? I would think it is the ListView to which you added the OnItemClickListener event handler.
Have some suggestion / tips:
1. This is more organized, also initializes first the Strings
Code:
package com.kabluey.messier;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MessierCatalog extends ListActivity
{
/** Called when the activity is first created. */
private static final String[] objects = {
"m1", "m2","m3","m4","m5", "m6","m7","m8","m9","m10","m11","m12","m13","m14"
,"m15","m16","m17","m18","m19","m19","m20","m21","m22","m23","m24","m25","m26","m27","m28","m29","m30","m31","m32","m33"
,"m34","m35","m36","m37","m38","m39","m40","m41","m42","m43","m44","m45","m46","m47","m48","m49","m50","m51","m52","m53"
,"m54","m55","m56","m57","m58","m59","m60","m61","m62","m63","m64","m65","m66","m67","m68","m69","m70","m71","m72","m73"
,"m74","m75","m76","m77","m78","m79","m80","m81","m82","m83","m84","m85","m86","m87","m88","m89","m90","m91","m92","m93"
,"m94","m95","m96","m97","m98","m99","m100","m101","m102","m103","m104","m105","m106","m107","m108","m109","m110"
};
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set string array
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, objects));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// When clicked, change text in object info to clicked object name
TextView objText = (TextView) findViewById(R.id.txtObjName);
objText.setText(((TextView) view).getText());
//Log here!, but im almost sure this is bad
Intent i = new Intent(MessierCatalog.this,Objectinfo.class);
startActivity(i);
}
});
}
2. Could you make a Log.i("APP", ((TextView) view).getText()) and upload output?. That appears like you're setting to your textview, the same text you got from it ( that's actually null / empty )
3. If output it's empty or not output, then this is the answer
Code:
package com.kabluey.messier;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MessierCatalog extends ListActivity
{
/** Called when the activity is first created. */
private static final String[] objects = {
"m1", "m2","m3","m4","m5", "m6","m7","m8","m9","m10","m11","m12","m13","m14"
,"m15","m16","m17","m18","m19","m19","m20","m21","m22","m23","m24","m25","m26","m27","m28","m29","m30","m31","m32","m33"
,"m34","m35","m36","m37","m38","m39","m40","m41","m42","m43","m44","m45","m46","m47","m48","m49","m50","m51","m52","m53"
,"m54","m55","m56","m57","m58","m59","m60","m61","m62","m63","m64","m65","m66","m67","m68","m69","m70","m71","m72","m73"
,"m74","m75","m76","m77","m78","m79","m80","m81","m82","m83","m84","m85","m86","m87","m88","m89","m90","m91","m92","m93"
,"m94","m95","m96","m97","m98","m99","m100","m101","m102","m103","m104","m105","m106","m107","m108","m109","m110"
};
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set string array
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, objects));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// When clicked, change text in object info to clicked object name
TextView objText = (TextView) findViewById(R.id.txtObjName);
objText.setText(objects[position]);
Intent i = new Intent(MessierCatalog.this,Objectinfo.class);
startActivity(i);
}
});
}
Also for this cases it's not really needed TableRow, With a Relative layout it's enough, but I don't think that's a prob.
If you have some problem, feel free to contact me through PM.
Good luck, and cheers from colombia.
D4.
solved!
I searched the nets and found a solution, it seems to be working quite well.
thanks for all the help
My click listener now stores the value so both activities can access it
Code:
public void onItemClick(AdapterView<?> parent, View view,int position,long id)
{
Intent i = new Intent(MessierCatalog.this,Objectinfo.class);
//Get ListViews text and store it to "myText"
i.putExtra("myText", (((TextView) view).getText()));
startActivity(i);
}
The ObjectInfo activity now has code to store the ListViews text in String Variable and then set the text..
Code:
public class Objectinfo extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.objectinfo);
TextView objName = (TextView) findViewById(R.id.txtObjName);
//store ListViews Text to String
String str = getIntent().getStringExtra("myText");
//Set text to stored ListView string
objName.setText(str);
}
}

Stackview Widget - Items not loading

I've tried to create simple dialer widget that will get contacts from contact book, put them into stackview and when clicked create a popup where you can call or create a message. I worked well as an application, but when I turned it into actual widget, I've encountered something weird. Despite all the methods firing, the items are not updated and stay on loading layout.
I've compared it to the example, tried to switch parts of code and nothing works. Even funnier, my code works perfectly when I put it in example widget, but example code doesn't work in mine. The same thing happens for listview and gridview. Is there any other file I should edit?
I provide the code, layouts and manifest. Logcat shows all methods firing in right order, so there's nothing to see there.
WidgetProvider.java
Code:
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds){
Log.d("WIDGET", "Called onUpdate()");
for (int i = 0; i < appWidgetIds.length; ++i){
// Set up the intent that starts the StackViewService, which will
// provide the views for this collection.
Intent intent = new Intent(context, WidgetService.class);
// Add the app widget ID to the intent extras.
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// Instantiate the RemoteViews object for the app widget layout.
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main);
rv.setRemoteAdapter(R.id.stackView, intent);
rv.setEmptyView(R.id.stackView, R.id.emptyView);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
Log.d("WIDGET", "Widget updated");
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
WidgetService.java
Code:
package com.chmis.superdialer;
import java.util.Collections;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
}
}
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
Context context;
Intent intent;
public StackRemoteViewsFactory(Context applicationContext, Intent intent) {
this.context = applicationContext;
this.intent = intent;
}
@Override
public void onCreate() {
Log.d("SERVICE", "Called onCreate()");
//Get contact Cursor
Cursor contactCursor = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
//Get contacts from cursor, store them as Contact objects inside ArrayList
while (contactCursor.moveToNext()) {
String name = contactCursor.getString(contactCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phone = contactCursor.getString(contactCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
long id = contactCursor.getLong(contactCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Contact.array.add(new Contact(name, phone, id));
}
//Sort contacts by name
Collections.sort(Contact.array, new ContactComparator(Contact.JOHN_SMITH));
}
@Override
public RemoteViews getViewAt(int position) {
Log.d("SERVICE", "Called getViewAt(" + position + ")");
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main);
rv.setTextViewText(R.id.contactNameTextView, Contact.array.get(position).getDefaultName());
// Return the remote views object.
return rv;
}
@Override
public int getCount() {
Log.d("SERVICE", "Called getCount(), returned " + Contact.array.size());
return Contact.array.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 0;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void onDataSetChanged() {}
@Override
public void onDestroy() {
Contact.array.clear();
Log.d("SERVICE", "Called onDestroy()");
}
}
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<StackView
android:id="@+id/stackView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:loopViews="true" />
<TextView
android:id="@+id/emptyView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@android:color/transparent"
android:textStyle="bold"
android:textColor="#ffffff"
android:text="@string/no_contacts" />
</FrameLayout>
contact_stack_item
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent_dark" >
<ImageView
android:id="@+id/contactPhotoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="@string/contact_image_description"
android:src="@drawable/contact_photo_default" />
<TextView
android:id="@+id/contactNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/contactPhotoImageView"
android:layout_alignLeft="@+id/contactPhotoImageView"
android:layout_alignRight="@+id/contactPhotoImageView"
android:background="@color/transparent_dark"
android:gravity="center_horizontal"
android:text="@string/contact_name_placeholder"
android:textColor="@android:color/white" />
</RelativeLayout>
manifest
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chmis.superdialer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application android:label="@string/app_name" >
<receiver android:name="WidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_provider_info" />
</receiver>
<service
android:name="WidgetService"
android:exported="false"
android:permission="android.permission.BIND_REMOTEVIEWS" />
</application>
</manifest>
@edit I've copied the whole service code again and now it decided to work. I don't know anything anymore.

Categories

Resources