I am brand new to android development and I am having difficulties trying to make my editText uneditable after a switch is set to off. The problem is that The switch function doesn't work. Even after it is set to off, I can still edit the text.
here is my code :
public void onCreate(Bundle savedInstanceState) {
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
final View switch1 = (Switch) findViewById(R.id.editSwitch);
final EditText mEdit = (EditText) findViewById(R.id.bioTxt);
switch1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (switch1.getContext().toString().equals("On")) {
mEdit.setEnabled(true);
}
else if (switch1.getContext().toString().equals("Off")) {
EditText mEdit = (EditText) findViewById(R.id.bioTxt);
mEdit.setEnabled(false);
}
}
});
};
}
Switch has a function isChecked() which returns true if the Switch is in the 'On' position. Therefore your onClick method can just be:
public void onClick(View v) {
mEdit.setEnabled(switch1.isChecked());
}
switch1.getContext().toString()
What are you trying to do here? getContext on a view returns the activity that owns it. It does not return anything that will turn into "On" or "Off". You need to call some other function. I'm not sure which, as it looks like its a custom view.
Related
for my android app, I use a button to go to the next Activity.
the problem is when I touch the button on the screen one instance of the activity is created but when
I use performClick() method to click the button programmatically, it creates two instances of the activity. ( performClick() is called from a callback method).
I used the CLEAR_TOP FLAG but it seems to break the back button.
Any idea how to solve this problem ?
this is what my code looks like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
startActivity(new Intent(Activity1.this, Activity2.class));
}
});
}
private void A_callback_method(){
if (some_condition_to_launch_activity){
btn.performClick();
}
}
Just a simple trick. create function
private void function_name(){
startActivity(new Intent(Activity1.this, Activity2.class));
}
Then on the button onClickListener
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
function_name();
}
});
and also inside A_callback_method
private void A_callback_method(){
if (some_condition_to_launch_activity){
function_name();
}
}
hope this will solve your problems.
I succesfully did a work around to solve this problem by adding a boolean variable intialise it to false in the onResume() method and then did the following:
private boolean clicked_btn;
private void A_callback_method(){
if ((some_condition_to_launch_activity)&&!clicked_btn){
clicked_btn=true;
btn.performClick();
}
}
I made one of the simplest programs that creates a login page, however, I cannot add an OnClickListener to my button, and I do not know why. I'm really new to Android Studio and have no idea what to do. When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #Override does not override what's above.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
regist1 = (Button)findViewById(R.id.btnregister1);
regist1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent teachtoregist = new Intent(TeacherInJ.this, TeacherRegisterInJ.class);
startActivity(teachtoregist);
}
});
etUsername = (EditText) findViewById(R.id.editText2);
etPassword = (EditText) findViewById(R.id.editText3);
bLogin = (Button) findViewById(R.id.btnlogin);
bLogin.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #override does not override what's above.
Both the above problems are because you didn't add the implements keyword to your Activity. You need to add it so the Activity can be regarded as OnClickListener interface by the button.
You need to do something like this (See the comments inside the code):
// see below the implements View.OnClickListener line that
// need to be added so the Activitiy can be regarded as the listener.
public class TeacherInJ extends AppCompatActivity implements View.OnClickListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
...
// now you can use this as the listener. It's because you have
// set the current Activity class as the View.OnClickListener
// this is refer to current Activity object.
bLogin.setOnClickListener(this);
}
// Now you can add the #Override to the onClick method from
// the View.OnClickListener.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
}
}
}
I'm new to Android development, I am currently trying to see if a value entered is equal to a value. Here I am seeing if the user input equals to 5, they are currently set as strings as to is the text field.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uk_postage);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button home = (Button) findViewById (R.id.btnHome);
Button calculate = (Button) findViewById (R.id.btnCalculate);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(ukPostage.this, MainActivity.class));
}
});
EditText lengthInput = (EditText) findViewById(R.id.editTextLength);
final String lengths = lengthInput.getText().toString();
final TextView amount = (TextView) findViewById (R.id.txtAmount);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
if (lengths.equals("5")) {
amount.setVisibility(View.VISIBLE);
}
}
});
The text goes to visible if i click the button without the if statement there however won't once I write the if statement.
Thanks in advance.
if (lengths.equals("5")) {
String is immutable and once you reference it you need to reference it again with your edit text to reflect the latest value.
Instead of the above code you need to reference the lengths back with the lengthInput value each time you click the calculate button.
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
lengths = lengthInput.getText().toString();
if (lengths.equals("5")) {
amount.setVisibility(View.VISIBLE);
}
}
});
IMPORTANT: You need to set the lengths as a global variable instead of being a final variable since it can only be initialized once.
This question already has answers here:
How to disable Button if EditText is empty ?
(15 answers)
Closed 6 years ago.
I'm working on an Android app, most of it was with the help of tutorials and reading material, my app is somewhat basic, make some calculations and display the result to the user
What I have
I created a fragment guided by this tutorial and I just replaced the button and textview with my own EditText and Button
I have an EditText, a Button, and a TextView, these are the widgets the user see when the fragment is opened
My goal
I want the user to input values in the EditText and when s/he click the button the TextView displays the result of some calculations
What I did
I've been trying some other answers from here, here, here, here, but I don't know how to wire up the code, every time I write some pieces of code my app crashes and I don't know what else to do
Here is my code
public class Circle_Perimeter extends Fragment
{
EditText edtxt;
Button btn;
TextView txt;
public static Circle_Perimeter newInstance()
{
Circle_Perimeter fragment = new Circle_Perimeter();
return fragment;
}
public Circle_Perimeter()
{
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_circle__perimeter,container,false);
edtxt = (EditText)rootView.findViewById(R.id.editText);
btn = (Button)rootView.findViewById(R.id.button6);
txt = (TextView) rootView.findViewById(R.id.textView6);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
}
});
return rootView;
}
}
Question
How and where do I write the necessary code to make the EditText send a Toast warning the user the EditText is empty? or disable the Button if the EditText is empty?
EDIT: Really?? A downvote?? Instead of downvoting... Just give a warning or something else
EDIT 2: I already said that I did follow another answers from here and I couldn't make my code work that's why I'm here looking for an answer WITH my piece of code
To notify the text changes in EditText you need to use TextWatcher.
Please have a look at this document https://developer.android.com/reference/android/text/TextWatcher.html
public class Circle_Perimeter extends Fragment
{
EditText edtxt;
Button btn;
TextView txt;
public static Circle_Perimeter newInstance()
{
Circle_Perimeter fragment = new Circle_Perimeter();
return fragment;
}
public Circle_Perimeter()
{
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_circle__perimeter,container,false);
edtxt = (EditText)rootView.findViewById(R.id.editText);
btn = (Button)rootView.findViewById(R.id.button6);
txt = (TextView) rootView.findViewById(R.id.textView6);
//Initially set it as disabled
btn.setEnabled(false);
//Add textWatcher to notify the user
edtxt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//Before user enters the text
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//On user changes the text
if(s.toString().trim().length()==0) {
btn.setEnabled(false);
Toast.makeText(getActivity(), "Text can not be empty..",
Toast.LENGTH_SHORT).show();
} else {
btn.setEnabled(true);
}
}
#Override
public void afterTextChanged(Editable s) {
//After user is done entering the text
}
});
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//do your final job here
}
});
return rootView;
}
}
Its simple to check that edit text is empty
if{edittext.getText == null){}
or
if(editText.isEmpty){}
Also best way is make for button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(edittext != null){
//do what u want for no empty button
}else{
//block ur button}
}
});
You can check the EditText for null instances using the TextUtils API that comes bundled with Android
if(!TextUtils.isEmpty(editText) {
//Do your desired work
}else {
//Notify user for rectification
}
To play around with the button, you can use either of them:
button.setVisibility(VIEW.GONE)
button.setEnabled(false)
I'm trying to learn android development with Android Studio but I can't seem to figure out why my clicks are not registering. Is there something I'm missing here?
I don't like the way Stack Overflow FORCES me to format things they way THEY want. Here is pastebin! :_
http://pastebin.com/hdzZpsud
call this changeText() method inside onCreate() method of your activity
your activity should look like this:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
changeText();
}
public void changeText(){
final Button button = (Button) findViewById(R.id.button1);
final TextView text = (TextView)findViewById(R.id.largetext);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setText("Button has been pressed");
text.setText("The large text has been changed");
}
});
}
}
In fact, you didn't set the listener. So you should add changeText() after setContentView(R.layout.activity_my);.