Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:41:07+00:00 2026-06-12T12:41:07+00:00

I want to develop a widget… But i have a problem when passing strings

  • 0

I want to develop a widget… But i have a problem when passing strings to the widget from AppWidgetProvider…I am doing this through shared preferences

The problem comes when I store a variable in SharedPreferences, when I try to obtain its value, it returns not a null value but something like this: “”

This value is returned although it exists that variable in WidgetPrefs.xml, i thimk there is an error with prefs variable, but i am not sure

This is the code of WidgetConfig:

public class ActivityWidgetConfig extends Activity implements OnClickListener{

private int widgetId = 0;

private String titlestring;
private String textstring;

private EditText etTitle;
private EditText etText;

private ImageButton btnTitle;
private ImageButton btnText;

private ImageView previewIcon;

private Button iconButton;
private Button createButton;

private int icon_id;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_widgetconfig);

    Intent intentOrigen = getIntent();
    Bundle params = intentOrigen.getExtras();

    widgetId = params.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

    setResult(RESULT_CANCELED);

    etTitle = (EditText) findViewById(R.id.etTitle);
    etText = (EditText) findViewById(R.id.etText);
    btnTitle = (ImageButton) findViewById(R.id.btnTitle);
    btnText = (ImageButton) findViewById(R.id.btnText);
    createButton = (Button)findViewById(R.id.createButton);
    iconButton = (Button)findViewById(R.id.iconButton);

    btnTitle.setOnClickListener(this);
    btnText.setOnClickListener(this);
    createButton.setOnClickListener(this);
    iconButton.setOnClickListener(this);

    SharedPreferences prefs = getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
    titlestring = prefs.getString("widgettitle", "");
    textstring = prefs.getString("widgettext", "");
    icon_id = prefs.getInt("widgeticon_id", android.R.drawable.stat_sys_warning);

    etTitle.setText(titlestring);
    etText.setText(textstring);

    previewIcon = (ImageView) findViewById(R.id.previewIcon);
    previewIcon.setImageResource(icon_id);

}

@Override
public void onClick(View v) {
    SharedPreferences prefs = getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
    Editor editor = prefs.edit();
    switch (v.getId()){
        case R.id.btnTitle:
            editor.remove("widgettitle");
            editor.commit();
            etTitle.setText("");
            break;
        case R.id.btnText:
            editor.remove("widgettext");
            editor.commit();
            etText.setText("");
            break;
        case R.id.createButton:
            editor.putString("widgettitle", titlestring);  
            final String textstring=etText.getText().toString();
            editor.putString("widgettext", textstring);
            editor.putInt("widgeticon_id", icon_id);
            editor.commit();

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ActivityWidgetConfig.this);
            WidgetOne.updateWidget(ActivityWidgetConfig.this, appWidgetManager, widgetId);
            Intent resultado = new Intent();
            resultado.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
            setResult(RESULT_OK, resultado);
            finish();
            break;
        case R.id.iconButton:
            Intent i = new Intent(this, ActivityIcons.class); 
            startActivityForResult(i, 1);
            break;
    }
}
 }

And this is the code of the widget:

public class WidgetOne extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        for (int i = 0; i < appWidgetIds.length; i++) 
        {
            //ID del widget actual
            int widgetId = appWidgetIds[i];

            //Actualizamos el widget actual
            //updateWidget(context, appWidgetManager, widgetId);
        }

}


@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals("com.iamaner.ACTUALIZAR_WIDGET")) {

//          //Obtenemos el widget manager y la lista de IDs de nuestro widget
//          ComponentName wgt = 
//              new ComponentName(context, MiWidget.class);
//          
//          AppWidgetManager widgetManager =
//              AppWidgetManager.getInstance(context);
//          
//          int[] appWidgetIds = widgetManager.getAppWidgetIds(wgt);
//          
//          //Actualizamos todos los widgets
//          for(int i=0; i<appWidgetIds.length; i++)
//              actualizarWidget(context, widgetManager, appWidgetIds[i]);

        //Obtenemos el ID del widget a actualizar
        int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

        //Obtenemos el widget manager de nuestro contexto
        AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);

        //Actualizamos el widget
        //if (widgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
        //  actualizarWidget(context, widgetManager, widgetId);
        //}
    }

    super.onReceive(context, intent);
}

public static void updateWidget(Context context, AppWidgetManager appWidgetManager, int widgetId)
{
    SharedPreferences prefs = context.getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
    RemoteViews controles = new RemoteViews(context.getPackageName(), R.layout.widget_1);

    //Asociamos los 'eventos' al widget
    Intent intent = new Intent("com.iamaner.ACTUALIZAR_WIDGET");
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

    controles.setTextViewText(R.id.txtMensaje, prefs.getString("widgettitle", ""));
    controles.setTextViewText(R.id.txtMensaje, prefs.getString("widgetext", ""));

    controles.setImageViewResource(R.id.ivIcon, icon_id);

    appWidgetManager.updateAppWidget(widgetId, controles);
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:41:09+00:00Added an answer on June 12, 2026 at 12:41 pm

    Nevermind, the problem was that I assigned different names to sharedpreferences strings…

    Solved it by doing this at Widget class:

    prefs.getString("widgettext","");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to develop my GWT Widget. This widget draws Gantt chart and has
I want to develop a little os x widget which fetches data from an
I want develop some web product with using javascript 3d library like three.js But
I want to develop cross platform mobile app. I have seen frameworks listed below
I want to develop an Android app in which I have key log press.
I want to develop an audio editor using Qt. For this, I need to
I want to develop a gesture recognition. I'm considering using the Kinect to have
I want to develop my iPhone app but I am not sure what could
I want a widget that behaves like a togglebutton and can have its background
i want to fill ** **Listview** ** Widget in android with Records from table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.