"In-place" EditTextPreference - java

I used the XML syntax to create a PreferenceActivity with a PreferenceScreen. I added an EditTextPreference and noticed that this renders as a dialog. Is there a way to make the EditText in-place i.e., the text field is displayed right in the PreferenceScreen instead of popping up as a dialog?

In case anyone comes across the same problem, this cannot be accomplished with the built-in Preference classes, but you can subclass Preference, and override getView() to return a ViewGroup containing an in-line EditText (or simply the EditText itself).
You could probably achieve this by providing the android:layout attribute in the XML too (although I haven't tried this).
EDIT:
I tried the android:layout approach above, and it works pretty well.

add android:layout="#layout/your_loyout".
where your_layout.xml is like
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />

Related

Relation of EditText with its Id in Android Studio?

Please Read My Question Carefully
I am learning Activity LifeCycle. I only have one EditText and a TextView in my XML layout With certain id. When I rotate the screen, nothing seems to change because Rotation doesn't affect EditText. But when I remove the id of EditText in XML and rotated the screen, The Rotation Starts affecting and the text in the editText removed due to rotation. I am confused about the relation of EditText with its Id.
I tried to explain the problem below in columns:
Explanation of below Column names
Having Id: Does EditText has id?
Rotation : Phone has Rotated or not
EditText : What happen to EditText After Rotation.
.
Having Id----------------Rotation---------------EditText
Yes Rotated Does not change
No Rotated Yes, Text Removed from editText
MainActivity.java is empty and main_activity.xml code is below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter here"
android:inputType="textPersonName"
android:textSize="26sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.371" />
<TextView
android:id="#+id/textView2"
android:layout_width="85dp"
android:layout_height="25dp"
android:layout_marginTop="88dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Sorry for using bad English and High-resolution pictures as I have no other ways. Best Regards
One of the most important aspects in Android programming is to handle the configuration changes and most common of them is orientation changes. Although it is not recommended to handle configuration changes yourself, due to the hidden complexity of handling the configuration changes, there are cases where we might need to handle manually.
If you need to save the state, the common approach is to implement onSaveInstanceState to store & restore the values which are passed as Bundle.
Coming back to your question:
Relation of EditText with its Id in Android Studio?
When it comes to saving state of View Android OS automatically calls the View.onSaveInstanceState method on each of the views in your view hierarchy as long as you call through to the super method in onSaveInstanceState. In order to restore the data, it uses android:id attribute key for that particular View’s state.So for your case, when you don't provide the id for EditText, the View's state won't be stored due to missing key.
Hope this clarifies the relationship of id to EditText
When you rotate screen on android all activities and fragments in your application are destroyed and then recreated. Android does this so that your application can reload resources based on the new configuration.
When the system destroys the activity (e.g. when the screen rotates) then although the actual Activity instance is gone, the system remembers that it existed. The system creates a new instance of that activity using a set of saved data that describes the state of the activity when it was destroyed.
when your assign an id to a view it seems that android keeps track of that view. Hence it remembers it's state.
Okay when you rotate the device this is considered as a configuration change, so the system recreates the activity and data is lost.
One way to avoid this is go to the manifest and for your specific activity add this
android:configChanges="orientation|screenSize"
here:
<activity
android:name=".YourActivityName"
android:configChanges="orientation|screenSize"
....
...
This makes the system ignore the rotation of device.
In manifest of your app, add this line to activity tag:
android:configChanges="keyboardHidden|orientation|screenSize"
The reason for this is due to Android basically destroying the activity and creating it again every time you rotate the device. This is mainly to allow for different layouts based on portrait/landscape mode.
When you use id for your component, data will be assigned to its id, but when it hasn't id, it will be recreated and data will be disappeared after rotation.
See these questions and read their good answers, I don't copy and paste them:
1- TextView's text disappearing when device rotated
2- Handle screen rotation without losing data - Android
EditText is a focused view, so in PhoneWindow, it's state will be saved automatically in saveHierarchyState() method. Only EditText with id is going to save text.
Okay, the relation of one specific EditText item to its id is: "one to one" or "one to N". One EditText item in one layout.xml has one id, if you've configured it in the layout.xml. If you've defined more than one layout.xml's and the very same EditText item (having the same id) in more than one layout.xml's, then EditText has a "one to N" relation.

How to hide IME when EditText is focused

I want EditText not to extend IME when it is focused on Android Wear.
I read and implemented in the same way as the following thread:
Prevent EditText from automatically focusing
However, IME appears.
Is it impossible to hide IME for an EditText on Android Wear?
I tried 3 methods:
Added the following code in the onCreate() method:
{getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);}
Added the following code in the activity tag in AndroidManifest.xml
android:windowSoftInputMode="stateAlwaysHidden"
Added the following code in rect_activity_main.xml
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#android:color/transparent"
android:focusable="true" android:focusableInTouchMode="true"/>
None of them worked.
Best regards, gellpro
Check Show the Input Method On Demand. It's noted that the system hides the input method when the user finishes the task in the test field or the user can hide it with a system control. With this, try to specify how your UI should respond. It's mentioned that,
To declare your preferred treatment in an activity, use the android:windowSoftInputMode attribute in your manifest's element with one of the "adjust" values.
Then, to implement hiding IME, try using InputMethodManager with constant set to RESULT_UNCHANGED_HIDDEN. This will flag for the ResultReceiver result code from showSoftInput(View, int, ResultReceiver) and hideSoftInputFromWindow(IBinder, int, ResultReceiver): the state of the soft input window was unchanged and remains hidden.
Hope that helps.

Change ImageView to TextView in GridView based on user input

I am developing an android app as an educational project. I have created a GridView object, which uses a custom GridAdapter. The grid, as default, loads a set of placeholder images. I want the user to be able to replace those placeholder images with either their own image, some text, or a link to a youtube video.
I can't find any info on whether this is feasible. I specify an Imageview in my XML file, so I don't know how I could change this at run time? I wondered if I should create a TextView object in the XML, and layer them, and then create a method that brings one to the front? Any advice at all would be very much appreciated. It would be great to know if it's doable or not.....
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_centerInParent="true"
/>
My suggestion is define TextView etc. in xml with gone visibility. When user has some actions get visible them and set visibility gone of ImageView.
Set the visibilities of the container based on user input... What ever user chooses make tat container visible and other two as Gone... This would work

Changing AlertDialog buttons alignment

Been fighting for hours about changing the alignment of the buttons inside AlertDialog (support.v7 one), since they won't align themselves according to the locale view direction, despite the whole app DOES align to left and also the text inside the AlertDialog.
(Why would this happen you say? I'm programatically configuring the locale language to be "en" since that's my default app language, even though the system locale might be something else).
So like I said, I don't need to touch the message inside the dialog, but as an example, that's how to change it's direction:
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.RIGHT); // or LEFT
Of course it doesn't work on the buttons, since I need to change the layout gravity instead.
Here's how I find the buttons (after I call show() on the AlertDialog.Builder of course, else they would be null):
AppCompatButton accept = (AppCompatButton)dialog.findViewById(android.R.id.button1);
AppCompatButton cancel = (AppCompatButton)dialog.findViewById(android.R.id.button2);
And here's how I attempt to change their alignment inside their parent LinearLayout:
((LinearLayout.LayoutParams)accept.getLayoutParams).gravity = Gravity.RIGHT;
((LinearLayout.LayoutParams)cancel.getLayoutParams).gravity = Gravity.RIGHT;
I chose RIGHT since the side of the buttons inside the dialog is always opposite to the side which the text is aligned to. (Yes - I tried LEFT also, nothing changed).
This doesn't work. Does anyone have an idea how to achieve this? It seems they are just stuck to their place.
Edit:
Title isn't aligned also, I just confirmed this (for some reason it appears on the right, like my system configuration, and not like my locale configuration).
The problem isn't the Gravity settings... When you look at the xml source (../sdk/platforms/android-23/data/res/layout/alert_dialog_material.xml), the layout's AlertDialog contains this:
<LinearLayout android:id="#+id/buttonPanel"
style="?attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" ...>
<Button android:id="#+id/button3"
style="?attr/buttonBarNeutralButtonStyle" ... />
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible" />
<Button android:id="#+id/button2"
style="?attr/buttonBarNegativeButtonStyle" ... />
<Button android:id="#+id/button1"
style="?attr/buttonBarPositiveButtonStyle" ... />
</LinearLayout>
There is a Space view with the buttons. This view fills the container with its weight. Therefore, you actually have a widget which pushes the buttons at the right's parent container.
A simple solution might to get the parent's buttons container and remove the Space element before setting a gravity.
// get the container
LinearLayout containerButtons = (LinearLayout) dialog.findViewById(R.id.buttonPanel);
// remove the Space view
containerButtons.removeView(containerButtons.getChildAt(1));
// set a gravity to the children buttons
containerButtons.setGravity(Gravity.RIGHT);
However, you should create and use your own custom layout, in case of future Google's development changes might occurring.
You can completely customize an AlertDialog.
The trick here is probably to use a custom view for the dialog, and create your own buttons in that view.
For an example, see
How can can I add custom buttons into an AlertDialog's layout?

RelativeLayout and accessebility support

I am adding accessebility support to some application. It works fine with standart UI elements (buttons for example), but for some reason does not work with my custom element, wich is RelativeLayout with ImageView and TextView (it looks like icon). I've defined android:focusable="true" and set contentDescription.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:contentDescription=”my content description”
android:focusable="true">
<ImageView
...
/>
<TextView
...
/>
</RelativeLayout>
Could someone please list here all posible causes?
UPDATE:
Is there some way to know what layouts are on the screen at the moment and what order do they have (some layouts are transparent)?
Use hierarchy viewer for understanding where is your invisible views.
The android docs have a section about designing for accessibility, is this any use to you?

Categories

Resources