Monday, October 7, 2013

Entering text and pausing for android

This is a test of calling the default input scheme,  whatever it is, and obtaining a "keyboard", i.e., alpha numeric, input for future use.  Also, we look into timing, sequencing, and pausing. This code was stolen from http://stackoverflow.com/questions/4111905/how-do-you-have-the-code-pause-for-a-couple-of-seconds-in-android. Other code was stolen from http://stackoverflow.com/questions/12622742/get-value-from-dialogfragment.


package com.mycompany.test1;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.Intent;
import android.widget.EditText;



public class MainActivity extends Activity
{

public final static String EXTRA_MESSAGE = "com.wpd0101.firstapp.MESSAGE";
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
/*System.out.println("hello");*/
    }
/** Called when the user clicks the Send button */
public void sendMessage(View view)
{
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}

Comments:
1)  /*System.out.println("hello");*/ is a commented out statement to show us that the program started,. LogCat needs to be viewed in order to see this comment.
2)  Intent intent = new Intent(this, DisplayMessageActivity.class); This defines an "intent". DisplayMessageActivity is what used to be called an external subroutine. It's code can be found below. It is held in a separate file in the same directory as the source (above).


package com.mycompany.test1;
/*
http://stackoverflow.com/questions/4111905/how-do-you-have-the-code-pause-for-a-couple-of-seconds-in-android
*/
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class DisplayMessageActivity extends Activity
{
private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);

// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

// Create the text view
final TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);

// Set the text view as the activity layout
setContentView(textView);
CharSequence text = message;
int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
text = text + " 2nd message";
toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
handler.postDelayed(new Runnable() {
public void run()
{
// doStuff();
textView.append(", delay");
}
}, 5000);
handler.postDelayed(new Runnable() {
public void run()
{
// doStuff();
textView.setText("try 2");
setContentView(textView);
}
}, 5000);
}

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
{
        switch (item.getItemId())
{
case android.R.id.home:
return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Here we use "toast" to put messages on the screen. Playing around with the delays and messages might help to clarify what's going on.

parsing filename into parts

This is an example of String manipulation using Android/Java. The code uses a subroutine to do the work, and is explained below. First the complete code:




package com.wpd0101.parseFileName;

import android.app.*;
import android.os.*;
import android.util.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
//http://android-er.blogspot.com/2011/04/how-to-get-file-extension-using-java.html?m=1
private static final String TAG = "DEBUG-parseFileName";
    @Override
    public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

String infile = "jack.jpg";
String first = myFileName(infile);
Log.v(TAG," result = "+first);
    }
In above code, the call to the subroutine returns a value (String) of "first". Presumably, one would do something with this value elsewhere in one's programming.

public String myFileName(String infile){
Log.e("arg was ",infile);
int dotposition= infile.lastIndexOf(".");
Log.v("dotpos = ",String.valueOf(dotposition));
return(infile.substring(0,dotposition));
//ext = file.substring(dotposition + 1, file.length());
}
}
The second part of the code takes this incoming complete filename, locates the position of the "dot", and returns the string of characters leading up to but not including the "dot" itself. If one needed the extension, one could define a String called "ext" in this example, and remove the "//" from the code to pass that value back.