Android - define groups in R.id - java

Is there any possibility to group the R.id?
android:id="#+id/button1"
R.id.button1
I want something like this:
android:id="#+id/main/button1"
R.id.main.button1
But that does not work.

The only way I know to group like id's like that is to set all the tags the same. Then you get reference the id's with the same tag.
Button myButton = (Button)findViewById(R.id.button1);
Button mySecondButton = (Button)findViewById(R.id.button2);
myButton.setTag("Tag1");
mySecondButton.setTag("Tag1");
This way you can still programatically reference your group.
In the R.java file, the id itself is a group, and there is no way to superclass it with something else.

As a matter of fact you can. This isn't documented, and IntelliJ does not play along nicely (Eclipse does), but you can do something like this...
layout.xml
android:id="#+id_group1/button1"
Activity.java
Button myButton = (Button)findViewById(R.id_group1.button1);

Related

Why I can't enforce EditTextPreference to take just numbers?

GOAL: Use EditTextPreference to take a number input from an user.
PROBLEM: I'm using support.v7.preference and, from what I've understood, this library bring problems to creates a custom DialogPreference (HowTo use support.v7.preference with AppCompat and potential drawbacks). Anyway I tried to create a NumberPickerPreference extending DialogPreference but I got ClassCastException.
Also I found another approach: programmatically set the input type of an EditTextPreference object like below:
EditTextPreference pref = (EditTextPreference) findPreference("pref_edit_text");
if (pref != null) {
pref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
}
No way, I always get a ClassCastException .
Last approach I tried : modify the xml
android:inputType="numberDecimal"
android:digits="0123456789"
or android:numeric="integer"
No way again, on the Dialog I get the full keyboard, so the user can write everything.
Is there a way to properly enforce EditTextReference to take just number input?
You can do it by adding android: inputType = "numberPassword" to your XML file
Then you can set your custom keyboard programmatically in your java like this:
yourEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
The next thing you could do is to create a custom class:
private class YourNumericKeyboardMethod extends PasswordTransformationMethod {
#Override
public CharSequence getTransformation(CharSequence source, View view) {
return source;
}
}
The last step is to implement it to your EditText in order to make it display only the numbers:
yourEditText.setTransformationMethod(new YourNumericKeyboardMethod());
After all this steps you should get a keyboard that has only numbers from 0 to 9 on it
Try one of these:
android:inputType="numberSigned"
android:inputType="phone"
EDIT:
android:inputType="numberDecimal|numberSigned"
android:digits="0123456789"
EDIT:
There´s this lib that is supposed to make it work, if you´re willing to try:
https://github.com/Gericop/Android-Support-Preference-V7-Fix
Reading their github page you can find examples of custom inputs and their example code that looks similar to what you are doing now:
"The sample app shows an example of setting (via XML) and querying (programmatically) the input type of the EditTextPreference:
<EditTextPreference
android:inputType="phone"
android:key="edit_text_test" />
EditTextPreference etPref = (EditTextPreference) findPreference("edit_text_test");
if (etPref != null) {
int inputType = etPref.getEditText().getInputType();
// do something with inputType
}
Try to set inputType as the number.
android:inputType="number"

Cant setId for a view programatically with AndroidStudio

