Android Open Source - 2014-Droid-code Main Activity






From Project

Back to project page 2014-Droid-code.

License

The source code is released under:

GNU General Public License

If you think the Android project 2014-Droid-code listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ca.campbell.layoutprogrammatically;
/*from w w  w. ja v a 2 s.co m*/
/*
 * This app is the same app created in week2-rw-views, it has exactly
 * the same functionality.  
 * 
 * See: https://waldo2.dawsoncollege.qc.ca/svn/Samples518/week2-using-views/
 * 
 * The difference is that the layout & associated views are created only programmatically.
 * None of the UI is created through xml.
 */
import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
  private static final String TAG = "RWPROGVIEWS";
  private static final String SECRET = (String) "guacamole";
  private LinearLayout.LayoutParams  buttonParams, params;
  private EditText et;
  private CharSequence str;
  TextView tvresulthdr, tvresult;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    createLayout();
  }

  public void createLayout() {

    // set up the Linear Layout container)
    LinearLayout ll = new LinearLayout(this);
    // specifying vertical orientation
    ll.setOrientation(LinearLayout.VERTICAL);
    // creating LayoutParams
    // public LinearLayout.LayoutParams (int width, int height, float
    // weight)
    params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
        LayoutParams.MATCH_PARENT);

    // set LinearLayout as a root element of the screen
    ll.setLayoutParams(params);

    TextView tv = new TextView(this);
    tv.setText(R.string.message);
    // default width wrap, height wrap so no params
    ll.addView(tv);

    // width MATCH_PARENT, set before, here for illustration
    params.width = LayoutParams.MATCH_PARENT;
    params.height = LayoutParams.WRAP_CONTENT;
    et = new EditText(this);
    et.setHint(R.string.hint);
    et.setLayoutParams(params);
    ll.addView(et);

    LinearLayout ll2 = new LinearLayout(this);
    // same params as EditText
    ll2.setLayoutParams(params);
    ll2.setOrientation(LinearLayout.HORIZONTAL);

    buttonParams = new LinearLayout.LayoutParams(0,
        LayoutParams.WRAP_CONTENT, (float) 2);
    Button bt1 = new Button(this);
    bt1.setLayoutParams(buttonParams);
    bt1.setText(R.string.btsubmit);
    ll2.addView(bt1);

    Button bt2 = new Button(this);
    buttonParams.weight = (float) 1;
    bt2.setLayoutParams(buttonParams);
    bt2.setText(R.string.btclear);
    ll2.addView(bt2);

    ll.addView(ll2);

    tvresulthdr = new TextView(this);
    // params same
    tvresulthdr.setVisibility(TextView.INVISIBLE);
    tvresulthdr.setLayoutParams(params);
    ll.addView(tvresulthdr);

    tvresult = new TextView(this);
    // default width wrap, height wrap
    tvresult.setVisibility(TextView.INVISIBLE);
    ll.addView(tvresult);

    // add listeners for on click events
    // button 1
    bt1.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        showData(view);
      }
    });
    // button 2
    bt2.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        clearData(view);
      }
    });
    // Use the layout objects 
    setContentView(ll);  

  }

  // showData() is the method called by the View.OnClickListener()
  public void showData(View view) {
    // get the references to the view widgets
    // no reference to the header TextView, I don't manipulate it
    // et = (EditText) findViewById(R.id.input);
    // get the data input
    str = et.getText().toString();

    // tvresulthdr = (TextView) findViewById(R.id.resultheader);
    tvresulthdr.setVisibility(TextView.VISIBLE);

    // tvresult = (TextView) findViewById(R.id.result);
    tvresult.setVisibility(TextView.VISIBLE);
    tvresult.setText(str);

    // need minimum api 9 for str.isEmpty() :(
    if (((String) str).isEmpty()) {
      tvresulthdr.setText(R.string.emptymsg);
      Log.w(TAG, "showData(): no data");
    } else {
      if (str.equals(SECRET)) {
        tvresulthdr.setText(R.string.successmsg);
        Log.w(TAG, "showData(): guessed correctly");
      } else {
        tvresulthdr.setText(R.string.tryagainmsg);
        Log.w(TAG, "showData(): bad guess");
      }
    }
  }

  public void clearData(View view) {
    if (tvresulthdr != null) {
      tvresulthdr.setVisibility(TextView.INVISIBLE);
      et.setText("");
      Log.w(TAG, "clearData(): reset fields");
    }
    Log.w(TAG, "clearData(): fields not set yet");
  }

}




Java Source Code List

ca.campbell.httpexample.HttpExample.java
ca.campbell.httpexamplepost.HttpsExamplePOST.java
ca.campbell.layoutprogrammatically.MainActivity.java
ca.campbell.networkcheckstatus.MainActivity.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity3.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.simplegridview.MainActivity.java
ca.campbell.week2_rw_views.Activity2.java
ca.campbell.week2_rw_views.MainActivity.java
com.androidbook.simpleasync.ChoiceActivity.java
com.androidbook.simpleasync.SimpleAsyncActivity.java
com.androidbook.simpleasync.SimpleNoBGThread.java
com.androidbook.simpleasync.SimpleThreadActivity.java
com.cookbook.internet.search.GoogleSearch.java
com.introtoandroid.simplefragments.FieldNoteListFragment.java
com.introtoandroid.simplefragments.FieldNoteViewActivity.java
com.introtoandroid.simplefragments.FieldNoteWebViewFragment.java
com.introtoandroid.simplefragments.SimpleFragmentsActivity.java
com.introtoandroid.simplelayout.FrameLayoutActivity.java
com.introtoandroid.simplelayout.GridLayoutActivity.java
com.introtoandroid.simplelayout.LinearLayoutActivity.java
com.introtoandroid.simplelayout.MenuActivity.java
com.introtoandroid.simplelayout.MultipleLayoutActivity.java
com.introtoandroid.simplelayout.RelativeLayoutActivity.java
com.introtoandroid.simplelayout.SimpleLayoutActivity.java
com.introtoandroid.simplelayout.TableLayoutActivity.java
com.introtoandroid.viewsamples.ButtonsActivity.java
com.introtoandroid.viewsamples.ContainersActivity.java
com.introtoandroid.viewsamples.EventsActivity.java
com.introtoandroid.viewsamples.FormsActivity.java
com.introtoandroid.viewsamples.IndicatorsActivity.java
com.introtoandroid.viewsamples.MenuActivity.java
com.introtoandroid.viewsamples.PickersActivity.java
com.introtoandroid.viewsamples.TextDisplayActivity.java
com.introtoandroid.viewsamples.TextInputActivity.java
com.introtoandroid.viewsamples.ViewSampleActivity.java
cs518.sample.activityLifecycle.Activity2.java
cs518.sample.activityLifecycle.MyActivityLifeCycleActivity.java
cs518.sample.database.AddStudent.java
cs518.sample.database.DBHelper.java
cs518.sample.database.DatabaseActivity.java
cs518.sample.database.Thirty.java
cs518.sample.dbcursoradapter.AddStudent.java
cs518.sample.dbcursoradapter.DBHelper.java
cs518.sample.dbcursoradapter.DatabaseActivity.java
cs518.sample.dbcursoradapter.Thirty.java
cs518.sample.localisation.MainActivity.java
cs518.sample.multiactivity.Activity1.java
cs518.sample.multiactivity.Activity2.java
cs518.sample.multiactivity.Activity3.java
cs518.sample.multiactivity.Activity4.java
cs518.sample.multiactivity.Activity5.java
cs518.sample.multiactivity.Activity6.java
cs518.sample.multiactivity.Constants.java
cs518.sample.usecalendarcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovidercursorloader2.MainActivity.java
cs518.sample.usemediastorecontentprovider.MainActivity.java
cs518.samples.imageswap.MainActivity.java
cs518.samples.sharedpreferences.MainActivity.java
cs534.sample.dbAsyncTask.AddStudent.java
cs534.sample.dbAsyncTask.DBHelper.java
cs534.sample.dbAsyncTask.DatabaseActivity.java
cs534.sample.dbAsyncTask.Thirty.java
cs534.sample.implicitintents.MainActivity.java
cs534.sample.multithread.MultiThread.java
cs534.sample.simplelistview.SimpleLV.java
cs534.samples.simplestlv.MainActivity.java