Want to make button that grabs values from dynamicaly created editTexts [duplicate] - java

For an Android App, I'm using a GridView and extending BaseAdapter to organize its contents. For the function getView that I override in my extended BaseAdapter class, I create a LinearLayout, which I attach an ImageView and 3 TextViews to. Now, I need to implement convertView, but because I created my views programmatically, I didn't think I can use findViewById to find these child views to change their properties (like text and bitmaps).
I had the idea of assigning a unique ID pertaining to different types of views for each one when I create them (example: give my name textview the id of 1, description textview the id of 2, etc), but I was not sure if the ids have to be unique among every view, whether they're the same kind of view or not.
Ultimately, how do I find a child view that's part of a linearlayout view which were all created programmatically?

You can get children of LinearLayout via getChildAt() but it's hard to distinguish what child exactly you get.
I think assigning IDs is better way. You can assign IDs to your views and later get views via findViewById(). IDs doesn't need to be unique for each LinearLayout.
Something like this:
// Give more sensible names to ID's
int IMAGEVIEW_ID = 0;
int TEXTVIEW1_ID = 1;
int TEXTVIEW2_ID = 2;
int TEXTVIEW3_ID = 3;
imageView.setId(IMAGEVIEW_ID);
textView1.setId(TEXTVIEW1_ID);
textView2.setId(TEXTVIEW2_ID);
textView3.setId(TEXTVIEW3_ID);
...
// somewhere later
ImageView imageView = (ImageView) convertView.findViewById(IMAGEVIEW_ID);

Related

LinearLayout not working when creating text view2

I'm facing a problem where the code works fine but my text (textview) doesn't appear in the linearlayout which I'm not sure why
this is my code:
LinearLayout linearScrollableMin = findViewById(R.id.linearScrollableMin);
TextView tryingTV1 = new TextView(this);
tryingTV1.setBackgroundColor(000);
tryingTV1.setText("1");
tryingTV1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
((LinearLayout) linearScrollableMin).addView(tryingTV1);
and a picture of the component tree:
There are some things I want to note here:
Always set the background color using java binary numbers (here 0x000000) --> int. Setting the color black would work with 000, because black has the ID 0, but it wouldn't work for any other colors.
When you add a TextView to a ViewGroup (and LinearLayout extends ViewGroup), its LayoutParams are automatically overridden. The LayoutParams are always given to a view by its parent and describe all properties of a view in relation to the parent / siblings
You are casting a LinearLayout to LinearLayout ;)
Actual solution:
Most probably, your LinearLayout is not displayed itself, so its childs can't be either. You can check whether this is true by either calling linearScrollableMin.setBackgroundColor(Color.BLUE) or just add android:background="#color/blue" to your resource file (and remove it later).
If this is the case, work yourself up in the parent tree and see if a sibling blocks out a view in a parent (by f.e. having the width and height MATCH_PARENT). In this case, just use RelativeLayout and you'll be fine.
Another possible reason
could be, that the textColor of tryingTV1 is Color.BLACK (which it is in this case) and your background is also Color.BLACK (which the background of your TextView is). You can solve this by calling tryingTV1.setTextColor(Color.WHITE);.
Edit (how to copy a TextView)
If you want to clone a TextView that you created in your layout file (NOT by calling new TextView(this);), you copy it by calling:
TextView realTextView = findViewById(R.id.whateverView);
ViewGroup layout = getLayoutInflater().inflate(R.layout.whateverLayout);
tryingTV1 = layout.getChildAt(0); // I assume that the TextView is your first child in the layout, if it is somewhere inside of a nested layout, you have to call `getChildAt(index)` multiple times and always cast to `ViewGroup`, or simply just use my suggestion below.
There is no way of cloning a TextView that you created by calling new TextView(this);, but you might try to create one by copying all the nessesary attributes:
TextView realTextView = new TextView(this);
// do something with the realTextView
copy = new TextView(this);
copy.setText(realTextView.getText());
copy.setTextColor(realTextView.getTextColor());
copy.setBackground(realTextView.getBackground());
...
tryingTV1 = copy;

Difrences between changing layout background color in java

I'm a newbie to the android development.
I have two approaches to change the layout background color via Java. Which one could be more appropriate and why?
The first approach is:
LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.translation_container);
layout.setBackgroundColor(mColor);
and the second one:
View view = convertView.findViewById(R.id.translation_container);
int color = ContextCompat.getColor(getContext(),mColor);
view.setBackgroundColor(color);
Well for one there is something very wrong between the two, and that is the fact that you are calling ContextCompat.getColor(getContext(), mColor) with one and directly setBackgroundColor(mColor) on the other.
ContextCompat.getColor is supposed to be called with a color resource ID, ie. a R.color.value. It returns the color value from your resources that corresponds to that resource ID.
Depending on what you have in the value of ```mColor''', there is a good chance that one of these options is not setting the correct color you are expecting.
If mColor is a color resource ID, then the second one is correct. If mColor is the color you want to set, then the first is correct.
When it comes to the cast you are doing into a LinearLayout it is unnecessary. Both options call the same method View.setBackgroundColor. You can go with View for now as the type of your view variable.
LinearLayout extends ViewGroup which extends View. So LinearLayout also a View.
So there is no difference between View.setBackgroundColor() and LinearLayout.setBackgroundColor(). LinearLayout did not override this method.
See View.setBackgroundColor()
See: https://developer.android.com/reference/android/widget/LinearLayout.html

Getting Currently Set Content View

We are creating an app with two main views: sView and sViewSettings. If the Android Back button is pressed we want an if statment to check if the current view is set to sView settings, if it is then call the sView.
Already have a listener setup for the back button just need it to call the if statement to check the current view.
Have already tried
if (this.findViewById(android.R.id.content) == sViewSettings)
Any ideas on this?
Thank you for Reading,
Travis
The view with id android.R.id.content is a FrameLayout holding your content view. Try this:
ViewGroup contentFrame = (ViewGroup) findViewById(android.R.id.content);
if (contentFrame.getChild(0) == sViewSettings) { ... }
However, I suggest a slightly different approach: use a ViewSwitcher (or any kind of ViewAnimator) to flip between the two main views and keep track in your code of which one is on display.
EDIT: If you want to keep your layouts loaded separately, you can assign an id (the same one) to the root view of each layout and then retrieve the content view directly using findViewById.

Android - how to inflate a view with multiple EditText fields and read their values?

For my Android application, I want to have a view which allows a user to click a plus button to add EditText fields, and next to the EditText fields, I want to have minus buttons that will remove them from the view. In essence, something that is very similar to adding multiple phone numbers/email addresses in the edit Contact interface on Android.
I imagine I will need to do this by inflating my main view with a separate one that contains the EditText and button I want to add each time. However, I have no idea how I will manage identifying each EditText and button with a unique ID, and thus, I have no idea how I would manage to grab the values of each EditText for saving to my database. Can someone advise me on what I need to do? Thank you.
If you have inflated a sub-layout, then you should now have a View object.
You can then call findViewById(R.id.edit_text_1) on that View — assuming that you supplied IDs in the sub-layout XML.
So long as you keep track of each of the parent Views, you can always use findViewById() on them. Or after inflation, if you really want you can set a new, globally-unique ID on each EditText using setId().
You can assign a view widget dynamically, and generate your own ID as it is assigned.
Button b = new Button(this);
b.setId(myId);
Then you can refer back to that widget.
findViewById(myId);

Calling Button from different view in Android

In my app I have 3 radiobuttons on top of the view. When i choose one the 'body' underneath that changes to the XML-view i defined.
(If you want more information + pics, I asked a question about this earlier: Dynamically change view inside view in Android)
Now i want to call and change the text of the buttons and edittext's from the different views.
When I do btnAutopech = (Button) findViewById(R.id.btnAutopech); it's gives an NullPointerException.
How can I do this?
Try this.......
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll= new LinearLayout(context);
ll=(LinearLayout)layoutInflater.inflate(R.layout.gegevens_verzekeringen, ll);
btnAutopech = (Button) ll.findViewById(R.id.btnAutopech);
Thanks.........
Depends on how you solved the last question you linked to. If your inflating new views into the area you want to change then you won't be able to retreive a reference to them using finViewById. I.e. they don't actually exist.
When you re-display your view, within which you want to display text based, on your other views outcome, you would have to re-assign the text when you re-attach/display your view. You could then assign text from a member variable of the class, or perhaps from sharedPreferences or a contentProvider.
EIther way, this sort of all depends on how you've solved the issue of your original question and when you attach/remove your views.
To summerise:
It looks like your removing your other views when you visit other views, so store your data in a member variable that persists.
attach view A
got to view B
Click button or something in view B and update member variable used by view A
Go to view A (Removing view B and attaching view A)
set text on view A from member variable (updated as a result of clicking button in view B for example)

Categories

Resources