problems implemented BottonBar

1

Hi, I'm trying to implement a BottonBar , it's already in my xml file it shows perfectly in the activity the problem is. To add the functions android studio does not recognize me setOnNavigationItemSelectedListener

This is the code.

public class Centimeters extends AppCompatActivity {

    private  BottomNavigationItemView bottomNavigationView;
    TextView text1 , text2 , text3 ;
    EditText editText;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_centimeters);

       BottomNavigationItemView bottomNavigationItemView = (BottomNavigationItemView)findViewById(R.id.bottom_navigation);
        bottomNavigationItemView.setOnNavigationItemSelectedListener (new BottomNavigationItemView())



        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getSupportActionBar().hide();


        text1 = (TextView)findViewById(R.id.tex1centimeters);
        text2 = (TextView)findViewById(R.id.tex2centimeters);
        text3 = (TextView)findViewById(R.id.tex1centimeters);



        final DecimalFormat decimales = new DecimalFormat("0.00"); /** la cantidad de digitos decimales que se muestra */




        final EditText editText = (EditText)findViewById(R.id.editcentimeters);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!(editText.getText().toString()).isEmpty()){

                    double valor = Double.parseDouble(editText.getText().toString());



                    if (valor >= 0 ){

                        double resu1 = valor / 3.48; /**centimeters to feet formula */


                        text1.setText(decimales.format(resu1));


                        double resu2= valor / 2.54 ;/**centimeters to inches formula  */

                        text2.setText(decimales.format(resu2));



                        double resu3 = valor*10;/**centimeter to mm formula*/

                        text3.setText(decimales.format(resu3));






                    }

                    else {

                        text1.setText("");
                        text2.setText("");
                        text3.setText("");
                    }
                }




            }
        });

    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.liantonypozo.calculosmatematicos3"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })




    compile 'com.android.support:appcompat-v7:25.3.0'
    compile 'com.android.support:design:25.3.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
    compile 'com.android.support:support-v4:25.3.0'

    testCompile 'junit:junit:4.12'
}
    
asked by Liantony Pozo 01.05.2017 в 01:32
source

2 answers

1

This is incorrect since you are defining a BottomNavigationItemView() method ??? and not a listener OnNavigationItemSelectedListener :

bottomNavigationItemView.setOnNavigationItemSelectedListener (new BottomNavigationItemView())

The proper way is:

    BottomNavigationItemView bottomNavigationItemView = (BottomNavigationItemView)findViewById(R.id.bottom_navigation);

    bottomNavigationItemView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                  public boolean onNavigationItemSelected(MenuItem item) {

                    /**** Ejemplo de como realizar una acción al seleccionar un item mediante su id****/
                   switch (item.getItemId()) {
                      case R.id.navigation_home:
                        //Acción
                      return true;
                  case R.id.navigation_dashboard:
                       //Acción
                      return true;
                  case R.id.navigation_notifications:
                      //Acción
                      return true;
                   }
                  /************************/




                return false ;
       }
     });

we must remember to add the dependency of the design library inside the file build.gradle , remembering that this new widget is contained from the version 25.0.0 :

compile 'com.android.support:appcompat-v7:25.0.0' 
compile ‘com.android.support:design:25.0.0’

and the view define it as:

<android.support.design.widget.BottomNavigationView

    
answered by 01.05.2017 в 01:47
0

Your code should be something like that

bottomBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            return false;
        }
    });
    
answered by 14.12.2017 в 23:58