Dynamically made Layout with EditText - Android - java

This isn't much of a question, more of an explanation, I like to know whats happening and not treat things like a black box. I'm making an android app, and I created a form where you can dynamically make a new field to add to the form, and the field has 2 edit text next to each other.
formLayout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="#+id/team_form_player_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginRight="7dp"
android:layout_weight="1"
android:background="#color/white"
android:hint="#string/form_hint_team_player_name"
android:inputType="textCapWords"
android:padding="15dp"/>
<EditText
android:id="#+id/team_form_player_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#color/white"
android:hint="#string/form_hint_team_player_number"
android:inputType="number"
android:padding="15dp"/>
</LinearLayout>
And this is inside a button listener when clicked, it'll create the new field
View layout = getLayoutInflater().inflate(R.layout.layout_add_new_field_team, null);
EditText name = (EditText)layout.findViewById(R.id.team_form_player_name);
EditText number = (EditText)layout.findViewById(R.id.team_form_player_number);
playerName.add(name); //arraylist
playerNumber.add(number); //arraylist
containerLayout.addView(layout);
being that its a different layout i had to inflate the view. anyway, my question is this. how does this even work? by code, I'm adding an edit text, each time to its respectful array. im adding the same layout each time, im not even using "new" so its not instantiating anything new. and if I create 10 fields at once, then fill in the fields. and run this method
for(EditText edit : playerNumber) {
String test = edit.getText().toString();
System.out.println(test);
}
it gives me all the correct values in each field. How does that happen? Because on add field click, it instantly adds the edit text to the array with the fields EMPTY. and theres no code inputting the values into the array. I'm just boggled that this works and would like to know how it does. anyone have any idea?

.inflate(layoutResId) == "create new instance of this layout"
So basically every time you use inflate you are creating instances of all the Views in the layout, in your case a LinearLayout and two EditText Views inside it.

Related

Android make layout and use it again [duplicate]

This question already has answers here:
How to include layout inside layout?
(6 answers)
Closed last year.
I have my main activity. Inside this main activity I want to create an unknown number of layouts that each one of them include one button.
I want smart way to do it - make the layout one time and than use it a lot of time.
What is a good way to do it?
Thanks
Make One Layout Resource File Using Below Code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="#dimen/_100sdp"
android:layout_marginTop="#dimen/_15sdp"
android:gravity="center"
android:textColor="#color/black"
android:textSize="#dimen/_13sdp" />
<Button
android:layout_width="match_parent"
android:layout_height="#dimen/_35sdp"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginTop="#dimen/_20sdp"
android:layout_weight="1"
android:gravity="center"
android:text="Submit"
android:textColor="#color/white"
android:textSize="#dimen/_10sdp" />
</LinearLayout>
And In Your Main Activity Layout You Can Use Only
<include layout="#layout/layoutresourcefilename"/>
Trying to be smart commonly results in the opposite. Better keep it simple;
Which means, just add three buttons and then show either one of them.
This has the advance, that the events are already bound, ready to click.
And also, meanwhile it's a whole lot more common to inflate Fragment
or to data-bind views, which would permit for hiding/showing buttons.

Problem with aligning textView to the end of LinearLayout

