FindViewById returning null EditText - android

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
}

Related

How can I make a button in one activity to modify components in other

I'm trying to make some layouts and listviews gone and visible by pressing a button located in another activity.
Also I want to input some text in an edittext located in the same activity as the layouts
I've looked all over this place and I found several examples but I just couldn't make them work so I'm posting my own code.
btnnext1.setOnClickListener(new OnClickListener() {
val next1 = Intent(this, next1::class.java)
startActivity(next1)
categories.visibility = View.GONE
listacats.visibility = View.VISIBLE
ListView.visibility = View.VISIBLE
e_search.setText("blackcat")
})
From what I found out this can be achieved by using intent.PutExtra, but I just couldn't make it work.
Thank you in advance for your support
The main task is pass "blackcat" to another activity, you could do like this:
In MainActivity:
final Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("passed_str", "blackcat");
startActivity(intent);
}
});
In OtherActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
String receivedStr = getIntent().getStringExtra("passed_str");//receive string
final EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(receivedStr);
categories.setVisibility(View.GONE); //change wiget's visibility
listacats.setVisibility(View.INVISIBLE);
ListView.setVisibility(View.INVISIBLE);
}

Android view isDirty inside onClick

I want to check if the text in some EditText is changed, after user clicks some Button. But View#isDirty seems not to return the correct state of the EditText if called inside onClick. For instance, I wrote something like this:
public class MainActivity extends Activity {
EditText editText;
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.f);
editText = (EditText) findViewById(R.id.e);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println((editText.isDirty() ? "is dirty" : "is clean"));
}
});
}
}
before i make any change to the editText, it outputs is clean, as expected. But the same is clean is printed even after I write something in editText.
When will isDirty be called? And is it the correct way to do this at all?
Update:
I also want to check if some Switch and Spinner values are changed. Is isDirty() the correct way to do this?
By the time you click your button edittext is no longer dirty - text is already updated and view redrawn. Maybe if you change your onclick handler you will understand better what is going on.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText = (EditText) findViewById(R.id.e);
System.out.println((editText.isDirty() ? "is dirty" : "is clean"));
}
});
isDirty will return true only as long as view has not been redrawn. This happens quite quickly and basically you do not have (and dont need) any control over this.
I think you need to use some other methods to achieve what you want.
I would suggest to use:
https://stackoverflow.com/a/9459848/5684335
The comment from Okas is a good explanation why.

How do i create a button click event and make sure that only when i click on the button something it will do something?

In the MainActivity.java i changed it to:
public abstract class MainActivity extends ActionBarActivity implements OnClickListener
changed to abstract and added the implements OnClickListener.
Now i want to add event click for the button.
This is the button settings in the layout.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Ip"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
I want to do that when i click the button only if i clicked on the button area not the screen then do something.
And when i finished done everything set the button to be not visible and also not clickable instead if i click now on the place where the button is it will act now like i touch the screen.
This is what i tried so far.
In the designer added a button changed the id of the button to checkforip.
Then in the MainActivity.java i did:
public abstract class MainActivity extends ActionBarActivity implements OnClickListener
{
private static final int MY_DATA_CHECK_CODE = 0;
public static MainActivity currentActivity;
TextToSpeech mTts;
private String targetURL;
private String urlParameters;
private Button btnClick;
private String clicking = "clicked";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(new SingleTouchEventView(this, null));
btnClick = (Button) findViewById(R.id.checkipbutton);
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
clicking = "clicked";
}
});
First the abstract is wrong i guess also when running the program with debug mode it dosen't even pass the abstract and shut down the program.
Second i want the button click event handler to be on it's own out of the onCreate i guess.
First get button in your Activity:
Button btn = (Button) findViewById(R.id.button);
Second, listen when the button is clicked:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// add here what you want to do when button is clicked.
}
});
Third, if you want to make button disappear then add this line in onClick
button.setVisibility(View.GONE);
First there is no need to define the class as an abstract you should remove it and remove the implementation of the listener .
Instead there is an attribute for all views in android which is onClick and you can define as follow :
<Button
......
android:onClick="onBtnClick"
\>
After that you define a method in the java file that display this layout as follow :
#override
Public void onBtnClick (View v){
//to make it unclickable
v.setClickable (false);
//to hide it
v.setVisibility (View.GONE);
}

How to change a sting Variable name dynamically and programatically, this is challenging

I have MainText1 displays a text, when you click on button 3 in the Menu MainText1 displays text3 which is coming from the super class Text. What I want is that dynamically when you click on any button it reads the number and displayed the respective text, that's all about. ;)
I want to get rid of switch case in my activity, so I'm trying now for 2days :( to change the name of the string variable dynamically, but I think I'm using a wrong code as string variable is different from resources (confused), here's my code, this really challenging me this weekend:
public class MainText1 extends Text {
String
tx1=text1,tx2=text2,tx3=text3,
tx,stringReceived;//text1,text2...textn strings coming from the Super class Text
num = Integer.parseInt(getIntent().getStringExtra("somekey1")); // this data is coming from the menu, it depends on which button is clicked
tx="text"+num; // text is the name of the string variable, it should be in format like that : text1,text2,...textn which have predefined string content
stringId = getApplicationContext().getResources().getIdentifier(tx, "string", getPackageName());
if (stringId > 0) {
stringReceived=getApplicationContext().getResources().getString(stringId);
I guess what you're trying to do is to change a TextView contents. So, you could do the following:
public YourActivity extends Activity {
//Here you will declare your layout items
private Button button1;
private Button button2;
private Button button3;
private TextView txtView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//Here you will get you layout elements
button1 = (Button) findViewById(R.id.button1_id);
button2 = (Button) findViewById(R.id.button2_id);
button3 = (Button) findViewById(R.id.button3_id);
txtView = (TextView) findViewById(R.id.txtview_id);
//Now you will have to set the onClickListeners
button1.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button1
txtView.setText(getResources()
.getString(R.string.button1_string);)
}
});
button2.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button2
txtView.setText(getResources()
.getString(R.string.button2_string);)
}
});
button3.addOnCLickListener(new OnClickListener() {
#Override
private onClick() {
//Set the text in the text view to the string related
//to button3
txtView.setText(getResources()
.getString(R.string.button3_string);)
}
});
}
}
This should do the trick. Although, like the other guys suggested it, you should take a look at some tutorials before coding on

How is view state saved/restored in Android?

I have this class
public class MainActivity extends Activity {
int mClicks = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView clicksText = (TextView) findViewById(R.id.clicksText);
final Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mClicks++;
clicksText.setText(mClicks + " clicks");
}
});
}
}
Isn't it supposed to save view state at onSaveInstanceState and restore it at onCreate? I click the button a couple times and rotate the device to find the UI in the exact same state as the view XML defines. I don't expect mClicks to be restored as that is my job, not the framework's but the TextView should hold the previous X clicks! value. Am i wrong here?
You can try using freezesText
http://developer.android.com/reference/android/widget/TextView.html#attr_android:freezesText
The default onSaveInstanceState saves the EditText text (what the user writes) but does not save TextView text because it is supposed that it won't change by default. If you want to save that, you need to explicity do it in you onSaveInstanceState method

Categories

Resources