zoukankan      html  css  js  c++  java
  • Android Spinner In Toolbar

    As the title of the post suggest in this tutorial we will see how to have spinner widget inside the toolbar in the previous series of tutorial we have seen many example on how to set up the android spinner widget and also we have seen how to have android material design toolbar , in this tutorial we will see the combination of both .

    String Constant

    Lets have Array of string constant which will be set to spinner

    file : string.xml

    <resources>
    
    <string name="app_name">SpinnerToolbar</string>
    
    <string name="action_settings">Settings</string>
    
    <!-- Category -->
    
    <string-array name="category">
    
    <item>Top News</item>
    
    <item>Business</item>
    
    <item>Politics</item>
    
    <item>Sports</item>
    
    <item>Technology</item>
    
    <item>Movies</item>
    
    </string-array>
    
    </resources>
    XML Layout

    Create a layout/activity_main.xml file which contains Toolbar widget.

    file : activity_main.xml

    <LinearLayout
    
    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"
    
    tools:context=".MainActivity">
    
    <android.support.v7.widget.Toolbar
    
    android:id="@+id/toolbar"
    
    android:layout_width="match_parent"
    
    android:layout_height="wrap_content"
    
    android:background="?attr/colorPrimary"
    
    android:minHeight="?attr/actionBarSize" />
    
    </LinearLayout>

    create a spinner_dropdown_item.xml which contains TextView which represents view for each items of spinner .

    file : spinner_dropdown_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    
    android:id="@android:id/text1"
    
    android:layout_width="match_parent"
    
    android:layout_height="wrap_content"
    
    android:background="@color/colorPrimary"
    
    android:gravity="center_vertical"
    
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    
    android:paddingLeft="12dp"
    
    android:paddingRight="12dp"
    
    android:textAppearance="?android:attr/textAppearanceListItemSmall" />

     

    MainActivity

    Create a class MainActivity which extends AppCompatActivity , now inside onCreate() add spinner to toolbar dyanamically and also set onItemSelectListener.

    file : MainActivity.java

    package com.tutorialsbuzz.spinnertoolbar;
    
    import android.support.v7.app.ActionBarActivity;
    
    import android.os.Bundle;
    
    import android.support.v7.app.AppCompatActivity;
    
    import android.support.v7.widget.Toolbar;
    
    import android.view.View;
    
    import android.widget.AdapterView;
    
    import android.widget.ArrayAdapter;
    
    import android.widget.Spinner;
    
    import android.widget.SpinnerAdapter;
    
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
    private Toolbar toolbar=null;
    
    private String[] category=null;
    
    @Override
    
    protected void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.activity_main);
    
    category = getResources().getStringArray(R.array.category);
    
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    
    setSupportActionBar(toolbar);
    
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    
    toolbar.setLogo(R.drawable.ic_drawer);
    
    SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.category, R.layout.spinner_dropdown_item);
    
    Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
    
    navigationSpinner.setAdapter(spinnerAdapter);
    
    toolbar.addView(navigationSpinner, 0);
    
    navigationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
    @Override
    
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
    Toast.makeText(MainActivity.this,
    
    "you selected: " + category[position],
    
    Toast.LENGTH_SHORT).show();
    
    }
    
    @Override
    
    public void onNothingSelected(AdapterView<?> parent) {
    
    }
    
    });
    
    }
    
    }
  • 相关阅读:
    来了!GitHub for mobile 发布!iOS beta 版已来,Android 版即将发布
    五角场之殇。曾与张江、漕河泾、紫竹齐名。如今,上海四大IT科技园是否还在?
    Visual Studio Online 的 FAQ:iPad 支持、自托管环境、Web 版 VS Code、Azure 账号等
    VS Code 1.40 发布!可自行搭建 Web 版 VS Code!
    Visual Studio Online,带来四种开发模式,未来已来。
    基于七牛云对象存储,搭建一个自己专属的极简Web图床应用(手摸手的注释讲解核心部分的实现原理)
    css伪元素::before与::after使用基础示例
    gcc生成静态链接库与动态链接库步骤,并链接生成可执行文件的简单示例
    shell求水仙花数
    shell实现简单的数组排序
  • 原文地址:https://www.cnblogs.com/jasonkent27/p/5133396.html
Copyright © 2011-2022 走看看