I have a EditText that I have set to invisible by default. I would like to make this box visible onclick of a ImageView, but cant find any documentation online to help me, how would I go about doing this?
In your xml
<EditText
android:id="#+id/my_edit_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="number"
android:visibility="gone"
android:paddingRight="8dp" />
In your Activity Class
import android.widget.Button;
import android.widget.EditText;
public class MyActivity extends Activity {
private EditText editTxt = null;
private Button myBtn = null;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_xml_layout);
editTxt = (EditText) findViewById(R.id.my_edit_text);
myBtn = (Button) findViewById(R.id.my_button);
myBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editTxt.setVisibility(View.VISIBLE);
}
});
}
}
well instead of invisible you can make it disable. This way you can prevent it from user input. And it is very easy to enable or disable EditTex.
You can use JQuery to toggle display (display:none vs. display:block) on your EditText field. You probably want to start out with display:none on the EditTextBox. You will also need to have JQuery loaded in your page. Can't make it a detailed tutorial here so giving you enough to get to next level.
$jq(document).ready(function() {
$jq("#yourImageView").click(function(e) {
$jq("#yourEditTextBox").fadeToggle("slow", "linear");
});
Related
I have common problem with findViewById() function, it returns null:
<EditText
android:id="#+id/nameText"
/>
<Button
android:text="Save"
android:onClick="buttonClick1"
/>
</TableRow
android:onClick="buttonClick1"
/>
Activity1.java:
public class Activity1 extends ActionBarActivity {
public void buttonClick1(View view) {
setContentView(view);
EditText nameText = (EditText) view.findViewById(R.id.nameText);
EditText lastNameText = (EditText) view.findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) view.findViewById(R.id.indexNumberText);
Log.d(">>>> ", nameText.getText().toString());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
}
}
}
In buttonClick1() findViewById() returns null. Please explain why?
Remove setContentView(view); from buttonClick1 method
and initialise all your textview in this manner by removing view.
EditText nameText = (EditText) findViewById(R.id.nameText);
EditText lastNameText = (EditText) findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) findViewById(R.id.indexNumberText);
Also initialize the controls in the xml properly by giving height and width to controls
Multiple problems unless you're not posting all your code.
1) In your XML you close a TableRow tag, but I don't see you opening it. Might just be lazy copy-pasting.
2) In your onCreate you seem to be missing a listener for your button. There is nothing to indicate that you have a button anywhere. You need to find the view of your button as follows:
Button button = (Button) findViewById(R.id.nameOfButton);
Then you need to set a listener for it like this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here goes whatever should happen when you click the button.
}
});
3) As for your ButtonClick1 method, delete it. It looks like you were trying to create a listener, but it is pretty far from what it should look like.
Try to replace your code by this one :
public class Activity1 extends ActionBarActivity {
#
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
EditText nameText = (EditText) findViewById(R.id.nameText);
EditText lastNameText = (EditText) findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) findViewById(R.id.indexNumberText);
}
public void buttonClick1(View view) {
//Your stuff
Log.d(">>>> ", nameText.getText().toString());
}
}
Since now would be the normal code that have you tried, if you want to set a click on a button you'll have to declare it as :
Button button1 = (Button) findViewById(R.id.button1);
Then you can do the :
public void buttonClick1(View v) {
// Your stuff
}
EDIT
Make sure that you have on your XML all of your EditText and all of your Button for example : or , also make sure that you have an android:id="#+id/yourID in all of your stuff..., by the way instead of using an onClick method in your XML for your Button, you could use this to use an OnClickListener
The point 1 to 3 should go inside of onCreate the point 4 should go outside of onCreate.
1.- Implements View.OnClickListener in your Activity1
public class Activity1 extends ActionBarActivity implements View.OnClickListener {
2.- Declare it
Button button1 = (Button) findViewById(R.id.button1);
3.- Do the setOnClickListener like this :
button1.setOnClickListener(this);
4.- Create an OnClick method :
#Override
public void onClick(View view) {
if (View.equals(button1))
//Your stuff
}
I've coded professionally for many many years in other languages, but I'm pretty new at Android and I'm learning a lot from sites like stack overflow.
After hours of searching I'm struggling with trying to get a reference to an EditText in a Button click method. Both the EditText and Button are part of the same Activity.
Here's a cut down part of my activity_my.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/my_layout"
tools:context=".MyActivity" >
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:singleLine="true"
android:id="#+id/name" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:text="#string/save"
android:id="#+id/save" />
</RelativeLayout>
And a cut down part of my MyActivity.java:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutInflater().inflate(R.layout.activity_my, null));
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveClick(view);
}
});
}
public void saveClick(View view)
{
EditText editText = (EditText) view.findViewById(R.id.name); // this is always null
if (editText != null)
{
String name = editText.getText().toString();
}
}
}
I think it's because I'm trying to use the findViewById method on the view parameter. I remember reading somewhere that the parameter to a button click event is the Button itself, not the layout. If that's correct how do I get a reference to the layout in order to use the findViewById method? Or is there something else wrong?
I've seen examples where the onClick event wraps an anonymous method where you can successfully use the findViewById because (I think) you are essentially nested in the onCreate event. However, I'd like to keep my button click event as a separate (non-anonymous) method.
Thanks for any help in advance.
Change this
EditText editText = (EditText) view.findViewById(R.id.name);
to
EditText editText = (EditText)findViewById(R.id.name);
and you can simply use
setContentView(R.layout.activity_my);
directly.
I guess EditText belongs to activity_my.xml
Also there is no need to initialize edittext everytime on button click
EditText editText;// declare as instance variable
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
editText = (EditText) view.findViewById(R.id.name)
public void saveClick(View view)
{
EditText editText = (EditText) view.findViewById(R.id.name); // this is always null
if (editText != null)
{
String name = editText.getText().toString();
}
}
that returns a valid View (an object != null) only if the EditText is inside the Button. You should look for the EditText in the View's hierarchy
use this
EditText editText = (EditText) findViewById(R.id.name);
The View.findViewById() method looks for child views.
In your case, the findViewById call is in the onClickListener of your edittext view. This view does not have a child, it is simple an edittext. That's why you're getting null.
I'm doing some basic programming for my android in eclipse
I'm trying to do something simple as getting the user to enter a name in an EditText, but when I press the textbox the soft keyboard doesn't appear and input is impossible.
I don't know if I have forgotten anything in the code or anything else,
this is what I have so far:
public class Menu extends Activity implements OnClickListener {
private EditText enterName;
private Button accept;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
enterName = (EditText)findViewById(R.id.name);
accept = (Button)findViewById(R.id.acceptName);
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
And the layout .XML-file
...
<EditText
android:id="#+id/name"
android:hint="Enter Name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text">
<requestFocus />
</EditText>
...
So at the moment all I want is to get the user to be able to write text in the box.
EDIT:---->New Problem---->
Ok, so last problem involved something that I didn't mention in the post.
In my activity I change the layout with command:
setContentView(R.id.(XML-file));
And when I put the EditView in the second XML (not the one that shows on create) it wont work =/. Sorry if I left this out before, but does it make any sense?
first I would like to say thank you to every one out here as i am a nOOb and have learned a lot just by reading questions and answers that you post. I am trying to pass a great deal of text to the end users and while being able to do this with new classes and .xml files this is becoming cumbersome. i thought of stream lining the app by just having a single xml layout for a particular set of text strings and just change the #string/????? via button onclick and setText but have learned that I can not change the initial value of #string in an xml file. question is that TRUE? and is there a more efficient way to do this ie (setting android:text to a var and setting var in java to a particular string) or do i need a new xml layout for each string? (that's a lots of waste if you ask me) and a little insight, there are at this time approx 250 different strings with min 5 paragraphs and growing.
here is my code thus far.
snippet of first java
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Monlt extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.monolt);
final MediaPlayer buttonsound = MediaPlayer.create(Monlt.this, (R.raw.buttonclick));
Button button1 = (Button) findViewById(id.button1);
Button button2 = (Button) findViewById(id.button2);
Button button3 = (Button) findViewById(id.button3);
Button button4 = (Button) findViewById(id.button4);
Button button5 = (Button) findViewById(id.button5);
Button button6 = (Button) findViewById(id.button6);
Button button7 = (Button) findViewById(id.button7);
Button button8 = (Button) findViewById(id.button8);
Button button9 = (Button) findViewById(id.button9);
Button button11 = (Button) findViewById(id.button11);
Button button12 = (Button) findViewById(id.button12);
Button button13 = (Button) findViewById(id.button13);
Button button14 = (Button) findViewById(id.button14);
Button button15 = (Button) findViewById(id.button15);
Button button16 = (Button) findViewById(id.button16);
Button button17 = (Button) findViewById(id.button17);
Button button18 = (Button) findViewById(id.button18);
Button button19 = (Button) findViewById(id.button19);
Button button21 = (Button) findViewById(id.button21);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonsound.start();
final TextView mview = (TextView) findViewById(R.id.solayout2);
mview.setText("mono1"); //this was my first string to pass
startActivity(new Intent("com.nvar.Sorders.Mono.ASO1"));
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonsound.start();
startActivity(new Intent("com.nvar.Sorders.ASO1"));
final TextView mview = (TextView) findViewById(R.id.solayout2);
mview.setText("#string/mono2");/this is the second string to pass
}
});
`
now this code works when i remove the 2 text view lines in the onclick
so then it call another class Aso1 that I would like to keep in place for later use.
Aso1 java code
`
package com.nvar.Sorders.Mono;
import com.nvar.Sorders.R;
import android.app.Activity;
import android.os.Bundle;
public class Aso1 extends Activity {
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.solayout2);
}
}
`
and then the first xml
`
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/solayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mview"/>
<!-- android:text="#string/monoaso1"/>
-->
<!-- this is were i was playing with the strings />
-->
</LinearLayout>
</ScrollView>
`
any help in this matter would be greatly appreciated, and remember "noob" to java / android! so if there is a sample of what i could or should be looking at, don't hesitate to smack me in the head and point me in the right direction. i don't mind reading :)
thanks again.
When displaying the Strings, What you could use is a TextView and have multiple lines and then just update the text value of it in code. It doesn't even need default text
<ScrollView
...some params...>
<TextView
android:id="#+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="10"/>
</ScrollView>
This will give you a TextView 10 lines long. Then in code you can do something like this to update the value:
String longText = getVeryLongText();
((TextView)findViewById(R.id.my_text)).setText(longText);
Then you'll have something that looks like a scrollable paragraph of text
*#string/string_name* in xml are not designed to change the value. They are there to help you with localization. Currently that xml is located here http://developer.android.com/guide/topics/resources/localization.html
res/values/string.xml
you can have another string xml with different language like in the following location, for example france
res/values-fr/strings.xml
you can read more about that over here.
Now, let's move on to how to reference that string_name from java
Resources res = Monlt.this.getResources();
mview.setText(res.getString(R.string.string_name));
//do something
mview.setText(res.getString(R.string.mono2));
I want to set OnClickListener on my button3.
It's an activity in the second tab in TabHost.
import android.content.DialogInterface.OnClickListener;
public class tab_act extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_tab);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}
}
XML:
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:drawableLeft="#drawable/icon_search"
android:drawablePadding="15dip"
android:text="Найти совпадения" />
And I get an err:
The method setOnClickListener(View.OnClickListener) in the type View
is not applicable for the arguments (new
DialogInterface.OnClickListener(){})
in this line:
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
You've imported the wrong OnClickListener - it should be
import android.view.View.OnClickListener;
instead of import android.content.DialogInterface.OnClickListener;. Also you are setting OnClickListener for button with id button1 while xml you provided declares button with id button3
edit It's better to implicitly specify it like so:
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
...
});
to prevent such errors from happening.
You are doing findViewById(R.id.button1) and you should be doing findViewById(R.id.button3)
One more thing: if you imported more than one method from 2 different places (for example you imported and use in the same activity both android.content.DialogInterface.OnClickListener and android.view.View.OnClickListener) you can't use shortcuts for both times when calling the OnClickListener, and you will somethimes have to call specificaly like so:
findViewById(R.id.button1).setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
}
});