• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Changing Button Text Rrogrammatically Not Working

ShatterStar

Newbie
Dec 22, 2016
32
2
Hi,

I have a datepicker dialog fragment and on the OnDateSetListener function I am trying to change the text on a button in one of my views but this isn't working for some reason, why is this? Here's my code for my DateDialog.java file

Java:
package com.example.myapplication2017;

        import android.app.Dialog;
        import android.app.DialogFragment;
        import android.content.Context;
        import android.os.Bundle;
        import android.support.v7.app.AlertDialog;
        import android.util.Log;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.app.DatePickerDialog;
        import android.view.ViewDebug;
        import android.widget.Button;
        import android.widget.DatePicker;
        import android.widget.TextView;
        import android.widget.Toast;

        import java.util.Calendar;
        import java.util.zip.Inflater;

public class DateDialog extends DialogFragment {

    private View dialogView;
    private int year, month, day;
    private Calendar calendar;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
       

        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), dpListener, year, month, day);
    }

    private DatePickerDialog.OnDateSetListener dpListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int year, int month, int day) {
        // TODO Auto-generated method stub
        // arg1 = year
        // arg2 = month
        // arg3 = day

        Bundle bundle = new Bundle();
        bundle.putInt("selectedYear", year);
        bundle.putInt("selectedMonth", month);
        bundle.putInt("selectedDay", day);

            Log.i("Day = ", Integer.toString(day));
            Log.i("Month = ", Integer.toString(month + 1));
            Log.i("Yesr = ", Integer.toString(year));



            LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View activity1View = inflater.inflate(R.layout.activity_1, null);
            TextView textViewDateSelected = (TextView) activity1View.findViewById(R.id.textViewDateSelected);
            textViewDateSelected.setText(Integer.toString(day) + "/" + Integer.toString(month + 1) + "/" + Integer.toString(year));

            try {
                LayoutInflater inflaterActivity3 = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View viewActivity3 = inflaterActivity3.inflate(R.layout.activity_3, null);
                Button btnSendMsgOn = (Button) viewActivity3.findViewById(R.id.button4);
                //btnSendMsgOn.setText(Integer.toString(day) + "/" + Integer.toString(month + 1) + "/" + Integer.toString(year));
                btnSendMsgOn.setText(bundle.getInt("selectedYear") + "/" + bundle.getInt("selectedMonth") + "/" + bundle.getInt("selectedDay"));
            }
            catch (Exception ex ){
                Log.i("Button error msg", ex.getMessage());
            }


        }
    };

}
 
That's not more precise. The term 'Not working' is vague. You have to describe exactly what's not working.

Ay guy you're a moderator in this forum and you seem to be having a problem understanding what I am saying or are you just naturally rude? I have even posted my code what more do you want or do you simply not have a good enough command of the English language? How are you even a moderator?
 
Upvote 0
Nothing I've said here is rude. I've only asked for clarification of your problem, in order to offer some help. If you think asking to clarify the statement "Not working" is rude, then please feel free to open a conversation in the forum for private chats with the moderators.

My reply may seem terse, but I offer many responses in this forum, and I often have to keep things short, and to the point.

So to answer your question above about what more is required, then could you to please elaborate on your statement that the "button setText is not working". This statement could have a multitude of possible symptoms. For example:-

1. Is this a compile time problem?
2. Does the application run, but then crash?
3. Are you getting a nullPointerException
4. Does the UI not look like you expected? Can you provide some screenshots

So you can see that a problem statement of "not working" gives very little for readers of this forum to go on. This is why I asked you to clarify. I could have put all this in my first reply, but I thought it unnecessary because any reasonable person would simply clarify it. And besides, this post has now taken me several minutes to type, so if I did that for every single thread in here, explaining in minute detail reasons for asking question clarification, I'd be here all day, and would have little time for anything else.
 
