I'm a noob in writing Android app.
The below 2 examples about declaring buttons are all from the Android developer site. (So both of them should be correct and working.)
Example 1: from http://developer.android.com/training/basics/firstapp/building-ui.html
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
Example 2: from http://developer.android.com/guide/topics/ui/declaring-layout.html#attributes
<--! (In xml file) Define a view/widget in the layout file and assign it a unique ID: -->
<Button android:id="#+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/my_button_text"/>
//(In java file) Then create an instance of the view object and capture it from the layout (typically in the onCreate() method):
Button myButton = (Button) findViewById(R.id.my_button);
1) So when would I want to assign "Android:id" for my button?
2) What would happen if I assigned "Android:id" for my button in the xml file but I did not declare the button in the "onCreate()" in "MainActivity.java"?
Android:id is just an identifier for your element, in your case its Button. It will not do anything if you didnt use it in the onCreate method. The Id will be useful when you create any listener for the Button. ie., telling your what to do when it is clicked.
You will use something like this.
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Let's go over a few things really quick.
android:id is exactly what it sounds like, its an identification that is used to specify certain Views. Let's say for example you have two buttons on a single layout and you want the buttons to do certain things or maybe you just want to change the text of a button. You would need to initialize the buttons in your Activity onCreate method to do things with them through code. The id is used to differentiate between the views. In our two button example you might give one button the id of buttonOne and the other buttonTwo. This way Android knows which button you are talking about when you refer to them in code. If you assign an id to a button in XML and don't refer to it in code than the button simply does nothing. If you need more info on initializing Views than check out this post on my website. I'm fairly new to Android too so I can explain things to you in a way that you can understand =]
http://www.androidianlabs.com/android-basics-lesson-one.html
Related
I would like to change the background of all the Buttons in a View.
android:background="#drawable/button_red"
And i would like to do this in an OnClick() event.
android:onClick="ChangeCouleur"
I would like to do this in a foreach loop, but i am not sure how to do this.
For example:
for( b in ... )
if (b.getid()!=idofthebutton)
b.setbackgroud(button_red)
Thanks for any help!
Put all your Buttons in an Array of Buttons, then cycle to it an change the background.
Button button1 = (Button)this.findViewById(...);
Button button2 = (Button)this.findViewById(...);
Button button3 = (Button)this.findViewById(...);
Button[] buttons={button1, button2, button3};
for (Button currentButton : buttons) {
currentButton.setBackgroundResource(R.drawable.my_new_background);
}
One thing you could do is subclass Button, then make all the buttons in you app instances of your new class. That way, if you decide that you want to adjust the color or change something else, you only have to do it once, and it will change all the buttons in your app. Here's a question that should give you some pointers on how to do that.
For changing the background of the clicked button
public void changeColor(View v) {
v.setBackground(btn_red);
}
You will have to get a reference to all the Buttons. Add them to an ArrayList or something similar. In your ChangeCouleur method use a loop to iterate through all the buttons changing the color of each one.
Create .XML file in your “res/drawable/” and use selector attribute in it.And use different images for the button.Please refer this link.
http://www.mkyong.com/android/android-imagebutton-selector-example/
This will clear your idea. :)
Once I set the contentView to a layout, how can I manage the UI via java? Also, if I start a new activity, do I reset the contentView?
ContentView will only be in one activity.
Assuming you are in something that extends Activity all you have to do, after setting content view, is access your views. Say you have a button in your layout:
Button homeButton = (Button) findViewById(R.id.homeButton);
homeButton.setText(); homeButton.setOnClickListener() etc
If you're in a dialog, or need to access certain views in specific circumstances, or you're in an intent and need to access stuff in your main activity's layout:
Button secondButton = (Button) getActivity().findViewById(R.id.secondButton);
//other methods on the button
EDITS--
Well images, you just add an ImageView into the layout:
<ImageView android:id="#+id/logoImageView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/logo" android:contentDescription="#string/app_name"
android:layout_gravity="center_horizontal"/>
You don't need to do anything with code specifically, it should just show up.
Buttons and other views can also be added via XML. You can add things programmatically as well, though it's not as common.
If I remember correctly, the constructor to create a button in Java that's not in your XML file, it would be Button aNewButton = new Button(getApplicationContext());
aNewButton.setText("whatever");
want to make an Android app that starts with a main layout and when you push a button (called stateButton) that is in this layout the layout changes to a main2 layout containing another button (called boton2), and when you push this one you get back to the first main.
I want to do this in the same activity without creating or starting another one.
Here I show you part of the code:
public class NuevoshActivity extends Activity
implements SensorEventListener, OnClickListener {
private Button stateButton;
private Button boton2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.stateButton = (Button) this.findViewById(R.id.boton);
this.boton2 = (Button) this.findViewById(R.id.boton2);
stateButton.setOnClickListener(this);
boton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==stateButton) {
setContentView(R.layout.main2);
}
else if(v==boton2) {
setContentView(R.layout.main);
}
}
}
The mains only have some images, text views and the buttons.
But I've some troubles. Can't it just be as simple as that or what am I missing or what is wrong?
When you use findViewById, you are actually trying to find a view inside the layout you specified by the setContentView. So using setContentView again and again might bring problems when you are trying to check for buttons.
Instead of using a setContentView, I would add the 2 layouts for the screen as child's for a view-flipper which only shows one child at a time. And you can specify the index of which child to show. The benefit of using a view flipper is that you can easily specify a 'in' and 'out' animation for the view if you need an animation when you switch between views. This is a lot cleaner method then recalling setContentView again and again.
The FrameLayout handles this wonderfully... Use this with the <include... contstruct to load multiple other layouts, then you can switch back and forth between them by using setvisibility(View.VISIBLE); and setVisibility(View.INVISIBLE); on the individual layouts.
For example:
Main XML including two other layouts:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="#+id/frameLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<include android:id="#+id/buildinvoice_step1_layout" layout="#layout/buildinvoice_step1" android:layout_width="fill_parent" android:layout_height="fill_parent"></include>
<include android:id="#+id/buildinvoice_step2_layout" android:layout_width="fill_parent" layout="#layout/buildinvoice_step2" android:layout_height="fill_parent"></include>
</FrameLayout>
Code to switch between layouts:
findViewById(R.id.buildinvoice_step1_layout).setVisibility(View.VISIBLE);
findViewById(R.id.buildinvoice_step2_layout).setVisibility(View.INVISIBLE);
You will also need to set the visibility of the individual layouts when the activity starts (or in XML) otherwise you will see them both - one on top of the other.
Your boton2 button will be NULL because the definition of the button is in main2.xml.
The only views you will be able to find are the views which are defined in main.xml.
Thanks!!! All the info was usefull to understand a lot of things and as C0deAttack commented I've got troubles with the button on the main2. What I've done is to set View.VISIBLE and View.GONE to the TextViews and Buttons that I wanted in each layout. Thank you very much.
I am new to Android robotium. I am having custom widgets(MyButton, MyTextView, MyCheckBox etc..,) which got inherited from native android widgets. How can i add a click event for my custom controls in a robotium script?
I tried using Solo.clickOnButton("Test Button") where the "Test Button" is an instance of MyButton, but i am not getting click event for the Button. Any suggestions would be really helpful.
Thanks,
-Ron..
I suposse you create the MyButton using extend Button etc etc
Well to asign click action you should use the normal form. For expample
main.xml:
<com.Ron.MyButton
android:id="#+id/custom_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
In your code you can acces to that button
Button myButton = (Button)findViewById(R.id.custom_button);
And then asign onClick action like you do with other normal button:
myButton.setOnclickListener(new onclickListener ....
Other method to asing onClickAction to all views is use int the xml :
<com.Ron.MyButton
android:id="#+id/custom_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="nameOfMethod"/>
And then in your code:
public void nameOfMethod (View v){
//code when click the view
}
(This works with all view, linearlayout, imageviews, bustom button .... all )
Hey guys please help me out I am new to android application development
Here is the scenerio: This is my layout declaring xml file:
<LinearLayout xxx
<Textview aaa>
</TextView>
</LinearLayout>
//The below LinearLayout I need to display when it meets some condition in java class
i.e if(true) then display the following layout else dont. I can check this condition only after user provides some input.
<LinearLayout xxx
<Textview aaa>
To be displayed after the condition is checked
</TextView>
</LinearLayout>
//following layout is displayed with the first one.
<LinearLayout xxx
<Textview aaa>
</TextView>
</LinearLayout>
Any idea how to do it?
Take a few moments to read the android dev guide. It is worth the time: http://developer.android.com/guide/topics/ui/index.html
Basically, you want to use IDs to refer to the xml layout:
android:id="#+id/myxmlid"
and in your java file:
LinearLayout ll = findViewById(R.id.myxmlid);
if (yourCondition)
mainLayout.add(ll);
I'm assuming that you want to add a widgets to the current layout, rather than just change the text of the current TextView.
Also, this assumes that you want to add more than just a new TextView. If you only need that, you don't need to wrap it in a LinearLayout, which is used to add rows or columns of widgets.
You don't replace your entire layout programmatically just to change the text in one TextView. The way this kind of thing is handled in android, is to include a field in your Activity class for your textview, then instantiate it in your onCreate() method with findViewById() after you've called setContentView() to load the layout so that you can access that TextView's fields and methods.
First, you TextView needs an id in the layout xml.
<TextView android:id="#+id/sometext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Then in your Activity...
TextView mTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.sometext);
}
Somewhere else in the program...
public void myMethod(){
mTextView.setText("Text says this now");
}
Hopefully that gets the idea across. Good luck!
Thank Aleadam for suggesting me to read the link. Follwoing was my approach to get the output.
What I did was I assigned my LinearLayout Visibility to "GONE" (android:Visibility="GONE") when declarning the XML, and in the program after the condition is met, changed the visibility to "VISIBLE". (by using layout.setVisibility(View.VISIBLE))