How to add EditText elements in Android on the same activity? - java

I have a DetailsActivity which has a TextView and TimeEditText for Date. User can add a date in that. There is a Button called Add Detail
I want that when a user enters a date like d-m-yy (future) then below this TimeEditText element, the activity should show a no. of WeekEditText as
Week1: WeekEditText1 (To add details for 1st week)
Week2: WeekEditText2 (To add details for 2nd week) and so on.
The total no. of week is (AddedDate - CurrentDate)/7
One of way of doing is calling another new activity on clicking Add Detail button and then showing all EditTexts on next activity while passsing AddedDate etc through Intent.
But is it possible to show EditText on same page below TimeEditText after user enters a date?

Have a Button that when clicked adds the number of EditTexts you need
public void onClick(View v)
{
// do whatever else you need here
int numWeeks =(AddedDate - CurrentDate)/7;
for (i=1; i<=numWeeks;i++)
{
EditText et = new EditText(YourActivity.this);
// add whatever here
}
}
You can add whatever kind of Layout you want before creating the EditText then add each one to that Layout. Something like that ought to work for you

You can dynamically inflate a layout or layout element such as and Edit Text.
For Example,
if(TimeEditText.getText != null)
EditText et = new EditText();

Related

How to display the checked radio button label in the next activity?

int radioId = radioGroup2.getCheckedRadioButtonId();
radioButton2 = findViewById(radioId);
textView1.setText(radioButton2.getText() + " > ");
I want to display the label of checked radio button in the next activity like this -----> Android Developers > Docs > Guiders
Please help me.
Next, how can I display the label of checked radio button
(first activity) in the third activity ?
There is several ways that's depend on your need:
Using Intent Data
You can put that text as extra into your intent that starting next activity:
Intent i = new Intent(firstActivity.this, nextActivity.class);
i.putExtra("CheckedRadioText", radioButton2.getText());
startActivity(i);
And get extra at next activity's onCreate method like below:
Intent i = getIntent();
String text = i.getStringExtra("CheckedRadioText");
myTextView.setText(text);
You can set radio button's text to a static string and access it from next activity easily (personally, not recommended).
If this radio button is something like setting selection that you need to save this. Then you can save that text to a sharedPreference. There are several tutorials available for this approach.

How to generate unique button with button click

I have a floating action button that, when clicked, an editText field opens up where the user can enter text. The text would then be taken via getText().toString() and entered into the text of a new button. I can make it so that each time the user goes through this process, a new button is created and positioned below the previously created button. The problem is, each new button is not unique. It creates a new instance of the exact same button so that every button has the exact same id. How do I generate a new, completely unique button with unique id in order to reference each button individually later on?
confirmBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!editTitle.getText().toString().isEmpty()) {
projectBtn = new Button(getActivity());
projectBtn.setId(R.id.projNewBtn);
projectBtn.setText(editTitle.getText().toString());
project_layout.addView(projectBtn);
}
editTitle.setText("");
project_layout.removeView(editTitle);
project_layout.removeView(linearLayout);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
});
I just solved my own problem by simply adding two lines of code; one to initialize a button ArrayList, and one to add the button to the array each time a new button is created. I simply set the id as a variable int so that once stored in the array, I am able to reference it via the corresponding number position.

Android advice on creating new Layout in Activity and dynamically changing items inside Layout

I have an Activity from which I get some input that I must use afterwards. I get an Integer number from a NumberPicker and an ArrayList<String> from a multiple choice AlertDialog. In that Activity I also have a button that starts the "game". When the button is clicked I want to open a new Activity so I can use the Integer number and the ArrayList but I will lose them if I go to a new Activity. So my guess is I should create a new Layout from the current Activity and handle the input there. I need some TextViews and a counter that can be dynamically changed on button click. For example the first TextView will be the first element of that ArrayList. On button clicks the counter must add 1 (counter += 1). When the counter equals the Integer number from the NumberPicker (user input) the TextView must change from ArrayList(first element) to ArrayList(second element). I don't need help on the logic. This was just to clarify things. I need to know how to do that with a layout dynamically. I need some guidance how can I do that or is that the way to go (layout not Activity). Tutorial links/examples/advice will help me a lot.
When the button is clicked I want to open a new Activity so I can use
the Integer number and the ArrayList but I will lose them if I go to a
new Activity.
Look into the putStringArrayListExtra() method in the Intent class. This will allow you to pass your ArrayList to the new Activity:
List<String> stringArrayList = new ArrayList<>();
int intValue = 5;
Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
i.putExtra("int_key", intValue);
i.putStringArrayListExtra("string_key", stringArrayList);
startActivity(i);

using checkboxes to check data when button is clicked

