Friday, 2 June 2017

Kotlin :: Android Button Click Listener - Global Declaration

We will get Button action in 3 steps (Button declared as a Globally)
---------------------------------------------------------------------------

1. Create Globar variable for Button
lateinit var button : Button;

2. get Button reference from xml
button=findViewById(R.id.button) as Button;

3. set OnClickListener to Button
button.setOnClickListener(View.OnClickListener {
                   });
---------------------------------------------------------------------------
Screen Shots:
---------------------------------------------------------------------------

1. MainActivity.kt
class MainActivity : AppCompatActivity() {
    lateinit var button : Button;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button=findViewById(R.id.button) as Button;

        button.setOnClickListener(View.OnClickListener {
           Toast.makeText(applicationContext,"You Clicked  
                       Button",Toast.LENGTH_SHORT).show();
        });

    }

}
 
2.  activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#FF00FF"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:textStyle="bold"
        android:textSize="20dp"
        android:text="Click Me"
        android:textColor="#ffffff"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
 
************************************

    Download Full Source Code HERE

************************************

Wednesday, 31 May 2017

Android JSONObject String Parsing Explanations

While parsing JSONObject String we will use below methods

1. has("key_name");
it used for check specific KEY is exists or not
if key exists in jsonObject it returns TRUE otherwise it returns FALSE.
                            
2. isNull("key_name");
it used for check specific OBJECT exists or not, if exists it
checks specific object is NULL or not
if key not exists or exists but equals NULL it returns TRUE otherwise it returns FALSE.
                            
3. keys();
Used for get all keys exists from JSON
                            
4. getBoolean("key_name");
Used for get boolean value from JSON;
                            
5. getInt("key_name");
Used for get Integer value from JSON;
                            
6. getLong("key_name");
Used for get Long value from JSON;
                            
7. getDouble("key_name");
Used for get Double value from JSON;
                            
8. getString("key_name");
Used for get String value from JSON;
                            
9.get("key_name");
Used for get Object value from JSON;
                            
10. getJSONObject("key_name");
Used for get JSONObject value from JSON;
                            
11. getJSONObject("key_name");
Used for get JSONArray value from JSON;

------------------------------------------------------------
Sample JSON String:

{"boolean_key":true,"int_key":42798,"long_key":5349,"double_key":70.25,"string_key":"ganesh","json_object_key":{"name":"venkat","phno":"0123456789","city":"Hyd"},"json_array_key":[50,60,70,80]}

------------------------------------------------------------

1. has("key_name");

Syntax:
jsonObject.has("key_name");
it used for check specific KEY is exists or not
if sno key exists in jsonObject it returns TRUE otherwise it returns FALSE.

Example
boolean b1 = jsonObject.has("string_key");
boolean b2 = jsonObject.has("int_key");
boolean b3 = jsonObject.has("dummy_key");
                            
show output in Log
Log.e("value1", b1 + "");
Log.e("value2", b2 + "");
Log.e("value3", b3 + "");
                            
Output
************************************
true
true
false
************************************

2. isNull("key_name");

Syntax
jsonObject.isNull("object_key_name");
it used for check specific OBJECT exists or not, if exists it checks specific object is NULL or not
if key not exists or exists but equals NULL it returns TRUE otherwise it returns FALSE.

Example
boolean b1 = jsonObject.isNull("dummy_object_key");
boolean b2 = jsonObject.isNull("json_object_key");
boolean b3 = jsonObject.isNull("json_array_key");

show output in Log
Log.e("value1", b1 + "");
Log.e("value2", b2 + "");
Log.e("value3", b3 + "");

Output
************************************
true
false
false
************************************

3. keys();

Syntax
jsonObject.keys();
Used for get JSONArray value from JSON;

Ex
Iterator<String> jsonKeys = jsonObject.keys();

show output in Log
int i = 0;
while (jsonKeys.hasNext()) {
Log.e("Value", "Keys : " + i + " : " + jsonKeys.next());
                                      i++;
}

Output
************************************
Keys : 0 : boolean_key
Keys : 1 : int_key
Keys : 2 : long_key
Keys : 3 : double_key
Keys : 4 : string_key
Keys : 5 : json_object_key
Keys : 6 : json_array_key
************************************
    
4. getBoolean("key_name");
         
Syntax
jsonObject.getBoolean("boolean_key_name");
Used for get boolean value from JSON.

Ex
boolean b = jsonObject.getBoolean("boolean_key");

show output in Log
Log.e("Value", "boolean : " + b);

Output
************************************
boolean : true
************************************

5. getInt("key_name");

Syntax
jsonObject.getInt("int_key_name");
Used for get Integer value from JSON;

Ex
int i = jsonObject.getInt("int_key");

show output in Log
Log.e("Value", "int : " + i);

Output
************************************
int : 42798
************************************

6. getLong("key_name");

Syntax
jsonObject.getLong("long_key_name");
Used for get Long value from JSON;

Ex
long l = jsonObject.getInt("long_key");

show output in Log
Log.e("Value", "long : " + l);

Output
************************************
long : 5349
************************************

7. getDouble("key_name");

Syntax
jsonObject.getDouble("double_key_name");
Used for get Double value from JSON;

Ex
double d = jsonObject.getInt("double_key");

show output in Log
Log.e("Value", "double : " + d);

Output
************************************
double : 70.0
************************************

8. getString("key_name");

Syntax
jsonObject.getString("string_key_name");
Used for get String value from JSON;

Ex
String s = jsonObject.getString("string_key");

show output in Log
Log.e("Value", "String : " + s);

Output
************************************
String : ganesh
************************************

9.get("key_name");

Syntax
jsonObject.get("object_key_name");
Used for get Object value from JSON;

Ex
Object o1 = jsonObject.get("json_object_key");
Object o2 = jsonObject.get("json_array_key");

show output in Log
Log.e("Value", "Object : " + o1);
Log.e("Value", "Object : " + o2);

Output
************************************
Object1 : {"name":"venkat","phno":"0123456789","city":"Hyd"}
Object2 : [50,60,70,80]
************************************

10. getJSONObject("key_name");

Syntax
jsonObject.getJSONObject("json_object_key_name");
Used for get JSONObject value from JSON;

Ex
JSONObject jsonObject1 = jsonObject
                                                .getJSONObject("json_object_key");

show output in Log
Log.e("Value", "JSONObject : " + jsonObject1);

Output
************************************
JSONObject : {"name":"venkat","phno":"0123456789","city":"Hyd"}
************************************

11. getJSONObject("key_name");

Syntax
jsonObject.getJSONObject("json_array_key_name");
Used for get JSONArray value from JSON;

Ex
JSONArray jsonArray = jsonObject.getJSONArray("json_array_key");

show output in Log
Log.e("Value", "JSONArray : " + jsonArray);

Output
************************************
JSONArray : [50,60,70,80]
************************************

************************************

    Download Full Source Code HERE

************************************