Search code examples
androidbuttondynamicfindviewbyid

Dynamically populate a button value android


I have seen lots of example to which one use a if condition or a case statement to programmatically change the conditions of elements...yadda yadda. I need to change the value of a button based on what the user clicks. Below is the code that I currently have.

 Button btnOpenPopup = (Button)findViewById(R.id.polarity);
        btnOpenPopup = (Button) findViewById(R.id.color); 
final Button finalBtnOpenPopup = btnOpenPopup;         

btnOpenPopup.setOnClickListener(new Button.OnClickListener(){CONTINUES TO OTHER FUNCTIONS }

I basically need to know what button was pressed. Then dynamically populate it into findViewById() function. i.e.

btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);

This way by the time it gets to the final Button part of the code it will have the value to which the user clicked on. Code works if I only want to have one button or a page mile deep in different configuration (not ideal).

All the examples I have seen so far are after the user clicks the button (which is what I want) but they name the buttons name statically like above code shows but very much static.

Hope that all makes sense.

UPDATE:

I think I may have confused the situation. Below is the remaining code. Hopefully this will provide context. The btnOpenPopup needs to remain the same as it's used in the call to execute the command for a new window to actually popup. Hopefully this will provide a bit more context for what I'm trying to achieve.

 final Button finalBtnOpenPopup = btnOpenPopup;
        btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.meditationpopup, null);

                //set the title of the popup
                TextView titletext = (TextView) popupView.findViewById(R.id.chakratitle);
                titletext.setText(activityName);

                if (activityName.equals("Root"))
                {
                    switch (arg0.getId())
                    {
                        case R.id.color:
                            //Rename the string so to get the value from the string xml
                            String stringName = activityName.toLowerCase().replaceAll(" ","")+"color";
                            TextView desctext = (TextView) popupView.findViewById(R.id.popupDesc);
                            desctext.setText(getString(getStringResource(getApplicationContext(),stringName)));
                        break;
                        case R.id.polarity:
                            //Rename the string so to get the value from the string xml
                            String polarityString = activityName.toLowerCase().replaceAll(" ","")+"polarity";
                            TextView polarityDesc = (TextView) popupView.findViewById(R.id.popupDesc);
                            //polarityDesc.setText(activityName);
                            polarityDesc.setText(getString(getStringResource(getApplicationContext(),polarityString)));
                        break;
                    }
                }

Solution

  • I think

    Button btnOpenPopup = (Button)findViewById(R.id.polarity);
           btnOpenPopup = (Button) findViewById(R.id.color); 
    

    should be

     Button btnOpenPopupFirst = (Button)findViewById(R.id.polarity);
     Button btnOpenPopupSecond = (Button) findViewById(R.id.color); 
    

    you should declare different different button for diffrerent findviewbyid

    also in my eclipse it is not accepting

    btnOpenPopup.setOnClickListener(new Button.OnClickListener()
    

    instead it works with

    btnOpenPopup.setOnClickListener(new View.OnClickListener() {}
    

    and you need to provide more clear view of what you want to perform

    new thoughts,try doing this:

     btnOpenPopupFirst.setOnClickListener(this);
     btnOpenPopupSecond.setOnClickListener(this);
    

    then option will come on both the above code lines

    The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
    

    choose this

    let MainActivity implement OnClickListener  
    

    then this option will come

    The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)
    

    choose

    add unimplemented method
    

    now

    @Override
    public void onClick(View v)
    

    will be created

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.polarity:
            Toast.makeText(getApplicationContext(), "btnOpenPopupFirst(polarity) is pressed", Toast.LENGTH_SHORT).show();
    //other code to be performed regarding this button
            break;
        case R.id.color:
            Toast.makeText(getApplicationContext(), "btnOpenPopupSecond(color) is pressed", Toast.LENGTH_SHORT).show();
    //other code to be performed regarding this button
        default:
            break;
        }
    }
    

    And post your views after implementing this way.