0

getting the following error Type mismatch: cannot convert from android.support.v7.app.ActionBar.LayoutParams to android.app.ActionBar.LayoutParams

LayoutInflater inflater = (LayoutInflater) getActionBar().getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);

            final View spinnerView = inflater.inflate(R.layout.layout_spinner, null);
            Spinner spinner = (Spinner) spinnerView.findViewById(R.id.my_spinner);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Location, R.layout.spinner_item);
            adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
            spinner.setAdapter(adapter);

//          spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//              @Override
//              public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                  // Do whatever you want with your selected item. You can get it as: parent.getItemAtPosition(position); 
//              }
//
//              @Override
//              public void onNothingSelected(AdapterView<?> parent) {}
//          });

            getActionBar().setIcon(getResources().getDrawable(R.drawable.loc));//set your actionbar logo
            getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE );

            LayoutParams layoutParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParams.gravity = Gravity.RIGHT; // set your layout's gravity to 'right'
            getActionBar().setCustomView(spinnerView, layoutParams); //place your layout on the actionbar

i am getting error at new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);

please help...i am not able to solve the error as i am new to android

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
anonymous
  • 321
  • 4
  • 16
  • you have imported wrong line change `import android.app.ActionBar.LayoutParams` to `import android.support.v7.app.ActionBar.LayoutParams` – SweetWisher ツ Oct 28 '14 at 07:17
  • The method setCustomView(android.view.View, android.app.ActionBar.LayoutParams) in the type ActionBar is not applicable for the arguments (android.view.View, android.support.v7.app.ActionBar.LayoutParams) now i am getting this error – anonymous Oct 28 '14 at 07:23
  • thanks i got it ..can u please tell me how to add the value of a variable ny default to the spinner on page load – anonymous Oct 28 '14 at 09:14

2 Answers2

1

You have to import correct LayoutParams

Remove :

import android.app.ActionBar.LayoutParams 

Add :

import android.support.v7.app.ActionBar.LayoutParams 

If you still get the exception, do Vice-versa

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
  • The method setCustomView(android.view.View, android.app.ActionBar.LayoutParams) in the type ActionBar is not applicable for the arguments (android.view.View, android.support.v7.app.ActionBar.LayoutParams)...now i am getting this error – anonymous Oct 28 '14 at 07:24
  • are you using support lib ? then do vice-versa and check – SweetWisher ツ Oct 28 '14 at 07:25
  • thanks i got it ..can u please tell me how to add the value of a variable ny default to the spinner on page load – anonymous Oct 28 '14 at 09:15
  • can you please tell me how to add a autocomplete text view in place of spinner in the action bar – anonymous Oct 28 '14 at 09:30
1

If you are using support jar then change

import android.app.ActionBar.LayoutParams

to

import android.support.v7.app.ActionBar.LayoutParams

and if not then vice-versa.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • now i am getting this error The method setCustomView(android.view.View, android.app.ActionBar.LayoutParams) in the type ActionBar is not applicable for the arguments (android.view.View, android.support.v7.app.ActionBar.LayoutParams) – anonymous Oct 28 '14 at 07:23
  • You need to do as suggested in my answer in whole of your application. Changing it at one place won't work because passing argument is of one type and receiving is of another type. Do you need to use support jar in your application for backward compatibility? – Rohit5k2 Oct 28 '14 at 07:25
  • thanks i got it ..can u please tell me how to add the value of a variable ny default to the spinner on page load – anonymous Oct 28 '14 at 09:14
  • What is your android:spinnerMode in xml file? – Rohit5k2 Oct 28 '14 at 09:22
  • can you please tell me how to add a autocomplete text view in place of spinner in the action bar – anonymous Oct 28 '14 at 09:30
  • If android:spinnerMode is "dialog" than do this android:prompt="value" and if its "dropdown" then do this `spinner.setSelection(index)` where index is the position of item to be set as default in the adapter – Rohit5k2 Oct 28 '14 at 09:30
  • thanks but can you tell me how to add autocompletetext view in place of spinner – anonymous Oct 28 '14 at 09:41
  • Check out this http://stackoverflow.com/questions/15804805/android-actionbar-searchview-as-autocomplete – Rohit5k2 Oct 28 '14 at 09:45
  • thanku soo much...how to add a initial default value to this textview can u please help me out – anonymous Oct 28 '14 at 09:50
  • Post a new question for this with details of what have you dont so far because this is going to be off topic now. – Rohit5k2 Oct 28 '14 at 09:59