I've built a custom editText component by extending LinearLayout and aligning a textview and an editText. I reuse them for building multiple input programmatically.
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_input_label"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="#dimen/very_small_margin"
android:layout_weight="1"
android:textAppearance="#style/TextAppearance.MaterialComponents.Body1"
android:textColor="#color/input_label_color" />
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/text_input_edit"
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_marginBottom="#dimen/small_margin"
android:drawablePadding="#dimen/small_margin"
android:background="#drawable/custom_input_bg"
android:paddingStart="#dimen/default_margin"
android:paddingEnd="#dimen/small_margin"
android:paddingTop="#dimen/small_margin"
android:paddingBottom="#dimen/small_margin"/>
</merge>
The problem is that with this way of rendering inputs, I endup with editText sharing the same ID and when I leave the fragment containing them and I come back, the editText all has the same values. Even setting their ID to random int does not work.
What can I do to ?
Thanks for any kind of advice or help.
I have defined the id to View.NO_ID
And it seems that the problem is now solved.
Related
Few days ago I want to customize a TabLayout like this (RallyTab), which uses Flow and can rearrange the elements according to the clicked icon.
I read its source code, and its function of rearranging elements seems to be achieved by the ConstraintHelper.setReferencedIds(int[] ids) on line 168. But when I try the following code, it does not work:
findViewById(R.id.button).setOnClickListener(v -> {
Flow flow = findViewById(R.id.flow);
int[] ids = new int[] {R.id.image1, R.id.image3, R.id.image2, R.id.image4, R.id.image5};
flow.setReferencedIds(ids);
});
This is layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp">
<androidx.constraintlayout.helper.widget.Flow
android:id="#+id/flow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:constraint_referenced_ids="image1, image2, image3, image4, image5"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/image1"
android:layout_width="52dp"
android:layout_height="52dp"
app:srcCompat="#drawable/ic_baseline_exposure_neg_2_24" />
<!-- image2, image3, image4, image5 are the same as image1, only the id and srcCompat are different -->
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
</LinearLayout>
Even if I copy its source code and layout xml to my project, it still not work. When i clicked on its icon, the position of elements did not change, only the TextView was playing its beautiful animation.
So I realized that the ConstraintHelper.setReferencedIds(int[] ids) method does not change the position of elements (or it will, welcome to correct me :-)), and RallyTab may have called some method in somewhere that I did not notice to rearrange them.
How do I do to change the position of elements like RallyTab? Can anybody point out where I missed? Or what should I do to achieve this function without Flow? Thanks in advance.
I would like to display the following five times
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/message"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/add"/>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/separator_height"
android:background="#color/separator"/>
Thus I store it in data_item.xml and try calling it via
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/data_one"
android:layout_height="match_parent">
<include layout="#layout/data_item"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/data_two"
android:layout_height="match_parent">
<include layout="#layout/data_item"/>
</LinearLayout>
However if I do that, then all of the items inside data_item.xml layout are going to have duplicate Ids. How would I go about making sure that they do not have duplicate ids, or at least somehow retrieving them? Let's say I want to get title view text from data_three liner layout. How would I do that?
the first question is why you don't use ListView instead and set values in your adapter? this is better.
But if you have to design your layout in this way, you have to at first get reference from your LinearLayout, the find your view by using LinearLayout reference, as following:
LinearLayout dataOne = findViewById(R.is.data_one);
TextView dataOneTitle = dataOne.findViewById(R.id.title);
I have a view(Let's say it is MyFramLayout) extends FrameLayout, I can not get this FrameLayout get focus when press key(I am working in TV platform).
the layout is something like this:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.roger.MyFrameLayout
android:id="#android:id/my_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true" />
</LinearLayout>
Anyway to fix the problem?
Whenever you want your custom layout to get focus you will have to call
boolean b = yourLayout.requestFocus();
This will return a whether focus is gained or not.
In my android app, in my profile edit page, when I start the activity, the first edittext field is focused (blinking cursor), and the keyboard gets displayed.
How can I keep it being focused on startup (blinking cursor) but prevent the keyboard from showing up? If that is not possible, then just not focus the edittext on startup.
Thanks.
<EditText
android:id="#+id/textinput_firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="test" />
Just add this line of code in your onCreate(...) method
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
This is probably happening for EditText for most users, all you have to do is, just go to the layout file and set the layout's view groups attributes as follows:
android:focusable="true"
android:focusableInTouchMode="true"
For Example.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="41dp"
/>
</LinearLayout>
Note: you have to set the attributes for the first view in the layout
no matter what it is This will set the default focus to false;
Does Android EditText has input type with clear button?
It should work like html 5 search input type.
There is no internal input-type, but you can customize it to work so. This link might help you: Stack-Overflow: EditText with clear button
If you're using TextInputLayout that you can use endIconMode and endIconDrawable
You can use the following, it works quite well
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:endIconMode="clear_text"
tools:hint="Label">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Value" />
</com.google.android.material.textfield.TextInputLayout>
But this approach would not be valid for a MaterialAutoCompleteTextView