1. Are you ready for the Galaxy S20? Here is everything we know so far!

Java/Android Syntax

Discussion in 'Android Development' started by __Jon__, Aug 16, 2010.

  1. __Jon__

    __Jon__ Newbie
    Thread Starter

    Hi. I'm new to Android. I started writing apps last week. I have a little background in VB and Python with some Java a long time ago, but I'm getting confused with some Java syntax. I don't remember seeing this before (I have used swing/AWT to make applets, a long time ago so my memory is fuzzy), and my old Java refrence book is no help.
    the syntax is:
    Code (Text):
    1. ImageButton b1 = (ImageButton)
    I don't understand this. The right hand side specifically. I would have though the way to construct a new button (or any such object) is:

    Code (Text):
    1. ImageButton b1 = new ImageButton;
    Any help would be great.
     


  2. jonbonazza

    jonbonazza Android Expert

    Actually, neither of those is correct.

    If you were creating a new button all together, you would use:
    Code (Text):
    1.  
    2. ImageButton b1 = new ImageButton();
    3.  
    But with android, we have the luxury of using XML to create the layouts using pre-defined XML tags, so in your layout file (i.e. main.xml), you would have something like this:
    Code (Text):
    1.  
    2. <ImageButton
    3.           android:id="@+id/MyImageButton"
    4.           android:layout_width="wrap_content"
    5.           android:layout_height="wrap_content"
    6.           android:src="@drawable/button_pic" />
    7.  
    Then, to set your button up, first, in you would define a class variable like so:
    Code (Text):
    1.  
    2. ImageButton b1;
    3.  
    and then in the onCreate() method of your activity, you would put:
    Code (Text):
    1.  
    2. b1 = (ImageButton)findViewById(R.id.MyImageButton);
    3.  
     
  3. __Jon__

    __Jon__ Newbie
    Thread Starter

    Thanks. Thats just the answer I was looking for. On a separate but similar problem, I'm trying to set text color and textview background color. I'm having a nightmare. The main problem is I don't know XML very well, or how it interacts with the java files.

    In the main.xml file I wrote
    Code (Text):
    1. <TextView android:id="@+id/firsttext2"  
    2. android:text="TITLE OF PROJECT"            
    3. android:layout_width="wrap_content"              
    4. android:layout_height="wrap_content"            
    5.  />
    and in the java file I wrote:
    Code (Text):
    1. TextView title = new TextView(this);
    2.         title.setText("R.layout.firsttext2");
    3.         title.setTextColor(R.color.Txtcol);
    Where Txtcol is a color I defined in the values directory. Actually its in the string.xml file with another color, which is probably not a good idea since it isn't a string.
    Code (Text):
    1. <color name="Bckgrd">#00ff00</color>
    2.  
    3. <color name="Txtcol">#ffd700</color>
    However, on emulation nothing appears.

    So my question is: How can I predefine colors using xml to make a textview string with a chosen color and chosen background color.

    Also I tried, in the java file, writing:
    Code (Text):
    1. title.setTextColor(#ffd700);
    But that came up with an error.
     
  4. Scottmc

    Scottmc Member

    First of all, just a little pedantry. The construct you were asking for in the beginning is called a cast operator. This is a special operator in Java (and exist in a similar fashion in c++, c# and most other strongly typed languages). What this operator does is try to coerce the object it's operating on to be the type you specify in the parenthesis. For example: Foo f = (Foo)bar;
    In this case, bar can be any type, even object, as long as it can be converted to a Foo (it extends Foo or implements an interface called Foo). This is an example of polymorphism, one of several important tenants of OOD.

    For your second question, setTextColor takes an int - take a look at the documentation here: TextView | Android Developers.

    You are passing in something it doesn't understand since the preceeding hash '#' is not valid for an integer. It is valid for XML markup, however, which is probably where you are getting confused. The proper way to call that function is like this:

    Code (Text):
    1. title.setTextColor(0xffd700);
    Notice the '0x' preceeding the hexadecimal number. That is proper hex notation for Java.

    HTH.
     
  5. __Jon__

    __Jon__ Newbie
    Thread Starter

    Scott, thanks. And actually, I could do with some pedantry. Actually I have no formal programming training, so your explanation was very useful.

    I'm gonna try and get my head around the XML now. Thanks!
     
  6. Scottmc

    Scottmc Member

    Np Jon - glad that helped. We are all beginners once :cool:

    Another thing you can do in the setText call is this:
    Code (Text):
    1. title.setTextColor(getResources().getColor(R.color.Txtcol));
    This will set the text color to the resource value you added above.

    Send PM if you need help with anything else.
     
Loading...
Similar Threads - Java Android Syntax
  1. Gleis
    Replies:
    0
    Views:
    59
  2. Nieve1
    Replies:
    0
    Views:
    360
  3. AKA001
    Replies:
    0
    Views:
    550
  4. Gleis
    Replies:
    4
    Views:
    833
  5. Gleis
    Replies:
    6
    Views:
    674
  6. anthr
    Replies:
    0
    Views:
    136
  7. Cinemato
    Replies:
    0
    Views:
    407
  8. Rubinia
    Replies:
    0
    Views:
    447
  9. Pawan Choudhary
    Replies:
    1
    Views:
    763
  10. criscap1985
    Replies:
    0
    Views:
    319

Share This Page

Loading...