Example usage for android.content.res TypedArray length

List of usage examples for android.content.res TypedArray length

Introduction

In this page you can find the example usage for android.content.res TypedArray length.

Prototype

public int length() 

Source Link

Document

Returns the number of values in this array.

Usage

From source file:es.uniovi.imovil.fcrtrainer.highscores.HighscoresFragment.java

private void initializeLevelSpinner() {
    ArrayList<String> levelNames = new ArrayList<String>();

    TypedArray array = getResources().obtainTypedArray(R.array.pref_level_values);

    for (int i = 0; i < array.length(); i++) {
        int defaultId = 0;
        int resourceId = array.getResourceId(i, defaultId);

        String name = getResources().getString(resourceId);
        levelNames.add(name);// w w  w . j  ava2s  . co m
    }

    array.recycle();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,
            levelNames);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    mLevelSpinner = (Spinner) mRootView.findViewById(R.id.spinner_level);
    mLevelSpinner.setAdapter(adapter);

    mLevelSpinner.setOnItemSelectedListener(this);
}

From source file:com.mikecorrigan.trainscorekeeper.Players.java

@SuppressLint("NewApi")
public Players(final Activity context, ViewGroup viewGroup, final JSONArray jsonPlayers) {
    Log.vc(VERBOSE, TAG,/*from w w w  . ja va2s  .  c o m*/
            "ctor: context=" + context + ", viewGroup=" + viewGroup + ", jsonPlayers=" + jsonPlayers);

    viewGroup.removeAllViews();

    Resources resources = context.getResources();

    String[] playerNames = resources.getStringArray(R.array.playerNames);

    TypedArray drawablesArray = resources.obtainTypedArray(R.array.playerDrawables);

    TypedArray colorIdsArray = resources.obtainTypedArray(R.array.playerTextColors);
    int[] colorIds = new int[colorIdsArray.length()];
    for (int i = 0; i < colorIdsArray.length(); i++) {
        colorIds[i] = colorIdsArray.getResourceId(i, -1);
    }

    listeners = new HashSet<Listener>();
    players = new SparseArray<Player>();

    for (int i = 0; i < playerNames.length; i++) {
        PlayerButton toggleButton = new PlayerButton(context);
        toggleButton.setPlayerId(i);
        LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, 1f);
        toggleButton.setLayoutParams(layoutParams);
        Drawable drawable = drawablesArray.getDrawable(i);
        int sdk = android.os.Build.VERSION.SDK_INT;
        if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            toggleButton.setBackgroundDrawable(drawable);
        } else {
            toggleButton.setBackground(drawable);
        }
        toggleButton.setPadding(10, 10, 10, 10);
        toggleButton.setTextColor(resources.getColor(colorIds[i]));
        toggleButton.setOnClickListener(onSelect);

        viewGroup.addView(toggleButton);

        final String name = playerNames[i];

        boolean enabled = true;

        // If the players configuration is available, determine which players are enabled.
        if (jsonPlayers != null) {
            enabled = false;
            for (int j = 0; j < jsonPlayers.length(); j++) {
                String jsonName = jsonPlayers.optString(j, "");
                if (jsonName.equalsIgnoreCase(name)) {
                    enabled = true;
                    break;
                }
            }
        }

        Player player = new Player(toggleButton, i, name, enabled);
        players.put(i, player);
    }

    setSelection(-1);

    drawablesArray.recycle();
    colorIdsArray.recycle();
}

From source file:org.iisgcp.waterwalk.fragment.AllRoutesFragment.java

private List<RowItem> getListChildren(int resId) {
    List<RowItem> rowItems = null;

    TypedArray pois = getResources().obtainTypedArray(resId);

    List<Integer> imageIds = new ArrayList<Integer>();

    rowItems = new ArrayList<RowItem>();

    for (int i = 0; i < pois.length(); i++) {
        TypedArray poi = getResources().obtainTypedArray(pois.getResourceId(i, -1));

        String title = getString(poi.getResourceId(0, -1));
        int imageId = poi.getResourceId(1, -1);

        imageIds.add(imageId);//from   w  ww  . j  ava  2  s  .co  m

        rowItems.add(buildRow(imageId, title, pois.getResourceId(i, -1)));
        poi.recycle();
    }

    pois.recycle();

    return rowItems;
}

From source file:org.iisgcp.waterwalk.fragment.RoutesGridFragment.java

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    List<RowItem> rowItems = null;

    TypedArray routes = getResources().obtainTypedArray(R.array.routes);

    List<Integer> imageIds = new ArrayList<Integer>();

    rowItems = new ArrayList<RowItem>();

    for (int i = 0; i < routes.length(); i++) {
        TypedArray route = getResources().obtainTypedArray(routes.getResourceId(i, -1));

        String title = getString(route.getResourceId(0, -1));
        int imageId = route.getResourceId(1, -1);

        imageIds.add(imageId);//from  ww  w .j  a va 2s  .co  m

        rowItems.add(buildRow(imageId, title, routes.getResourceId(i, -1)));
        route.recycle();
    }

    routes.recycle();

    mAdapter = new RouteGridAdapter(getActivity(), mImageFetcher, rowItems);

    mList.setAdapter(mAdapter);
    mList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            RowItem rowItem = (RowItem) parent.getAdapter().getItem(position);

            Intent intent = new Intent(RoutesGridFragment.this.getActivity(), PointOfInterestActivity.class);
            intent.putExtra(Constants.INTENT_TITLE, rowItem.getTitle());
            intent.putExtra(Constants.INTENT_RES_ID, rowItem.getResId());
            intent.putExtra(Constants.INTENT_TAB, 0);
            intent.putExtra(Constants.INTENT_POSITION, position);
            intent.putExtra(Constants.INTENT_DRAWER_POSITION, 0);
            RoutesGridFragment.this.startActivity(intent);
        }

    });
}

From source file:org.iisgcp.waterwalk.fragment.PointOfInterestGridViewPagerFragment.java

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // Instantiate a ViewPager and a PagerAdapter.
    mAdapter = new GridPagerAdapter(getChildFragmentManager());
    mPager.setAdapter(mAdapter);/*  w  ww .j  ava  2  s  .  c  o  m*/
    mPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {
            mListener.onPageSelected(position);

            String title = null;

            TypedArray routes = PointOfInterestGridViewPagerFragment.this.getResources()
                    .obtainTypedArray(R.array.routes);

            for (int i = 0; i < routes.length(); i++) {
                if (i == position) {
                    int resid = routes.getResourceId(i, -1);
                    TypedArray route = PointOfInterestGridViewPagerFragment.this.getResources()
                            .obtainTypedArray(resid);
                    title = getString(route.getResourceId(0, -1));
                    route.recycle();
                }
            }

            routes.recycle();

            PointOfInterestGridViewPagerFragment.this.getActivity().setTitle(title);
        }

    });
    mPager.setCurrentItem(getArguments().getInt(INTENT_POSITION));
}

From source file:org.iisgcp.waterwalk.fragment.PointOfInterestGridFragment.java

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    List<RowItem> rowItems = null;

    TypedArray route = getResources().obtainTypedArray(getArguments().getInt(Constants.INTENT_RES_ID));
    TypedArray pois = getResources().obtainTypedArray(route.getResourceId(5, -1));
    route.recycle();//ww  w.  ja  va2s . com

    List<Integer> imageIds = new ArrayList<Integer>();

    rowItems = new ArrayList<RowItem>();

    for (int i = 0; i < pois.length(); i++) {
        TypedArray poi = getResources().obtainTypedArray(pois.getResourceId(i, -1));

        String title = getString(poi.getResourceId(0, -1));
        int imageId = poi.getResourceId(1, -1);

        imageIds.add(imageId);

        rowItems.add(buildRow(imageId, title, pois.getResourceId(i, -1)));
        poi.recycle();
    }

    pois.recycle();

    mAdapter = new RouteGridAdapter(getActivity(),
            ((PointOfInterestGridViewPagerFragment) getParentFragment()).getImageFetcher(), rowItems);

    mList.setAdapter(mAdapter);
    mList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            RowItem rowItem = (RowItem) parent.getAdapter().getItem(position);

            Intent intent = new Intent(PointOfInterestGridFragment.this.getActivity(),
                    PointOfInterestDetailActivity.class);
            intent.putExtra(Constants.INTENT_TITLE, rowItem.getTitle());
            intent.putExtra(Constants.INTENT_RES_ID, rowItem.getResId());
            mListener.onItemClicked(intent);
        }

    });
}

From source file:com.eurecalab.eureca.fragments.SettingsFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_settings, container, false);

    reverseColor = (SwitchCompat) rootView.findViewById(R.id.reverseColor);
    apply = (Button) rootView.findViewById(R.id.apply);
    colorSpinner = (Spinner) rootView.findViewById(R.id.colorSpinner);
    signOutButton = (Button) rootView.findViewById(R.id.sign_out_button);
    loggedUser = (TextView) rootView.findViewById(R.id.logged_user);
    appLicense = (TextView) rootView.findViewById(R.id.app_license);
    upgrade = (Button) rootView.findViewById(R.id.upgrade);
    expiresIn = (TextView) rootView.findViewById(R.id.expires_in);

    parent = getActivity();//  ww w .  jav a2  s  .c om

    gs = (GlobalState) parent.getApplication();
    user = gs.getAuthenticatedUser();
    loggedUser.setText(user.getDisplayName());

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail().build();

    mGoogleApiClient = new GoogleApiClient.Builder(parent)
            .enableAutoManage(parent, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();

    signOutButton.setOnClickListener(this);
    upgrade.setOnClickListener(this);

    updateUI();

    TypedArray colorArray = getResources().obtainTypedArray(R.array.theme_colors);
    int[] colors = new int[colorArray.length()];

    int defaultColor = ContextCompat.getColor(getActivity(), R.color.color_primary_red);

    for (int i = 0; i < colors.length; i++) {
        colors[i] = colorArray.getColor(i, defaultColor);
    }

    colorArray.recycle();

    colorSpinnerAdapter = new ColorSpinnerAdapter(getActivity(), colors);

    colorSpinner.setAdapter(colorSpinnerAdapter);

    apply.setOnClickListener(this);

    sharedPreferences = getActivity().getSharedPreferences(getString(R.string.preference_file_key),
            Context.MODE_PRIVATE);
    int color = sharedPreferences.getInt(getString(R.string.saved_color), R.color.color_primary_red);
    boolean reverse = sharedPreferences.getBoolean(getString(R.string.saved_reverse), false);

    int position = -1;
    for (int i = 0; i < colors.length; i++) {
        if (colors[i] == color) {
            position = i;
            break;
        }
    }

    colorSpinner.setSelection(position);

    reverseColor.setChecked(reverse);

    ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IInAppBillingService.Stub.asInterface(service);
        }
    };

    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    getActivity().bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);

    return rootView;
}

From source file:org.iisgcp.waterwalk.fragment.AllRoutesFragment.java

private HashMap<RowItem, List<RowItem>> getListHeaders() {
    LinkedHashMap<RowItem, List<RowItem>> rowItems = null;

    TypedArray routes = getResources().obtainTypedArray(R.array.routes);

    List<Integer> imageIds = new ArrayList<Integer>();

    rowItems = new LinkedHashMap<RowItem, List<RowItem>>();

    for (int i = 0; i < routes.length(); i++) {
        TypedArray route = getResources().obtainTypedArray(routes.getResourceId(i, -1));

        String title = getString(route.getResourceId(0, -1));
        int imageId = route.getResourceId(1, -1);

        imageIds.add(imageId);/* w  ww .ja  va 2s .c o m*/

        rowItems.put(buildRow(imageId, title, routes.getResourceId(i, -1)),
                getListChildren(route.getResourceId(5, -1)));

        route.recycle();
    }

    routes.recycle();

    return rowItems;
}

From source file:org.connectbot.EditHostActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mHostDb = HostDatabase.get(this);
    mPubkeyDb = PubkeyDatabase.get(this);

    mTerminalConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            TerminalManager bound = ((TerminalManager.TerminalBinder) service).getService();
            mBridge = bound.getConnectedBridge(mHost);
        }//from   ww w  . j a  v  a2 s.c  o  m

        public void onServiceDisconnected(ComponentName name) {
            mBridge = null;
        }
    };

    long hostId = getIntent().getLongExtra(EXTRA_EXISTING_HOST_ID, NO_HOST_ID);
    mIsCreating = hostId == NO_HOST_ID;
    mHost = mIsCreating ? null : mHostDb.findHostById(hostId);

    // Note that the lists must be explicitly declared as ArrayLists because Bundle only accepts
    // ArrayLists of Strings.
    ArrayList<String> pubkeyNames = new ArrayList<>();
    ArrayList<String> pubkeyValues = new ArrayList<>();

    // First, add default pubkey names and values (e.g., "use any" and "don't use any").
    TypedArray defaultPubkeyNames = getResources().obtainTypedArray(R.array.list_pubkeyids);
    for (int i = 0; i < defaultPubkeyNames.length(); i++) {
        pubkeyNames.add(defaultPubkeyNames.getString(i));
    }
    TypedArray defaultPubkeyValues = getResources().obtainTypedArray(R.array.list_pubkeyids_value);
    for (int i = 0; i < defaultPubkeyValues.length(); i++) {
        pubkeyValues.add(defaultPubkeyValues.getString(i));
    }

    // Now, add pubkeys which have been added by the user.
    for (CharSequence cs : mPubkeyDb.allValues(PubkeyDatabase.FIELD_PUBKEY_NICKNAME)) {
        pubkeyNames.add(cs.toString());
    }
    for (CharSequence cs : mPubkeyDb.allValues("_id")) {
        pubkeyValues.add(cs.toString());
    }

    setContentView(R.layout.activity_edit_host);
    FragmentManager fm = getSupportFragmentManager();
    HostEditorFragment fragment = (HostEditorFragment) fm.findFragmentById(R.id.fragment_container);

    if (fragment == null) {
        fragment = HostEditorFragment.newInstance(mHost, pubkeyNames, pubkeyValues);
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commit();
    }

    defaultPubkeyNames.recycle();
    defaultPubkeyValues.recycle();
}

From source file:android.support.wear.widget.CircularProgressLayout.java

private int[] getColorListFromResources(Resources resources, int arrayResId) {
    TypedArray colorArray = resources.obtainTypedArray(arrayResId);
    int[] colors = new int[colorArray.length()];
    for (int i = 0; i < colorArray.length(); i++) {
        colors[i] = colorArray.getColor(i, 0);
    }/*from w  w w. ja  v  a 2s. c o  m*/
    colorArray.recycle();
    return colors;
}