Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: need help understanding code

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help understanding code

    So I am studying how to build android apps by myself and I am stuck trying to understand some of the life of codes for this particular code:

    package com.ebookfrenzy.javalayout;
     
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.RelativeLayout;
     
    public class JavaLayoutActivity extends AppCompatActivity
    {
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            Button myButton = new Button(this);
            RelativeLayout myLayout = new RelativeLayout(this);
            myLayout.addView(myButton);
            setContentView(myLayout);
        }
    }

    This is to create a simple button. However, I do not under what "this" in:
    Button myButton = new Button(this);
    RelativeLayout myLayout = new RelativeLayout(this);

    refers to, and the book does not say.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help understanding code

    under what "this" in
    In standard java the this variable holds a reference to the the class it is in. For the posted code, this would refer to the instance of the JavaLayoutActivity class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help understanding code

    Ah, I see now. Thanks

    --- Update ---

    I have one more question (I don't want to create another thread since it is basically more on understanding the code)

    This is the same code, but added more to create a background of blue and a yellow button that says "PRESS ME".

    package com.ebookfrenzy.javalayout;
     
    import android.annotation.SuppressLint;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    import android.graphics.Color;
    import android.widget.EditText;
    import android.content.res.Resources;
    import android.util.TypedValue;
     
    public class JavaLayoutActivity extends AppCompatActivity
    {
        @SuppressLint("ResourceType")
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
     
            //sets button
            Button myButton = new Button(this);
            myButton.setText("Press Me");
            myButton.setBackgroundColor(Color.YELLOW);
     
            //sets background color
            RelativeLayout myLayout = new RelativeLayout(this);
            myLayout.setBackgroundColor(Color.BLUE);
     
            EditText myEditText = new EditText(this);
     
            myButton.setId(1);
            myEditText.setId(2);
     
            //sets location of the button relative to the screen
            RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
     
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
            );
     
            RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(
     
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
            );
     
            textParams.addRule(RelativeLayout.ABOVE, myButton.getId());
     
            textParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            textParams.setMargins(0, 0, 0, 80);
     
            buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
     
            myLayout.addView(myButton, buttonParams);
            myLayout.addView(myEditText, textParams);
            setContentView(myLayout);
        }
    }

    My question is, for:
    myButton.setId(1);
    myEditText.setId(2);
    there was an error, and automatically is created something called: @SuppressLint("ResourceType")
    What does this mean? and why was there an error for the setId methods?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help understanding code

    Have you read the API doc for the setId method? What is it supposed to do? Why is it in the code?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Difficulty understanding code with for loop?
    By chakana101 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 25th, 2014, 11:35 PM
  2. Understanding methods in my example code
    By bodylojohn in forum Java Theory & Questions
    Replies: 7
    Last Post: November 19th, 2013, 04:38 AM
  3. Need help understanding this code!
    By alex067 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 23rd, 2012, 01:14 AM
  4. NEEDING HELP UNDERSTANDING A CODE FROM THREE CLASSES!!!
    By BlackShadow in forum Java Theory & Questions
    Replies: 1
    Last Post: April 6th, 2012, 10:11 PM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM