programmerJavaPL programmerJavaPL - 1 month ago 11
Android Question

How do I add Spinner without extending Activity

I have a class that extends the Fragment. I want to add a Spinner but he needs to expand Activity. Can it be done differently?

The following should work if I expanded a class Activity but I can not do in this case.

public class Parameters extends Fragment implements AdapterView.OnItemSelectedListener{

Spinner spinner;
ArrayAdapter<CharSequence> adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.parameters_tab3, container, false);
return rootView;
spinner = (Spinner) findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this.getContext(), R.array.strength_class_of_concrete, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

}


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

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

}
}


.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kaminski.bartek.columnec2.MainActivity$PlaceholderFragment">

<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp"
android:id="@+id/spinner" />
</RelativeLayout>


.

<resources>
<string name="app_name">Column EC2</string>
<string name="action_settings">Settings</string>

<string-array name="strength_class_of_concrete">
<item>C12/15</item>
<item>C16/20</item>
<item>C20/25</item>
<item>C25/30</item>
<item>C30/37</item>
<item>C35/45</item>
<item>C40/50</item>
<item>C45/55</item>
<item>C50/60</item>
<item>C55/67</item>
<item>C60/75</item>
</string-array>
</resources>

Answer

You are exiting the function before it sets up spinner. Write return rootView after setting spinner up. Like this

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.parameters_tab3, container, false);
    spinner = (Spinner) rootView.findViewById(R.id.spinner);
    adapter = ArrayAdapter.createFromResource(this.getContext(), R.array.strength_class_of_concrete, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    return rootView;
}

EDIT

If you are working with fragments, then view is inflated in onCreate. Now you should find views in that inflated view. Like

spinner = (Spinner) rootView.findViewById(R.id.spinner);
Comments