[source code] Android Development Tutorial - 16

+14 Bucky Roberts · December 22, 2014

package com.thenewboston.allison;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.Button;
import android.graphics.Color;
import android.widget.EditText;
import android.content.res.Resources;
import android.util.TypedValue;

public class MainActivity extends ActionBarActivity {

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

        //Layout
        RelativeLayout buckysLayout = new RelativeLayout(this);
        buckysLayout.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());
        usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
        usernameDetails.setMargins(0,0,0,50);

        buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
        buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);

        Resources r = getResources();
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,200,
                r.getDisplayMetrics()
                );

        username.setWidth(px);

        //Add widget to layout(button is now a child of layout)
        buckysLayout.addView(redButton, buttonDetails);
        buckysLayout.addView(username, usernameDetails);

        //Set this activities content/display to this view
        setContentView(buckysLayout);

    }


    @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);
    }
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Tito Mafe · August 10, 2015
Hello fellow belch lovers!!

so i just finished lessons 12-16 and i have some questions i pray someone could help me with.



}}}}}}}



package com.example.daphne.tutorial_11;


import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.RelativeLayout;

import android.widget.Button;

import android.widget.EditText;

import android.graphics.Color;

import android.widget.TextView;

import android.content.res.Resources;

import android.util.TypedValue;






public class DesigningUserInterface1 extends ActionBarActivity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);


//layout

RelativeLayout voidLayout = new RelativeLayout(this);

voidLayout.setBackgroundColor(Color.WHITE);




//Signin Text

TextView signin = new TextView(this);

signin.setText("Sign In");




//Login Button

Button button1 = new Button(this);

button1.setText("Log In");

button1.setBackgroundColor(Color.GREEN);


//Name Cancel Button

Button button2 = new Button(this);

button2.setText("x");


//Password Cancel Button

Button button3 = new Button(this);

button3.setText("x");


//User Name

EditText username = new EditText(this);


//Password

EditText password = new EditText(this);



button1.setId(1);

button2.setId(2);

button3.setId(3);

username.setId(4);

password.setId(5);

signin.setId(6);

voidLayout.setId(7);


//login button rule and position

RelativeLayout.LayoutParams button1Details = new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT


);


button1Details.addRule(RelativeLayout.CENTER_HORIZONTAL);

button1Details.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, voidLayout.getId());

button1Details.setMargins(0,0,0,70);


//name cancel button position

RelativeLayout.LayoutParams button2Details = new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT


);


button2Details.addRule(RelativeLayout.RIGHT_OF, username.getId());

button2Details.addRule(RelativeLayout.ABOVE, password.getId());

button2Details.setMargins(5, 0, 0, 0);


//password cancel button rules and position

RelativeLayout.LayoutParams button3Details = new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT


);


button3Details.addRule(RelativeLayout.RIGHT_OF, password.getId());

button3Details.addRule(RelativeLayout.ABOVE, button1.getId());

button3Details.setMargins(5,0,0,100);


//username rules and position

RelativeLayout.LayoutParams usernameDetails = new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT


);


usernameDetails.addRule(RelativeLayout.ABOVE, password.getId());

usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);

usernameDetails.setMargins(0,0,0,10);


//password rules and position

RelativeLayout.LayoutParams passwordDetails = new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT


);


passwordDetails.addRule(RelativeLayout.ABOVE, button1.getId());

passwordDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);

passwordDetails.setMargins(0, 0, 0, 100);


Resources r = getResources();

int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,200,

r.getDisplayMetrics()

);


password.setWidth(px);

username.setWidth(px);



//Final Look Of Layout

voidLayout.addView(signin);

voidLayout.addView(button1,button1Details);

voidLayout.addView(button2, button2Details);

voidLayout.addView(button3, button3Details);

voidLayout.addView(username, usernameDetails);

voidLayout.addView(password, passwordDetails);



//Use Layout For Activity.

setContentView(voidLayout);


}


@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_designing_user_interface1, 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);

}

}

}}}}}}





so that there is my code, could someone please take the time to run it and help me answer three questions.

1. how do i change the color and size of the sign in text at the top left corner?
2. how do i reduce the size of the "x" buttons beside the EditText fields? (i want to make them really small)
3. How or Where can i get more colors like the full list of Color codes (if something like that exists)?


thank you for your help.
0 Yellow flash · December 26, 2015
in the statment    "Resources r = getResources();"
"Resources  " is a class right? and "r" is an object. so we can assign an object with other methods rather than new keyword and constructors ?
  • 1

Java / Android Development

103,780 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator