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 4556868
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:25:39+00:00 2026-05-21T17:25:39+00:00

Im very new to android and Im trying to load/persist values from my customized

  • 0

Im very new to android and Im trying to load/persist values from my customized DialogPreference. Currently, this fails because findViewById returns null. Is the way I (try) to do it correct? How do I get access to my EditText widgets in the code?

public class AddressDialogPreference extends DialogPreference {

public AddressDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    setDialogLayoutResource(R.layout.address_dialog);
}

@Override
protected void onBindDialogView(View view) {

    EditText idField = (EditText) view.findViewById(R.id.hostID);
    EditText ipField = (EditText) view.findViewById(R.id.hostIP);

    SharedPreferences pref = getSharedPreferences();
    idField.setText(pref.getString(getKey() + "_id","ExampleHostname"));
    ipField.setText(pref.getString(getKey() + "_ip","192.168.1.1"));

    super.onBindDialogView(view);
}

@Override
protected void onDialogClosed(boolean positiveResult) {

    if(!positiveResult)
        return;

    Dialog myDial = getDialog();
    EditText idField = (EditText) myDial.findViewById(R.id.hostID);
    EditText ipField = (EditText) myDial.findViewById(R.id.hostIP);

    SharedPreferences.Editor editor = getEditor();
    editor.putString(getKey() + "_id",idField.getText().toString());
    editor.putString(getKey() + "_ip",ipField.getText().toString());
}

address_dialog.xml:

<TextView
    android:text="Insert IP address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/hostIP" />

<TextView
    android:text="Insert identifier"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/hostID" />

  • 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-05-21T17:25:40+00:00Added an answer on May 21, 2026 at 5:25 pm

    Ok I found it out myself. Well, I still do not know what caused the error, but I did a lot of changes to the layout and code and suddenly it just worked. I tried to revert to the code that I posted here, but I cannot reproduce the error. Im posting my working code, so anybody who runs into this problem, may use it.

    An admin may also choose to delete this post, as it may be not possible to reproduce the error.

    Here is the layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView
        android:text="Insert IP address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/AddressBox" />
    
    <TextView
        android:text="Insert identifier"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/HostnameBox" />
    </LinearLayout>
    

    and the AddressDialogPreference.java:

    public class AddressDialogPreference extends DialogPreference {
    
    private EditText ipBox;
    private EditText hostBox;
    
    public AddressDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        setDialogLayoutResource(R.layout.address_dialog);
    }
    
    @Override
    protected void onBindDialogView(View view) {
    
        ipBox = (EditText) view.findViewById(R.id.AddressBox);
        hostBox = (EditText) view.findViewById(R.id.HostnameBox);
    
        SharedPreferences pref = getSharedPreferences();
    
        hostBox.setText(pref.getString(getKey() + "_host","ExampleHostname"));
        ipBox.setText(pref.getString(getKey() + "_ip","192.168.1.1"));
    
        super.onBindDialogView(view);
    }
    
    @Override
    protected void onDialogClosed(boolean positiveResult) {
    
        if(!positiveResult)
            return;
    
        SharedPreferences.Editor editor = getEditor();
        editor.putString(getKey() + "_host",hostBox.getText().toString());
        editor.putString(getKey() + "_ip",ipBox.getText().toString());
        editor.commit();
    
        super.onDialogClosed(positiveResult);
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to an Android platform and currently trying figure out why things
Very new to Android and trying to install a Android project from an SVN
I am very new to android and I am developing this app which allows
I am very new to android development and have been trying to draw a
I am very new to Android. I am trying to build inflate a context
I'm very new to Android programming, so this is probably something pretty basic. I
I'm very new to Android so I've been working primarily with information from the
I'm very new to Android development and just started to study. What I'm trying
I am very new to java and trying to develop an Android app called
I am very much new to android and trying to learn various techniques including

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.