Asif Sb
Asif Sb

Reputation: 805

How can I set text left to the edit text programatically?

I just want to set the TextView to left of the edit text programatically.i searched a lot,i didn't find the solution yet and i don't what is my mistake.

here is my code:

        LinearLayout ll_text = new LinearLayout(this);
            LayoutParams ll_text_params = new LayoutParams(width,
                    height);
            ll_text.setGravity(Gravity.CENTER);
            ll_text.setOrientation(LinearLayout.VERTICAL);
            ll_text.setId(Integer.parseInt(question_id));
            //idOfRel++;

            TextView tv_question_text = new TextView(this);
            LayoutParams text_question_params = new LayoutParams(
                    android.view.ViewGroup.LayoutParams.FILL_PARENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
            text_question_params.setMargins(Math.round(82 * multiplier), 0,
                    Math.round(82 * multiplier), 0);
            tv_question_text.setGravity(Gravity.CENTER);

            tv_question_text.setId(Integer.parseInt(question_id));
            tv_question_text.setTextSize(30.0f);
            tv_question_text.setText(question_no+"."+getTitleString(question_id, tv_question_text));
            tv_question_text.setTextColor(Color.WHITE);
            tv_question_text.setTypeface(nexa_bold);

            TextView tv_at=new TextView(this);
            LayoutParams tv_at_params=new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
            tv_at_params.setMargins(Math.round(20*multiplier),0, Math.round(290*multiplier),0);
            tv_at_params.topMargin = Math.round(30 * multiplier);
            tv_at.setId(6);
            tv_at.setText("@");
            tv_at.setTextSize(40.0f);



            ActionEditText et_text = new ActionEditText(this);
            RelativeLayout.LayoutParams et_text_params = new RelativeLayout.LayoutParams(
                    Math.round(472 * multiplier), Math.round(63 * multiplier));
    //      et_text_params.setMargins(Math.round(30*multiplier),0, Math.round(270*multiplier), 0);
            et_text_params.addRule(RelativeLayout.RIGHT_OF, 6);
            et_text.setId(1);
            et_text.setPadding(5, 0, 5, 0);
            et_text.setFilters(new InputFilter[] { new InputFilter.LengthFilter(40) });
            et_text.setHint("Twitter Name");
            et_text.setSingleLine(true);
            et_text.setPaddingRelative(15, 0, 15, 0);
            et_text.setTextSize(20.0f);
            et_text.setImeOptions(EditorInfo.IME_ACTION_DONE);

need help thanks in advance..

enter image description here

like this..

Upvotes: 0

Views: 123

Answers (2)

Suresh Kumar
Suresh Kumar

Reputation: 2034

Use one more LinearLayout below the first TextView and add both the second TextView and EditText to this LinearLayout. Like this:

LinearLayout ll_text = new LinearLayout(this);
    LayoutParams ll_text_params = new LayoutParams(width,
            height);
    ll_text.setGravity(Gravity.CENTER);
    ll_text.setOrientation(LinearLayout.VERTICAL);
    ll_text.setId(Integer.parseInt(question_id));
    //idOfRel++;

    TextView tv_question_text = new TextView(this);
    LayoutParams text_question_params = new LayoutParams(
            android.view.ViewGroup.LayoutParams.FILL_PARENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    text_question_params.setMargins(Math.round(82 * multiplier), 0,
            Math.round(82 * multiplier), 0);
    tv_question_text.setGravity(Gravity.CENTER);

    tv_question_text.setId(Integer.parseInt(question_id));
    tv_question_text.setTextSize(30.0f);
    tv_question_text.setText(question_no+"."+getTitleString(question_id, tv_question_text));
    tv_question_text.setTextColor(Color.WHITE);
    tv_question_text.setTypeface(nexa_bold);

    TextView tv_at=new TextView(this);
    LayoutParams tv_at_params=new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    tv_at_params.setMargins(Math.round(20*multiplier),0, Math.round(290*multiplier),0);
    tv_at_params.topMargin = Math.round(30 * multiplier);
    tv_at.setId(6);
    tv_at.setText("@");
    tv_at.setTextSize(40.0f);



    ActionEditText et_text = new ActionEditText(this);
    RelativeLayout.LayoutParams et_text_params = new RelativeLayout.LayoutParams(
            Math.round(472 * multiplier), Math.round(63 * multiplier));
//      et_text_params.setMargins(Math.round(30*multiplier),0, Math.round(270*multiplier), 0);
        et_text_params.addRule(RelativeLayout.RIGHT_OF, 6);
        et_text.setId(1);
        et_text.setPadding(5, 0, 5, 0);
        et_text.setFilters(new InputFilter[] { new InputFilter.LengthFilter(40) });
        et_text.setHint("Twitter Name");
        et_text.setSingleLine(true);
        et_text.setPaddingRelative(15, 0, 15, 0);
        et_text.setTextSize(20.0f);
        et_text.setImeOptions(EditorInfo.IME_ACTION_DONE);

    //Additional Code to your code

    LinearLayout innerContainer = new LinearLayout(this);
    LayoutParams innerContainerParams = new LayoutParams(match_parent, wrap_content);
    innerContainerParams.setOrientation(LinearLayout.HORIZONTAL);
    innerContainer.addView(tv_at);
    innerContainer.addView(et_text);

    ll_text.addView(tv_question_text);
    ll_text.addView(innerContainer);

I guess you looking for something like this.

et_text.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myDrawable, 0, 0, 0);

Upvotes: 0

Saritha
Saritha

Reputation: 488

Add this line to your editText:

editText.setGravity(Gravity.LEFT);

Upvotes: 1

Related Questions