How to dynamically add a button to a different format in Android?

I want to pretend. There will be multiple options here, and one option will have multiple buttons. The data will be retrieved from the web service. The whole process must be dynamic.

enter image description here

My code is here and I did it, but I need to do it as shown above.

enter image description here

@Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_option_selection);
    tableLayout = (TableLayout) findViewById(R.id.linear);
    int itemId = getIntent().getIntExtra("itemId", 0);
    getGroupName(itemId);
}

private void getGroupName( final int itemId) {
    StringRequest stringrequest = new StringRequest(Method.GET,
            getString(R.string.base_url)+"ItemsOptions/GetGroupByItemID?itemID="
                    + itemId, new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {                   
                    try {
                        jsonArray = new JSONArray(response);


                        for (int i = 0; i < jsonArray.length(); i++) {
                             groupId = jsonArray.getJSONObject(i)
                                    .getString("GroupName");
                             showOption(itemId,groupId);

                        }
                        //showOption(itemId,groupId);

                    } catch (JSONException e) {

                        e.printStackTrace();
                    }

                }

            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError arg0) {

                }
            });

    AppController.getInstance().addToRequestQueue(stringrequest);

}

protected void showOption(int itemId,String groupName) {
    StringRequest stringrequest = new StringRequest(Method.GET,
            getString(R.string.base_url)+"ItemsOptions/GetAllOptionByItemGroupID?itemID="+itemId+"&groupID="+groupName+"", 
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    try {
                        jsonArray = new JSONArray(response);
                        for (int i = 0; i < jsonArray.length(); i++) {

                             String option = jsonArray.getJSONObject(i)
                                    .getString("OptionsName");

                             if (i % 3 == 0) {
                                    tableRow = new TableRow(
                                            getApplicationContext());
                                    tableLayout.addView(tableRow);
                                }

                                orderBtn = new OrderButton(
                                        getApplicationContext());
                                orderBtn.setId(i);
                                orderBtn.setText(option);
                                orderBtn.setTextColor(Color
                                        .parseColor("#FFFFFF"));
                                orderBtn.setBackgroundResource((R.drawable.selector));
                                tableRow.addView(orderBtn);
                                orderBtn.setOnClickListener(new OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        v.setSelected(true);
                                        enabledButton = orderBtn.getId();
                                        deselectButtons();

                                    }
                                });

                        }

                    } catch (JSONException e) {

                        e.printStackTrace();
                    }

                }

            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError arg0) {

                }
            });

    AppController.getInstance().addToRequestQueue(stringrequest);

      

+3


source to share


1 answer


whateverbuttonclicked.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

  LinearLayout layoutyouwantbuttonin = (LinearLayout)findViewById(R.id.layoutyouwantbuttonin);
  Button brandnewbutton = new Button(NameofActivity.this);
  String bc = whateverbuttonclicked.getText().toString();
  brandnewbutton.setText(bc); 
  layoutyouwantbuttonin.addView(brandnewbutton);

}});

      



0


source







All Articles