1. Check out our companion app, Forums for Android! Download from Google Play

Changing Button Text Rrogrammatically Not Working

Discussion in 'Application Development' started by ShatterStar, Apr 19, 2017.

  1. ShatterStar

    ShatterStar Member
    Thread Starter
    26

    Dec 22, 2016
    14
    1
    26
    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

    Code (Java):
    1. package com.example.myapplication2017;
    2.  
    3.         import android.app.Dialog;
    4.         import android.app.DialogFragment;
    5.         import android.content.Context;
    6.         import android.os.Bundle;
    7.         import android.support.v7.app.AlertDialog;
    8.         import android.util.Log;
    9.         import android.view.LayoutInflater;
    10.         import android.view.View;
    11.         import android.app.DatePickerDialog;
    12.         import android.view.ViewDebug;
    13.         import android.widget.Button;
    14.         import android.widget.DatePicker;
    15.         import android.widget.TextView;
    16.         import android.widget.Toast;
    17.  
    18.         import java.util.Calendar;
    19.         import java.util.zip.Inflater;
    20.  
    21. public class DateDialog extends DialogFragment {
    22.  
    23.     private View dialogView;
    24.     private int year, month, day;
    25.     private Calendar calendar;
    26.  
    27.     @Override
    28.     public Dialog onCreateDialog(Bundle savedInstanceState){
    29.        
    30.  
    31.         calendar = Calendar.getInstance();
    32.         year = calendar.get(Calendar.YEAR);
    33.         month = calendar.get(Calendar.MONTH) + 1;
    34.         day = calendar.get(Calendar.DAY_OF_MONTH);
    35.  
    36.         return new DatePickerDialog(getActivity(), dpListener, year, month, day);
    37.     }
    38.  
    39.     private DatePickerDialog.OnDateSetListener dpListener = new DatePickerDialog.OnDateSetListener() {
    40.         @Override
    41.         public void onDateSet(DatePicker arg0, int year, int month, int day) {
    42.         // TODO Auto-generated method stub
    43.         // arg1 = year
    44.         // arg2 = month
    45.         // arg3 = day
    46.  
    47.         Bundle bundle = new Bundle();
    48.         bundle.putInt("selectedYear", year);
    49.         bundle.putInt("selectedMonth", month);
    50.         bundle.putInt("selectedDay", day);
    51.  
    52.             Log.i("Day = ", Integer.toString(day));
    53.             Log.i("Month = ", Integer.toString(month + 1));
    54.             Log.i("Yesr = ", Integer.toString(year));
    55.  
    56.  
    57.  
    58.             LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    59.             View activity1View = inflater.inflate(R.layout.activity_1, null);
    60.             TextView textViewDateSelected = (TextView) activity1View.findViewById(R.id.textViewDateSelected);
    61.             textViewDateSelected.setText(Integer.toString(day) + "/" + Integer.toString(month + 1) + "/" + Integer.toString(year));
    62.  
    63.             try {
    64.                 LayoutInflater inflaterActivity3 = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    65.                 View viewActivity3 = inflaterActivity3.inflate(R.layout.activity_3, null);
    66.                 Button btnSendMsgOn = (Button) viewActivity3.findViewById(R.id.button4);
    67.                 //btnSendMsgOn.setText(Integer.toString(day) + "/" + Integer.toString(month + 1) + "/" + Integer.toString(year));
    68.                 btnSendMsgOn.setText(bundle.getInt("selectedYear") + "/" + bundle.getInt("selectedMonth") + "/" + bundle.getInt("selectedDay"));
    69.             }
    70.             catch (Exception ex ){
    71.                 Log.i("Button error msg", ex.getMessage());
    72.             }
    73.  
    74.  
    75.         }
    76.     };
    77.  
    78. }
    79.  
     

    Advertisement

  2. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,822
    4,974
    738
    Male
    Software developer
    UK
    What's not working? Please describe the problem.
     
  3. ShatterStar

    ShatterStar Member
    Thread Starter
    26

    Dec 22, 2016
    14
    1
    26
    To be more precise than above, the button setText is not working, the button is located on another view which I get by using a LayoutInflater
     
  4. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,822
    4,974
    738
    Male
    Software developer
    UK
    That's not more precise. The term 'Not working' is vague. You have to describe exactly what's not working.
     
  5. ShatterStar

    ShatterStar Member
    Thread Starter
    26

    Dec 22, 2016
    14
    1
    26
    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?
     
  6. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,822
    4,974
    738
    Male
    Software developer
    UK
    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.
     
  7. Unforgiven

    Unforgiven -.. --- - / -.. .- ... ....
    Moderator
    4,238

    Jun 23, 2010
    33,218
    32,559
    4,238
    Male
    Douglas, MA
    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. :)
     
    kate, codesplice and LV426 like this.
  8. ShatterStar

    ShatterStar Member
    Thread Starter
    26

    Dec 22, 2016
    14
    1
    26
    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!
     
    Unforgiven likes this.
  9. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,822
    4,974
    738
    Male
    Software developer
    UK
    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?
     
    El Presidente and Unforgiven like this.
  10. ShatterStar

    ShatterStar Member
    Thread Starter
    26

    Dec 22, 2016
    14
    1
    26
    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"
     
  11. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,822
    4,974
    738
    Male
    Software developer
    UK
    Can you please explain what didn't work about that line of code?
     
  12. ShatterStar

    ShatterStar Member
    Thread Starter
    26

    Dec 22, 2016
    14
    1
    26
    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
     
  13. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,822
    4,974
    738
    Male
    Software developer
    UK
    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.
     
    Unforgiven likes this.
  14. LV426

    LV426 Illegitimi non carborundum
    Moderator
    738

    Oct 16, 2015
    3,822
    4,974
    738
    Male
    Software developer
    UK
    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.
     
    #14 LV426, Apr 21, 2017
    Last edited: Apr 21, 2017
    Unforgiven likes this.

Share This Page

Loading...