Upvote 0
Ay guy you're a moderator in this forum and you seem to be having a problem understanding what I am saying or are you just naturally rude? I have even posted my code what more do you want or do you simply not have a good enough command of the English language? How are you even a moderator?
Hi,
I can assure you LV is both highly competent in his support for development questions and only attempting to understand your problem so he can offer advice. All of the moderators are members first and help folks where we can in threads just like this. :)
 
Upvote 0
Nothing I've said here is rude. I've only asked for clarification of your problem, in order to offer some help. If you think asking to clarify the statement "Not working" is rude, then please feel free to open a conversation in the forum for private chats with the moderators.

My reply may seem terse, but I offer many responses in this forum, and I often have to keep things short, and to the point.

So to answer your question above about what more is required, then could you to please elaborate on your statement that the "button setText is not working". This statement could have a multitude of possible symptoms. For example:-

1. Is this a compile time problem?
2. Does the application run, but then crash?
3. Are you getting a nullPointerException
4. Does the UI not look like you expected? Can you provide some screenshots

So you can see that a problem statement of "not working" gives very little for readers of this forum to go on. This is why I asked you to clarify. I could have put all this in my first reply, but I thought it unnecessary because any reasonable person would simply clarify it. And besides, this post has now taken me several minutes to type, so if I did that for every single thread in here, explaining in minute detail reasons for asking question clarification, I'd be here all day, and would have little time for anything else.

Right I am going to give this another go, I don't have time to waste I seek answers, to answer your questions
1. Is this a compile time problem? I don't think so as the app wouldn't run if it isn't compiled
2. Does the application run, but then crash? It runs but doesn't crash
3. Are you getting a nullPointerException? No, I checked this while debugging
4. Does the UI not look like you expected? The UI looks exactly as expected except for the text change on the button element

Now I am going into detail as much as I can, I have a button in an activity that when clicked opens a datepicker dialog, once the user selects a date and clicks ok I would like the text on the button to change to the date that the user selected from the datepicker (like my code above) but for some reason this isn't working I would like assistance as to why it isn't working!
 
  • Like
Reactions: Unforgiven
Upvote 0
Why is your code using a Bundle? Bundle objects are for transfer of data between Activities, using an Intent. I don't see any reason to use a Bundle in the code fragment you posted. Why not use the method parameters (year, month, day) directly? In fact I see you have a commented out line which does exactly that. Why did you comment it out, and what was the problem there?
 
Upvote 0
Why is your code using a Bundle? Bundle objects are for transfer of data between Activities, using an Intent. I don't see any reason to use a Bundle in the code fragment you posted. Why not use the method parameters (year, month, day) directly? In fact I see you have a commented out line which does exactly that. Why did you comment it out, and what was the problem there?

I did try it the way you suggested but that commented out line of code didn't work, I then tried the alternate way with the bundle still no joy. What I was doing with the bundle was in the view with the onClick method I was getting the year, month and day values from the bundle and them tried to change the button text in the method but this does this before the dialog opens and I end up with values like this "0/0/0"
 
Upvote 0
The initial text on the button is "Send message on" after clicking the ok button on the datepicker dialog the text should change to the date selected something like "2017/02/05" but it doesn;t change the text on the button

Right, if you'd said that to begin with, and as subsequently requested, we'd have had a clearer picture of what's not working. Thanks for the information.
 
  • Like
Reactions: Unforgiven
Upvote 0
Your approach of inflating the layout of another Activity, in order to set the value of a Button label is wrong. It will never work because any change you make to components after inflation in the Dialog code are transient, and only apply to that specific part of the code. When the layout is inflated in its corresponding Activity, the changes you made to any child components are lost, and the configuration is taken from the XML.

You could store the selected date values as class variables in the DateDialog class, and access them from the Activity containing the Button. I assume that the DateDialog is created from this Activity class, but you have not shown this code.

Another solution would be to pass in the Button component to the DateDialog class, as a constructor parameter. You could then call Button.setText() directly on that object. This is probably the easiest solution.
 
Last edited by a moderator:
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones