• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Java/Android Syntax

__Jon__

Newbie
Aug 16, 2010
15
0
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:
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:
ImageButton b1 = new ImageButton;

Any help would be great.
 
Actually, neither of those is correct.

If you were creating a new button all together, you would use:
Code:
ImageButton b1 = new ImageButton();

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:
<ImageButton
          android:id="@+id/MyImageButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/button_pic" />

Then, to set your button up, first, in you would define a class variable like so:
Code:
ImageButton b1;

and then in the onCreate() method of your activity, you would put:
Code:
b1 = (ImageButton)findViewById(R.id.MyImageButton);
 
Upvote 0
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:
<TextView android:id="@+id/firsttext2"  
android:text="TITLE OF PROJECT"            
android:layout_width="wrap_content"              
android:layout_height="wrap_content"             
 />

and in the java file I wrote:
Code:
TextView title = new TextView(this);
        title.setText("R.layout.firsttext2");
        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:
<color name="Bckgrd">#00ff00</color>
 
<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:
title.setTextColor(#ffd700);

But that came up with an error.
 
Upvote 0
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:
<TextView android:id="@+id/firsttext2"  
android:text="TITLE OF PROJECT"            
android:layout_width="wrap_content"              
android:layout_height="wrap_content"             
 />

and in the java file I wrote:
Code:
TextView title = new TextView(this);
        title.setText("R.layout.firsttext2");
        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:
<color name="Bckgrd">#00ff00</color>
 
<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:
title.setTextColor(#ffd700);

But that came up with an error.

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:
title.setTextColor(0xffd700);

Notice the '0x' preceeding the hexadecimal number. That is proper hex notation for Java.

HTH.
 
Upvote 0
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!

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

Another thing you can do in the setText call is this:
Code:
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones