Hot questions for Using Android EditText in integer

Question:

This is my activity.xml

<EditText
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/inputnumber"
 android:inputType="number" />

this is mainactivity.java :

EditText num = (EditText) findViewById(R.Id.inputnumber) ;

the question is how can I get the integer value from this field as an integer and use it for example in a custom class I made like this to add 1 to the value of this input field?

my customclass.java

public static int addone(int a) 
{
  int b = a+1;
  return b;
}

Answer:

you can achieve this as

int val = Integer.parseInt( num.getText().toString() );

and than pass val to the method addone(val);

Question:

I am working on a code and the part of converting the string into and integer as shown above is causing the application to stop working before it starts when I upload it to the mobile. I have reduced the code as minimum as possible. the following code is giving the exact same problem. What would the solution be?

  String no=edttxt.getText().toString();       //this will get a string
  int no2=Integer.parseInt(no);              //this will get a no from the 

This is my code:

 public class MainActivity extends Activity {
    TextView txtview;
    EditText edttxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtview=(TextView)findViewById(R.id.textView);
    edttxt=(EditText)findViewById(R.id.edittxt);

    String no=edttxt.getText().toString();       //this will get a string
    int no2=Integer.parseInt(no);              //this will get a no from the string

    Button btn=(Button)findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            txtview.setText(edttxt.getText());
        }
    });

}
}

Answer:

You should be getting a java.lang.NumberFormatException because as soon as the app starts you are getting the string from edittext in onCreate(). This would return "" which would cause the exception in the line Integer.parseInt(). Depending on your use case for no2, you need to move it accordingly.

Question:

I am trying just to pass the int user input to the next class and print it, see that it works before continuing on using it or something else.

Home.java

start and exit button

public class Home extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button one = (Button) findViewById(R.id.b1);
        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToSecondActivity();
            }
        });
        Button two = (Button) findViewById(R.id.b2);
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void goToSecondActivity() {
        Intent i = new Intent(this, SelectNumberOfPlayers.class);
        startActivity(i);
    }
}

SelectNumberOfPlayers.java

Taking only the numbers from the input and passing it to StartGame.class

public class SelectNumberOfPlayers extends AppCompatActivity {

    EditText numberOfPlayers;
    Button three;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enter_number_of_players);
        three = (Button) findViewById(R.id.button3);
        three.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){

                String txt = numberOfPlayers.getText().toString();
                Intent i = new Intent(getApplicationContext(), StartGame.class);
                i.putExtra("players", txt);
                startActivity(i);
            }
        });
    }
}

StartGame.java

Receiving Int and printing to TextView

public class StartGame extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_game);

        Intent i = getIntent();

        TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
        numOfPlayersVal.setText(i.getStringExtra("Player number"));
    }
}

Where is the error happening? I've set the input keyboard to take only numbers


Answer:

1. you forgot to initialize youe editext first initialize it in your SelectNumberOfPlayers Activity

numberOfPlayers = (EditText) findViewById(R.id.numberOfPlayers);

2. the key must be same to pass and receive data with intent

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_game);

    Intent i = getIntent();

    TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
    numOfPlayersVal.setText(i.getStringExtra("players"));
}

Question:

I am attempting to convert an EditText, which is of type number in xml, to an Integer in order to calculate the value in seconds.

    hoursIn = (EditText) findViewById(R.id.hoursET);
    minIn = (EditText) findViewById(R.id.minET);
    start = (Button) findViewById(R.id.startButton);
    stop = (Button) findViewById(R.id.stopButton);
    textViewTime = (TextView) findViewById(R.id.timeDisp);

    inHr = Integer.parseInt(hoursIn.getText().toString());
    inMin = Integer.parseInt(minIn.getText().toString());

    hoursMs = hrsToMs(inHr);
    minMs = minToMs(inMin);
    totalTime = hoursMs + minMs;

When I comment the lines where inHr and inMin are initialized I get no error in runtime, however when I leave the code as it is above I get the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{dit.assignment3/dit.assignment3.Timer}: java.lang.NumberFormatException: Invalid int: ""

I have also attempted this while getting the same error starting at the same line of code:

    final CounterClass timer = new CounterClass(totalTime, 1000);
    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (hoursIn != null)
            {
                inHr = Integer.parseInt(hoursIn.getText().toString());
                hoursMs = hrsToMs(inHr);

            }
            if (minIn != null)
            {
                inMin = Integer.parseInt(minIn.getText().toString());
                minMs = minToMs(inMin);
            }
            else
            {
                textViewTime.setText("PLEASE GIVE A TIME");
            }
            totalTime = hoursMs + minMs;
            timer.start();
        }
    });

Thanks in advance :)


Answer:

I'm certain that this codes blocks are exactly same as you've shown here. That means You are directly initializing EditText and immediately calling getText() method which causes Exception.

There wont be any value immediately after initialization so that you are getting NumberFormatException when calling Integer.parseInt to empty value.

So I suggest you to put these codes inside some event like buttonClicked like here, so that you can be sure that you've entered some texts. And It's better checking if empty as well,

public void buttonClicked(View v){
    inHr = Integer.parseInt(hoursIn.getText().toString());
    inMin = Integer.parseInt(minIn.getText().toString());
}

Question:

Hi I am trying to do an EditText that only accepts float or integer numbers. I am doing like that but not works fine:

myedit=(EditText)findViewById(R.id.my_edit);

myedit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

I have also write like that:

if(!myedit.getText().toString().matches("[a-zA-Z? ]*"){
   //do something
}
else{
 //no correct values
}

Can somebody help me?


Answer:

Sorry this is late, but possibly it may help someone else stuck with this.

The easiest way I have found to accomplish this is to set the android:input value like so:

<EditText
      android:id="@+id/editText"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="numberDecimal|numberSigned" />
  • "numberDecimal" - means that it will accept floating point numbers.
  • "numberSigned" - means it will accept positive & negative numbers.

This ^^^ combination will only allow users to enter integers or floating point numbers!

Then if you wish to parse this value you may want to try something like this:

String numberAsString = "22.13";
try {
    int number = Integer.parseInt(numberAsString);
} catch (NumberFormatException e) {
    try {
        float number = Float.parseFloat(numberAsString);
    } catch (NumberFormatException ex) {
        Log.e(TAG, "Not float value", e);
        Log.e(TAG, "Not integer value", ex);
    }
}

Question:

So I'm developing currently my own private App which I want to use only for me and maybe some friends. Well I'm german so my english is maybe not the best I hope you can forgive me.

Now my Problem is that I want to set in my Optionsmenu a Budget for the current month to keep track of. I'm doing that by using an EditText with a Button.

Now I want to save this String which is getting entered in my EditText to a String value and a Integer value because I want to show the Value in a TextView on my MainPage and use the Integer value to calculate my current budget I got and so on.

Now I'm showing you guys my code and I hope u can tell me whats wrong with it. I'm trying to get my Value in the Options class which is related to my OptionsMenu and later Trying to get the Value out of my Options class into my Main class.

public class Options extends Activity {
Button GoBack, SetBudget;
private int Budget;
String BudgetString = "";
EditText BudgetEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.options);
    GoBack = (Button) findViewById(R.id.button2);
    GoBack.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent GoBackToMain = new Intent("com.lunarrepublic.STARTINGPOINT");
            startActivity(GoBackToMain);

        }
    });

    SetBudget = (Button) findViewById(R.id.button8);

    SetBudget.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            BudgetEdit = (EditText) findViewById(R.id.editText1);
            BudgetString = EditText.getText();
            //In the Line above this is  the error "Type mismatch. Cannot convert Editable to String
            Budget = Integer.parseInt(BudgetString);

        }
    });
}

But if I try to set my "BudgetString" to Editable It won't work either. The GoBack Button is unnecessary for my problem here so you don't have to read over it.

So I hope you guys understood what my problem is and can maybe help me getting it fixed

Thanks in advance


Answer:

This is wrong

BudgetString = EditText.getText();

Use below one

BudgetString = BudgetEdit.getText().toString();

Question:

To avoid unnecessary stuff, the validate function is called when the update, delete, insert button are clicked. The problem is with the EditText with inputType="number" i.e with etPrice and etSNumber.I think,there is something wrong with the validate_price() and validate_supplier_no().Please Correct Me.

public class QueryActivity extends AppCompatActivity {

        private EditText etName, etPrice, etSupplier, etSNumber;
        private Button insert_btn, increment, decrement, update_btn, delete_btn, call_btn;
        private TextView quantity_tv;
        private int quantity_value = 0;
        private TextInputLayout inputLayout_name, inputLayout_price, inputLayout_supplier, inputLayout_supplier_no;
        int _id, price, quantity, supplier_no = 0;
        String name, supplier;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_query);

            increment = findViewById(R.id.increment);
            decrement = findViewById(R.id.decrement);
            insert_btn = findViewById(R.id.insert_btn);
            update_btn = findViewById(R.id.update_product);
            delete_btn = findViewById(R.id.delete_product);
            call_btn = findViewById(R.id.call_btn);

            etName = findViewById(R.id.et_name);
            etPrice = findViewById(R.id.et_price);
            quantity_tv = findViewById(R.id.quantity);
            etSupplier = findViewById(R.id.et_supplier);
            etSNumber = findViewById(R.id.et_sNumber);


            inputLayout_name = findViewById(R.id.textInput_name);
            inputLayout_price = findViewById(R.id.textInput_price);
            inputLayout_supplier = findViewById(R.id.textInput_supplier);
            inputLayout_supplier_no = findViewById(R.id.textInput_supplier_no);


            Intent intent = getIntent();
            _id = intent.getIntExtra("_id", 0);
            name = intent.getStringExtra("name");
            price = intent.getIntExtra("price", 0);
            quantity = intent.getIntExtra("quantity", 0);
            quantity_value = quantity;
            supplier = intent.getStringExtra("supplier");
            supplier_no = intent.getIntExtra("supplier_no", 0);

            String price_str = String.valueOf(price);

            if (_id != 0) {
                etName.setText(name.toString());
                etPrice.setText(String.valueOf(price));
                quantity_tv.setText(String.valueOf(quantity).toString());
                etSupplier.setText(supplier.toString());
                etSNumber.setText(String.valueOf(supplier_no));
                insert_btn.setVisibility(View.GONE);
            } else {
                update_btn.setVisibility(View.GONE);
                delete_btn.setVisibility(View.GONE);
            }


            //OnClickListeners
            insert_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    validate(v);
                    insertProduct();
                }
            });
            update_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    validate(v);
                    updateProduct();
                }
            });
            delete_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    validate(v);
                    deleteProduct();
                }
            });

            //add quantity btn
            increment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    quantity_value += 1;
                    display(quantity_value);
                }
            });

            //subtract quantity btn
            decrement.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    quantity_value -= 1;
                    if (quantity_value <= -1) {
                        quantity_value = 0;
                    }
                    display(quantity_value);
                }
            });

            call_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(supplier_no==0){
                        Toast.makeText(getApplicationContext(),"Please Enter Supplier Number",Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Intent intent_call = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", String.valueOf(supplier_no), null));
                        startActivity(intent_call);
                    }
                }
            });


        }//onCreate Ends


        public void validate(View view) {
           try {
               if (validate_name() && validate_price() && validate_supplier() && validate_supplier_no()) {
                   Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT).show();
               }
           }
           catch (NumberFormatException e){
               Toast.makeText(getApplicationContext(), "Not Success!", Toast.LENGTH_SHORT).show();
           }


        }
        private boolean validate_supplier_no() {

        if (TextUtils.isEmpty(etSNumber.getText().toString())) {
            inputLayout_supplier_no.setError("Invalid input");
            return false;
        } else {
            inputLayout_supplier_no.setErrorEnabled(false);
            return true;
        }
    }

        private boolean validate_supplier() {

            if (etSupplier.getText().toString().isEmpty()) {
                inputLayout_supplier.setError("Supplier cannot be blanked");
                return false;
            } else {
                inputLayout_supplier.setErrorEnabled(false);
                return true;
            }
        }

        private boolean validate_price() {

            if (TextUtils.isEmpty(etPrice.getText().toString())) {
                inputLayout_price.setError("Invalid input");
                return false;
            } else {
                inputLayout_price.setErrorEnabled(false);
                return true;
            }
        }

        private boolean validate_name() {
            if (etName.getText().toString().isEmpty()) {
                inputLayout_name.setError("Name cannot be blanked");
                return false;
            } else {
                inputLayout_name.setErrorEnabled(false);
                return true;
            }
        }


        private void deleteProduct() {

            String selection = _ID + " = ? ";
            String[] selectionArgs = {String.valueOf(_id)};

            Uri uri = ContentUris.withAppendedId(CONTENT_URI, _id);
            int rowsDeleted = getContentResolver().delete(uri, selection, selectionArgs);
        }

        private void updateProduct() {

            String selection = _ID + " = ? ";
            String[] selectionArgs = {String.valueOf(_id)};

            String et_productName = etName.getText().toString();
            int et_productPrice = Integer.parseInt(etPrice.getText().toString());
            int tv_productQuantity = Integer.parseInt(quantity_tv.getText().toString());
            String et_productSupplier = etSupplier.getText().toString();
            int et_productSNumber = Integer.parseInt(etSNumber.getText().toString());

            ContentValues values = new ContentValues();
            values.put(PRODUCT_NAME, et_productName);
            values.put(PRICE, et_productPrice);
            values.put(QUANTITY, tv_productQuantity);
            values.put(SUPPLIER, et_productSupplier);
            values.put(SUPPLIER_NO, et_productSNumber);


            Uri uri = CONTENT_URI;
            int rowsUpdated = getContentResolver().update(uri, values, selection, selectionArgs);
            Toast.makeText(this, "Item inserted at: " + rowsUpdated, Toast.LENGTH_SHORT).show();


        }


        private void insertProduct() {

            String et_productName = etName.getText().toString();
            int et_productPrice = Integer.parseInt(etPrice.getText().toString());
            int tv_productQuantity = Integer.parseInt(quantity_tv.getText().toString());
            String et_productSupplier = etSupplier.getText().toString();
            int et_productSNumber = Integer.parseInt(etSNumber.getText().toString());

            ContentValues values = new ContentValues();
            values.put(PRODUCT_NAME, et_productName);
            values.put(PRICE, et_productPrice);
            values.put(QUANTITY, tv_productQuantity);
            values.put(SUPPLIER, et_productSupplier);
            values.put(SUPPLIER_NO, et_productSNumber);

            Uri uri = CONTENT_URI;
            Uri uriRowsInserted = getContentResolver().insert(uri, values);
            Toast.makeText(this, "Item inserted at: " + uriRowsInserted, Toast.LENGTH_SHORT).show();
        }

        //for updating the quantity_tv
        public void display(int number) {
            quantity_tv.setText(String.valueOf(number));
        }
    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e0e0e0"
    android:orientation="vertical"
    tools:context=".QueryActivity"

    >

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInput_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Product Name..."
            android:inputType="text" />
    </android.support.design.widget.TextInputLayout>


    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInput_price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/et_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Product Price..."
            android:inputType="number" />

    </android.support.design.widget.TextInputLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/increment"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:text="+" />

        <TextView
            android:id="@+id/quantity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="0"
            android:textSize="24sp" />

        <Button
            android:id="@+id/decrement"
            android:layout_width="55dp"

            android:layout_height="wrap_content"
            android:text="-" />


    </LinearLayout>


    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInput_supplier"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/et_supplier"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Supplier Name..."
            android:inputType="text" />
    </android.support.design.widget.TextInputLayout>


    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInput_supplier_no"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/et_sNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter SupplierNumber..."
            android:inputType="number" />
    </android.support.design.widget.TextInputLayout>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <Button
            android:id="@+id/insert_btn"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:background="@drawable/button_layout"
            android:text="ADD IT" />

        <Button
            android:id="@+id/delete_product"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:background="@drawable/button_layout"
            android:text="DELETE IT" />

        <Button
            android:id="@+id/update_product"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:background="@drawable/button_layout"
            android:text="Save Changes" />

        <Button
            android:id="@+id/call_btn"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:background="@drawable/button_layout"
            android:drawableLeft="@drawable/call"
            android:drawablePadding="-40dp"
            android:text="CALL" />


    </LinearLayout>
