Showing posts with label EditText. Show all posts
Showing posts with label EditText. Show all posts

Monday 3 November 2014

Android Tutorial 40 : Adding EditText in Dialog

We can create a dialog with Edittext   and other views like Button, CheckBoxes, RadioButtons etc.

For this we need to Create A xml layout and and  inflate it in AlertDialog



                                

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
   
    <EditText
        android:id="@+id/editTextKeywordsToBlock"
        android:hint="Enter 1 or more keywords. Use space berween two keywords"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <LinearLayout
                 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
           
            android:layout_marginTop="10dp">
                 

     <Button
         android:id="@+id/buttonBlockByKeyword"
         android:layout_marginTop="15dp"
         android:layout_weight="1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="SAVE"
         />
    
      <Button
         android:id="@+id/buttonCancelBlockKeyword"
         android:layout_marginTop="15dp"
         android:layout_weight="1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Cancel"
         />
   
     </LinearLayout>
   

</LinearLayout>


And inflate this Layout at run time    like following



final Dialog dialog = new Dialog(this);

                    dialog.setContentView(R.layout.block_by_keyword);
                    dialog.setTitle("Keyword To Block");

                    final EditText editTextKeywordToBlock=(EditText)dialog.findViewById(R.id.editTextKeywordsToBlock);
                    Button btnBlock=(Button)dialog.findViewById(R.id.buttonBlockByKeyword);
                    Button btnCancel=(Button)dialog.findViewById(R.id.buttonCancelBlockKeyword);
                    dialog.show();

Saturday 1 November 2014

Android Tutorial 6 : Using EditText In Android

Hi Friends
In this post   we will learn to use the Buttons, EditText , and TextViews.
EditText are used to take input from user. (like Textbox in java).
TextViews are used to show something (like Label in java.)

Note :If you are new to android and have not create any application in Android the read this Create First Project in Android   and then proceed.

So what we are going to do ?
We will create an application "Calculator" which will perform addition and subtraction.
We will have two EditText to take inputs.
A text view to show the result
And two buttons:
Add Button : to perform addition
Subtract button: to perform subtraction.

Create a new Project named as "Calculator"  and give the name "CalculatorActivity" to your activity.

Edit the main.xml file

edit your main.xml file add "Butttons", "Textviews" and "Edittexts" ,
It should like below.( you can just copy the code and paste in amin.xml file)

                           

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   

    <TextView
        android:layout_marginTop="30dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="First Number" />

    <EditText
        android:id="@+id/FirstNumber"
        android:hint="First Number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" >

        <requestFocus />

    </EditText>
   
   
   

    <TextView
         android:layout_marginTop="15dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Second  Number" />
    <EditText
        android:id="@+id/SecondNumber"
        android:hint="Second Number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" />
   
   
   

    <TextView
        android:id="@+id/result"
        android:layout_marginTop="30dp"
        android:textSize="25dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />
   
   
   <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >
        <Button
               android:id="@+id/buttonAdd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="ADD" />
         <Button
             android:id="@+id/buttonSubtract"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="SUBTRACT" />

    </LinearLayout>
</LinearLayout>



Editing CalculatorActivity file


now open your CalculatorActivity file , it should look like 

public class CalculatorActivity extends Activity
{
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
       
                   
                    // get the Id's for refference
                    final TextView result=(TextView)findViewById(R.id.result);
                    final EditText editTextFirstNumber=(EditText)findViewById(R.id.FirstNumber);
                    final EditText editTextSecondNumber=(EditText)findViewById(R.id.SecondNumber);
                    Button addButton=(Button)findViewById(R.id.buttonAdd);
                    Button subtractButton=(Button)findViewById(R.id.buttonSubtract);
                   
                    //  add Button OnclickListener
                   
                    addButton.setOnClickListener(new View.OnClickListener() {
                       
                        public void onClick(View v)
                        {
                              int firstNumber=Integer.parseInt(editTextFirstNumber.getText().toString());
                              int secondNumber=Integer.parseInt(editTextSecondNumber.getText().toString());
                              result.setText("Answer is : "+String.valueOf(firstNumber+secondNumber));
                        }
                    });
                   
                    subtractButton.setOnClickListener(new View.OnClickListener() {
                       
                        public void onClick(View v)
                        {
                              int firstNumber=Integer.parseInt(editTextFirstNumber.getText().toString());
                              int secondNumber=Integer.parseInt(editTextSecondNumber.getText().toString());
                              result.setText("Answer is : "+String.valueOf(firstNumber-secondNumber));
                        }
                    });
       
    }
}


now run your application  and perform add or subtract action.

                                             

 Hope you enjoyed the post .
Comments are Welcome



Android Tutorial 5 : Using Buttons in Android

Hi Friends today  we will learn to use the Buttons, EditText , and TextViews.
EditText are used to take input from user. (like Textbox in java).
TextViews are used to show something (like Label in java.)

Note :If you are new to android and have not create any application in Android the read this Create First Project in Android   and then proceed.

So what we are going to do ?
We will create an application "Calculator" which will perform addition and subtraction.
We will have two EditText to take inputs.
A text view to show the result
And two buttons:
Add Button : to perform addition
Subtract button: to perform subtraction.

Create a new Project named as "Calculator"  and give the name "CalculatorActivity" to your activity.

Edit the main.xml file

edit your main.xml file add "Butttons", "Textviews" and "Edittexts" ,
It should like below.( you can just copy the code and paste in amin.xml file)

                           

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   

    <TextView
        android:layout_marginTop="30dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="First Number" />

    <EditText
        android:id="@+id/FirstNumber"
        android:hint="First Number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" >

        <requestFocus />

    </EditText>
   
   
   

    <TextView
         android:layout_marginTop="15dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Second  Number" />
    <EditText
        android:id="@+id/SecondNumber"
        android:hint="Second Number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone" />
   
   
   

    <TextView
        android:id="@+id/result"
        android:layout_marginTop="30dp"
        android:textSize="25dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />
   
   
   <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >
        <Button
               android:id="@+id/buttonAdd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="ADD" />
         <Button
             android:id="@+id/buttonSubtract"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="SUBTRACT" />

    </LinearLayout>
</LinearLayout>



Editing CalculatorActivity file


now open your CalculatorActivity file , it should look like 

public class CalculatorActivity extends Activity
{
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
       
                   
                    // get the Id's for refference
                    final TextView textViewResult=(TextView)findViewById(R.id.result);
                    final EditText editTextFirstNumber=(EditText)findViewById(R.id.FirstNumber);
                    final EditText editTextSecondNumber=(EditText)findViewById(R.id.SecondNumber);
                    Button addButton=(Button)findViewById(R.id.buttonAdd);
                    Button subtractButton=(Button)findViewById(R.id.buttonSubtract);
                   
                   //  add Button OnclickListener
     
        addButton.setOnClickListener(new View.OnClickListener() {
         
            public void onClick(View v)
            {
                String strFirstNumber=editTextFirstNumber.getText().toString();
                String strSecondNumber=editTextSecondNumber.getText().toString();
              
                // If user has Entered Nothing
                if(strFirstNumber.equals("")||strSecondNumber.equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Please Eneter Both the Numbers", Toast.LENGTH_LONG).show();
                    return;
                }
              
                int firstNumber=Integer.parseInt(strFirstNumber);
                int secondNumber=Integer.parseInt(strSecondNumber);
                  textViewResult.setText("Answer is : "+String.valueOf(firstNumber+secondNumber));
            }
        });
     
        subtractButton.setOnClickListener(new View.OnClickListener() {
         
            public void onClick(View v)
            {
                 String strFirstNumber=editTextFirstNumber.getText().toString();
                 String strSecondNumber=editTextSecondNumber.getText().toString();
               
                 // If User has Entered Nothing
                 if(strFirstNumber.equals("")||strSecondNumber.equals(""))
                 {
                     Toast.makeText(getApplicationContext(), "Please Eneter Bothe the Numbers", Toast.LENGTH_LONG).show();
                     return;
                 }
               
                 int firstNumber=Integer.parseInt(strFirstNumber);
                 int secondNumber=Integer.parseInt(strSecondNumber);
                  textViewResult.setText("Answer is : "+String.valueOf(firstNumber-secondNumber));
            }
        });

       
    }
}


now run your application  and perform add or subtract action.

                                             

 Hope you enjoyed the post .
Comments are Welcome