I just started learning android programming and can't seem to work out how to permit the user to create a selected number (n) of EditText fields by clicking a button n times such that each would be uniquely identified, namely 1,2,3...,n as well as accessible programatically, from a different method (invoked by a different button click). I hope the question is clear enough as I don't really have that much code to provide.
Do something like this
// A list to keep reference to your created edit texts
List<EditText> mEditTexts = new ArrayList<EditText>();
// Get root view of your activity
ViewGroup viewGroup = (ViewGroup) ((ViewGroup)
findViewById(android.R.id.content)).getChildAt(0);
// Get the button and set a click listener to it
Button mButton = (Button) findViewById(R.id.button_id);
mButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
// Build edit text
EditText mEditText = new EditText(v.getContext());
// Pass two args (arg1/arg2); must be LayoutParams.MATCH_PARENT,
// LayoutParams.WRAP_CONTENT, or an integer pixel value.
mEditText.setLayoutParams(new LayoutParams(arg1, arg2));
// Add the edit text to your list
mEditTexts.add(mEditText);
// Add edit text to your root view
viewGroup.addView(mEditText);
}
}
To check your edit text fields you can then access them from the list
for(EditText editText : mEditTexts){
Log.d(TAG, editText.getEditableText().toString());
}
or explicitly
int specificPosition = (SOME_INT);
EditText specificEditText = mEditTexts.get(specificPosition);
Haven't been able to test it so it might need some modifying but it should be something along those lines. You can also use your layout directly if you don't want to use the viewGroup. Modify it to something like
LinearLayout mLayout = (LinearLayout) findViewById(R.id.layout_id);
....
mLayout.addView(mEditText);
Related
With RecyclerView, the off-screen EditTexts will be recycled. For example, if we have a list of 200 items, and it shows 2 items at one time, we will only ever have 2 EditText. They will reuse the higher EditText for the lower elements.
For example, here is a list that contains EditText showing only 2 at a time, and as the user scrolls, it will recycle and reuse them.
EditText A
Edittext B
EditText C (recycled)
EditText D (recycled)
....
This means we cannot just loop over all the elements later and get the values, as they don't store their values.
So, what i am asking is that is there anyway to loop over all the items and get there values even the recycled ones and save them to firebase
EDIT:
I have a recyclerview with an edit text inside it the user enter numbers inside edit text and when he click save button a for loop iterate over all the recyclerview edit text and save data to Firebase
this is my fragment class
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.purchases_list_activity, container, false);
mItem = new ArrayList<>();
mAdapter = new PurchasesAdapter(getContext(), mItem, this);
re = view.findViewById(R.id.itemsPListView);
re.setLayoutManager(new LinearLayoutManager(getActivity()));
re.setAdapter(mAdapter);
fab = view.findViewById(R.id.fab_purchases);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < mItem.size(); i++) {
final ItemsAdapter mylist = mItem.get(i);
View childView = re.getChildAt(i);
final EditText Qty = childView.findViewById(R.id.itemNewPcs);
final String qty = Qty.getText().toString();
Toast.makeText(getContext(),qty+"",Toast.LENGTH_LONG).show();
..
}
I think that what you might need is the RecyclerView's OnChildAttachStateChangeListener functionality. Basically, you implement your own class that implements the OnChildAttachStateChangeListener interface. Specifically, the implement the onChildViewAttachedToWindow method to do save to Firebase or add to a list that can be added to Firebase later - basically whatever you need. You just attach your custom listener via the addOnChildAttachStateChangeListener() method.
Hope that helps.
So i have this program which create my list of cards which are relative layouts and they look like this.
Here is the code of it creation. Ps its in a for loop
LayoutInflater mInflater = (LayoutInflater) atv.getSystemService(atv.LAYOUT_INFLATER_SERVICE);
LinearLayout relativeLayoutz = (LinearLayout) mInflater.inflate(R.layout.rtl_enterprise, parent);
RelativeLayout rl = (RelativeLayout) relativeLayoutz.findViewById(R.id.rl_empresa);
rl.setId(i);
ImageView img = (ImageView) rl.getChildAt(0);
//
//Performance bottleneck needs fixing/ not loading all images and overloading thread
//
//loadImageByUrl(atv, enterprises.getEnterprises().get(k).getPhoto().toString(), img);
TextView txt = (TextView) rl.getChildAt(1);
txt.setText(enterprises.getEnterprises().get(i).getEnterpriseName());
TextView txt2 = (TextView) rl.getChildAt(2);
txt2.setText(enterprises.getEnterprises().get(i).getEnterpriseType().getEnterpriseTypeName());
TextView txt3 = (TextView) rl.getChildAt(3);
txt3.setText(enterprises.getEnterprises().get(i).getCountry());
LinearLayout relativeLayout = (LinearLayout) mInflater.inflate(R.layout.rtl_enterprise, parent);
relativeLayout.setId(i);
everything there works kinda ok, but i have a problem, i need to create a
onClick listener
Why?
I would call a method which needs some info about the card that has been clicked, and then redirect to a new activity which contains the info about the card that he clicked.
But i would need that onclick listener for each of these relative layouts, and i assume it would need to be initialized when the card is created since it create + 50 cards, but i have no idea of how to setup each listener.
Why do you create it like that?
a better solution to use RecyclerView.
and in the adapter, you can listen for the item click. write the code one time and when any item clicked. you will know the position of the clicked item.
see this tutorial
https://developer.android.com/guide/topics/ui/layout/recyclerview
https://www.androidhive.info/2016/01/android-working-with-recycler-view/
I've created a method which adds a number of TextViews to a LinearLayout in a while loop. I'm having trouble identifying those TextViews by click. If I click on them, it prints out that they are the ID of my linear layout. Is there any way I can access them?
while(i.hasNext()){
TextView x = new TextView(this);
linearLayout.addView(x);
}
EDIT:
I understand this was an absurdly dumb question.
This...
x.setOnClickListener(this);
...did the trick.
Simple, you will get the reference of clicked view in OnClickListener() callback. type cast that view into textview and call getText() method of textview.
Here is sample code:
while(i.hasNext()) {
TextView x = new TextView(this);
linearLayout.addView(x);
x.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView) v;
v.getText();
}
});
}
if you'd be using a for loop you could do something like this x.setId(i+30); any number in place of 30. In while you can still take a variable i and do something like x.setId(x++ +30); . now if you want to access them from a loop its simple findViewById(i+30); or else findViewById(31);
I have a 'ChecklistItem' class that has the following properties:
private CheckBox checkBox;
private ImageButton noteButton;
private TextView vitalField;
I have an onClick Listener for my checkbox. Now the problem is, when I click on that checkbox and the OnClick() method gets called, how can I figure out what ChecklistItem that checkbox is a part of?
Whenever I click on a checkbox, I want to add the ChecklistItem that the checkbox is a part of to an array, but the OnClick() only knows about the checkbox that called it.
How can I get around this?
Ok so this answer is according to the "long discussion" we had
let's assume you want to make a - re usable - view of your list and you wrote a separate xml layout file called list_item as the following:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view"/>
so now let's assume you are in the activity or fragment or wherever you want to host your view , NOW I have to point out this is just an example , usually a list view is what you would need in this case but again I have very little details about your app so I'm going to keep it simple
Assuming you have a vertical linear layout and you want to add these "rows" to it, each row represents one of your custom view
LinearLayout layout = findViewById(R.id.layout);
LayoutInflater inflater = LayoutInflater.from(this); // This inflater is responsible of creating instances of your view
View myView = inflater.inflate(R.layout.list_item, layout, false); // This view objects is the view you made in your xml file
CheckBox checkBox = (CheckBox) myView.findViewById(R.id.checkbox);
TextView textView = (TextView) myView.findViewById(R.id.text_view);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//if checkbox is checked enable textview for example
// here you have a reference to all the views you just created
// Weather you want to save them in a class together that's up to you and your app's logic
}
});
layout.addView((myView));
if the list is might exceed the screen height you may want to wrap your linear layout in a scroll view.
BTW: ListView is just a neat way to do this automatically by defining how you want each row to appear, and of course it manages your views for you and recycle them when they get of screen, but I just wanted to point out the concept.
Hope this helps you
In the code below, the first TextView symbol cannot be resolved, and the findById method cannot be resolved. Can someone explain to me what the problem is and how I can fix it?
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String fact = "";
// Randomly select a fact.
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
You need to give more background. Are you in a fragment, or an activity? How was the layout set? I'm guessing the answer is that you need to be calling findViewById() on the View of your layout - e.g. view.findViewById() but it depends on how your layout was set.
If you're in an activity, calling findViewById() on the Activity object will only work if the current Activity layout is set by setContentView. If your layout was set a different way, then you need to get the View object of the layout and call findViewById() on it. If you're in a fragment, and you're in onCreateView() then the view has been passed in for you and you just need to call view.findViewById()
I am asuming that you want some text to set on text view on click of button try this
final TextView factLabel = (TextView)findViewById(R.id.factTextView);
Button showFactButton = (Button)findViewById(R.id.showFactButton);
showFactButton.setOnClickListener(new View.onClickListener()
{
#Override
public void onClick(View v) {
String fact = "Your Text Here";
// Randomly select a fact.
// Update the label with our dynamic fact
factLabel.setText(fact);
});
Make sure you are importing all the packages you need. Also make sure all the dependancies are working. Lastly make sure that there is a text view in the XML for the activity. Hooe some of this is helpful.