Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with custom dialog #62

Open
srimel1 opened this issue Aug 1, 2020 · 1 comment
Open

Help with custom dialog #62

srimel1 opened this issue Aug 1, 2020 · 1 comment

Comments

@srimel1
Copy link

srimel1 commented Aug 1, 2020

i am trying to use your custom dialog, but i am having a problem. I cannot access the EditText strings, every time i push it to the database it shows up as an empty string like this "". Will you please help? There is no problem with passing hardcoded strings to the database, the only problem is with converting edittext to string and passing it to the database, it just comes up like this "". I think it is something wrong with how i configured my view, i had to guess because i could not find any examples of the custom dialog. here is my code

public class MainActivity extends AppCompatActivity {

    private static final int ID_TEXT_INPUT_DIALOG = R.id.fab;
    private String mUsername;
    private String mPhotoUrl;
    private SharedPreferences mSharedPreferences;
    private GoogleSignInClient mSignInClient;
    private static final String MESSAGE_URL = "http://pantryapp.firebase.google.com/message/";
    public static final String ANONYMOUS = "anonymous";
    public static final String FOODS_CHILD = "Pantry2";
    private DrawerLayout mDrawerLayout;
    private EditText mName, mQuantity, mLifecycle;

    // Firebase instance variables
    private FirebaseAuth mFirebaseAuth;
    private FirebaseUser mFirebaseUser;
    private DatabaseReference mFirebaseDatabaseReference;
    private View PrivateChatsView;


    FirebaseDatabase database = FirebaseDatabase.getInstance();
    private DatabaseReference mRef = database.getReference("foods");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        //remove below if it breaks
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

//         Set default username as anonymous.
        mUsername = ANONYMOUS;

        // Initialize Firebase Auth
        mFirebaseAuth = FirebaseAuth.getInstance();
        mFirebaseUser = mFirebaseAuth.getCurrentUser();
        if (mFirebaseUser == null) {
            // Not signed in, launch the Sign In activity
            startActivity(new Intent(this, SignInActivity.class));
            finish();
            return;
        } else {
            mUsername = mFirebaseUser.getDisplayName();
            if (mFirebaseUser.getPhotoUrl() != null) {
                mPhotoUrl = mFirebaseUser.getPhotoUrl().toString();
            }
        }


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);

        //new child entries
        mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);
        }

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {


                //this is the one with all three fields that works.
                showEditDialog();

            }
        });

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void showTextInputDialog(Bundle savedInstanceState) {
        new LovelyTextInputDialog(this, R.style.EditTextTintTheme)
                .setTopColorRes(R.color.PINK)
                .setTitle(R.string.text_input_title)
                .setMessage(R.string.text_input_message)
                .setIcon(R.drawable.ic_forum)
                .setInstanceStateHandler(ID_TEXT_INPUT_DIALOG, new LovelySaveStateHandler())
                .setConfirmButton(android.R.string.ok, new LovelyTextInputDialog.OnTextInputConfirmListener() {
                    @Override
                    public void onTextInputConfirmed(String text) {
                        Toast.makeText(MainActivity.this,"Added "+ text, Toast.LENGTH_SHORT).show();

                    }
                })

                .setSavedInstanceState(savedInstanceState)
                .show();
    }

    private void showEditDialog(){

        final Context context = this;
        final LovelyCustomDialog mDialog = new LovelyCustomDialog(context);

        mDialog.setView(R.layout.item_donate_option);
        LayoutInflater inflater = this.getLayoutInflater();

        mDialog.setTopColorRes(R.color.PINK);
        mDialog.setTitle(R.string.text_input_title);

        mDialog.setInstanceStateHandler(ID_TEXT_INPUT_DIALOG, new LovelySaveStateHandler());
        mDialog.show();
        mDialog.setListener(R.id.ld_btn_confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                final View v = getLayoutInflater().inflate(R.layout.item_donate_option, null);
                final EditText name = (EditText) v.findViewById(R.id.item_name);
                final EditText quantity = (EditText) v.findViewById(R.id.item_quantity);
                final EditText lifecycle = (EditText) v.findViewById(R.id.item_lifecycle);

                String mName = name.getText().toString().trim();
                String mQuantity = quantity.getText().toString().trim();
                String mLifecycle = lifecycle.getText().toString().trim();
                String id = "1";

                Foods food = new Foods(mName, mQuantity, mLifecycle);

                String lemon = "lemon";
                String quant = "3";
                String life = "5";

                DatabaseReference foodRef = mRef.child("Pantry");
                DatabaseReference newFoodRef = foodRef.push();
                newFoodRef.setValue(food);
                mRef.child("Pantry").setValue(new Foods(lemon, quant, life));



                Snackbar.make(view, "Added a " + food.getmName(), Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

 public void writeNewFood(String name, String quantity, String lifecycle) {
        Foods foods = new Foods(name, quantity, lifecycle);
        mRef.child(name).setValue(foods);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sample_actions, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        switch (AppCompatDelegate.getDefaultNightMode()) {
            case AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM:
                menu.findItem(R.id.menu_night_mode_system).setChecked(true);
                break;
            case AppCompatDelegate.MODE_NIGHT_AUTO:
                menu.findItem(R.id.menu_night_mode_auto).setChecked(true);
                break;
            case AppCompatDelegate.MODE_NIGHT_YES:
                menu.findItem(R.id.menu_night_mode_night).setChecked(true);
                break;
            case AppCompatDelegate.MODE_NIGHT_NO:
                menu.findItem(R.id.menu_night_mode_day).setChecked(true);
                break;
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.sign_out_menu:
                mFirebaseAuth.signOut();
                mSignInClient.signOut();
                mUsername = ANONYMOUS;
                startActivity(new Intent(this, SignInActivity.class));
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                return true;
            case R.id.menu_night_mode_system:
                setNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                break;
            case R.id.menu_night_mode_day:
                setNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                break;
            case R.id.menu_night_mode_night:
                setNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                break;
            case R.id.menu_night_mode_auto:
                setNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
                break;


        }
        return super.onOptionsItemSelected(item);
    }

And here is my layout file

<?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"
    >

    <include layout="@layout/view_color_area" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/texts">

            <Space
                android:layout_width="wrap_content"
                android:layout_height="12dp" />

            <include layout="@layout/view_title_and_message" />



                <EditText
                    android:id="@+id/item_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:ems="20"
                    android:hint="@string/food_name"
                    android:padding="22dp" />


                <EditText
                    android:id="@+id/item_quantity"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:ems="20"
                    android:hint="@string/item_quantity"
                    android:padding="22dp" />


                <EditText
                    android:id="@+id/item_lifecycle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:ems="20"
                    android:hint="@string/item_lifecycle_in_days"
                    android:padding="22dp" />




            <Space
                android:layout_width="wrap_content"
                android:layout_height="8dp" />

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:orientation="horizontal">


        <Button
            android:id="@+id/ld_btn_confirm"
            style="@style/DialogButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="@android:string/ok"/>

    </LinearLayout>

</LinearLayout>

Please can you help me i have been working on this for days. Its my first android project and i really need to figure this out. Thank you for your time, You are my last hope.

@kl3jvi
Copy link

kl3jvi commented Apr 16, 2021

I managed to solve this for anyone that is having this issue.
First Inflate your custom View than pass the view inflated to the .setView() method, dont pass your layout R.layout ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants