Example usage for android.widget LinearLayout setLayoutParams

List of usage examples for android.widget LinearLayout setLayoutParams

Introduction

In this page you can find the example usage for android.widget LinearLayout setLayoutParams.

Prototype

public void setLayoutParams(ViewGroup.LayoutParams params) 

Source Link

Document

Set the layout parameters associated with this view.

Usage

From source file:br.org.funcate.dynamicforms.views.GDateView.java

/**
 * @param fragment   the fragment to use.
 * @param attrs attributes./*  ww w  . j a  v a  2 s . c  om*/
 * @param parentView parent
 * @param label label
 * @param value value
 * @param constraintDescription constraints
 * @param readonly if <code>false</code>, the item is disabled for editing.
 */
public GDateView(final Fragment fragment, AttributeSet attrs, LinearLayout parentView, String label,
        String value, String constraintDescription, boolean readonly) {
    super(fragment.getActivity(), attrs);

    final Context context = fragment.getActivity();

    LinearLayout textLayout = new LinearLayout(context);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10, 10, 10, 10);
    textLayout.setLayoutParams(layoutParams);
    textLayout.setOrientation(LinearLayout.VERTICAL);
    parentView.addView(textLayout);

    TextView textView = new TextView(context);
    textView.setLayoutParams(
            new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    textView.setPadding(2, 2, 2, 2);
    textView.setText(label.replace(UNDERSCORE, " ").replace(COLON, " ") + " " + constraintDescription);
    textView.setTextColor(context.getResources().getColor(R.color.formcolor));
    textLayout.addView(textView);

    button = new Button(context);
    button.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    button.setPadding(15, 5, 15, 5);

    final SimpleDateFormat dateFormatter = TimeUtilities.INSTANCE.DATEONLY_FORMATTER;
    if (value == null || value.length() == 0) {
        String dateStr = dateFormatter.format(new Date());
        button.setText(dateStr);
    } else {
        button.setText(value);
    }
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String dateStr = button.getText().toString();
            Date date = null;
            try {
                date = dateFormatter.parse(dateStr);
            } catch (ParseException e) {
                //GPLog.error(this, null, e);
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                date = new Date();
            }
            final Calendar c = Calendar.getInstance();
            c.setTime(date);
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            FormDatePickerFragment newFragment = new FormDatePickerFragment();
            newFragment.setAttributes(year, month, day, button);
            newFragment.show(fragment.getFragmentManager(), "datePicker");
        }
    });
    button.setEnabled(!readonly);

    textLayout.addView(button);
}

From source file:mobisocial.musubi.objects.IntroductionObj.java

@Override
public View createView(Context context, ViewGroup frame) {
    LinearLayout wrap = new LinearLayout(context);
    wrap.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    wrap.setOrientation(LinearLayout.VERTICAL);
    wrap.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
    wrap.setEnabled(false);//from   w  w  w  .  ja  va2  s  .  c o m
    wrap.setFocusableInTouchMode(false);
    wrap.setFocusable(false);
    wrap.setClickable(false);

    TextView title = new TextView(context);
    title.setText(R.string.introduced);
    title.setTypeface(null, Typeface.BOLD);
    title.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    wrap.addView(title);

    Gallery intro = new Gallery(context);
    intro.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    hackGalleryInit(context, intro);
    wrap.addView(intro);
    return wrap;
}

From source file:org.zywx.wbpalmstar.plugin.uexPopoverMenu.EUExPopoverMenu.java

