Ir al contenido principal

Android - First Step


Get an element

To get an specific element by id use the method findViewById
findViewById(R.id.name)

After you do a cast to the corresponding type
private EditText mName = (EditText) findViewById(R.id.name);

Link events with code

In the file activity_main.xml inside the object (for example: ImageButton, Button) use the attribute android:onClick="method_name"

After in the your Activity class (for example MainActivity.java) declare your method as you named previously (in this case method_name()), passing as parameter the View.

    public void method_name(View v) {
     //Your code
    }

Send message in the view (Toast)

Inside your method create a Toast object and use first the method makeText() and then show() method.


Toast.makeText(this.getApplicationContext(), R.string.app_name, Toast.LENGTH_LONG).show();

Animation

Inside the activity method (Ex. public void processForm(View view) {}  ), create the Animation object. In this case the method belong to a button.

Animation anim = AnimationUtils.makeOutAnimation(this, true);
view.startAnimation(anim);

----------------------- Intent ---------------------------------

Call another View

To call another View use the following code, where  SettingsActivity is your class' name.
 Intent i = new Intent(this,SettingsActivity.class);
            startActivity(i);

 Send a plain text message

Inside the activity method (Ex. public void processForm(View view) {}  ), create Intent object.

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "This is my message");
startActivity(i);

Send sms

Inside the activity method (Ex. public void processForm(View view) {}  ), create Intent object.

mPhone = (EditText) findViewById(R.id.phone); // this is a textbox

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.fromParts("sms", mPhone));
intent.putExtra("sms_body", comments);

Send mail and review configured email client

 Inside the activity method (Ex. public void processForm(View view) {}  ), create Intent object.

 Intent intent = new Intent(Intent.SEND_TO);
intent.setData( Uri.fromParts("mailto", "destiny_mail", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "message");

   Validate email client


if( intent.resolveActivity(getPackageManager()) == null ){}

When the method resolveActivity return null there is not application to execute the Intent

Send an image file

Inside the activity method (Ex. public void processForm(View view) {}  ), create Intent object.
In this case you have to define the type of file and extension share.setType("image/jpeg").
Also you have to declare the URL where is the file with share.putExtra(Intent.EXTRA_STREAM, uri);
Finally you need to execute the activity like the others with startActivity and if you want configure the chooser application with your own message.

Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpeg");
        share.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(share, "Share using..."));

Get Resource / startActivityForResult()

This intent open the gallery. The method startActivityForResult return the result of the new created activity.


Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
 intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent,"Select..."), REQUEST_CODE);


After the intent finished the task execute the method onActivityResult, to use it you have to override the method in your code, one of the parameters the method received is resultCode, the parameter received as possible values: RESULT_CANCELED or RESULT_OK


 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {

 
    }
}

Scan new images

This intent do a refresh action to view the new images in our cellphone. The difference for this case is that instead of use startActivity to execute the intent, now you have to use
sendBroadcast method.

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(uri);
        sendBroadcast(intent);



Comentarios

Entradas populares de este blog

C# Using tabs

To use tabs in C# use the TabContainer element from AjaxControlToolkit Include AjaxControlToolkit  Include in the Web.config file, inside the tag <system.web> the following code  <pages>       <controls>         <add tagPrefix="ajaxCTK" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>       </controls>     </pages>   Include TabContainer element First  include TabContainer element that is the section where all the tabs will be displayed. <ajaxCTK:TabContainer ID="TabContainerUpdate" runat="server"                 Height="800"                 CssClass="ajax__tab_style"> </ajaxCTK:TabContainer> Second per each tab include the following code corresponding to each ...

Rails - Basic Steps III

pValidations Validations are a type of ActiveRecord Validations are defined in our models Implement Validations Go to   root_app/app/models Open files  *.rb for each model Mandatory field validates_presence_of   :field Ex:   validates_presence_of    :title Classes The basic syntax is class MyClass        @global_variable                def my_method              @method_variable        end end Create an instance myInstance = MyClass.new Invoke a mehod mc.my_method class() method returns the type of the object In Ruby, last character of method define the behavior If ends with a question -> return a boolean value If ends with an exclamation -> change the state of the object Getter / Setter method def global_variable       return @global_variable end ...

Python create package

Create a root folder Create a sub-folder "example_pkg" that contains the funtionallity packaging_tutorial/ example_pkg/ __init__.py In the root folder create the following structure  packaging_tutorial/ example_pkg/ __init__.py tests/ setup.py LICENSE README.md in the setup.py contains the configuration of the packages your package is found by find_packages() import setuptools with open ( "README.md" , "r" ) as fh : long_description = fh . read () setuptools . setup ( name = "example-pkg-YOUR-USERNAME-HERE" , # Replace with your own username version = "0.0.1" , author = "Example Author" , author_email = "author@example.com" , description = "A small example package" , long_description = long_description , long_description_content_type = "text/markdown" , url = "https://github.com/pypa/sam...