I want to programmatically add a TextView and other widgets. Here is how I do it at the moment (everything works):

 public void AddTextInput(View view) { // Указываем через ID контейнерный элемент, куда будем добавлять виджет LinearLayout AddItemContainer = (LinearLayout) findViewById(R.id.AddItemContainer); // Устанавливаем разметы виджета LinearLayout.LayoutParams AddedItemsSizing = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); EditText TextInputA = new EditText(this); // Создаём объект EditText TextInputA.setLayoutParams(AddedItemsSizing); // Задаём ему размеры TextInputA.setHint(R.string.TextInputAValue); // Устанавливаем параметры AddItemContainer.addView(TextInputA); // Добавляем виджет } 

This method adds only a text input field, but now I want to create a method that adds other widgets (via helper methods). Here is my wrong code:

 public void AddItem(int Item_ID){ // Указываем через ID контейнерный элемент, куда будем добавлять виджет LinearLayout AddItemContainer = (LinearLayout) findViewById(R.id.AddItemContainer); // Устанавливаем разметы виджета LinearLayout.LayoutParams AddedItemsSizing = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); switch (Item_ID){ case 1: AddTextInput(); break; case 2: AddPenInput(); break; case 3: AddPhoto(); break; case 4: AddVideo(); break; case 5: AddSoundRec(); break; } } public void AddTextInput(View view) { EditText TextInputA = new EditText(this); // Создаём объект EditText TextInputA.setLayoutParams(AddedItemsSizing); // Задаём ему размеры TextInputA.setHint(R.string.TextInputAValue); // Устанавливаем параметры AddItemContainer.addView(TextInputA); // Добавляем виджет } 

Now AddTextInput is an auxiliary method that is called from the main AddItem . The size settings and all widgets will be the same, so I brought them to the main method.

So the questions are:

  • How to make AddItemContainer and AddedItemsSizing available in AddTextInput and other helper methods?
  • In the Switch block, I incorrectly call helper methods. What exactly is incorrect? (I also tried AddTextInput(View view) and AddTextInput(view) )
  • The AddItem method has an argument - the widget ID. Did I set the OnClick method OnClick in the XML markup?

enter image description here

Closed due to the fact that the issue is too general a participant Nick Volynkin Mar 2 '17 at 4:10 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    According to the rules of this resource, the question should contain one specific problem, the solution of which (ideally) can help other people with a similar problem. In the current form, your question does not fit the goals of this resource. I recommend to reformulate it and split it into several questions. - pavlofff
  • Replaced this question with another one . - Bokov Gleb
  • You can not delete it because there is an answer. But this is not a problem, let it remain) - Nick Volynkin

1 answer 1

  1. Pass dimensions as a method parameter (approx. AddTextInput (View view, float width, float height)).
  2. I did not quite understand why you are passing the View parameter in your "helper method". Example, there is a method AddTextInput (float width, float height), then it will be called like this: AddTextInput (WRAP_CONTENT, WRAP_CONTENT).
  3. In the markup, you handle clicking on the container itself. If you want to handle clicks on a specific view, you need to define an onClick method in it. Method - setOnClickListener