I am making chatting app and want to style messages as they are in messenger app.
And this is my code to style them:
textView.setTextColor(Color.parseColor("#FFFFFFFF"));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(400,0,10,10);
params.gravity = Gravity.END;
textView.setLayoutParams(params);
linearLayout.addView(textView);
This is my XML code:
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="12dp"
android:background="#drawable/background"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="#+id/editTextTextPersonName2"
app:layout_constraintTop_toBottomOf="#+id/name">
<LinearLayout
android:id="#+id/chatMessages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
And my TextViews are added to this ^ linear layout.
And this is what I am receiving in app: (don't translate messages they are only for test purposes and they are stupid)
And I do not why those gaps are made, someone please help me.
It is the normal behavior of textView. You can test on the SIGNAL app by typing a long word and it will move it to the next line and on the upper line, you will get to see the same space left as you are getting in yours.
If you mean to change the alignment of text in the textview then you can define the textAlignment property of textView according to your requirement.
That's happening because of the long length of the last word of your messages, if Android sees that he can't put a sequence of chars (word) into the current line, then it types it down on the next one, so to solve this problem you have 2 different solutions (or more):
1- But it may live a gap from the other side
textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
2- It centers the text in its container which may live a gap from both sides
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
Good luck.

Make infinite scrollable ImageViews

I'm making an app which allows a user to search the various countries. The user can make filtered searches (such as, for example, search only countries of a specific continent, etc...). All this info (Countries and their continents are stored in my Firebase Realtime Database).
In my FilteredResults.java fragment I want to have a variable number of ImageViews (the number of the size of a List<String>).
This sketch I drew might help you understand it better:
Each one of this rectangles are ImageViews.
This is my XML (I just have a scrollview because I don't know how to create an "array" of ImageViews...)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtResultadosFiltrados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="28dp"
android:layout_marginTop="0dp"
android:text="Resultados Filtrados"
android:textColor="#323B45"
android:textSize="24sp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="28dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ScrollView>
</RelativeLayout>
In order to solve your problem in an optimal way, you should use one of:
a plain old ListView,
a newer, more powerful RecyclerView.
These views support so called adapters which map between a model (in your case: List<String>) and a list of items (in your case a list of ImageView's).
Here you have a complete example of a simple app which loads lists of posts from the Firebase Database:
https://github.com/firebase/quickstart-android/tree/master/database
This example is based on RecyclerViews, you can find an a direct usage here:
https://github.com/firebase/quickstart-android/blob/master/database/app/src/main/java/com/google/firebase/quickstart/database/fragment/PostListFragment.java
Use RecyclerView instead of ScrollView. As keeping array of ImageView and adding them in a ScrollView will create a explosive headache.

Android - Hidden but Select-able EditText

I would like to have a EditText in my Android app that you can not see, however you can select and type text into. In other words I would like it so that it can not be seen at all however other than that it behaves completely normally.
Another option is to have an image or button on the screen which the user presses to enter text into the EditText which is hidden behind another EditText.
Thanks for the help, however it is looking more likely that I will have to use the second option. To elaborate on it, for example I have a TextView that says 'Welcome'. I would like the user to be able to click on this text to bring up the keyboard and edit in the EditText field. The reason for having the EditText field hidden behind another is to cover up the cursor while making it seem the user is typing the text which appears on the screen.
If you would like to make the EditText fully invisible (even what the user types in) , but still be able to retrieve the entered data through myEditText.getText().toString(),
you could add:
android:textColor="#android:color/transparent"
android:background="#00000000"
android:cursorVisible="false"
if I'm getting you correctly. Here is how you can accomplish the first option of yours by setting the background to transparent and cursor visibility to false.
To check this you have to click at the center of screen.
e.g. code snippet:
<EditText
android:id="#+id/eT1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#00000000"
android:cursorVisible="false"
android:ems="10" >
</EditText>
Set transparent Background to EditText.
android:background="#00000000"
Sorry but I didn't understand your another question.

Dynamically defining how an icon is drawn

I want to have a "comments" button on my Android e-reader app that displays the number of comments currently posted inside the icon... so basically I want a comment bubble with a number inside it.
I could have multiple drawables, each being the bubble with a different number inside of it, and use a switch statement to choose which one to load each time based on the int number_of_comments field of the element being displayed. This approach seems a little wasteful though, and in any case I have a feeling there's a more elegant way to do it.
Any ideas?
You can do better. You can have a textview on top of the image view and keep updating its value everytime a new comment is added. You can define the overlap in xml like below and adjust your code logic accordingly to increase the comment count. For now I have just set up a dummy text Hello to show on top of the ImageView. You can add your comment count using the TextView's setText method.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myImageSouce" />
<TextView
android:id="#+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/myImageView"
android:layout_alignTop="#+id/myImageView"
android:layout_alignRight="#+id/myImageView"
android:layout_alignBottom="#+id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
Hope this helps...
check out this 3rd party created widget
Android ViewBadger
You can use it to create the little number bubbles that you are looking for I think. This gives you the benefit of not having modify your layouts so much to achieve what you are trying to get.
Here is the sample code to apply a "badge"
View target = findViewById(R.id.target_view);
BadgeView badge = new BadgeView(this, target);
badge.setText("1");
badge.show();

Categories