Wednesday, 30 September 2015

print a random number every second

package com.example.raj.testview;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.ThemedSpinnerAdapter;

import java.util.Random;
import java.util.TimerTask;



public class MainActivity extends AppCompatActivity {
    TextView tv;
    Handler handler;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         tv=(TextView)findViewById(R.id.tv);
        Thread th=new Thread(new MyThread());
        th.start();
        handler=new Handler(){
            @Override            public void handleMessage(Message msg) {
                //super.handleMessage(msg);                int k=msg.arg1;
                tv.setText(Integer.toString(k));
            }
        };


    }

    class MyThread implements Runnable{


        @Override        public void run() {

            for(int i=0;i<100;i++)
            {
                Message message=Message.obtain();
                Random r=new Random();
                int y=r.nextInt();
                message.arg1=y;
                handler.sendMessage(message);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();

        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Saturday, 26 September 2015

custom button


Custom button
other than having a png image in drawable we also need a xml in drawable the will totally define the button that we have
xml example

<item android:state_presses="true" android:drawable="@drawable/plusselected"></item>
<item android:state_focused="true" android:drawable="@drawable/plushlight"></item>
<item android:drawable="@drawable/plus"></item>


now in the xml where the actual button resides , we should change the
layout width and height to wrap_content , so that the image remains to itself
and the background of the button should be @drawable/ourxml

Create a ListMenu for Android

Create a ListMenu for Android

unlike other classes our class for Listmenu will extend ListActivity not Activity

two methods need to be overrided here
1.onCreate like we did previously
2.onListItemClick

 Then we set up a String array (as instance varible bcz we want to use it in both the methods)
 String classes[]={"startingPoint","example1","example2","example3"};// we will name it exact same as the class name(case sensitive)

 we will not use any layout here,
 we will develop all in java here

 now we need to set setListAdapter(); // it takes a array adapter or a list adapter
 setListAdapter(new ArrayAdapter<String>(context,int,StringArray));//<> gives the type
 cotext= ClassName.this
 StringArray =classes
 int = android.R.layout.simple_list_item1

 for any of the list item when clicked it will call method onListItemClick

 WE need one more method now,
 3.onListItemClick
 the function has a parameter called position which will tell us about the item clicked
 we now set up a local string there
 String cheese= classes[position];// classes was our global string array containing the names of all the classes
 so now we can setup our class for intent according to the list item that was clicked

 Class ourclass- Class.forName("com.thenewboston.travis."+cheese);// will return the class name of particular class which has the intent filter in the manifest

 Intent ourIntent= new Intent(Menu.this, ourClass);
 startActivity(ourIntent);


 

onClick function

declaring a single onClick function for all instead of using all innerClass stuff

first we need to implements View.OnClickListener

then in the onClick funtion which recives View as parameter
we need to setup swich Case to know which element has been clicked
so in the the switch case we need to write

switch(view.getId())
{
case R.id.b1: .......;break;
case R.id.b2: .......;break;
}

email

Sending email from android
using android email Intent

Intent emailIntent= new Intent(android.content.Intent,ACTION_SEND);
emailIntent.putExtra(messsage,value);
emailIntent.putExtra(android.content.Intent.Extra_EMAIL, String array value of email address(emailAdress));//adding email array

//adding subject
emailIntent.putExtra(android.content.Intent.Extra_SUBJECT, "this is my subject area");

//adding message
emailIntent.putExtra(android.content.Intent.Extra_TEXT, message);

//we can also setup text as plain type in case we are using encoding as /n and all
emailIntent.setType("plain/text");

finally start the intent
startActivity(emailIntent);

starting activity for result

starting activity for result

make the intent
pass it to startActivityForResult(intent, Acode)

overRide method onActivityResult(int requestCode, int resultCode, Intent data)
{
// inside it we first check wherether some proper result has been returned or not
if(resultCode==RESULT_OK)
{
 Bundle extra= data.getExtras();
            bit=(Bitmap)extra.get("data");
            iv.setImageBitmap(bit);

}


}

whenever we put some extra at the backend side where the data is recieved we can grab that extra
using data.getExtras() method.

Thursday, 17 September 2015

setting up onclick method

declaring a single onClick function for all instead of using all anonymous   innerClass stuff

first we need to implements View.OnClickListener

then in the onClick funtion which recives View as parameter
we need to setup swich Case to know which element has been clicked
so in the the switch case we need to write

switch(view.getId())
{
case R.id.b1: .......;break;
case R.id.b2: .......;break;
}

and in the setOnClickListener thing insead on declaring whole view.onclicklistener
just type (this)