Problem adding spinner to my form

Asked

Viewed 80 times

0

I am developing a form where I want to add the spinner, but when I add it in the xml file, nothing appears on the screen, only the box marked as you can see below:erro

I already added my spinner options in STRINGS. Here’s my xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@android:color/background_light">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/nome1"
        android:textColorHint="#9b9a9a"
        android:hint="*Nome:"
        android:textSize="14dp"
        android:textColorLink="#050000"
        android:backgroundTint="#020000"
        android:textColor="#050000"
        android:layout_above="@+id/email"
        android:layout_alignLeft="@+id/msg"
        android:layout_alignRight="@+id/msg"
        android:layout_alignEnd="@+id/msg" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColorHint="#9b9a9a"
        android:inputType="textPostalAddress"
        android:ems="10"
        android:id="@+id/email"
        android:hint="*E-mail:"
        android:textSize="14dp"
        android:textColorLink="#050000"
        android:backgroundTint="#020000"
        android:textColor="#050000"
        android:layout_above="@+id/msg"
        android:layout_alignLeft="@+id/msg"
        android:layout_alignStart="@+id/msg"
        android:layout_marginBottom="36dp"
        android:layout_alignRight="@+id/msg"
        android:layout_alignEnd="@+id/msg" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColorHint="#9b9a9a"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/telefone"
        android:hint="Telefone:"
        android:textSize="14dp"
        android:textColorLink="#050000"
        android:backgroundTint="#020000"
        android:textColor="#050000"
        android:layout_above="@+id/nome1"
        android:layout_alignLeft="@+id/msg"
        android:layout_alignRight="@+id/msg"
        android:layout_alignEnd="@+id/msg" />

    <Button
        android:layout_width="85dp"
        android:layout_height="40dp"
        android:text="Enviar"
        android:id="@+id/send"
        android:layout_above="@+id/textView8"
        android:layout_alignRight="@+id/textView8"
        android:layout_alignEnd="@+id/textView8" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColorHint="#9b9a9a"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/endereco"
        android:hint="Endereço:"
        android:textSize="14dp"
        android:textColorLink="#050000"
        android:backgroundTint="#020000"
        android:textColor="#050000"
        android:layout_above="@+id/telefone"
        android:layout_alignLeft="@+id/msg"
        android:layout_alignRight="@+id/msg"
        android:layout_alignEnd="@+id/msg" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="120dp"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="5"
        android:ems="10"
        android:id="@+id/msg"
        android:hint="*Mensagem"
        android:textSize="14dp"
        android:textColorHint="#9b9a9a"
        android:layout_above="@+id/send"
        android:layout_alignRight="@+id/send"
        android:layout_alignEnd="@+id/send"
        android:layout_alignLeft="@+id/textView8"
        android:layout_alignStart="@+id/textView8"
        android:backgroundTint="#020000"
        android:textColor="#050000"
        android:textColorLink="#050000" />

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_below="@+id/imageView"
        android:layout_alignLeft="@+id/endereco"
        android:layout_alignStart="@+id/endereco"
        android:clickable="false"
        android:spinnerMode="dropdown" />

</RelativeLayout>

And here is my class where it is called the form Intent:

public class Reclame extends Cronograma implements AdapterView.OnItemSelectedListener{

    Spinner opcoes;
    Button btnSend;
    EditText Nome;
    String Subject;
    EditText Message;
    EditText Endereco;
    EditText Telefone;
    EditText Email;
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.reclame, container, false);



        Nome = (EditText) view.findViewById(R.id.nome1);
        Subject = "Reclamação";
        Message = (EditText) view.findViewById(R.id.msg);
        Endereco = (EditText) view.findViewById(R.id.endereco);
        Telefone = (EditText) view.findViewById(R.id.telefone);
        Email = (EditText) view.findViewById(R.id.email);
        btnSend = (Button) view.findViewById(R.id.send);
        opcoes = (Spinner)view.findViewById(R.id.spinner);
        opcoes.setOnItemSelectedListener(this);


        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ArrayAdapter adapter = ArrayAdapter.createFromResource(v.getContext(), R.array.option, android.R.layout.simple_spinner_item);
                opcoes.setAdapter(adapter);


                String nome = Nome.getText().toString();
                String message = Message.getText().toString();
                String matricula = Endereco.getText().toString();
                String telefone = Telefone.getText().toString();
                String ema = Email.getText().toString();
                Intent email = new Intent(Intent.ACTION_SEND);
                email.putExtra(Intent.EXTRA_EMAIL, new String[]{"meu_email"});
                email.putExtra(Intent.EXTRA_SUBJECT, Subject);
                email.putExtra(Intent.EXTRA_TEXT, "Nome: " +nome +'\n'+  "Endereço: "+matricula + '\n'+ "Telefone: "+telefone + '\n'+ "E-mail: "+ema + '\n'+ '\n'+message);


                email.setType("message/rfc822");
                startActivity(Intent.createChooser(email, "Selecione o serviço de e-mail:"));
            }
        });
        return view;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        TextView mtext = (TextView) view;
        //Toast.makeText(this, "Você selecionou: "+mtext.getText(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

What am I doing wrong, that does not appear the spinner?

1 answer

1


Pass the code that initializes the spinner Adapter into the onCreateView()

First get a reference to a Context to use in ArrayAdapter.createFromResource():

private Context contex;
@Override
public void onAttach(Activity activity) {

    context = activity; //Guarda context.

    super.onAttach(activity);
}

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.reclame, container, false);

    ......
    opcoes = (Spinner)view.findViewById(R.id.spinner);

    ArrayAdapter adapter = ArrayAdapter.createFromResource(context, R.array.option, android.R.layout.simple_spinner_item);
    opcoes.setAdapter(adapter);

    opcoes.setOnItemSelectedListener(this);

    .......
}
  • I did what you suggested and showed up, only the spinner still doesn’t show up. what I find strange is that when I add it in xml it does not show the ITEM 1 box, it just shows the box as you see in the first image.

  • The array option has elements?

  • ramaral I managed to solve the problem, it is adding the spinner in the form, the pro is that the invisible spinner ta, I only found pq when it leaves by clicking on the screen moved down the options of the spinner, but when I choose an option it turns invisible again

  • In the spinner has android:layout_below="@+id/imageView" I don’t see any imageView

  • I removed that line.. and changed my theme and already appeared the options, but when I choose the option, it disappears again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.