Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Monday, April 27, 2015

What is android application manifest file?


Application Manifest


Every application must have an AndroidManifest.xml file (with precisely that name) in its root directory. The manifest file presents essential information about your app to the Android system, information the system must have before it can run any of the app's code. Among other things, the manifest does the following:
  • It names the Java package for the application. The package name serves as a unique identifier for the application.
  • It describes the components of the application — the activities, services, broadcast receivers, and content providers that the application is composed of. It names the classes that implement each of the components and publishes their capabilities (for example, which Intent messages they can handle). These declarations let the Android system know what the components are and under what conditions they can be launched.
  • It determines which processes will host application components.
  • It declares which permissions the application must have in order to access protected parts of the API and interact with other applications.
  • It also declares the permissions that others are required to have in order to interact with the application's components.
  • It lists the Instrumentation classes that provide profiling and other information as the application is running. These declarations are present in the manifest only while the application is being developed and tested; they're removed before the application is published.
  • It declares the minimum level of the Android API that the application requires.
  • It lists the libraries that the application must be linked against.

Android change text colour programatically



We can use various functions from the Color class to  change text colour programatically
  • TextView text = (TextView) findViewById(R.id.text); text.setTextColor(Color.parseColor("#FFFFFF"));
  • holder.text.setTextColor(Color.RED);
  • Color.parseColor  (like LEX uses)text.setTextColor(Color.parseColor("#FFFFFF"));
  • Color.rgb and Color.argb holder.text.setTextColor(Color.rgb(200,0,0)); holder.text.setTextColor(Color.argb(0,200,0,0));
  • And of course, if you want to define your color in an XML file, you can do this:
<color name="errorColor">#f00</color>
           and then use this code to show it:
textView.setTextColor(getResources().getColor(R.color.errorColor));

  • You can also insert plain HEX, like so: myTextView.setTextColor(0xAARRGGBB);


What is Intent in Android ?



    An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use-cases:

    • To start an activity:


    An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent tostartActivity(). The Intent describes the activity to start and carries any necessary data.

    If you want to receive a result from the activity when it finishes, call startActivityForResult(). Your activity receives the result as a separate Intent object in your activity's onActivityResult()callback. For more information, see the Activities guide.

    • To start a service:


    A Service is a component that performs operations in the background without a user interface. You can start a service to perform a one-time operation (such as download a file) by passing an Intent to startService(). The Intent describes the service to start and carries any necessary data.

    If the service is designed with a client-server interface, you can bind to the service from another component by passing an Intent to bindService(). For more information, see the Services guide.

    • To deliver a broadcast:


    A broadcast is a message that any app can receive. The system delivers various broadcasts for system events, such as when the system boots up or the device starts charging. You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(),sendOrderedBroadcast(), or sendStickyBroadcast().

    Android application parsing JSON tutorial


    Advantage of JSON over XML

    1) JSON is faster and easier than xml for AJAX applications.
    2) Unlike XML, it is shorter and quicker to read and write.
    3) It uses array.

    Example of android JSON parsing

    activity_main.xml
    Drag the one textview from the pallete. Now the activity_main.xml file will look like this:
    <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="75dp"
    android:layout_marginTop="46dp"
    android:text="TextView" />

    </RelativeLayout>

    Activity class


    Let's write the code to parse the xml using dom parser.

    import org.json.JSONException;
    import org.json.JSONObject;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class MainActivity extends Activity {
    public static final String JSON_STRING="{\"employee\":{\"name\":\"Sachin\",\"salary\":56000}}";
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView textView1=(TextView)findViewById(R.id.textView1);
    try{
    JSONObject emp=(new JSONObject(JSON_STRING)).getJSONObject("employee");
    String empname=emp.getString("name");
    int empsalary=emp.getInt("salary");
    String str="Employee Name:"+empname+"\n"+"Employee Salary:"+empsalary;
    textView1.setText(str);
    }catch (Exception e) {e.printStackTrace();}
    }
    }

    Sunday, April 26, 2015

    Difference between android studio and eclipse

    Most of Google’s 2013 I/O event was about Android. One specific announcement managed to attract a lot of hype and that was no other than Google’s own Android IDE, Android Studio. It has been just over a year now since the first public release and Android Studio has come a long way, despite still being in beta. The IDE itself is based off the very popular IntelliJ IDEA from JetBrains and is being offered by Google for free.

    On the other hand, Eclipse is more mature than ever and Google’s ADT plugin which transforms the popular IDE into a fully featured Android developing environment has become very stable. Eclipse feels like home for many Java developers and is a natural starting point for Java developers who want to get into Android development.
    While both solutions look promising, which one has the edge over the other? In this blog we will compare the two in 5 distinct areas in an effort to reveal the main differences in the Android Studio vs Eclipse battle.


    Build Tools


    Android Studio utilizes the fast growing Gradle build system. It builds on top of the concepts of Apache Ant and Apache Maven but it also introduces a Groovy DSL (Domain-Specific Language) that allows for scripted builds which opens up many automation possibilities like uploading your beta .apk to TestFlight for testing. Eclipse on the other hand uses Apache Ant as its main build system which a very robust XML based build system that many Java developers may already be familiar with.

    Advanced Code Completion/Refactoring


    Both IDEs feature the standard Java code auto completion but in the case of Android Studio, Google has baked in deeper support for specific Android code and refactoring. Android Studio can refactor your code in places where it’s just not possible using Eclipse and ADT. In addition, in my opinion IntelliJ’s Java auto1completion seems more “intelligent” and predicts better what I want to do so there is definitely an improvement in this area over Eclipse.

    User Interface Design


    One of the main selling point Google used to market Android Studio when it came out was its completely redesigned user interface design tool. After working with it for some time, it’s clear that the new tool is much better than the old. It literally crashes it. The new interface design too in Android Studio is faster, responds to changes more rapidly and has more customization options that with Eclipse, you had to manually set in the XML.

    Project Organization


    Both IDEs work differently in an effort to help you manage and organize your projects. If you’ve used Eclipse then you must be familiar with the concept of workspaces. When Eclipse starts, you select the workspace that contains your projects and you can load all project of that workspace in your tree navigation. If you want to switch to a project in a different workspace, then you have to restart the whole IDE. Android Studio treats this situation differently by introducing the concept of modules. Your app could be one module, a library that you just downloaded can be another and the Ad SDK you are currently integrating could be a third. Each of these modules can have their own Gradle build files and declare their own dependencies. To me, Android Studio seems more natural but it takes some time to get used to if you have been using Eclipse for a long time.

    IDE Performance/Stability


    Eclipse is a purely Java based soft1ware, and a big one. In order to run it reliably you need to have more than decent amount of RAM and good CPU power to back it up. Many user who do not strictly meet these criteria are reporting very bad experiences with it. It is not unusual for Eclipse to crash while exporting an apk or having to restart it after using it for a few hours straight. Having said that, Android Studio is still in beta so it comes with its own bugs that crash the IDE every now and then but in the meanwhile, the whole experience feels faster and more robust.

    What is Android Studio?

    Android Studio provides graphical tools for creating and managing Android projects, which contain everything that define your Android apps, from app source code to build configurations and test code. Each project contains one or more different types of modules, such as application modules, library modules, and test modules.

    Android Studio is an integrated development environment (IDE) for developing on the Android platform. It was announced on May 16, 2013 at the Google I/O conference by Google's Product Manager, Ellie Powers. Android Studio is freely available under theApache License 2.0.

    Monday, July 15, 2013

    Custom Android Dialog Without Title

    I searched a lot for creating android custom dialog without title. At last i found the following. Create custom android dialog without title in the following way.

    Step-1: Add a android style in style.xml file :
              <style name="TitleLessDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
     </style>

    Step-2: While declaring the dialog in your activity file declare as following

    .
    import android.app.Dialog;

    import android.content.Context;
    .
    .
    Context context = this;
    Dialog dialog = new Dialog(context,R.style.TitleLessDialog);
    .
    .
    .


    The rest of your code is as usual Dialog building code.

    Thank you.
    bodhisattwa

    Creating Android Custom TextView Rounded Corner Programmatically

    To create a rounded corner custom TextView in your android application, follow the below steps.


    Step-1: Create a custom xml file named "custom_shape.xml" under /res/drawable folder.

    <?xml version="1.0" encoding="UTF-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <gradient  
         android:angle="270"/>
    <corners 
         android:bottomRightRadius="7dp" 
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp" 
         android:topRightRadius="7dp"/> 
      <solid android:color="@android:color/transparent"/>  
      <stroke 
          android:color="@android:color/black"
          android:width="5sp"/> 
    </shape> 

    Step-2: Create a custom TextView class "TextViewOutLined.java"

    public class TextViewOutLined extends TextView
    {
    public static int textSize = 30;  //The size of the text can be changed later
    public TextViewOutLined(Context context) 
    {  
    super(context); 
    init(); 
    }
    @Override   
     protected void onDraw(Canvas canvas) {  
    super.onDraw(canvas);                 
    this.setBackgroundResource(R.drawable.custom_shape);    
    } 
    private void init(){  
    setTextSize(textSize);  
    setPadding(5, 5, 5, 5);  //This will set the padding, and by increasing decreasing this 
    // parameters the text padding can be changed 
    }
    }

    Step-3: Use the class TextViewOutLined in your android program as:
    private final TextViewOutLined tv = new TextViewOutLined(context);
    tv.setText("www.appsoid.in");

    Thank you.
    bodhisattwa