Simple validation of editText not working (Eclipse) - java

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
}
}
});

Related

How to use Onclick event when using <include> tag

I have two java class and two layout for both the class.
Each layout is having one button in it.
Both classes are extending Activity.
Now in first layout I used include tag like this
<include
android:id="#+id/clicked"
layout="#layout/activity_main" />
I can now see two buttons but the second button is not working.
First You have to declare and initialise the include view and then decalre and initialise both buttons using view.findViewById() method as follows:
View includeView = (View)findViewById(R.id.clicked);
Button button1 = (Button)includeView.findViewById(R.id.button1ID); //decalre button like this
Button button2 = (Button)includeView.findViewById(R.id.button2ID);
And then set their onClickListeners
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
** EDIT **
Fixed the typo. Should be includeView on the findViewById.
Good explanation though!

I want to access values from a popup back to the activity from which the popup has been called

I've an Activity where I've a button which will lead to popup popup window in the same Activity. In that popup, I've multiple fields. And What I all want is to get back those values from popup to the same activity.
I've been stuck with it. Need a help :)
Related Code as follows.
This is the code in onCreate method for calling the popup.
Button button = (Button) findViewById(R.id.button9);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addProductDetails();
}
});
By calling addProductDetails() method popup gets displayed.
So in this method, the code as follows
private String addProductDetails() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.storeproductdetails_layout, (ViewGroup) findViewById(R.id.popup_element1), false);
final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//get txt view from "layout" which will be added into popup window
//before it you tried to find view in activity container
/* Field Data */
product1Code = (EditText) layout.findViewById(R.id.productCodeee);
quantity1 = (EditText) layout.findViewById(R.id.quantityy);
details1 = (EditText) layout.findViewById(R.id.editText23);
orderValue1 = (EditText) layout.findViewById(R.id.editText36);
productC = String.valueOf(product1Code.getText());
qty = String.valueOf(quantity1.getText());
dts = String.valueOf(details1.getText());
orderVal = String.valueOf(orderValue1.getText());
StringBuffer sb = new StringBuffer("");
sb.append(productC+","+qty+","+dts+","+orderVal);
System.out.println("StringBuffer Value: "+sb.toString());
/* End of the field Data */
Button doneAddingProduct = (Button) layout.findViewById(R.id.doneAddingProduct);
//init your button
Button btnClosePopup = (Button) layout.findViewById(R.id.closePopup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
doneAddingProduct.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Inside onclick of done adding");
Product product = new Product();
product.setProductCode(productC);
product.setQuantity(qty);
product.setDetails(dts);
product.setOrderValue(orderVal);
app.setProduct(product);
Intent intent = new Intent(OrderReturnMgmtSecondActivity.this, OrderReturnMgmtSecondActivity.class);
startActivity(intent);
}
});
//show popup window after you have done initialization of views
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
return sb.toString();
}
Here I was trying to do is two methods in fetching those field data back to the activity.
1) Simply trying to return the field data by concatenating as a string to who ever calls this method.
2) Creating a pojo By the name as Product(which contains the field data) and setting it in application class i.e MyApplication extends Application ( you see that code as app.setProduct(product)) and redirecting it to the same activity and trying to fetch the data from that application.
But still I'm not able to get those field data.
Any help will be appreciated :)
Create an interface
interface Listener {
void onResult(Product product);
}
Pass a listener to the method and call it
addProductDetails(Listener listener){
//...
//when finished:
listener.onResult(product);
//...
}
The caller now has to implement the interface
addProductDetails(new Listener(){
void onResult(Product product){
//do something with product
}
});

Programatically set View width on button click

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);

Textview changing according to spinner and edittext with button click

What I'm trying to do is simple, I have a spinner with a few items, edittext and a button. I want to be able to select a certain item with a spinner and then type a certain value to edittext and then click a button. According to which spinner item I have selected earlier a textview will then change in the activity.
public void submitButtonClick (View submit){
Spinner s1 = (Spinner)findViewById(R.id.spinner1);
Button b1 = (Button)findViewById(R.id.button2);
if (b1.performClick())
{
switch (){
}
}
}
This is what I came up with so far, if I click button b1, the following switch statement should start (in case item 1 is selected, do a certain thing, etc.) but I don't know how to achieve this. If someone could help I would appreciate it. Thank you
This is what I have so far:
public void submit (View v){
Button b1 = (Button)findViewById(R.id.button2);
final Spinner s1 = (Spinner)findViewById(R.id.spinner1);
final Context context = this;
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = s1.getSelectedItemPosition();
switch (position){
case 0:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Warning");
alertDialogBuilder.setMessage("Please choose an item from the list");
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Bifrost.this.finish();
}
});
AlertDialog spinnerError = alertDialogBuilder.create();
spinnerError.show();
break;
case 1:
break;
}
}
});
}
The code gives no errors and app starts normally but when I select the first item and then click the button nothing happens. Did I do something wrong creating the dialog?
You first need to set an onClickListener to your button, and in this you need to get the selected item of the spinner.
b1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
int position = s1.getSelectedItemPosition();
switch(position){
case 0: //first item
break;
case 1: //second item
break;
}
}
});
You can use one of the following methods to get the spinner's selected item.
spinner.getSelectedItem()
spinner.getSelectedItemPosition()
Which one you use, has to do with how you load items into your spinner.
Follow this link for more info on those methods.
You can do something like this. you need to set a onClickListener to the button. when the button is clicked then the onClick method will be called. At that method check the selected item of the spinner.. Forexmple
Spinner s1 = (Spinner)findViewById(R.id.spinner1);
Button b1 = (Button)findViewById(R.id.button2);
b1.setOnclickListener(new onClickListener(){
public void onCLick(View v){
switch(s1.getSelectedItemPosition()){
case 0:
// do something
`enter code here`break
..........
}
}
}
);
I just tried to give a conceptual idea the code is not exact , but this is how you should do it. So my suggestion is before implementing this leanr the basic features of buttons, spinners, onCLickListeners.
You can assign a listener to your spinner.
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
// will run every time the user select an item from your spinner
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// change your textView here, base on position (index of item selected in spinner)
if (position == 0) {
// user selected the first item in spinner
}
else if (position == 1) {
// user selected the second item in spinner
}
// and so on...
}
});
After that, you can assign another listener (similar way as the code above - anonymous inner class, anonymous declaration and initialization) for your button. It will also have to override onClick etc etc. There are lots of resources online regarding all of this.
Hope this helps!

Android: Create EditText on Runtime

I'm trying to create a view where the user can click a "plus" button, and have additional EditTexts be created. The goal is to have a base of 2 EditTexts, and each time the user clicks the button, add another 2 EditTexts.
How can I do this? I can add EditTexts from Java, but I can't figure out how to add and handle a list of them dynamically.
I was hoping to take however many pairs of EditTexts, and push it into a key/value HashMap or something.
Any ideas of how to do this? Thanks!
public class MyActivity extends Activity {
private LinearLayout main;
private int id = 0;
private List<EditText> editTexts = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
Button addButton = new Button(this);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addEditText();
}
});
Button submit = new Button(this);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (EditText editText : editTexts) {
editText.getText().toString();
// whatever u want to do with the strings
}
}
});
main.addView(addButton);
main.addView(submit);
setContentView(main);
}
private void addEditText() {
LinearLayout editTextLayout = new LinearLayout(this);
editTextLayout.setOrientation(LinearLayout.VERTICAL);
main.addView(editTextLayout);
EditText editText1 = new EditText(this);
editText1.setId(id++);
editTextLayout.addView(editText1);
editTexts.add(editText1);
EditText editText2 = new EditText(this);
editText2.setId(id++);
editTextLayout.addView(editText2);
editTexts.add(editText2);
}
Do it in a ListView.
Then you can just add them to a ListAdapter.
And then use adapter.notifyDatasetChanged()
May be I am not clear but Instead of adding Individual edit text you can add as Group View like Linear layout here you can use any flag values to add dynamic name conversions also.
That view you can update into List View like inflating rows in the List View....

Categories