I made my first application for Android using only Java code. How do I create an EditText in Java Code?
Is more or less works like this:
EditText et = new EditTex(context);
where the context is e.g. the Activity that hosts the EditText.
In practice you may want to do some customizing and then attach it to an existing layout like e.g.
EditText et = new EditText(getActivity());
et.setTextAppearance(getActivity(),R.style.table_cell); // add some style
et.setTag(name); // add a tag
if(PropertyType.isNumeric(spd.getType())) {
et.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); // Input are numbers only
}
if (spd.getDefaultValue()!=null) {
et.setText(""+spd.getDefaultValue()); // set a default text to be displayed
}
row.addView(et); // add it to a parent
After you have formatted it in XML:
<EditText
android:id="#+id/edittextid"
android:inputType="text" >
You can call it by declaring it and linking it to the view you specified in the XML like so:
EditText et = (EditText)view.findViewById(R.id.edittextid);
You can get more info on the class here
Related
I am new to kotlin and android and I just started getting into using viewbindings.
Im trying to make this locations app and when setting the date through a datepickerdialog, when i try to edit the text of a textview with a binding its giving me errors. Can someone help me?
private fun displaySelectedDate(timestamp : Long){
binding?.etDate?.text = format.format(timestamp)
Change it to
binding?.etDate?.setText(format.format(timestamp))
If you check the docs for EditText, you'll find a setText() method. It takes in a String and a TextView.BufferType. For example:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("This sets the text.", TextView.BufferType.EDITABLE);
It also inherits TextView's setText(CharSequence) and setText(int) methods, so you can set it just like a regular TextView:
editText.setText("Hello world!");
editText.setText(R.string.hello_world);
In java, we used to define the id by using findviewbyid. I am Wondering how we can define the id in kotlin without any use of third party library.
You don't have to define the view id's in Kotlin. All you have to do is use a non declared variable which has same name as the view in layout xml file. This reduces the chance of you running into a bug.
Assume you have this TextView in the layout xml
<TextView
android:id="#+id/mytextview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="18sp"/>
then this is how you can access this by a variable name mytextview, without declaring it in the file. This is how you would set the text
mytextview.text = "My text view"
You can read more at https://kotlinlang.org/docs/tutorials/android-plugin.html
Another way could be following but I wouldn't suggest it
private var textview: TextView? = null
textview = findViewById(R.id.mytextview) as TextView // old way
textview = findViewById<TextView>(R.id.mytextview) // new way
so I know the onClick part is quite useless, but just in case it does change anything, ive put it there. so ive got the onClick, and I would like it to add the editText to the current activity, which is called activity_calculation.
I currently have this code which I got from another question :
public void addCalc(View view){
EditText myEditText = new EditText(context); // Pass it an Activity or Context
myEditText.setLayoutParams(new LinearLayoutCompat.LayoutParams(MATCH_PARENT,WRAP_CONTENT)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
activity_calculation.addView(myEditText);
}
any help would be appreciated. maybe you can see what ive done wrong
First get a reference to the activity's root layout. To do this add an id attribute to your activity layout file's root layout. eg :
<LinearLayout
android:id="+id/rootLayout" />
Then, get a reference to it and add the created EditText.
//If your root layout is a RelativeLayout, use that instead
LinearLayout rootView = (LinearLayout) findViewById(R.id.rootLayout);
EditText myEditText = new EditText(rootView.getContext());
myEditText.setLayoutParams(new LinearLayoutCompat.LayoutParams(MATCH_PARENT,WRAP_CONTENT));
rootView.addView(myEditText);
I'm having issues with understanding how I should organize my user interface in Android. My original plan was to create TextViews and ListViews programatically and change them when buttons are clicked, etc.
Here's my first simple attempt. viewFriends is a method within my Activity class. It's called when a menu button is pressed.
private void viewFriends()
{
mText = new TextView(this);
mText.setText("Gathering information...");
setContentView(mText);
...irrelevant code follows
Why doesn't this seemingly simple example work? How should I logically organize and manage my user interface objects (TextViews, ListViews, Buttons, etc).
Thanks.
The best work would be having those listviews and textviews in your XML files and give them a suitable ID like following:
<ListView
android:id="#+id/myList"
android:layout_width="wrap_content"
android:layout_weight="1"
/>
Just like above have your text view too in XML file an add the android:id attribute.
Once you define this way in your java file have references to them:
ListView myListObj = (ListView)findViewById(R.id.myList);
Now you have an object called myListObj in your java file and now you can do whatever you want to do with it.
:)
Let me if you find any issue in this so that I can update the answer to meet your specific need.
Don`t use setContentView in your method. Usually it should only be called once in the onCreate method of your activity.
Best predefine your bottons/TextViews in xml, get a handle for them (findViewbyId...)
and modify them that way.
If you create them programaticly, just add them to a view containter from your xml.
Like :
setContentView(R.layout.main);
Lets say in main.xml there is a LinearLayout with the id: root.
// get accces to that layout:
LinearLayout rootLayout = (LinearLayout) findViewById (R.id.root);
// create a new TextView
TextView tv1 = new TextView (this);
tv.setText("Hello!");
// add it to your base layout
rootLayout.addView(tv1);
// done! :)
Make a double check on what you are getting in "this".
change it to your java file name.this
You have to reload/refresh your activity once you change it.
Try this
#Override
protected void onResume() {
if(param.equalsIgnoreCase("gr"))
{
finish();
Intent myIntent = new Intent(yourActivity.this, yourActivity.class);
startActivity(myIntent);
}
hi i am new developer on android i have written code for display simple dialog,in this dialog i have taken edit text view.when i entered text on edit text then i have changed orientation of the scree then the value of edit text has not appearing!
i have written code as follows
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
please any one can resolve this?
Basically, this involves overriding the onRetainNonConfigurationInstance method.
See here:
Faster Screen Orientation Change
Excerpt:
"The Activity class has a special
method called
onRetainNonConfigurationInstance().
This method can be used to pass an
arbitrary object your future self and
Android is smart enough to call this
method only when needed. In the case
of Photostream, the application used
this method to pass the downloaded
images to the future activity on
orientation change."
prasad... The editText box does not have an ID and if the view element does not have an ID the view state is not automagically saved on a soft kill when the user changes the orientation of the phone. You might be better off creating a custom dialog using an XML layout, then the edit text box should have and ID and the view state should be automagically saved on a soft kill.
JAL
I have some code here.
Edit: Prototype code taken from the Android Docs that barely works because I do not have the time to work on this. Create an XML layout in res/layout as alert_dialog_text_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:text="Stateful"
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
And then use this layout to create the alert:
AlertDialog.Builder builder= new AlertDialog.Builder(this);
LayoutInflater inflater= getLayoutInflater();
final View myView= inflater.inflate(R.layout.alert_dialog_text_entry, null);
builder.setTitle("About");
builder.setMessage(alertMessage+"Version: "+versionName);
builder.setView(myView);
AlertDialog alert= builder.create();
Since the editText box has an ID it appears to save state on a soft kill.
This question is old but it is still worth giving a simpler answer. JAL mentions the need to set the ID, but you can go ahead and do that directly in the Java. For example, add this new line into the original code above:
// Set an EditText view to get user input
final EditText input = new EditText(this);
// Id the EditText so the framework will save/restore it for us
input.setId(R.id.my_id_for_alert_box_inputs); // <-------- New line
alert.setView(input);
Under /res/values create a new XML file called ids.xml. In there, define this id we use in the Java:
<?xml version="1.0" encoding="utf-8"?>
<!-- Integer IDs used for tagging Views made in Java programmatically
without clashing with XML-defined views. -->
<resources>
<!-- Used to id the input box in a dialog. Reused in different dialogs. -->
<item type="id" name="my_id_for_alert_box_inputs" />
</resources>
This works because the Android application framework is supposed to save/restore views that have an ID defined. The whole XML part of the above is neat but not really needed. You could just plug a made-up integer into the Java View.setId() call to make this a one-line fix.