me again, iv tried following the android checkbox example, and whilst it has worked the most part, i need the code to only be initiated when a button is clicked, there is only one button in the program and this button is also used to do the maths calcs.
I think i would need a nested if statement, but im just wondering if any of you guys could help me with the construction of this, when the checkBox is checked, i would like it to check if there is data in the text field next to the checkbox, if there is data i would like the program to bring up an error statement, although this is only to be done once the calculation button has been called on.
can anyone give me any help on constructing this, i have tried adding listeners but i already have one on the calc button and if i add more into this method, i get error messages :/
There are three checkboxes, all used to check different field, however i also only want one checkbox to be checked at any one time and only if there is no data in the text field that applies to this.
cheers guys, sorry if im a bit vague but im starting to loose my rag with my lack of knowledge.
cheers
public class Current extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);
// Show the Up button in the action bar.
setupActionBar();
Button calc1 = (Button)findViewById(R.id.current_calculate);
calc1.setOnClickListener(new View.OnClickListener() {
CheckBox checktime = (CheckBox) findViewById(R.id.custom1);
CheckBox checkcurrent = (CheckBox) findViewById(R.id.custom2);
CheckBox checkcharge = (CheckBox) findViewById(R.id.custom3);
public void onClick(View v) {
//i would like this part to check that checktime is checked and that there is no data in editText Time1
if (((CheckBox) v).isChecked()) {
Toast.makeText(Current.this,
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
//i would like this part to check that checkCurrent is checked and that there is no data in editText current1
if (((CheckBox) v).isChecked()) {
Toast.makeText(Current.this,
"Bro, i mean it ", Toast.LENGTH_LONG).show();
}
//i would like this part to check that checkCharge is checked and that there is no data in editText charge1
if (((CheckBox) v).isChecked()) {
Toast.makeText(Current.this,
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
// please note that i have not yet added the current.text field yet as i cannot be bothered with it throwing up errors just yet, please bear
// in mind that i will be doing another two sums on this program, the next sum will be current1 / time1 and then the next will be charge1/current1
// any help would be much appreciated, for now i only want the program to check that EditText current1 is empty and if so do the calculation bellow
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
}
First of all as it is onclicklistener for a button, v would always be button in your case. So ((CheckBox) v).isChecked() would give runtime error. Change that to checktime.isChecked() or any other checkbox you would like to check its checked status. As for checking the emptyness of editext it can be done by edittext.getText().toString().isEmpty()
public void onClick(View v) {
//i would like this part to check that checktime is checked and that there is no data in editText Time1
if(Time1.length()==0){
checktime.setChecked(true);
Toast.makeText(Current.this,
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
//i would like this part to check that checkCurrent is checked and that there is no data in editText current1
if(current1.length()==0){
checkCurrent.setChecked(true);
Toast.makeText(Current.this,
"Bro, i mean it ", Toast.LENGTH_LONG).show();
}
//i would like this part to check that checkCharge is checked and that there is no data in editText charge1
if(charge1.length()==0){
checkCharge.setChecked(true);
Toast.makeText(Current.this,
"Bro, tryAndroid ", Toast.LENGTH_LONG).show();
}
// please note that i have not yet added the current.text field yet as i cannot be bothered with it throwing up errors just yet, please bear
// in mind that i will be doing another two sums on this program, the next sum will be current1 / time1 and then the next will be charge1/current1
// any help would be much appreciated, for now i only want the program to check that EditText current1 is empty and if so do the calculation bellow
// EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
// EditText Time1 = (EditText)findViewById(R.id.number_input_3);
// TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
Just make sure that you findviewbyid all the edit texts before the button click.

Trying to edit value of TextView on LongClick -- Almost working

I have two elements (TextView) in my XML layout that when a LongClick is pressed it will prompt the user to enter in a new value and then when the DONE button is clicked it should show the newly inputed value to the tvScoreHome using setText().
When I do a Long Click on the mentioned element the edit field and keyboard appear as expected. However, it won't allow me to type anything. When I type something it nothing shows up (but the device vibrates as if a button was pressed) and when the DONE button is clicked it vibrates as well but it does not exit the keyboard and show anything in the tvScoreHome element.
Any ideas why?
// set the onLongClickListener for tvScoreHome
tvScoreHome.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
final EditText userInput = (EditText) findViewById(R.id.userInput);
InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
userInput.setVisibility(View.VISIBLE);
imm.showSoftInput(userInput, 0);
tvScoreHome.setText( userInput.getText() );
userInput.setVisibility(View.INVISIBLE);
return true;
}
});
You need to give the user a chance to input some text before copying the data and hiding the EditText.
Remove these two lines from your listener:
tvScoreHome.setText( userInput.getText() );
userInput.setVisibility(View.INVISIBLE);
Perhaps you could use an OnFocusChangeListener to run these two lines when userInput loses focus.

Categories

Resources