private void showPopoverMenu(double x, double y, int direction, String bgColorStr, String dividerColor,
        String textColor, int textSize, JSONArray data) {
    LinearLayout rootView = (LinearLayout) LayoutInflater.from(mContext)
            .inflate(finder.getLayoutId("plugin_uex_popovermenu"), null);

    final PopupWindow popupWindow = new PopupWindow(rootView, LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, true);

    popupWindow.setContentView(rootView);
    ////from   w w w.  ja v  a2 s .  co m
    rootView.setBackgroundColor(Color.parseColor(bgColorStr));
    popupWindow.setContentView(rootView);
    //popupWindow?
    popupWindow.setBackgroundDrawable(new BitmapDrawable());
    popupWindow.setOutsideTouchable(true);

    int length = data.length();
    List<ItemData> itemDataList = new ArrayList<ItemData>();
    try {
        for (int i = 0; i < length; i++) {
            ItemData item = new ItemData();
            String icon = data.getJSONObject(i).optString("icon", "");
            if (!hasIcon && !TextUtils.isEmpty(icon)) {
                hasIcon = true;
            }
            if (hasIcon) {
                item.setIcon(data.getJSONObject(i).getString("icon"));
            }
            item.setText(data.getJSONObject(i).getString("text"));
            itemDataList.add(item);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    for (int i = 0; i < length; i++) {
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        if (hasIcon) {
            ImageView imageView = new ImageView(mContext);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            String imagePath = itemDataList.get(i).getIcon();
            imageView.setImageBitmap(getBitmapFromPath(imagePath));
            imageView.setPadding(DensityUtil.dip2px(mContext, 5), 0, 0, 0);
            linearLayout.addView(imageView);
        }
        TextView tvText = new TextView(mContext);
        LinearLayout.LayoutParams tvLayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        tvLayoutParams.gravity = Gravity.CENTER_VERTICAL;
        tvText.setLayoutParams(tvLayoutParams);

        tvText.setPadding(DensityUtil.dip2px(mContext, 5), 0, DensityUtil.dip2px(mContext, 8), 0);
        tvText.setText(itemDataList.get(i).getText());
        tvText.setTextColor(Color.parseColor(textColor));
        tvText.setTextSize(textSize);
        linearLayout.addView(tvText);
        linearLayout.setTag(i);
        linearLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                callBackPluginJs(CALLBACK_ITEM_SELECTED, String.valueOf(v.getTag()));
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        rootView.addView(linearLayout);

        //
        View dividerLine = new View(mContext);
        dividerLine.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                DensityUtil.dip2px(mContext, 1)));
        dividerLine.setBackgroundColor(Color.parseColor(dividerColor));
        rootView.addView(dividerLine);
    }
    int gravityParam;
    switch (direction) {
    case 0:
        gravityParam = Gravity.LEFT | Gravity.TOP;
        break;
    case 1:
        gravityParam = Gravity.RIGHT | Gravity.TOP;
        break;
    case 2:
        gravityParam = Gravity.LEFT | Gravity.BOTTOM;
        break;
    case 3:
        gravityParam = Gravity.RIGHT | Gravity.BOTTOM;
        break;
    default:
        gravityParam = Gravity.LEFT | Gravity.TOP;
        break;
    }
    popupWindow.showAtLocation(mBrwView.getRootView(), gravityParam, (int) x, (int) y);
}

From source file:com.pikachu.emoji.widget.EmojiView.java

/**
 * </br>//from   w w  w.  ja va  2  s . com
 * 
 * @return
 */
private ViewGroup createPointLinearlayout() {
    LinearLayout pointContainerLayout = new LinearLayout(getContext());
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    pointContainerLayout.setLayoutParams(params);
    pointContainerLayout.setOrientation(LinearLayout.HORIZONTAL);
    pointContainerLayout.setGravity(Gravity.CENTER);
    params.gravity = Gravity.CENTER;
    params.topMargin = CommonUtils.dp2px(getContext(), 15);
    params.bottomMargin = CommonUtils.dp2px(getContext(), 15);
    return pointContainerLayout;
}

From source file:com.openlocationcode.android.main.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_act);/*  www .  j a  v  a2 s .com*/
    MyMapView myMapView = (MyMapView) findViewById(R.id.myMapView);
    mMapView = myMapView.getMapView();
    mMainPresenter = new MainPresenter(this, (SearchView) findViewById(R.id.searchView),
            (DirectionView) findViewById(R.id.directionView), (CodeView) findViewById(R.id.codeView),
            myMapView);

    mMapView.onCreate(savedInstanceState);

    // Adjust the map controls and search box top margins to account for the translucent status
    // bar.
    int statusBarHeight = getStatusBarHeight(this);
    LinearLayout mapControl = (LinearLayout) findViewById(R.id.mapControls);
    FrameLayout.LayoutParams mapParams = (FrameLayout.LayoutParams) mapControl.getLayoutParams();
    mapParams.topMargin = mapParams.topMargin + statusBarHeight;
    mapControl.setLayoutParams(mapParams);

    RelativeLayout searchBox = (RelativeLayout) findViewById(R.id.searchBox);
    FrameLayout.LayoutParams searchParams = (FrameLayout.LayoutParams) searchBox.getLayoutParams();
    searchParams.topMargin = searchParams.topMargin + statusBarHeight;
    searchBox.setLayoutParams(searchParams);

    if (getIntent() != null && Intent.ACTION_VIEW.equals(getIntent().getAction())) {
        handleGeoIntent(getIntent());
    }
}

