I have an app and have decided to add a widget to it, I've followed all the official documentation for it, but when I install the app on my Galaxy Nexus, the widget doesn't appear in the app/widget list.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dysign.livetubecountdown"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.dysign.livetubecountdown.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.dysign.livetubecountdown.Widget" android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<intent-filter>
<action android:name="android.appwidget,action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/widget_provider" />
</receiver>
</application>
widget_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="80dp"
android:minWidth="80dp"
android:minResizeHeight="40dp"
android:minResizeWidth="40dp"
android:updatePeriodMillis="86400000"
android:initialLayout="#layout/widget_main"
android:previewImage="#drawable/ic_launcher"
android:widgetCategory="home_screen"
android:resizeMode="horizontal|vertical">
</appwidget-provider>
widget_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/widgetTextView"/>
</FrameLayout>
Widget.java
package com.dysign.livetubecountdown;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
public class Widget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerClass(context, appWidgetManager), 1, 1000);
}
private class TimerClass extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
public TimerClass(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
thisWidget = new ComponentName(context, Widget.class);
}
#Override
public void run() {
remoteViews.setTextViewText(R.id.widgetTextView, "TIME = " +format.format(new Date()));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
}
I've been looking all over the web to try to fix this, but every suggestion I try just doesn't work. Also there are no errors in LogCat or any mention of anything widget related.
Any help is much appreciated, thanks!
The problem turned out to be a typo in the manifest.
<action android:name="android.appwidget,action.APPWIDGET_UPDATE" />
Should of been:
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
Notice the comma after android.appwidget.
I have experienced the same problem a few times.
The thing that helped me was rebooting my phone, after that the widget started appearing in the widget list. Why is this happening i dont know i have googled and found out that many have similar problems, that it even happens that users install the app from the Market and the widget doesnt appear in the list.
Some solutions that were offered by others: re-installing the app(didn work for me), starting its main activity a few times(also nothing), rebooting phone(this one worked for me, since it even happened during development that the widget works fine for days and the suddenly it was missing in the list when re-installing from eclipse??? Rebooting the phone helped me.
Hope it helps!
Related
I am new to android development and learning listview from WSCube. I have seen several other youtube videos also but cannot find the solution to it. Please check and advise.
I have attached all the codes than i have worked on my system through watching from the videos of WsCube youtube channel.
enter image description here
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.ListView"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
</application>
</manifest>
Activity_Main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Main Activity
package com.example.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
ArrayList<String> arrNames = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView.findViewById(R.id.listView);
arrNames.add("Daniel01");
arrNames.add("Daniel02");
arrNames.add("Daniel03");
arrNames.add("Daniel04");
arrNames.add("Daniel05");
arrNames.add("Daniel06");
arrNames.add("Daniel07");
arrNames.add("Daniel08");
arrNames.add("Daniel09");
arrNames.add("Daniel10");
arrNames.add("Daniel11");
arrNames.add("Daniel12");
arrNames.add("Daniel13");
arrNames.add("Daniel14");
arrNames.add("Daniel15");
arrNames.add("Daniel16");
arrNames.add("Daniel17");
arrNames.add("Daniel18");
arrNames.add("Daniel19");
arrNames.add("Daniel20");
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, arrNames);
listView.setAdapter(adapter);
}
}
You made a mistake here.
listView.findViewById(R.id.listView);
That is wrong. Because you use a dot . between listView and findViewById(R.id.listView);.
You need to initialize the listView variable. You can do that with equal to = See the example I gave here.
Solution
listView = findViewById(R.id.listView);
I have a web view app that works perfectly fine on most android phones, but while testing, some said that the app even won't open for the first time after installation.
It keeps on showing the error as below :
"Keeps Stopping"
And in some mobile devices, it says "Runtime Exception" and crashes without even opening.
This was a debug APK, so in an attempt of trying to fix it, I even generated a signed APK. Still no luck.
Here is my whole Code:
Activity Main.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="match_parent"
android:id="#+id/webviewid"
android:layout_height="match_parent"
android:overScrollMode="never"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</WebView>
</RelativeLayout>
AndroidManifest.Xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxxxxxxxxxxx;">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" ></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" ></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_roundffd"
android:supportsRtl="true"
android:theme="#style/Theme_xxxxxx"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Mainactivity.java
package com.xxxxxxxxxxxxx;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.accessibilityservice.AccessibilityService;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.os.Handler;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.content.Context;
import android.net.ConnectivityManager;
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webviewid);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.some.com/");
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setGeolocationEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
}
// On double back press stuff starts --------------------------------------------------
private boolean doubleBackToExitPressedOnce = false;
#Override
protected void onResume() {
super.onResume();
// .... other stuff in my onResume ....
this.doubleBackToExitPressedOnce = false;
}
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
if(webView.canGoBack()){
webView.goBack();
}
}
}
Please note that the app works completely fine, It installs and opens with no issues, but when it comes to some phones, I am getting this "Keeps Stopping" error.
As per some blogs, I even tried uninstalling updates from the Android Web View setting, yet still no use.
Any help or suggestion is greatly appreciated.
can you remove the 'smallestscreensize' in android:configChanges in manifest file and let us know.
I'm new to the Android API and i try hard to start my app (NOT an Activity) from a Broadcast receiver. It's compiling and manualy launching on my Device, but not automatically launching when i plug an USB device in.
My goal : plug usb in -> start app -> "Hello World!"
===============================[edit]======================================
That's the final version that is working for me. If it's not working for you test with an other device.
Works on my Moto G 1st Gen. Android 5.1.1.
Do not work on my S3 I9300 Android 4.4.4
tests :
Reboot -> OS running -> plug USB in -> unlock the screen -> "Hello world"
Unlock -> plug in -> "Hello world"
...
The code :
package com.example.desktop.helloworld;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class HelloworldActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_helloworld);
TextView tv = new TextView(this);
tv.setText("Hello World!");
setContentView(tv);
}
public static class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, HelloworldActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
The Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.desktop.helloworld">
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".HelloworldActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:name=".HelloworldActivity$BootUpReceiver">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
</receiver>
</application>
device_filter (NOT REQUIERED, then comment the reffence in the manifest):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="0403" product-id="6001"/>
</resources>
Android Studio 2.0,
OTG Cable,
Android 4.4.4 .
I tried to run first example from "Beginning Android Games" book (by Mario Zechner, Apress.com):
I copied this code to my project (MainActivity.java file):
HelloWorldActivity.java
package com.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button button;
int touchCount;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button = new Button(this);
button.setText( "Touch me!" );
button.setOnClickListener(this);
setContentView(button);
}
public void onClick(View v) {
touchCount++;
button.setText("Touched me " + touchCount + " time(s)");
}
}
but when I have run this simple application, I didn't see any button on the emulator (I have seen only an "Android" label. I tried "project-->clean..": no result. (and "project-->Build Automatically is swiched on). Where do I need to declare and describe this button? But I followed all steps described in the book. And at the end, I removed xml-file from layout folder at all.
Thanks.
________________Edited: Added xml-files___________
res-->layout-->activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.firstbuttontest2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="11" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.firstbuttontest2.MainActivity"
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>
</manifest>
Try this it might help you.
Add button to particular layout. Like this
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.linearLayout);
linearLayout.addView(button);
I know that there is a addContentView method which may be more what you are looking for as opposed to setContentView.
Try
setContentView(R.layout.activity_main)
then
button = (Button) findviewbyid(R.id.Button1);
this may solve your problem
I have no idea what's going on. My OnReceive method is never called. It's probably something in the manifest.
Widget Provider:
package com.net_refresh;
import android.app.PendingIntent;
import android.appwidget.AppWidgetProvider;
import android.content.Intent;
import android.content.Context;
import android.provider.Settings;
import android.view.View;
import android.widget.RemoteViews;
import android.appwidget.AppWidgetManager;
public class NetRefreshWidget extends AppWidgetProvider
{
private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";
#Override
public void onUpdate(Context context, AppWidgetManager appwidgetmanager, int[] appWidgetIds)
{
final int length = appWidgetIds.length;
for (int i=0; i<length; i++)
{
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, NetRefreshWidget.class);
intent.setAction(ACTION_WIDGET_RECEIVER);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_initial_layout);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
appwidgetmanager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onDeleted(Context context, int[] appWidgetIds)
{
}
#Override
public void onEnabled(Context context)
{
}
#Override
public void onDisabled(Context context)
{
}
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER))
{
//Top secret code here.
}
}
Manifest:
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.net_refresh" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="7"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:label="#string/app_name" android:name=".NetRefresh">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".NetRefreshWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/widget_info"/>
</receiver>
</application>
</manifest>
Widget info XML:
<?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_initial_layout"
>
</appwidget-provider>
Widget layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget_root"
android:layout_width="72dp"
android:orientation="vertical"
android:layout_height="72dp"
>
<Button android:text="Button" android:layout_width="wrap_content" android:id="#+id/button1" android:layout_height="wrap_content"></Button>
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/icon" android:clickable="true" android:adjustViewBounds="false" android:id="#+id/img_refresh" android:layout_toRightOf="#+id/button1" android:layout_alignTop="#+id/button1" android:layout_alignBottom="#+id/button1">
</ImageView>
</RelativeLayout>
The code says nothing without you to point the compiler's errors & warnings.
Anyway you can wrap your code into .apk and install it by yourself into your device, BUT this is the curve way, because everyone I know are using the eclipse and SDK tools.
Have you tried using this: <uses-permission android:name="android.permission.INTERNET" />. It may solve it.
I figured it out a while back. I don't remember what I changed exactly but I at least added this line:
<action android:name="android.appwidget.action.ACTION_WIDGET_RECEIVER"/>
To the manifest, next to:
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
Very simple solution, now that I have an understanding of how broadcasts work.