Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bmsldev.thirdtest;
- import android.content.Context;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.text.style.RelativeSizeSpan;
- import android.util.AttributeSet;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.RelativeLayout;
- import android.widget.Button;
- import android.graphics.Color;
- import android.view.View;
- import android.widget.EditText;
- public class MainActivity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //Layout
- RelativeLayout testLayout = new RelativeLayout(this);
- testLayout.setBackgroundColor(Color.GREEN);
- //Button
- Button redButton = new Button(this);
- redButton.setText("Log In");
- redButton.setBackgroundColor(Color.RED);
- //Username input
- EditText username = new EditText(this);
- redButton.setId(1);
- username.setId(2);
- RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,
- RelativeLayout.LayoutParams.WRAP_CONTENT
- );
- RelativeLayout.LayoutParams usernameDetails = new RelativeLayout.LayoutParams(
- RelativeLayout.LayoutParams.WRAP_CONTENT,
- RelativeLayout.LayoutParams.WRAP_CONTENT
- );
- //Give rules to position widgets
- usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId(1));
- usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
- usernameDetails.setMargins(0,0,0,50);
- buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
- buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);
- //Add widget to layout (button is now a child of layout
- testLayout.addView(redButton, buttonDetails);
- testLayout.addView(username, usernameDetails);
- //Set this activity content/display to this view
- setContentView(testLayout);
- }
- @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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement