How do I position the cursor at a default position? - java

The image shows the screenshot of my application. As you can see that the cursor is at the text field pertaining to "Height". Whenever I launch my app the default cursor position is at "Height". I want it to be on "Mass".
How do I do it ? I can't figure out. Please help.!

Take a look at your .xml file:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:ems="10" >
<requestFocus />
</EditText>
And set the <requestFocus /> tag appropriately.
Also, you can call:
EditText.requestFocus();

Either update your .xml file as follows.
<EditText
android:id=#+id/etMass
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<requestFocus/> </EditText>
Or Use Code:
EditText etMass = (EditText) findViewById(R.id.etMass);
etMass.requestFocus();

Related

How to put gap for toggle image and hint text in Edittext android?

I am new for android. i want space gap between toggle image and hint text same field of edit text
.
This is my edit text xml code
<EditText
android:id="#+id/nation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/email"
android:layout_centerHorizontal="true"
android:drawableLeft="#drawable/ic_flag"
android:layout_marginTop="11dp"
android:inputType="textCapWords"
android:hint="Nationality"/>
use :
android:drawablePadding="20dp"
to change Hint text color use:
android:textColorHint="#android:color/darker_gray"
<EditText
android:id="#+id/nation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/email"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:drawableLeft="#drawable/ic_flag"
android:drawablePadding="20dp"
android:hint="Nationality"
android:textColorHint="#android:color/darker_gray"
android:inputType="textCapWords" />
Here is solution
I want space gap between toggle image and hint text same field of edit
text
set drawablePadding android:drawablePadding="15dp"
<EditText
android:id="#+id/nation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/email"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:drawableLeft="#drawable/ic_flag"
android:drawablePadding="15dp"
android:hint="Nationality"
android:inputType="textCapWords" />
in this edittext possible to decrease the hint text darkness
You can set hint color programmatically
editText.setHintTextColor(getResources().getColor(R.color.dark_gray));
use the drawablePadding property.
<EditText
android:drawablePadding="10dp"
android:id="#+id/nation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/email"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:drawableLeft="#drawable/ic_flag"
android:hint="Nationality"
android:inputType="textCapWords" />

How can set Hint Text in code?

I have:
<android.support.design.widget.TextInputLayout
android:id="#+id/ti_amount_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:theme="#style/BlueAccentColor">
<EditText
android:id="#+id/et_amount"
style="#style/EditTextRedBorder"
android:layout_width="#dimen/product_selector_width"
android:background="#drawable/contacts_bg_mess"
android:textColorHint="#color/palette_contacts_send_mess_text"
android:textColor="#color/palette_black"
android:inputType="numberDecimal">
</EditText>
</android.support.design.widget.TextInputLayout>
And I need add Hint Text, If I added hint text in xml, in EditText:
android:hint="#string/s_amount"
all nice, but if I add etAmount.setHint(R.string.s_amount); in code - String (hint text) does not popup ((, Why?
Set the hint to the TextInputLayout instead.
mTextInputLayout.setHint("your hint here");

how to get List of components and their id present in the given xml file in android?

. I am newbie to android . For some need i need to get all the components (i.e. TextViews ,Buttons,EditTexts,Imageviews,spinners ,checkboxes and so on . . ) and their ids in my layout xml file.
say i have following xml file sample.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
Currently i have only id of my xml file .I need to get all components and ids so that i can modyfy their text using java code. In short i need some kind of parsing.
Assuming that i dont know anything inside my xml file except its id.
SO please help me . .Thank u in advance . .
Actually i need to change the text of each textVeiw in that layout say i need to add * ath the end of the text of each text view.
AFAIK There is no way for you to achieve what you are looking for.
If all ID's were created and defined in the xml layout file - you will have to call them in you code - but you will have to know their given ID's.
If you create your ID's via the code for example textView.setId(1234); - then you are able to keep track of all names and do whatever you want with them.
So, unfortunately I don't think what you want is possible.

Is there a more systematic way to populate my layout?

I'm working on the layout as shown below.
So far I've had success with manually defining each View in xml (RelativeLayout within a ScrollView) and manually populating each spinner in java. But this is tedious. I wish I could somehow use "I have this array of items. For each item I want a row on the screen with a spinner and an EditText." I must then be able to grab the user input to put into a database. Eventually, I'll want to be able to come back and edit this screen, which means I'll have to repopulate these fields from a database query.
I tried working some with a ListView, but had trouble populating multiple rows and then read that RelativeLayout is supposed to perform better. What would you suggest? If I have to declare every field in xml and java manually, then so be it.
This is what my XML looks like...
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="180dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/category_spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<EditText
android:id="#+id/editWeight1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/percentSign1"
android:layout_alignBaseline="#id/category_spinner1"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="#id/percentSign1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#id/category_spinner1"
android:text="#string/percentSign"
android:textSize="20sp" >
</TextView>
<Spinner
android:id="#+id/category_spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/category_spinner1"
/>
<EditText
android:id="#+id/editWeight2"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/percentSign2"
android:layout_alignBaseline="#id/category_spinner2"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="#id/percentSign2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#id/category_spinner2"
android:text="#string/percentSign"
android:textSize="20sp" >
</TextView>
...
And my Java...
Spinner catSpin1 = (Spinner) rootView.findViewById(R.id.category_spinner1);
ArrayAdapter<String> catSpinAdapter1 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
catSpinAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catSpin1.setAdapter(catSpinAdapter1);
catSpin1.setSelection(0);
Spinner catSpin2 = (Spinner) rootView.findViewById(R.id.category_spinner2);
ArrayAdapter<String> catSpinAdapter2 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
catSpinAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catSpin2.setAdapter(catSpinAdapter2);
catSpin2.setSelection(1);
Spinner catSpin3 = (Spinner) rootView.findViewById(R.id.category_spinner3);
ArrayAdapter<String> catSpinAdapter3 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
catSpinAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catSpin3.setAdapter(catSpinAdapter3);
catSpin3.setSelection(2);
...
Sure, create a String array, for example:
<string-array name="my_array">
<item>One</item>
<item>Two</item>
</string-array>
Then add it to te spinner on the xml this way:
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:entries="#array/my_array"/>
My first intention would also have been a ListView, you could create a simple datastructure like a class with two string values that will get updated/displayed by this spinner/edittext combination and you could use this class also to persist it in your database.
Some side note: You should have a look at re-using layouts: http://developer.android.com/training/improving-layouts/reusing-layouts.html
That would have made your approach much easier. (define one spinner/edittext layout and re-use it inside your main xml)

How to make EditText not focused when creating Activity

I've read the other questions discussing this, and all of them work for my layouts, except for the very first one created.
At the moment, this is at the top of my onCreate method:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
^ That makes it so at least the keyboard doesn't pop up on startup, but the EditText is still focused on.
This is the XML for my EditText:
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
This is what it looks like when I bring up my activity:
The problem is that with some phones, when an EditText is focused like this, they can't write in it. I want it to not focus.
What I've done works for the next layouts brought up in that it the EditTexts are not focused on and would look more like this:
Notice that it's the same layout, though. This is the screen after the user has been brought back to this screen, which would indicate nothing wrong with the XML because this is the same XML, but the problem is that the EditText is only focused on when the activity is created.
I've done research and all of the other questions don't help me with this (they did however help the keyboard not show up, thankfully). How can I make it so the EditText on startup will look like the second screenshot, rather than the first?
You can set property of Layout like android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true"
Example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
</RelativeLayout>
May this one helpful ;)
XML code:
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:focusable="false"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
Java code:
EditText edPwd = (EditText)findViewById(R.id.password);
edtPwd.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
set focusable false in xml and set it true via the code
In your main_layout
add this 2 lines:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
example:
<RelativeLayout 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:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"> *YOUR LAYOUT CODE* </RelativeLayout>
the best solution is here:
https://stackoverflow.com/a/45139132/3172843
the correct and simple solution is to setFocusable false and setFocusableInTouchMode true . so the EditText gain focus only when user touch that EditText
android:focusable="false"
android:focusableInTouchMode="true"
Add this in onCreate()
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
or in onCreateView()
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
XML Code
<EditText
android:imeOptions="flagNoExtractUi"
android:focusable="false"
/>
Java Code in onClickListerner
mEdtEnterPhoneNumber.setFocusable(true);
mEdtEnterPhoneNumber.setFocusableInTouchMode(true);
Whenever I'm trying to open Editext after selection item of Spinner, so on that time not able to open keyboard & write down the values in Edit text, but I resolved my issue with this code.
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/titleName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:hint="#string/hint_your_full_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thanks!
You can do this programmatically. use editText.setEnabled(false) in your onStart() method of your activity (not in onCreate() - because this is some method for initializing GUI components)
It is possible to use android:focusable="false" to disable it, but if that does not work for some reason, then you can simply put a LinearLayout and it will take the focus without disrupting your layout.
NOTE: Eclipse will give you an error saying that your LinearLayout is useless because it has no contents. You should be able to disregard it with no problems.
For example:
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="false"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
</LinearLayout>
The easiest way is to add
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
in the activity tag of the Manifest.xml file
1) Its the simplest,open the manifest and put the following code between the activity tag:
android:windowSoftInputMode="stateHidden"
2) Put this attributes in the parent layout
android:focusable="true"
android:focusableInTouchMode="true"
In Manifest , copy and paste the beneath code.
<activity
android:name=".LoginActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden" />
android:windowSoftInputMode="stateAlwaysHidden"
add this line in the activity tag of the Manifest.xml file.when you click on the EditText the keyboard gets into focus.
use android:focusable="false" for focusing disable
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30"
android:focusable="false" >
</EditText>

Categories