From source file:org.odk.collect.android.fragments.dialogs.RankingWidgetDialog.java

private LinearLayout setUpPositionsLayout(List<String> values) {
    LinearLayout positionsLayout = new LinearLayout(getContext());
    positionsLayout.setOrientation(LinearLayout.VERTICAL);

    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(0, 0, 10, 0);
    positionsLayout.setLayoutParams(layoutParams);

    for (String value : values) {
        FrameLayout positionLayout = (FrameLayout) LayoutInflater.from(getContext())
                .inflate(R.layout.ranking_item, positionsLayout, false);
        TextView textView = positionLayout.findViewById(R.id.rank_item_text);
        textView.setText(String.valueOf(values.indexOf(value) + 1));
        textView.setTextSize(Collect.getQuestionFontsize());

        positionsLayout.addView(positionLayout);
    }/*from  w  ww. ja  va  2 s .  c o  m*/
    return positionsLayout;
}

From source file:com.achep.header2actionbar.HeaderFragmentSupportV4.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Activity activity = getActivity();
    assert activity != null;
    mFrameLayout = new FrameLayout(activity);

    mHeader = onCreateHeaderView(inflater, mFrameLayout);
    mHeaderHeader = mHeader.findViewById(android.R.id.title);
    mHeaderBackground = mHeader.findViewById(android.R.id.background);
    assert mHeader.getLayoutParams() != null;
    mHeader.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    mHeaderHeight = mHeader.getMeasuredHeight();

    mFakeHeader = new Space(activity);

    View content = onCreateContentView(inflater, mFrameLayout);
    mContentView = content;/*from w w w . jav a  2s.c  o m*/
    Log.i(TAG, "container:" + container.getMeasuredHeight() + ",mHeaderHeight=" + mHeaderHeight);

    final View topContentView = container;

    if (content instanceof ListView) {
        isListViewEmpty = true;

        final ListView listView = (ListView) content;
        mFakeHeader.setLayoutParams(new ListView.LayoutParams(0, mHeaderHeight));
        listView.addHeaderView(mFakeHeader);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScrollStateChanged(absListView, scrollState);
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (isListViewEmpty) {
                    scrollHeaderTo(0);
                } else {
                    final View child = absListView.getChildAt(0);
                    assert child != null;
                    scrollHeaderTo(child == mFakeHeader ? child.getTop() : -mHeaderHeight);
                }
            }
        });
    } else {
        topContentView.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @SuppressWarnings("deprecation")
                    @Override
                    public void onGlobalLayout() {
                        //Remove it here unless you want to get this callback for EVERY
                        //layout pass, which can get you into infinite loops if you ever
                        //modify the layout from within this method.
                        topContentView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                        //Now you can get the width and height from content
                        int actionBarHeight = 0;
                        TypedValue tv = new TypedValue();
                        if (getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                                    getResources().getDisplayMetrics());
                        }
                        Log.i(TAG, "topContentView:" + topContentView.getHeight() + ", actionBarHeight:"
                                + actionBarHeight + ", getStatusBarHeight:" + getStatusBarHeight());
                        ViewGroup.LayoutParams lp = mContentView.getLayoutParams();
                        lp.height = topContentView.getHeight() - actionBarHeight - getStatusBarHeight();
                        mContentView.setLayoutParams(lp);
                    }
                });

        // Merge fake header view and content view.
        final LinearLayout view = new LinearLayout(activity);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        view.setOrientation(LinearLayout.VERTICAL);
        mFakeHeader.setLayoutParams(new LinearLayout.LayoutParams(0, mHeaderHeight));
        view.addView(mFakeHeader);
        view.addView(content);

        // Put merged content to ScrollView
        final NotifyingScrollView scrollView = new NotifyingScrollView(activity);
        scrollView.addView(view);
        scrollView.setVerticalScrollBarEnabled(false);
        scrollView.setOverScrollMode(ScrollView.OVER_SCROLL_NEVER);
        scrollView.setOnScrollChangedListener(new NotifyingScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
                Log.i(TAG, "onScrollChanged scroll ot -t :" + (-t));
                scrollHeaderTo(-t);
            }
        });
        mContentWrapper = scrollView;
        content = scrollView;
    }

    mFrameLayout.addView(content);
    mFrameLayout.addView(mHeader);

    // Content overlay view always shows at the top of content.
    if ((mContentOverlay = onCreateContentOverlayView(inflater, mFrameLayout)) != null) {
        mFrameLayout.addView(mContentOverlay, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    // Post initial scroll
    mFrameLayout.post(new Runnable() {
        @Override
        public void run() {
            Log.i(TAG, "post initial scroll to 0");
            // SEAN: walk around for scroll position bug
            if (mContentWrapper != null) {
                mContentWrapper.scrollTo(0, 0);
            }
            scrollHeaderTo(0, true);
        }
    });

    return mFrameLayout;
}

From source file:com.modprobe.profit.CustomArrayAdapter.java

/**
 * Populates the item in the listview cell with the appropriate data. This
 * method sets the thumbnail image, the title and the extra text. This
 * method also updates the layout parameters of the item's view so that the
 * image and title are centered in the bounds of the collapsed view, and
 * such that the extra text is not displayed in the collapsed state of the
 * cell.//from www . ja  v a 2 s  . c o  m
 */
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final SuggestionExpandingListViewItem object = mData.get(position);

    if (convertView == null) {
        LayoutInflater inflater = ((AppCompatActivity) getContext()).getLayoutInflater();
        convertView = inflater.inflate(mLayoutViewResourceId, parent, false);
    }

    LinearLayout linearLayout = (LinearLayout) (convertView.findViewById(R.id.item_linear_layout));
    LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(
            AbsListView.LayoutParams.MATCH_PARENT, object.getCollapsedHeight());
    linearLayout.setLayoutParams(linearLayoutParams);

    TextView taskTextView = (TextView) convertView.findViewById(R.id.task);
    TextView intensityTextView = (TextView) convertView.findViewById(R.id.intensity);
    TextView durationTextView = (TextView) convertView.findViewById(R.id.duration);
    TextView descriptionTextView = (TextView) convertView.findViewById(R.id.description);
    taskTextView.setText(object.task);
    intensityTextView.setText(object.intensity);
    durationTextView.setText(object.duration);
    descriptionTextView.setText(object.description);
    //      linearLayout.setOnClickListener(new OnClickListener() {
    //         
    //         @Override
    //         public void onClick(View arg0) {
    //            if(mData.get(position).isExpanded()){
    //               arg0.getP
    //            }
    //            else{
    //               
    //            }
    //            
    //         }
    //      });
    Button doneButton = (Button) convertView.findViewById(R.id.done);
    Button nopeButton = (Button) convertView.findViewById(R.id.nope);

    doneButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO
            Toast.makeText(mContext, "Alright We'll log it!", Toast.LENGTH_SHORT).show();
            ActivityDataSource ads = new ActivityDataSource(mContext);
            ads.open();
            Suggestion ss = mData.get(position).suggestion;
            ads.createActivity(ss._subcat, ss._duration,
                    Helper.getFitons(ss._duration, ss._intensity, ss._subcat._exertion), ss._intensity,
                    (int) (Math.random() * 1000000));
            ads.close();
            mData.remove(position);
            notifyDataSetChanged();
            vpInstance.getAdapter().notifyDataSetChanged();
        }
    });

    nopeButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO
            Toast.makeText(mContext, "Maybe next time!", Toast.LENGTH_SHORT).show();
            mData.remove(position);
            notifyDataSetChanged();

        }
    });

    convertView.setLayoutParams(new ListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
            AbsListView.LayoutParams.WRAP_CONTENT));

    ExpandingLayout expandingLayout = (ExpandingLayout) convertView.findViewById(R.id.expanding_layout);
    expandingLayout.setExpandedHeight(object.getExpandedHeight());
    expandingLayout.setSizeChangedListener(object);

    if (!object.isExpanded()) {
        expandingLayout.setVisibility(View.GONE);
    } else {
        expandingLayout.setVisibility(View.VISIBLE);
    }

    return convertView;
}

From source file:org.nativescript.widgets.TabLayout.java

/**
 * Create a default view to be used for tabs.
 *//*from   w w  w.  j  a v  a 2  s.c  om*/
protected View createDefaultTabView(Context context, TabItemSpec tabItem) {
    float density = getResources().getDisplayMetrics().density;
    int padding = (int) (TAB_VIEW_PADDING_DIPS * density);

    LinearLayout ll = new LinearLayout(context);
    ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    ll.setGravity(Gravity.CENTER);
    ll.setOrientation(LinearLayout.VERTICAL);
    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    ll.setBackgroundResource(outValue.resourceId);

    ImageView imgView = new ImageView(context);
    imgView.setScaleType(ScaleType.FIT_CENTER);
    LinearLayout.LayoutParams imgLP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    imgLP.gravity = Gravity.CENTER;
    imgView.setLayoutParams(imgLP);

    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setMaxWidth((int) (TEXT_MAX_WIDTH * density));
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setEllipsize(TextUtils.TruncateAt.END);
    textView.setAllCaps(true);
    textView.setMaxLines(2);
    textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    textView.setPadding(padding, 0, padding, 0);

    this.setupItem(ll, textView, imgView, tabItem);

    ll.addView(imgView);
    ll.addView(textView);
    return ll;
}

From source file:com.loloof64.android.capturing_audio.MainActivity.java

public void purposeFileRenaming(final File externalStorageDir, final File tempAudioFile,
        final Calendar captureStartDate) {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setTitle(R.string.renaming_temporary_file);

    final LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.HORIZONTAL);

    final TextView label = new TextView(this);
    label.setText(R.string.defining_file_name_label);
    layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));

    final EditText input = new EditText(this);
    input.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    input.setInputType(InputType.TYPE_CLASS_TEXT);

    layout.addView(label);/* ww  w. jav  a2 s  . c  o m*/
    layout.addView(input);
    dialogBuilder.setView(layout);

    dialogBuilder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String defaultNameRadix = new SimpleDateFormat(DEFAULT_FILE_NAME_FORMAT, Locale.getDefault())
                    .format(captureStartDate.getTime());
            String nameRadix = input.getText().toString();

            if (nameRadix.isEmpty()) {
                nameRadix = defaultNameRadix;
            }

            // Append a number to the name radix if necessary
            File testedFile = new File(externalStorageDir, String.format(FINAL_FILE_NAME_FORMAT, nameRadix));
            boolean fileNameAlreadyUsed = testedFile.exists();
            if (fileNameAlreadyUsed) {
                int number = 1;
                String chosenNameRadix;
                while (true) {
                    chosenNameRadix = String.format(Locale.getDefault(), "%s%d", nameRadix, number);
                    testedFile = new File(externalStorageDir,
                            String.format(FINAL_FILE_NAME_FORMAT, chosenNameRadix));
                    fileNameAlreadyUsed = testedFile.exists();
                    if (!fileNameAlreadyUsed)
                        break;
                    number++;
                }
                nameRadix = chosenNameRadix;
            }

            String fileName = String.format(FINAL_FILE_NAME_FORMAT, nameRadix);
            File newFile = new File(externalStorageDir, fileName);
            boolean renameSuccess = tempAudioFile.renameTo(newFile);

            if (renameSuccess) {
                Toast.makeText(MainActivity.this, R.string.renamed_file, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, R.string.file_renaming_failure, Toast.LENGTH_SHORT).show();
            }
        }
    });

    dialogBuilder.show();
}