I've got a Fragment (ActionBar Activity with 3 tabs) with a TextView
There is also a Button which open an Alert, in which there is Button and an EditText
What i would like to do, is to set the value of the Fragment's TextView to the value of the Alert's EditText, when clicking on the Button.
Here is what i tried to do (the method i show you is in the MAinActivity and not in the Fragment) :
public void showTimePicker(Button but) {
final Dialog dialog = new Dialog(this);
final TimePicker t;
dialog.setContentView(R.layout.alert_time);
dialog.setTitle("Heure");
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TimePicker t = (TimePicker) dialog.findViewById(R.id.time_cri);
/* THIS LINE MAKE A NULLPOINTEREXCEPTION */
time.setText(getSelectedTime(t)); /* GET THE TIME FROM TIMEPICKER */
dialog.dismiss();
}
});
CriFragment f = (CriFragment) mSectionsPagerAdapter.getItem(2);
time = f.getTextView(); /* GETTER OF THE TEXTVIEW I'D LIKE TO SET */
dialog.show();
}
The line i pointed out make a nullPointerException (at onClick())
Thank you for reading !
You have to get the TextView "time". Because the TextView "time" isn't initialized. You can use dialog.findViewById, not f.getTextView.
Related
I want to create this textview, and then display it after this button is clicked, but no textview is displayed.
I dont't want to use findViewById(), if possible, because I want to create a new textview every time the button is pressed. I've tried making a linear layout first, but I've seen a lot of websites say that you don't need to, and I would prefer not to. Any advice would be helpful. Thank you.
EditText name=layout.findViewById(R.id.enterName);
final String Name=name.getText().toString();
Button create=layout.findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView ProgrammaticallyTextView = new TextView(MainActivity.this);
ProgrammaticallyTextView.setText(Name);
ProgrammaticallyTextView.setTextSize(22);
popup.dismiss();
}
});
There are no error messages and the logcat doesn't say that anything is wrong.
Try like this :
private LinearLayout lLayout;
private EditText lEditText;
private Button lButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lLayout = (LinearLayout) findViewById(R.id.linearLayout);
lButton = (Button) findViewById(R.id.button);
lEditText = (EditText) findViewById(R.id.editText);
lButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("Text New");
}
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
lLayout.addView(createTextView(lEditText.getText().toString()));
}
};
}
private TextView createTextView(String text) {
final LayoutParams loutParams = new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(loutParams );
textView.setText("Text is " + text);
return textView;
}
Use the activity's findViewById() method to reference your layout views. For example, you can replace
EditText name=layout.findViewById(R.id.enterName);
Button create=layout.findViewById(R.id.create);
with
EditText name=getActivity().findViewById(R.id.enterName);
Button create=getActivity().layout.findViewById(R.id.create);
Note: if you are not using fragments then there is no need to use getActivity since findViewById() is a method of the superclass AppCompactActvity( or Activity).
I guess your code is not working because the Button View and Editext Views have not been reference when activity starts for the oncreate() method
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.
I'm a beginner to Java and I want to validate an EditText. What I have in mind: my editText has to match "helloworld". When you press a button this has to be validated. If this is true--> go to a new class in which I have a setContentView to display a new layout.
If the text which I have just typed does not match "helloworld", it should do nothing. It seems very easy but since I'm a beginner you would help me BIGTIME!
Here's most of the logic handled. You will need to fill in your actual layout id's and make your launch intent. Put this code in your onCreate method in the activity with the layout that contains the edit text box
EditText editText = (EditText)findViewById(R.id.editTextBox);
Button btn = (Button)findViewById(R.id.checkBtn);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if(editText.getText().toString().equalsIgnoreCase("helloworld")){
//Launch activity with new view
}
}
});
In an activity (or android class) you have to get the instance of your EditText. Your edit text has an id, and you can get it using R. R is the resources for your app.
EditText t = (EditText)findViewById(R.id.<Name of your textfield>);
Then you can get the value of that textfield and compare it
t.getText().toString().equals("helloworld");
will return true or false. If you dont care about the case of the letters use
t.getText().toString().toLowerCase().equals("helloworld");
you will need an onClickListener for your button, check out the android api
http://developer.android.com/reference/android/view/View.OnClickListener.html
in your onCreate, when declaring your submit button, add a listener
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(submitListener);
make a new onClick listener and fire an Intent to start a new activity
View.OnClickListener submitListener = new View.OnClickListener() {
public void onClick(View v) {
//if string matches helloworld fire new activity
Intent newActivity = new Intent();
startActivity(newActivity);
}
};
// create a reference to the EditText in your layout
EditText editText = (EditText)findViewById(R.id.editTextIdInLayout);
// create a reference to the check Button in your layout
Button btn = (Button)findViewById(R.id.buttonIdInLayout);
// set up an onClick listener for the button reference
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String userInput = editText.getText().toString(); // get the user input
if (userInput.equals("helloworld") // see if the input is "helloworld"
{
setContentView(R.layout.newLayout); // change the content view
}
}
});
Right now I have an EditText with id "getUserName" and a button next to it (both in a linear view) with id "setName"
I want someone to be able to click setName, and have the EditText field disappear, the button disappear, and a TextView take it's place. Here's what I have thus far:
public void setName(View view){
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
String playerName = editText.getText().toString();
((ViewManager)editText.getParent()).removeView(editText);
((ViewManager)button.getParent()).removeView(button);
Log.d("ScoreKeeper", playerName);
}
So I am successfully removing the desired elements from the screen, but I don't know how to add the textView to take their place.
How can I do that? I'm brand new to Android, so forgive me if this seems ignorant. I've tried looking it up!
Thanks
OPSRCFTW
You can simply hide the EditText, Button and TextView using turn visibility on.
You can add textview in your xml file and keep it invisible..
On button click, just change its visibility...
So the code is on buton click like below:
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);
First -> make ur textview Gone,
textview..setVisibility(View.GONE)
when u click the button..
Second -> Make
`Make the EditText and Button GONE with` `edittext.setVisibility(View.GONE);` and make textview visible textview..setVisibility(View.VISIBLE)
What about starting with
textView.setVisibility(View.GONE);
and then set an OnClickListener to your button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setVisibility(View.VISIBLE);
}
});
Write code onCreate method of your class
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
textView.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
editText.setVisibility(View.GONE);
button.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
});
Hope it will help you.
You can also dynamically create the text view , like textview view= new textview(context); set the height and width thru layout params; and then add this view to parent view or pare layout like parent view.addview(textview). Change the visibility of the button and edittext rather than totally removing them.
on startup
textView.setVisibility(View.GONE);
on button click
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);
I have an EditText, and when a button is clicked, I want the text to be displayed on the screen. And the EditText can be used again to add other text and on and on. Thanks!
Something like:
EditText et = (EditText)findviewById(R.id.et1);
TextView display = (TextView)findviewById(R.id.tv1);
plusBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
tv1.setText(tv.getText().toString()+"\n"+et.getText().toString());
//prints the text from edittext and concatinates it to the textview previous text
}
});
Please use below code for that, it will solve your problem.
EditText editText1 = (EditText)findviewById(R.id.editText1);
TextView textView1 = (TextView)findviewById(R.id.textView1);
plusBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
textView1.append("\n"+editText1.getText().toString());
// OR
textView1.setText(textView1.getText().toString()+"\n"+editText1.getText().toString());
}
});
You can add Views to your Layout by using its addView() Method. So you could add a TextView upon each button-click which contains the content of your TextField.