I want to add view programatically to my parent view, my view is a RadioButton. I also want to set an Id for the view that I add in RadioGroup. But when I do this it gives me an error in: mRadioButton.setId(321);
Reports two types of problems:
Supplying the wrong type of resource identifier. For example, when calling Resources.getString(int id), you should be passing R.string.something, not R.drawable.something.
Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL.
I dont know why it gives me this error.
I found that one solution would be to create MyRadioButton class which extends RadioButton and there I could add a variable int MYID; and then add getters and setters for this view.
But this is a workaround, does anyone know the solution of this problem ?
Thanks!
EDIT:
Another workaround is to use setTag()
(I have used it for a horizontal view with ImageView's added dynamically and it helps me to detect which one was clicked)
If you are only supporting API 17 and higher,
you can call View.generateViewId
ImageButton mImageButton = new ImageButton(this);
mImageButton.setId(View.generateViewId());
Otherwise for apis lower than 17,
open your project's res/values/ folder
create an xml file called ids.xml
with the following content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="imageButtonId1" type="id" />
</resources>
then in your code,
ImageButton mImageButton = new ImageButton(this);
mImageButton.setId(R.id.imageButtonId1);

Android Development: Button- findViewById?

What is the intent behind this piece of code?
Button sButton = (Button) findViewById(R.id.button_1);
In your XML files, you create IDs for the widgets you put on the screen.
In that code, you are creating a button reference (sButton) to be the button corresponding to the ID of button_1
findViewById searches for a button based on its ID - the ID is located in the R.java file of your project: Project Folder > gen > package > R.java
The R.java file holds references to everything (or basically everything?) in your project. It's an essential part of it.
That is why the parameter of findViewById is R.id.button_1 because you are searching for the ID of button_1 in the R class (it's a static field).
You are then casting that ID of the button_1 to a button with the (Button) in front of the findViewById.
Extra note:
If you look at the R.java class, you have it declared as: public final class R { so that's where the R in R.id.button_1 comes from.
Then you have another inner class like this: public static final class id { so that's where the id comes from in R.id.button_1
Then, in the id inner class, you have this (amoung other things):
public static final int button_1=...; where the ... would be some code to represent the int value. That's where the button_1 comes from in R.id.button_1
So bascially, R.id.button_1 goes to the R class, then the id inner class, then accesses the actual int value of the id name.
it finds a view in the associated layout XML by it's id, and casts it to a Button. Was it what you were asking?
Its used to find a Button which you have created in XML and bring it into the Java code so you can work with it. If you didnt do this you would not be able to give the button anything to do.

Calling UIWebView method from Another Class

OK, I'm sure it's simple but I will try to explain everything I'm doing in detail so someone could show me my mistakes. Xcode, iOS SDK 6.1, I'm using StoryBoard.
-I have my MenuViewController (View Controller) with some buttons. In Identity inspector I have set my custom class. Every button has it's outlet in MenuViewController.h (using Ctrl drag). Every button is synthesized in MenuController.m and only thing I'm doing so far is setting background in -viewDidLoad Method in MenuViewController.m
-I have another WebViewController (View Controller with Navigation Bar, Bar Button and UIWebView). I have also created new class for that ViewController and set it in Identity inspector. WebView has it's outlet in WebViewController.h and it's synthesized in WebViewController.m
-I have made connection between those two ViewControllers also through Xcode. One button from my MenuViewController is opening WebViewController (I'v done that by Ctrl+Dragging that button to WebViewController and style is set to Modal). I'v done the same thing with button in NavigationBar of my WebViewController, made connection that is opening MenuViewController when clicked (something like Back button).
-Right now, that WebView is opening some webpage when -viewDidLoad method in WebViewController is called.
What I WOULD LIKE TO DO is to set buttonPressed actions in my MenuViewController and for example, if one button is pressed, I want to switch to WebViewController and open some specific URL. If another button is pressed I want also to open that same WebViewController and load some different URL. This should not be hard but I would just like someone to show me the right way for calling method of webView from another class. I'm new in Objective C and only language I know so far is Java, so I'm a bit confused with this .h .m stuff, not sure where to declare what properly. Also if someone could compare what would something here in Objective-C look in Java, that would be great for understanding.
Thanks folks!
I dont use storyboard and do some thing like this:
In your WebViewController .m file make a method some thing like this:
-(id)initWithURL:(NSString *)urlstring
{
self = [super init];
receivedURL = [NSURL URLWithString:urlstring];
return self;
}
and in .h file declare an object of NSURL
NSURL *receivedURL;
and just above the #end
-(id)initWithURL:(NSString *)urlstring;
and in your viewDidLoad Method of WebViewController do
[webview loadRequest:[NSURLRequest requestWithURL:receivedURL]];
Now in your MenuViewController when ever the button action is performed do something like this:
WebViewController *webViewObj = [[WebViewController alloc]initWithURL:yourURLString];
[self presentModalViewController:webViewObj animated:YES];

How can I navigate the view menu using SWTBot?

Is it possible to the view menu using SWTBot? An example of an view menu is the one of Problems view (see screenshot). For example how can I change the grouping to Type using SWTBot? I've tried:
for (final SWTBotViewMenu a : this.bot.viewById("org.eclipse.ui.views.ProblemView").menus()) {
System.out.println(a.getText());
}
this.bot.viewById("org.eclipse.ui.views.ProblemView").toolbarDropDownButton("View Menu").menuItem("Group By").menu("None").click();
The for loop doesn't give anything at all, and the second one gives an error, that the "View Menu" cannot be found. I have no idea how to navigate this menu ?
It's probably too late for the OP, but here goes:
For some reason, the straight-forward way to activate a view like "Problems" doesn't work. You can use this workaround:
this.bot.menu("Window").menu("Show View").menu("Problems").click();
SWTBotView problemsView = bot.activeView();
This will only help with the first part, though. You now have access to the toolbar buttons via:
List<SWTBotToolbarButton> toolbarButtons = problemsView.getToolbarButtons();
For the Problems view, this gives you access to the "Focus on active task" button, but the three buttons in the cornern, "view menu", "minimize" and "maximize" do not appear in this list. Unfortunately, I have no solution for this as of now.
[Edit]
You can bring up the view menu like that:
this.bot.menu("Window").menu("Navigation").menu("Show View Menu").click();
but I don't know how to select an item from it afterwards. Maybe someone else will know...
try this:
SWTBotView view = bot.viewByTitle("MyView");
view.show();
view.viewMenu().menu("MyContextOption").click();
The problem come from the fact that this menu is populated with dynamic entries. SWTBot doesn't handle this kind of entry.
See ViewMenuFinder.getMenuItem(). Different kind of IContributionItem are handled but in the situation of the Problems View, the items are of type DynamicMenuContributionItem.
I think you can try with:
theView.viewMenu().menu("Group By").menu("Type").click();
I am able to do the same thing with SWTBot 2.8.0 for Project Explorer view

Categories

Resources