[source code] Android Development Tutorial - 15

+11 Bucky Roberts · December 20, 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;

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

        //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
+1 Marc Ferrari · January 12, 2015
Hello all, I was wondering if anyone has any idea what I've got wrong here.  I could just paste in Bucky's code but I'd rather figure out where my mistake is.  Aside from renaming the layout and such I can't find a difference but when I try to compile I get the following message.

Error:(47, 63) error: method getId in class View cannot be applied to given types;
required: no arguments
found: int
reason: actual and formal argument lists differ in length
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED


The above error is pointing to the lines that include the following: 
        redButton.setId(1);
        username.setId(2);

Here is the entire source

http://pastebin.com/x2MVyK4v

Any help would be appreciated.
+1 Neil Cannon · January 12, 2015
Just check upper/lower case letters. I have followed bucky and it works.
+2 Neil Cannon · January 12, 2015
usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId(1));    WRONG!!!

usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId());      RIGHT!!!

Delete number 1 from line 43.
+1 Marc Ferrari · January 13, 2015
Thank you sir.  Hanging my head a little.  Although I must admit I'm a tad confused at why I need to create and ID and call and ID all the while referencing the same thing the ID represents?
+1 Neil Cannon · January 13, 2015
Hope it works now, I haven't looked at every line. I print out every code line for java.
  • 1

Java / Android Development

103,534 followers
About

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

Links
Moderators
Bucky Roberts Administrator