Example usage for android.view ViewGroup addView

List of usage examples for android.view ViewGroup addView

Introduction

In this page you can find the example usage for android.view ViewGroup addView.

Prototype

@Override
public void addView(View child, LayoutParams params) 

Source Link

Document

Adds a child view with the specified layout parameters.

Usage

From source file:com.android.systemui.qs.QSDragPanel.java

@Override
protected void setupViews() {
    updateResources();/*  w w  w. j ava 2  s  .co m*/

    mDetail = LayoutInflater.from(mContext).inflate(R.layout.qs_detail, this, false);
    mDetailButtons = (ViewGroup) mDetail.findViewById(R.id.buttons);
    mDetailContent = (ViewGroup) mDetail.findViewById(android.R.id.content);
    mDetailRemoveButton = (TextView) mDetail.findViewById(android.R.id.button3);
    mDetailSettingsButton = (TextView) mDetail.findViewById(android.R.id.button2);
    mDetailDoneButton = (TextView) mDetail.findViewById(android.R.id.button1);
    updateDetailText();
    mDetail.setVisibility(GONE);
    mDetail.setClickable(true);

    mQsPanelTop = (QSPanelTopView) LayoutInflater.from(mContext).inflate(R.layout.qs_tile_top, this, false);

    mBrightnessView = mQsPanelTop.getBrightnessView();
    mFooter = new QSFooter(this, mContext);

    // add target click listener
    mQsPanelTop.getAddTarget().setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            TilesListAdapter adapter = new TilesListAdapter(mContext, QSDragPanel.this);
            showDetailAdapter(true, adapter, v.getLocationOnScreen());
            mDetail.bringToFront();
        }
    });
    mViewPager = new QSViewPager(getContext());
    mViewPager.setDragPanel(this);

    mPageIndicator = new CirclePageIndicator(getContext());
    addView(mDetail);
    addView(mQsPanelTop);
    addView(mViewPager);
    addView(mPageIndicator);
    addView(mFooter.getView());

    mClipper = new QSDetailClipper(mDetail);

    mBrightnessController = new BrightnessController(getContext(),
            (ImageView) mQsPanelTop.getBrightnessView().findViewById(R.id.brightness_icon),
            (ToggleSlider) mQsPanelTop.getBrightnessView().findViewById(R.id.brightness_slider));

    mDetailDoneButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            announceForAccessibility(mContext.getString(R.string.accessibility_desc_quick_settings));
            closeDetail();
        }
    });

    mPagerAdapter = new PagerAdapter() {
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (DEBUG_TILES) {
                Log.d(TAG, "instantiateItem() called with " + "container = [" + container + "], position = ["
                        + position + "]");
            }

            if (mEditing && position == 0) {
                QSSettings qss = (QSSettings) View.inflate(container.getContext(), R.layout.qs_settings, null);
                qss.setHost(mHost);
                container.addView(qss, 0);
                return qss;
            } else {
                final int adjustedPosition = mEditing ? position - 1 : position;
                QSPage page = mPages.get(adjustedPosition);
                if (!page.isAttachedToWindow()) {
                    container.addView(page);
                }
                return page;
            }
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            if (DEBUG_TILES) {
                Log.d(TAG, "destroyItem() called with " + "container = [" + container + "], position = ["
                        + position + "], object = [" + object + "]");
            }
            if (object instanceof View) {
                container.removeView((View) object);
            }
        }

        @Override
        public int getItemPosition(Object object) {
            if (object instanceof QSPage) {
                if (mEditing != ((QSPage) object).getAdapterEditingState()) {
                    // position of item changes when we set change the editing mode,
                    // sync it and send the new position
                    ((QSPage) object).setAdapterEditingState(mEditing);

                    // calculate new position
                    int indexOf = ((QSPage) object).getPageIndex();
                    if (mEditing)
                        return indexOf + 1;
                    else
                        return indexOf;

                } else if (!mPages.contains(object) && !mDragging) {
                    // only return none if we aren't dragging (object may be removed from
                    // the records array temporarily and we might think we have less pages,
                    // we don't want to prematurely remove this page
                    return POSITION_NONE;
                } else {

                    return POSITION_UNCHANGED;
                }

            } else if (object instanceof QSSettings) {
                if (((QSSettings) object).getAdapterEditingState() != mEditing) {
                    ((QSSettings) object).setAdapterEditingState(mEditing);
                    if (mEditing)
                        return 0 /* locked at position 0 */;
                    else
                        return POSITION_NONE;
                } else {
                    return POSITION_UNCHANGED;
                }
            }
            return super.getItemPosition(object);
        }

        @Override
        public int getCount() {
            final int qsPages = Math.max(getCurrentMaxPageCount(), 1);

            if (mPages != null && qsPages > mPages.size()) {
                for (int i = mPages.size(); i < qsPages; i++) {
                    mPages.add(i, new QSPage(mViewPager.getContext(), QSDragPanel.this, i));
                }
            }

            if (mEditing)
                return qsPages + 1;
            return qsPages;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    };
    mViewPager.setAdapter(mPagerAdapter);

    mPageIndicator.setViewPager(mViewPager);
    mPageIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (DEBUG_DRAG) {
                Log.i(TAG, "onPageScrolled() called with " + "position = [" + position + "], positionOffset = ["
                        + positionOffset + "], positionOffsetPixels = [" + positionOffsetPixels + "]");
            }

            if (mEditing) {
                float targetTranslationX = 0;

                // targetTranslationX = where it's supposed to be - diff
                int homeLocation = mViewPager.getMeasuredWidth();

                // how far away from homeLocation is the scroll?
                if (positionOffsetPixels < homeLocation && position == 0) {
                    targetTranslationX = homeLocation - positionOffsetPixels;
                }
                mQsPanelTop.setTranslationX(targetTranslationX);
            }
        }

        @Override
        public void onPageSelected(int position) {
            if (mDragging && position != mDraggingRecord.page && !mViewPager.isFakeDragging() && !mRestoring) {
                if (DEBUG_DRAG) {
                    Log.w(TAG, "moving drag record to page: " + position);
                }

                // remove it from the previous page and add it here
                final QSPage sourceP = getPage(mDraggingRecord.page);
                final QSPage targetP = getPage(position);

                sourceP.removeView(mDraggingRecord.tileView);
                mDraggingRecord.page = position;
                targetP.addView(mDraggingRecord.tileView);

                // set coords off screen until we're ready to place it
                mDraggingRecord.tileView.setX(-mDraggingRecord.tileView.getMeasuredWidth());
                mDraggingRecord.tileView.setY(-mDraggingRecord.tileView.getMeasuredHeight());
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    mViewPager.setOverScrollMode(OVER_SCROLL_NEVER);

    setClipChildren(false);

    mSettingsObserver = new SettingsObserver(new Handler());

    mViewPager.setOnDragListener(QSDragPanel.this);
    mQsPanelTop.setOnDragListener(QSDragPanel.this);
    mPageIndicator.setOnDragListener(QSDragPanel.this);
    setOnDragListener(QSDragPanel.this);

    mViewPager.setOverScrollMode(View.OVER_SCROLL_NEVER);
}

From source file:com.android.launcher2.Launcher.java

public void showFirstRunWorkspaceCling() {
    // Enable the clings only if they have not been dismissed before
    if (isClingsEnabled() && !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)
            && !skipCustomClingIfNoAccounts()) {
        // If we're not using the default workspace layout, replace workspace cling
        // with a custom workspace cling (usually specified in an overlay)
        // For now, only do this on tablets
        if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0
                && getResources().getBoolean(R.bool.config_useCustomClings)) {
            // Use a custom cling
            View cling = findViewById(R.id.workspace_cling);
            ViewGroup clingParent = (ViewGroup) cling.getParent();
            int clingIndex = clingParent.indexOfChild(cling);
            clingParent.removeViewAt(clingIndex);
            View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
            clingParent.addView(customCling, clingIndex);
            customCling.setId(R.id.workspace_cling);
        }//from www  .j  a v  a  2 s  .  co  m
        initCling(R.id.workspace_cling, null, false, 0);
    } else {
        removeCling(R.id.workspace_cling);
    }
}

From source file:cs.umass.edu.prepare.view.activities.CalendarActivity.java

/**
 * Populates the details view with adherence data for a particular date.
 * @param dateKey corresponds to the selected date. Note that the date key must have zeroed
 *                out all fields excluding month, year and date.
 * @param insertPoint the parent to which the adherence views are to be added.
 *//*from www .ja  va  2 s .c o m*/
private void insertDetailsForDate(final Calendar dateKey, ViewGroup insertPoint) {
    final Map<Medication, Adherence[]> adherenceMap = adherenceData.get(dateKey);
    for (final Medication medication : medications) {
        View details = View.inflate(this, R.layout.view_adherence_details_full, null);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        View[] adherenceViews = new View[2];
        int[] viewIDs = new int[] { R.id.adherence_details1, R.id.adherence_details2 };
        int[] timeTakenIDs = new int[] { R.id.txtTimeTaken1, R.id.txtTimeTaken2 };
        int[] medicationImgIDs = new int[] { R.id.imgMedication1, R.id.imgMedication2 };
        final Adherence[] adherence = adherenceMap.get(medication);
        int dosage = dosageMapping.get(medication);
        Calendar[] schedule = dailySchedule.get(medication);

        for (int i = 0; i < viewIDs.length; i++) {
            final int index = i;
            adherenceViews[index] = details.findViewById(viewIDs[index]);
            // TODO : Swiping on details doesn't work as expected (only works for adherence view):
            //            motionEventListener.setOnSwipeListener(adherenceViews[index], onDetailsSwiped);
            adherenceViews[index].setOnLongClickListener(view -> {
                editAdherence(medication, dateKey, index);
                return false;
            });
            adherenceViews[index].setOnClickListener(view -> {
                if (adherence[index].getAdherenceType() == Adherence.AdherenceType.TAKEN_CLARIFY_TIME) {
                    setTimeTaken(medication, dateKey, index);
                }
            });
            adherenceViews[index]
                    .setBackground(Utils.getDrawableForAdherence(this, adherence[index].getAdherenceType()));
            TextView timeTaken = (TextView) details.findViewById(timeTakenIDs[index]);
            TextView imgMedication = (TextView) details.findViewById(medicationImgIDs[index]);
            BitmapDrawable medicationDrawable = new BitmapDrawable(getResources(), medication.getImage());
            imgMedication.setCompoundDrawablesWithIntrinsicBounds(null, medicationDrawable, null, null);
            imgMedication.setText(String.format(Locale.getDefault(), "%d mg", dosage));
            adherenceViews[index].setVisibility(View.VISIBLE);
            switch (adherence[index].getAdherenceType()) {
            case NONE:
                adherenceViews[index].setVisibility(View.INVISIBLE);
                break;
            case MISSED:
                timeTaken.setText(R.string.adherence_text_missed);
                break;
            case TAKEN_CLARIFY_TIME:
                timeTaken.setText(R.string.adherence_text_unknown);
                break;
            case FUTURE:
                timeTaken.setText(timeFormat.format(schedule[index].getTime()));
                break;
            default: // taken on-time or taken late/early
                Calendar time = adherence[index].getTimeTaken();
                if (time != null)
                    timeTaken.setText(timeFormat.format(time.getTime()));
                break;
            }
        }
        // set margins and add to parent
        layoutParams.setMargins(0, 15, 0, 15);
        insertPoint.addView(details, layoutParams);
    }
}

From source file:com.android.soma.Launcher.java

public void showFirstRunCling() {
    if (isClingsEnabled() && !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false)
            && !skipCustomClingIfNoAccounts()) {
        // If we're not using the default workspace layout, replace workspace cling
        // with a custom workspace cling (usually specified in an overlay)
        // For now, only do this on tablets
        if (!DISABLE_CUSTOM_CLINGS) {
            if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0
                    && getResources().getBoolean(R.bool.config_useCustomClings)) {
                // Use a custom cling
                View cling = findViewById(R.id.workspace_cling);
                ViewGroup clingParent = (ViewGroup) cling.getParent();
                int clingIndex = clingParent.indexOfChild(cling);
                clingParent.removeViewAt(clingIndex);
                View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
                clingParent.addView(customCling, clingIndex);
                customCling.setId(R.id.workspace_cling);
            }//from ww  w. j a  v a2  s.  c om
        }
        Cling cling = (Cling) findViewById(R.id.first_run_cling);
        if (cling != null) {
            String sbHintStr = getFirstRunClingSearchBarHint();
            String ccHintStr = getFirstRunCustomContentHint();
            if (!sbHintStr.isEmpty()) {
                TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
                sbHint.setText(sbHintStr);
                sbHint.setVisibility(View.VISIBLE);
            }
            setCustomContentHintVisibility(cling, ccHintStr, true, false);
        }
        initCling(R.id.first_run_cling, 0, false, true);
    } else {
        removeCling(R.id.first_run_cling);
    }
}

From source file:com.cw.litenote.note.Note_adapter.java

@SuppressLint("SetJavaScriptEnabled")
@Override/*from  w w  w.  j av  a2  s .  com*/
public Object instantiateItem(ViewGroup container, final int position) {
    System.out.println("Note_adapter / instantiateItem / position = " + position);
    // Inflate the layout containing 
    // 1. picture group: image,video, thumb nail, control buttons
    // 2. text group: title, body, time 
    View pagerView = inflater.inflate(R.layout.note_view_adapter, container, false);
    int style = Note.getStyle();
    pagerView.setBackgroundColor(ColorSet.mBG_ColorArray[style]);

    // Picture group
    ViewGroup pictureGroup = (ViewGroup) pagerView.findViewById(R.id.pictureContent);
    String tagPictureStr = "current" + position + "pictureView";
    pictureGroup.setTag(tagPictureStr);

    // image view
    TouchImageView imageView = ((TouchImageView) pagerView.findViewById(R.id.image_view));
    String tagImageStr = "current" + position + "imageView";
    imageView.setTag(tagImageStr);

    // video view
    VideoViewCustom videoView = ((VideoViewCustom) pagerView.findViewById(R.id.video_view));
    String tagVideoStr = "current" + position + "videoView";
    videoView.setTag(tagVideoStr);

    ProgressBar spinner = (ProgressBar) pagerView.findViewById(R.id.loading);

    // link web view
    CustomWebView linkWebView = ((CustomWebView) pagerView.findViewById(R.id.link_web_view));
    String tagStr = "current" + position + "linkWebView";
    linkWebView.setTag(tagStr);

    // line view
    View line_view = pagerView.findViewById(R.id.line_view);

    // text group
    ViewGroup textGroup = (ViewGroup) pagerView.findViewById(R.id.textGroup);

    // Set tag for text web view
    CustomWebView textWebView = ((CustomWebView) textGroup.findViewById(R.id.textBody));

    // set accessibility
    textGroup.setContentDescription(act.getResources().getString(R.string.note_text));
    textWebView.getRootView().setContentDescription(act.getResources().getString(R.string.note_text));

    tagStr = "current" + position + "textWebView";
    textWebView.setTag(tagStr);

    // set text web view
    setWebView(textWebView, spinner, CustomWebView.TEXT_VIEW);

    String linkUri = db_page.getNoteLinkUri(position, true);
    String strTitle = db_page.getNoteTitle(position, true);
    String strBody = db_page.getNoteBody(position, true);

    // View mode
    // picture only
    if (Note.isPictureMode()) {
        System.out.println("Note_adapter / _instantiateItem / isPictureMode ");
        pictureGroup.setVisibility(View.VISIBLE);
        showPictureView(position, imageView, videoView, linkWebView, spinner);

        line_view.setVisibility(View.GONE);
        textGroup.setVisibility(View.GONE);
    }
    // text only
    else if (Note.isTextMode()) {
        System.out.println("Note_adapter / _instantiateItem / isTextMode ");
        pictureGroup.setVisibility(View.GONE);

        line_view.setVisibility(View.VISIBLE);
        textGroup.setVisibility(View.VISIBLE);

        if (Util.isYouTubeLink(linkUri) || !Util.isEmptyString(strTitle) || !Util.isEmptyString(strBody)
                || linkUri.startsWith("http")) {
            showTextWebView(position, textWebView);
        }
    }
    // picture and text
    else if (Note.isViewAllMode()) {
        System.out.println("Note_adapter / _instantiateItem / isViewAllMode ");

        // picture
        pictureGroup.setVisibility(View.VISIBLE);
        showPictureView(position, imageView, videoView, linkWebView, spinner);

        line_view.setVisibility(View.VISIBLE);
        textGroup.setVisibility(View.VISIBLE);

        // text
        if (!Util.isEmptyString(strTitle) || !Util.isEmptyString(strBody) || Util.isYouTubeLink(linkUri)
                || linkUri.startsWith("http")) {
            showTextWebView(position, textWebView);
        } else {
            textGroup.setVisibility(View.GONE);
        }
    }

    // footer of note view
    TextView footerText = (TextView) pagerView.findViewById(R.id.note_view_footer);
    if (!Note.isPictureMode()) {
        footerText.setVisibility(View.VISIBLE);
        footerText.setText(String.valueOf(position + 1) + "/" + pager.getAdapter().getCount());
        footerText.setTextColor(ColorSet.mText_ColorArray[Note.mStyle]);
        footerText.setBackgroundColor(ColorSet.mBG_ColorArray[Note.mStyle]);
    } else
        footerText.setVisibility(View.GONE);

    container.addView(pagerView, 0);

    return pagerView;
}

