Robotium: how to use clickOnButton(<name>) for custom buttons - android

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 )

Related

Why when I use setClickable(true) it's not show the action of click

Actually, I use a ListView and when I use setClickable(false) I have the animation as if I clicked on a button you see? The animation that shows you click. Which is not normal, I think, basic.
And when I use setClickable(true) I no longer have the animation, as well as if I use
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
And i would like to use the OnClickListener but I think it would be better for the user to see that he can click, so to have the animation when you click.
So, I'd like to see when the user clicks on an item in the list, it does the action I want (I'll add that later) but let's imagine a Toast but it displays the effect as if you click on a button. The effect i got if i use setClickable(false) (the default setting).
That's the ripple effect !
In the row's layout of the ListView just add:
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
This will add the Ripple effect. If you want to show it on top of the other views, use the forground attribute:
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
add this foreground:?attr/selectableItemBackground to your view attribute, it should work

how to use Imageview as Clickable?

How can I use imageview as a button(clickable) in my project? As of now i am using button object but i want to display an image and not a button.
Thanks in advance
android
ImageView do listen to click events i.e. OnClickListener. So if you plan to use them as clickable you do not need any additional set up for it.
Simply create an ImageView,
<ImageView
android:id="#+id/imgClickable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your image"
/>
Then get its reference in your Java code,
ImageView clikableImage = (ImageView) findViewById(R.id.imgClickable);
clikableImage .setOnClickListener(this); //This make it clickable
And finally handle its onClick event by overriding
#override
public void onClick(View v){
//Your action
}
You can use android:clickable="true" attribute. And then you can use onClickListener to implement a listener.
But if you want to use an image instead of button you should consider ImageButton.
http://developer.android.com/reference/android/widget/ImageButton.html
It's pretty simple. Either in your layout when you define the ImageView you can add android:onClick="someFunction" and which calls someFunction whenever you click the image (make sure you define it in your code) or you can programmatically just add an onClickListener to the ImageView.
You can include the ImageView wherever you want on your layout and implement the OnClickListener interface to listen for click events. It would be something like:
ImageView image = (ImageView) findViewById(R.id.your_id);
imager.setOnClickListener(new View.OnClickListener() {
// Do something
});
Wll how about using ImageButton.
Uoc can find it when you open a layout xml file, and at the Grapichal layout - You would see ImageButton unter the Images&Media options menu.

Why declare buttons in onCreate() in MainActivity.java?

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

How to manage UI in java code?

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

How to dynamically add edit text boxes when button is clicked?

How would I implement a button such that a new TextBox is added dynamically on each click?
If you only, and always want to add two Edit Text widgets to your activity when the button is pressed you can do something like this (pseudo code). This assumes that you never want to have more than two edit text components beside your button.
<LinearLayout orientation="horizontal">
<Button >
<EditText id="#+id/et1" visibiltiy="gone" />
<EditText id="#+id/ed2" visibiltiy="gone" />
</LinearLayout>
in your button's onClick listener you can change the components visibility to visible by calling
findViewbyId(R.id.et1).setVisibility(Visible)
findViewbyId(R.id.et2).setVisibility(Visible)
This is my earlier post.
You need to use EditText instead of TextView.
Hope this helps for you.
You should have something like this:
Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText t = new EditText(myContext);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
root.addView(t);
}
});
root: is the root layout where you want to add the EditText.
myContext: could be the activity, etc, etc.
Hope this helps!!

Categories

Resources