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.
LineLinearLayout llayout = findViewById(R.id.yourlinearlayout);
EditText yourEditText= new EditText(this);
yourEditText.setLayoutParams(new LinearLayout.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
EditText edit_txt = (EditText) findViewById(R.id.search_edit);
edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(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(new LinearLayoutManager(context));
exploreTopDownRecyler.setAdapter(ExploreTopDownAdapter.getInstance(context));
topDownAdapter = ExploreTopDownAdapter.getInstance(context);
topDownAdapter.addExploreItem(edit_txt.getText().toString());
return true;
}
return false;
}
});
Now make adapter class to add no of edittexts you filled:
public class ExploreTopDownAdapter extends RecyclerView.Adapter<ExploreTopDownAdapter.ExploreItemRowViewHolder> {
String TAG = ExploreTopDownAdapter.class.getSimpleName();
int count = 0;
private int mLastAnimatedItemPosition = -1;
private static Context context;
private static ExploreTopDownAdapter mInstance;
public ExploreTopDownAdapter(Context context) {
this.context = context;
}
public static ExploreTopDownAdapter getInstance(Context context2) {
if (mInstance == null) {
mInstance = new ExploreTopDownAdapter(context2);
}
context = context2;
return mInstance;
}
public void addExploreItem(String item) {
count = Integer.parseInt(item);
notifyDataSetChanged();
}
private void animateItem(View view) {
}
@Override
public ExploreItemRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
context= parent.getContext();
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlayoutforedittext, null, false);
return new ExploreItemRowViewHolder(view);
}
@Override
public void onBindViewHolder(ExploreItemRowViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return count;
}
public static class ExploreItemRowViewHolder extends RecyclerView.ViewHolder {
EditText edittext;
public ExploreItemRowViewHolder(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"