</LinearLayout>

Note: With Empty etPrice or etSNumber,this code throws java.lang.NumberFormatException: Invalid int: ""


Answer:

You should call insertProduct, deleteProduct and updateProduct only if inputs are valid. Change your method validate like below

public boolean validate(View view) {
       try {
           if (validate_name() && validate_price() && validate_supplier() && validate_supplier_no()) {
               Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT).show();
               return true;
           }
       }
       catch (NumberFormatException e){
           Toast.makeText(getApplicationContext(), "Not Success!", Toast.LENGTH_SHORT).show();
       }
       return false;
    }

and start using as

       delete_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(validate(v)) {
                    deleteProduct();
                }
            }
        });

Question:

I am generating editTexts in a TableRow and the editTexts width is wrapping the content by default. I tried to set their width to match_parent like this:

TableRow tr = new TableRow(getActivity());
EditText et = new EditText(getActivity());

LayoutParams lparams = new TableRow.LayoutParams(
    LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT);

et.setLayoutParams(lparams);

It did not change anything, but i can set their width if i replace LayoutParams.MATCH_PARENT with an integer. Could you please point me in the right direction to see why i cannot set their width to MATCH_PARENT?


Answer:

I accidentally found out, that if i am not using a TableRow, but placing the editTexts into the TableLayout its width will automaticall match the parent's width.

Question:

I want to get int Value from Edittext and then access that value On Button. But When I Access On Button It Show Error Remove "Final" modifier of "val". When I remove it then on button listener mViewPager.setCurrentItem(val); Show error That Change modifier of 'val'to Final. Here Is my Code

     bt_Goto =(Button)findViewById(R.id.btn_goto);

     bt_next =(Button)findViewById(R.id.btn_next);
     bt_Prev =(Button)findViewById(R.id.btn_prev);

     ed_text =(EditText)findViewById(R.id.editText1);

     final int val=-1;
     if(ed_text.getText().toString().length() > 0){
     val = Integer.parseInt( ed_text.getText().toString() );
     }
    // String str = ed_text.getText().toString().trim();


     mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);

     mViewPager.setAdapter(new TouchImageAdapter());
     mViewPager.setCurrentItem(0);

        bt_Goto.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mViewPager.setCurrentItem(val);
            }
        });

Full Code:

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.app.Activity;

public class MainActivity extends Activity {
Button bt_next,bt_Prev,bt_Goto;
EditText ed_text;
ImageView iv;
ExtendedViewPager mViewPager;
AdView adView;

int val=-1;

private StartAppAd startAppAd = new StartAppAd(this);
private static final String AD_UNIT_ID = "ca-app-pub-3785804349839269/3077204438";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

StartAppSDK.init(this, "101607288", "201235812", true);

        startAppAd.showAd(); // show the ad
        startAppAd.loadAd(); // load the next ad

         AdView adView = (AdView) this.findViewById(R.id.adView);
         AdRequest adRequest = new AdRequest.Builder().build();
         adView.loadAd(adRequest);


         // Create an ad.
         adView = new AdView(this);
         adView.setAdSize(AdSize.BANNER);
         adView.setAdUnitId(AD_UNIT_ID);

         bt_Goto =(Button)findViewById(R.id.btn_goto);
         bt_next =(Button)findViewById(R.id.btn_next);
         bt_Prev =(Button)findViewById(R.id.btn_prev);

         ed_text =(EditText)findViewById(R.id.editText1);


         if(ed_text.getText().toString().length() > 0){
         val = Integer.parseInt( ed_text.getText().toString() );
         }
        // String str = ed_text.getText().toString().trim();


         mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);

         mViewPager.setAdapter(new TouchImageAdapter());
         mViewPager.setCurrentItem(0);

            bt_Goto.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mViewPager.setCurrentItem(val);
                }
            });

        }

        static class TouchImageAdapter extends PagerAdapter {

            private static int[] images = 
                {R.raw.a0002,R.raw.a0003,R.raw.a0004,R.raw.a0005,R.raw.a0006,R.raw.a0007,R.raw.a0008,R.raw.a0009,R.raw.a0010,
                R.raw.a0011,R.raw.a0012,R.raw.a0013,R.raw.a0014,R.raw.a0015,R.raw.a0016,R.raw.a0017,R.raw.a0018,R.raw.a0019,R.raw.a0020,
                R.raw.a0021,R.raw.a0022,R.raw.a0023,R.raw.a0024,R.raw.a0025,R.raw.a0026,R.raw.a0027,R.raw.a0028,R.raw.a0029,R.raw.a0030,
                R.raw.a0031,R.raw.a0032,R.raw.a0033,R.raw.a0034,R.raw.a0035,R.raw.a0036,R.raw.a0037,R.raw.a0038,R.raw.a0039,R.raw.a0040,
                R.raw.a0041,R.raw.a0042,R.raw.a0043,R.raw.a0044,R.raw.a0045,R.raw.a0046,R.raw.a0047,R.raw.a0048,R.raw.a0049,R.raw.a0050,
                R.raw.a0051,R.raw.a0052,R.raw.a0053,R.raw.a0054,R.raw.a0055,R.raw.a0056,R.raw.a0057,R.raw.a0058,R.raw.a0059,R.raw.a0060,
                R.raw.a0061,R.raw.a0062,R.raw.a0063,R.raw.a0064,R.raw.a0065,R.raw.a0066,R.raw.a0067,R.raw.a0068,R.raw.a0069,R.raw.a0070,
                R.raw.a0071,R.raw.a0072,R.raw.a0073,R.raw.a0074,R.raw.a0075,R.raw.a0076,R.raw.a0077,R.raw.a0078,R.raw.a0079,R.raw.a0080,
                R.raw.a0081,R.raw.a0082,R.raw.a0083,R.raw.a0084,R.raw.a0085,R.raw.a0086,R.raw.a0087,R.raw.a0088,R.raw.a0089,R.raw.a0090,
                R.raw.a0091,R.raw.a0092,R.raw.a0093,R.raw.a0094,R.raw.a0095,R.raw.a0096,R.raw.a0097,R.raw.a0098,R.raw.a0099,R.raw.a0100,

                R.raw.a0101,R.raw.a0102,R.raw.a0103,R.raw.a0104,R.raw.a0105,R.raw.a0106,R.raw.a0107,R.raw.a0108,R.raw.a0109,R.raw.a0110,
                R.raw.a0111,R.raw.a0112,R.raw.a0113,R.raw.a0114,R.raw.a0115,R.raw.a0116,R.raw.a0117,R.raw.a0118,R.raw.a0119,R.raw.a0120,
                R.raw.a0121,R.raw.a0122,R.raw.a0123,R.raw.a0124,R.raw.a0125,R.raw.a0126,R.raw.a0127,R.raw.a0128,R.raw.a0129,R.raw.a0130,
                R.raw.a0131,R.raw.a0132,R.raw.a0133,R.raw.a0134,R.raw.a0135,R.raw.a0136,R.raw.a0137,R.raw.a0138,R.raw.a0139,R.raw.a0140,
                R.raw.a0141,R.raw.a0142,R.raw.a0143,R.raw.a0144,R.raw.a0145,R.raw.a0146,R.raw.a0147,R.raw.a0148,R.raw.a0149,R.raw.a0150,
                R.raw.a0151,R.raw.a0152,R.raw.a0153,R.raw.a0154,R.raw.a0155,R.raw.a0156,R.raw.a0157,R.raw.a0158,R.raw.a0159,R.raw.a0160,
                R.raw.a0161,R.raw.a0162,R.raw.a0163,R.raw.a0164,R.raw.a0165,R.raw.a0166,R.raw.a0167,R.raw.a0168,R.raw.a0169,R.raw.a0170,
                R.raw.a0171,R.raw.a0172,R.raw.a0173,R.raw.a0174,R.raw.a0175,R.raw.a0176,R.raw.a0177,R.raw.a0178,R.raw.a0179,R.raw.a0180,
                R.raw.a0181,R.raw.a0182,R.raw.a0183,R.raw.a0184,R.raw.a0185,R.raw.a0186,R.raw.a0187,R.raw.a0188,R.raw.a0189,R.raw.a0190,
                R.raw.a0191,R.raw.a0192,R.raw.a0193,R.raw.a0194,R.raw.a0195,R.raw.a0196,R.raw.a0197,R.raw.a0198,R.raw.a0199,R.raw.a0200,

                R.raw.a0201,R.raw.a0202,R.raw.a0203,R.raw.a0204,R.raw.a0205,R.raw.a0206,R.raw.a0207,R.raw.a0208,R.raw.a0209,R.raw.a0210,
                R.raw.a0211,R.raw.a0212,R.raw.a0213,R.raw.a0214,R.raw.a0215,R.raw.a0216,R.raw.a0217,R.raw.a0218,R.raw.a0219,R.raw.a0220,
                R.raw.a0221,R.raw.a0222,R.raw.a0223,R.raw.a0224,R.raw.a0225,R.raw.a0226,R.raw.a0227,R.raw.a0228,R.raw.a0229,R.raw.a0230,
                R.raw.a0231,R.raw.a0232,R.raw.a0233,R.raw.a0234,R.raw.a0235,R.raw.a0236,R.raw.a0237,R.raw.a0238,R.raw.a0239,R.raw.a0240,
                R.raw.a0241,R.raw.a0242,R.raw.a0343,R.raw.a0344,R.raw.a0245,R.raw.a0246,R.raw.a0347,R.raw.a0248,R.raw.a0249,R.raw.a0250,
                R.raw.a0251,R.raw.a0252,R.raw.a0253,R.raw.a0254,R.raw.a0255,R.raw.a0256,R.raw.a0257,R.raw.a0258,R.raw.a0259,R.raw.a0260,
                R.raw.a0261,R.raw.a0262,R.raw.a0263,R.raw.a0264,R.raw.a0265,R.raw.a0266,R.raw.a0267,R.raw.a0268,R.raw.a0269,R.raw.a0270,
                R.raw.a0271,R.raw.a0272,R.raw.a0273,R.raw.a0274,R.raw.a0275,R.raw.a0276,R.raw.a0277,R.raw.a0278,R.raw.a0279,R.raw.a0280,
                R.raw.a0281,R.raw.a0282,R.raw.a0283,R.raw.a0284,R.raw.a0285,R.raw.a0286,R.raw.a0287,R.raw.a0288,R.raw.a0289,R.raw.a0290,
                R.raw.a0291,R.raw.a0292,R.raw.a0293,R.raw.a0294,R.raw.a0295,R.raw.a0296,R.raw.a0297,R.raw.a0298,R.raw.a0299,R.raw.a0300,

                R.raw.a0301,R.raw.a0302,R.raw.a0303,R.raw.a0304,R.raw.a0305,R.raw.a0306,R.raw.a0307,R.raw.a0308,R.raw.a0309,R.raw.a0310,
                R.raw.a0311,R.raw.a0312,R.raw.a0313,R.raw.a0314,R.raw.a0315,R.raw.a0316,R.raw.a0317,R.raw.a0318,R.raw.a0319,R.raw.a0320,
                R.raw.a0321,R.raw.a0322,R.raw.a0323,R.raw.a0324,R.raw.a0325,R.raw.a0326,R.raw.a0327,R.raw.a0328,R.raw.a0329,R.raw.a0330,
                R.raw.a0331,R.raw.a0332,R.raw.a0333,R.raw.a0334,R.raw.a0335,R.raw.a0336,R.raw.a0337,R.raw.a0338,R.raw.a0339,R.raw.a0340,
                R.raw.a0341,R.raw.a0342,R.raw.a0343,R.raw.a0344,R.raw.a0345,R.raw.a0346,R.raw.a0347,R.raw.a0348,R.raw.a0349,R.raw.a0350,
                R.raw.a0351,R.raw.a0352,R.raw.a0353,R.raw.a0354,R.raw.a0355,R.raw.a0356,R.raw.a0357,R.raw.a0358,R.raw.a0359,R.raw.a0360,
                R.raw.a0361,R.raw.a0362,R.raw.a0363,R.raw.a0364};

            @Override
            public int getCount() {
                return images.length;
            }


            @Override
            public View instantiateItem(ViewGroup container, int position) {
                TouchImageView img = new TouchImageView(container.getContext());
                img.setImageResource(images[position]);
                container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                return img;


            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }


    }



}

Answer:

Declare the ed_text and mViewPager in your onCreate like this

 final EditText ed_text =(EditText)findViewById(R.id.editText1);
 final ExtendedViewPager mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);

and now you can access the ed_text value in the bt_Goto click listener like this

 bt_Goto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(ed_text.getText().toString().length() > 0){
                    mViewPager.setCurrentItem(Integer.parseInt( ed_text.getText().toString()));
                }
            }
        });