Android Eclipse development Clickable..text pops up - android

I have the following code:
<TextView
android:text="Color Yellow"
android:textColor="#000000"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="True"
/>
The android:clickable="True" was added because I thought it needed to be there (please do correct me if I'm wrong). However, the answer I'm seeking right now is how do I go by making another box (filled with text) pop-up upon clicking the "yellow box".
I would be grateful if someone could provide me with ideas and/or hints regarding how to actually create this scenario.

The android:clickable element does what you think and what it name tells you. It allows you to receive click events for that view (TextView here) to act on these.
To create a popup, you have to assign something to that TextView that tells you when it actually gets clicked. That is a OnClickListener. You can do that either in code or partially in code and XML. I'll just focus on the code example, but for the record, the XML one is also pretty easy. It involves setting the android:onClick="myOnClick" attribute to a certain function name that you like ("myOnClick" here) and creating a function like public void myOnClick(View v) in your activity.
What you have to do in code is
Referencing the TextView that you have in your layout
Assign the OnClickListener
Write an action that will be executed once the click gets registered
First point: To reference your TextView you have to use findViewById
TextView myTextView = (TextView) findViewById(R.id.mytextviewid);
Note that you have to assign a ID to your TextView to identify it. You can set that id via the android:id attribute in your XML layout (e.g. android:id="#+id/mytextviewid").
Second point: Once you have the reference, use TextView.setOnClickListener() to register one.
This usually looks like this:
myTextView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Add an action here
}
});
Third point: All you have to do now is displaying your dialog/message insite the onClick() function. There is more than one way to display that, you may use a Toast or an AlertDialog. Check out the links, there are some examples for that.

Related

Accessing buttons in a GridLayout in Java?

I am making a hangman game, in the main Activity, there is a GridLayout containing the buttons for each letter.
I would like to set a Button array in java to include all of the buttons and access them (in order to use them in java methods, as my teacher instructed us). My question is, what would be the easiest way to identify the buttons in Java, and how can I add them to said Button array?
Thanks in advance. This is my first question here.
EDIT
The letters are not English letters and in this picture you can see the design of the GridLayout.
Notice that the language direction is RTL instead of LTR which means I used a reverse order of the columns.
So, in addition to my first question, is there any way to 'reverse' the numbers' order from LTR to RTL?
You don't need an array to identify the buttons. Use an array to identify just the buttons that was pressed.
If your button contains the letter as a text...:
<Button
android:id="#+id/alef_button"
...
android:text="א" />
<Button
android:id="#+id/bet_button"
...
android:text="ב" />
Then you can use the same method as a listener for all buttons:
public void onButtonClick(View view) {
Button pressedButton = (Button) view;
if("א".equals(pressedButton.getText())) {
putOnArray("alef");
} else if("ב".equals(pressedButton.getText())) {
putOnArray("bet");
} ...
}

Can I give an additional label for a TextView that's only announced by TalkBack, not shown visually?

Say I have a list of items, and one of the views in each item is a message, in a simple TextView, that is populated in code (dynamic data from a backend service).
<TextView
android:id="#+id/message"
style="#style/DefaultText"
/>
Visually it doesn't need a label; the list item layout is pretty clear. But listening to how TalkBack reads the screen, I think it would be beneficial to have a "label" or description for it. So that TalkBack would read something like:
Message: [the actual dynamic message content]
My question is: is it possible to add a label/description for a TextView that, firstly, does not replace the TextView content, but is read along with it, and secondly, only affects TalkBack (not the visual presentation)?
What I tried:
contentDescription for the TextView. Doesn't work: if set, the actual content is not announced, only the contentDescription. Hmm, maybe if I'd set this in code with the description prepended to the actual content... but is there no easier way?
Separate TextView with labelFor pointing to #+id/message. The problem is that it's also shown visually and screws up the design. If I make it not visible, one way or other, it seems TalkBack won't read it either.
Alright, I found two solutions! As mentioned in the question, I'd rather have avoided doing it in code, but it seems for optimal results that's the way to go.
Use android:hint in XML
The most simple way is to set android:hint="Message" for the TextView. TalkBack reads it after the actual text:
[the actual dynamic message content] Message
For my needs, with the message being optional, this has some drawbacks: the hint is announced even if the TextView has no actual content (I'd prefer to omit it then), and in that case it's also shown visually (not what I want). Also, I'd prefer TalkBack to read the hint before the actual text.
Set contentDescription programmatically
Not as simple, but not complicated either, and you have complete control over what TalkBack says.
In my case, preferring to omit the description if message is missing and having it read before the message content, I'd do something like this in the code that populates the list items:
textView.setText(message);
if (!TextUtils.isEmpty(message)) {
textView.setContentDescription(
context.getString(R.string.message_desc, message));
}
With resource:
<string name="message_desc">Message: %s</string>
You can use this namespace it not visually show on real screen
xmlns:tools="http://schemas.android.com/tools"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="your text"/>
In your XML file, set the importance to "no". You can also do this at run time using the View.setImportantForAccessibility() API.
android:importantForAccessibility="no"
Programmatically you can say:
view.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
Example click here
click here for more info

Questions related to android:onClick="selfDestruct" xml method

New to android and programming in general. This question might sound silly but I'd appreciate the answer. The question description and reasoning is at the beginning and the question is at the end of it all.
I want to apply a listener to a button in android. The way I understood from android.googlesource.com is that there is two way to do it:
applying an OnClickListener to the button in the activity.java
or
assign a method to my button in the xml layout using this
{#link android.R.attr#onClick android:onClick}
they gave the following xml layout example:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selfDestruct"
android:text="#string/self_destruct" />
plus the the code in activity.java
public void selfDestruct(View view) {
// Kabloey
}
android.googlesource.com
Questions:
According to this: {#link android.R.attr#onClick android:onClick} android.R.attr in the example are the following:
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
?
Does this:
android:text="#string/self_destruct"
android:onClick="selfDestruct"
mean that the button called self_destruct registered as a listener?
If I want to add more than one button listener in xml form how do I write it in the java document?
Thank you in advance
Really appreciate it.
Does this:
android:text="#string/self_destruct" android:onClick="selfDestruct"
mean that the button called self_destruct registered as a listener?
The button isn't "called" anything, it just has the text of the value for #string/self_destruct defined in the strings.xml file.
But, yes, the public void selfDestruct(View view) method is the method that will be called for the listener that is setup by the XML.
If I want to add more than one button listener in xml form how do I write it in the java document?
You can only set one click listener for a View.
Yes, layout_height, layout_width and text are attributes. You
can learn more about android attributes
here.
But keep in mind, that different views can use the same attributes
in different way.
It does not. The android:text attribute in this sample just refer to a string-resource called self_destruct to decide which text should be shown within the button. To distinguish views you can use android:id.
As already mentioned by other people, you are able to specify only one onClickListener using XML.

Change button android:background to different drawable

I am new to Android and Java but have managed to teach myself and find most answers to questions on stackoverflow without needing to ask questions. Until now....
Here goes, I have many colored buttons which, when clicked, change color to a range of different colors.
There are many buttons defined for example as:
<Button
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/orange_button"
android:gravity="center"
android:onClick="onClick" />
Could someone please advise me how to change the android:background using code to change the above example to yellow, for example, when the button is clicked.
In the code below clickedButton is the ID of the button for which I need to change the background.
public void onClick(View v) {
int id=v.getId();
String clickedButton = getResources().getResourceEntryName(id);
Change button to Yellow here??
// Temporary code below to check which button was pressed
// and convert its number to an integer to eventually access an array
final TextView tvGameTimer = (TextView) findViewById(R.id.tvGameTimer);
int buttonNumber = Integer.parseInt(clickedButton.substring(6,8));
tvGameTimer.setText("" + buttonNumber);
}
I am using custom button styles to define the button colors:
res/drawable/yellow_button.xml
res/drawable/blue_button.xml
res/drawable/red_button.xml
res/drawable/orange_button.xml
res/drawable/green_button.xml
For now I just need to work out how to change the button from Orange to Yellow. I can then add the logic to change the colors as and when the app requires.
Many thanks for any help.
I am supposing that the onClick method you post is of the same Button whose background you are trying to change. Use this code.
v.setBackgroundResource(R.drawable.yellow_button);
If onClick is not the method of the same button then, use
findViewById(R.id.button17).setBackgroundResource(R.drawable.yellow_button);
Button btn = (Button) findViewById(R.id.button17);
btn.setBackground(this.getResources().getDrawable(R.drawable.yellow_button));
Try this.

Is it possible to change text on xml button?

If I have an xml button in a file called test.xml like this:
<Button
android:id="#+id/newgame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Game"
/>
Is it possible to change the "New game" text to something else? I have to do this many times.
The big problem is: I have to do it from inside a java file! Is that even possible?
Yes.
1) Get a reference to the Button
2) use the setText method to give text a new value
Button button = (Button)findViewById(R.id.newgame);
button.setText("Something Else");
definitly buddy,this type of question shoul not be ask for smart phones ,u can do it by update in xml as well as by java coding
ex....
Button buttontext = (Button)findViewById(R.id.button1);
button.setText("this is new text");
or in xml..
in xml it would be static for make it dynamic,set text in java class..
thanks

Categories

Resources