From source file:com.cairoconfessions.MainActivity.java

public void addItem(View view) {
    // Instantiate a new "row" view.
    final ViewGroup mFilter = (ViewGroup) findViewById(R.id.filter_cat);
    final ViewGroup mFilterLoc = (ViewGroup) findViewById(R.id.filter_loc);
    final ViewGroup mFilterMain = (ViewGroup) findViewById(R.id.filter_main);
    final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.list_item_example, null);
    final ViewGroup newViewLoc = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.list_item_example,
            null);/*from   ww  w . ja  v  a2s  .c  om*/
    final ViewGroup newViewMain = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.list_item_example,
            null);
    ArrayList<View> presentView = new ArrayList<View>();

    mFilter.findViewsWithText(presentView, ((TextView) view).getText(), 1);
    if (presentView.size() == 0) {
        final String filterName = ((TextView) view).getText().toString();
        switch (((LinearLayout) view.getParent()).getId()) {
        case R.id.cat_filter_list:
            Categories.add(filterName);
            break;
        case R.id.locations:
            Cities.add(filterName);
            break;
        }
        if (filterName.equals("Love")) {
            newView.getChildAt(0).setBackgroundResource(R.color.love);
            newViewLoc.getChildAt(0).setBackgroundResource(R.color.love);
            newViewMain.getChildAt(0).setBackgroundResource(R.color.love);
        }
        if (filterName.equals("Pain")) {
            newView.getChildAt(0).setBackgroundResource(R.color.pain);
            newViewLoc.getChildAt(0).setBackgroundResource(R.color.pain);
            newViewMain.getChildAt(0).setBackgroundResource(R.color.pain);
        }
        if (filterName.equals("Guilt")) {
            newView.getChildAt(0).setBackgroundResource(R.color.guilt);
            newViewLoc.getChildAt(0).setBackgroundResource(R.color.guilt);
            newViewMain.getChildAt(0).setBackgroundResource(R.color.guilt);
        }
        if (filterName.equals("Fantasy")) {
            newView.getChildAt(0).setBackgroundResource(R.color.fantasy);
            newViewLoc.getChildAt(0).setBackgroundResource(R.color.fantasy);
            newViewMain.getChildAt(0).setBackgroundResource(R.color.fantasy);
        }
        if (filterName.equals("Dream")) {
            newView.getChildAt(0).setBackgroundResource(R.color.dream);
            newViewLoc.getChildAt(0).setBackgroundResource(R.color.dream);
            newViewMain.getChildAt(0).setBackgroundResource(R.color.dream);
        }
        ((TextView) newView.findViewById(android.R.id.text1)).setText(filterName);
        ((TextView) newViewLoc.findViewById(android.R.id.text1)).setText(filterName);
        ((TextView) newViewMain.findViewById(android.R.id.text1)).setText(filterName);
        // Set a click listener for the "X" button in the row that will
        // remove the row.

        newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFilter.removeView(newView);
                mFilterLoc.removeView(newViewLoc);
                mFilterMain.removeView(newViewMain);
                if (mFilter.getChildCount() == 0) {
                    findViewById(R.id.content_loc).setVisibility(View.GONE);
                    findViewById(R.id.content_cat).setVisibility(View.GONE);
                    findViewById(R.id.content_main).setVisibility(View.GONE);
                }
                if (Categories.contains(filterName))
                    while (Categories.remove(filterName))
                        ;
                if (Cities.contains(filterName))
                    while (Cities.remove(filterName))
                        ;
                updateFilters();
            }
        });
        newViewLoc.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFilterLoc.removeView(newViewLoc);
                mFilter.removeView(newView);
                mFilterMain.removeView(newViewMain);
                if (mFilterLoc.getChildCount() == 0) {
                    findViewById(R.id.content_loc).setVisibility(View.GONE);
                    findViewById(R.id.content_cat).setVisibility(View.GONE);
                    findViewById(R.id.content_main).setVisibility(View.GONE);
                }
                if (Categories.contains(filterName))
                    while (Categories.remove(filterName))
                        ;
                if (Cities.contains(filterName))
                    while (Cities.remove(filterName))
                        ;
                updateFilters();
            }

        });
        newViewMain.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFilterLoc.removeView(newViewLoc);
                mFilter.removeView(newView);
                mFilterMain.removeView(newViewMain);
                if (mFilterMain.getChildCount() == 0) {
                    findViewById(R.id.content_loc).setVisibility(View.GONE);
                    findViewById(R.id.content_cat).setVisibility(View.GONE);
                    findViewById(R.id.content_main).setVisibility(View.GONE);
                }
                if (Categories.contains(filterName))
                    while (Categories.remove(filterName))
                        ;
                if (Cities.contains(filterName))
                    while (Cities.remove(filterName))
                        ;
                updateFilters();
            }

        });

        // Because mFilter has android:animateLayoutChanges set to true,
        // adding this view is automatically animated.
        // mFilterCat.addView(newViewCat);
        mFilter.addView(newView, 0);
        mFilterLoc.addView(newViewLoc, 0);
        mFilterMain.addView(newViewMain, 0);
        findViewById(R.id.content_loc).setVisibility(View.VISIBLE);
        findViewById(R.id.content_cat).setVisibility(View.VISIBLE);
        findViewById(R.id.content_main).setVisibility(View.VISIBLE);
        updateFilters();
        Toast.makeText(this, filterName + " filter added!", Toast.LENGTH_LONG).show();
    } else
        Toast.makeText(this, "Already added!", Toast.LENGTH_LONG).show();
}