Skip to content Skip to sidebar Skip to footer

Android Studio Name Tabs In Activity

I have an editText in which an user can choose a number between 2 and 8. For example, if user selected 5 then I want to show 5 new editTexts in the same activity. How can I do this

Solution 1:

As said by @kumud kala Add a Linear Layout inside a Scrollview and then you can create a EditText programmatically like this.

LineLinearLayoutllayout= findViewById(R.id.yourlinearlayout);
EditText yourEditText= newEditText(this);
yourEditText.setLayoutParams(newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
llayout.addView(yourEditText);

If you want more editTexts, you can initialize an EditText Array, and then loop through the number of edittexts

Solution 2:

Take a ScrollView. In ScrollView you have a LinearLayout. Programatically if user enters n number then take loop upto n. In the loop create a new EditText prgramatically and add to the linearlayout by using myLayout.addView(myEditText).

Solution 3:

Place edittext & recyclerview in your activity_main.xml file:

<EditText
    android:id="@+id/search_edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:singleLine="true"
    android:imeOptions="actionDone" />

    <android.support.v7.widget.RecyclerView
                android:animateLayoutChanges="false"
                android:id="@+id/explore_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

then go its java class

EditTextedit_txt= (EditText) findViewById(R.id.search_edit);

      edit_txt.setOnEditorActionListener(newEditText.OnEditorActionListener() {
        @OverridepublicbooleanonEditorAction(TextView v, int actionId, KeyEvent event) {
           if (actionId == EditorInfo.IME_ACTION_DONE) {

            exploreTopDownRecyler.setDrawingCacheEnabled(true);
            exploreTopDownRecyler.setHasFixedSize(true);
       exploreTopDownRecyler.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
            exploreTopDownRecyler.setLayoutManager(newLinearLayoutManager(context));
        exploreTopDownRecyler.setAdapter(ExploreTopDownAdapter.getInstance(context));
         topDownAdapter = ExploreTopDownAdapter.getInstance(context);
         topDownAdapter.addExploreItem(edit_txt.getText().toString());

         returntrue;
     }
     returnfalse;
 }
  });

Now make adapter class to add no of edittexts you filled:

publicclassExploreTopDownAdapterextendsRecyclerView.Adapter<ExploreTopDownAdapter.ExploreItemRowViewHolder> {

StringTAG= ExploreTopDownAdapter.class.getSimpleName();
intcount=0;
privateintmLastAnimatedItemPosition= -1;
privatestatic Context context;
privatestatic ExploreTopDownAdapter mInstance;


publicExploreTopDownAdapter(Context context) {

    this.context = context;
}

publicstatic ExploreTopDownAdapter getInstance(Context context2) {
    if (mInstance == null) {
        mInstance = newExploreTopDownAdapter(context2);
    }
    context = context2;
    return mInstance;
}

publicvoidaddExploreItem(String item) {

    count = Integer.parseInt(item);
    notifyDataSetChanged();

}

privatevoidanimateItem(View view) {
}

@Overridepublic ExploreItemRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view;
    context= parent.getContext();
    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlayoutforedittext, null, false);
    returnnewExploreItemRowViewHolder(view);
}

@OverridepublicvoidonBindViewHolder(ExploreItemRowViewHolder holder, int position) {

}

@OverridepublicintgetItemCount() {

    return count;

}


publicstaticclassExploreItemRowViewHolderextendsRecyclerView.ViewHolder {


    EditText edittext;


    publicExploreItemRowViewHolder(View itemView) {
        super(itemView);
        edittext = (EditText) itemView.findViewById(R.id.edit);
    }

}
  }

itemlayoutforedittext.xml

<EditText
android:id="@+id/edit"android:layout_width="match_parent"android:layout_height="wrap_content"android:singleLine="true"
 />

Post a Comment for "Android Studio Name Tabs In Activity"