Advertisement
Guest User

Android Development Tutorial - 15 - Error

a guest
Jan 12th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package com.bmsldev.thirdtest;
  2.  
  3. import android.content.Context;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.text.style.RelativeSizeSpan;
  7. import android.util.AttributeSet;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.widget.RelativeLayout;
  11. import android.widget.Button;
  12. import android.graphics.Color;
  13. import android.view.View;
  14. import android.widget.EditText;
  15.  
  16. public class MainActivity extends ActionBarActivity {
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.  
  22.         //Layout
  23.         RelativeLayout testLayout = new RelativeLayout(this);
  24.         testLayout.setBackgroundColor(Color.GREEN);
  25.  
  26.         //Button
  27.         Button redButton = new Button(this);
  28.         redButton.setText("Log In");
  29.         redButton.setBackgroundColor(Color.RED);
  30.  
  31.         //Username input
  32.         EditText username = new EditText(this);
  33.  
  34.         redButton.setId(1);
  35.         username.setId(2);
  36.  
  37.         RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(
  38.                 RelativeLayout.LayoutParams.WRAP_CONTENT,
  39.                 RelativeLayout.LayoutParams.WRAP_CONTENT
  40.         );
  41.         RelativeLayout.LayoutParams usernameDetails = new RelativeLayout.LayoutParams(
  42.                 RelativeLayout.LayoutParams.WRAP_CONTENT,
  43.                 RelativeLayout.LayoutParams.WRAP_CONTENT
  44.         );
  45.  
  46.         //Give rules to position widgets
  47.         usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId(1));
  48.         usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
  49.         usernameDetails.setMargins(0,0,0,50);
  50.  
  51.         buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
  52.         buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);
  53.  
  54.         //Add widget to layout (button is now a child of layout
  55.         testLayout.addView(redButton, buttonDetails);
  56.         testLayout.addView(username, usernameDetails);
  57.  
  58.         //Set this activity content/display to this view
  59.         setContentView(testLayout);
  60.         }
  61.  
  62.     @Override
  63.     public boolean onCreateOptionsMenu(Menu menu) {
  64.         // Inflate the menu; this adds items to the action bar if it is present.
  65.         getMenuInflater().inflate(R.menu.menu_main, menu);
  66.         return true;
  67.     }
  68.  
  69.     @Override
  70.     public boolean onOptionsItemSelected(MenuItem item) {
  71.         // Handle action bar item clicks here. The action bar will
  72.         // automatically handle clicks on the Home/Up button, so long
  73.         // as you specify a parent activity in AndroidManifest.xml.
  74.         int id = item.getItemId();
  75.  
  76.         //noinspection SimplifiableIfStatement
  77.         if (id == R.id.action_settings) {
  78.             return true;
  79.         }
  80.  
  81.         return super.onOptionsItemSelected(item);
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement