I want to change the Text style on a button click. When the user clicks the button it makes the selected area italic. When the user clicks it again I want the selected area to become non italic. How is this possible. This is my code:
if (id == R.id.italic) {
int startSelection = noteContent.getSelectionStart();
int endSelection = noteContent.getSelectionEnd();
Spannable spannable = noteContent.getText();
StyleSpan[] spans = spannable.getSpans(startSelection, endSelection, StyleSpan.class);
if (spans.length == 0 ) {
spannable.setSpan(new StyleSpan(Typeface.ITALIC), startSelection, endSelection , 0);
} else {
StyleSpanRemover spanRemover = new StyleSpanRemover();
spanRemover.RemoveStyle(spannable, startSelection, endSelection, Typeface.ITALIC);
}
}
When I run this code the selected area becomes Italic on the button click. Then if I were to click the button again the same selected area would become un italic. Sounds like it works fine. But it does not. For some reason if I were to take the same selected area and re italicize it it will not become italic. This is because spans.length is still set to 1. It did not go back to zero when I made the Italicized text normal. Any idea on how to set spans.length back to 0?
Related
I am trying to create an activity that contains an edit text and a button. Whenever the button gets clicked the font family of the edit text should be changed according to the fonts that are been provided in the array list. The activity contains an ArrayList of all the font id in it. whenever the font button will be clicked the if statement will be called that will check whether the font selected is last one or not if it is the last element from the list then it will set the font family and reset the font position to 0 else it will set the font family from the list of that specified font position and increment the font position. This if-else statement will perform the activity to set a new font family according to the array list.
I have searched over the internet and found a solution as setTypeface when I used the solution, it gave an error that says
Unable to start activity ComponentInfo{com.nanb.alpha/com.nanb.alpha.postcreater}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=2131296256
codes are been provided below:-
ArrayList<Integer> fontstyle = new ArrayList<Integer>(7);
int fontpostion = 0;
private void fontstylemethod() {
fontstyle.add(R.font.abril_fatface);
fontstyle.add(R.font.cedarville_cursive);
fontstyle.add(R.font.alfa_slab_one);
fontstyle.add(R.font.annie_use_your_telescope);
fontstyle.add(R.font.aclonica);
fontstyle.add(R.font.aguafina_script);
fontstyle.add(R.font.arizonia);
editText.setTypeface(Typeface.defaultFromStyle(fontstyle.get(0)));
font.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(fontpostion == 6){
//Toast.makeText(view.getContext(),"last element",Toast.LENGTH_SHORT).show();
editText.setTypeface(Typeface.defaultFromStyle(fontstyle.get(fontpostion)));
fontpostion= 0;
}else{
fontpostion++;
editText.setTypeface(Typeface.defaultFromStyle(fontstyle.get(fontpostion)));
}
}
});
}
I need the same JButton to perform a different actions once it's clicked again. Like the first time I click the button, a text will appear in the first row of my JTextField, then the second time I click it, a text will appear in the second row if text field. How should I do it?
Here is my code BTW:
private void addActionPerformed(java.awt.event.ActionEvent evt) {
String items1 =(String)list.getSelectedItem();
String qty1 = qty.getText();
String price1 = price.getText();
int qty2 = Integer.parseInt(qty1);
int price2 = Integer.parseInt(price1);
if(evt.getSource() == add){
order1.setText(Integer.toString(qty2));
order2.setText(Integer.toString(price2));
order3.setText(items1);
}
I literally have no idea what to do next.
Here is the pic for the design GUI: http://prntscr.com/pfh96z
Take a Boolean isClickedOnce and change its state upon clicking on your button
private Boolean isClickedOnce = false;
//..
private void addActionPerformed(java.awt.event.ActionEvent evt) {
if(!isClickedOnce) {
//first click
//..
} else {
//second click
//..
}
isClickedOnce = !isClickedOnce;
}
Note: it'll consider every odd number click as first click and every even number of click as second click. it will toggle through your first and second row.
If your case is different lets say you have n number of rows, above procedure won't work and you might wanna do something similar with a list.
I am trying to build a feature where users can select a text in an Edit text and make the text italic or bold by clicking buttons in a small toolbar. I wrote some code in android studio but there is an issue.
public void bold(EditText text){
int start = text.getSelectionStart();
int end = text.getSelectionEnd();
CharacterStyle style = new StyleSpan(Typeface.BOLD);
SpannableStringBuilder sb = new SpannableStringBuilder(text.getText().toString());
sb.setSpan(style, start, end, 0);
text.setText(sb);
}
When user click bold button, it calls the bold() function and bolds the selected text.but if i change the selection and click italics, it italics the selected text but deletes the bold style i already applied to the edit text.
For example
exampleone exampletwo (bolds the text)
But if click select exampletwo and click italic
it becomes
exampleone exampletwo
I lost the style of the first word in the Edittext.
How can i fix this issue ?
SpannableStringBuilder ssb = newSpannableStringBuilder(bodyView.getText());
cs = new StyleSpan(Typeface.BOLD);
ssb.setSpan(cs, start, end, 1);
cs = new StyleSpan(Typeface.ITALIC);
ssb.setSpan(cs, start, end, 1);
I think you should use same Spannablestringbuilder for bold and italic.
It works for me.
I tried using the same example from TreeCursor documentation:
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/plain/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet360.java
On whichever cell I press enter (SWT.CR) for the first time, say I press on the cell 'root11', after this any cell that I click, 'root11' appears in it.
My requirement is to select the text from a cell to copy. I do not want to edit. So every time i click on a cell to copy its text, the text from the cell that was selected first time appears.
Any pointers on what could be causing this?
Thanks in advance!
When ENTER is pressed on an item, the widgetDefaultSelected method is called. There, the ControlEditor sets a Text with the selectedTreeItem text as its editor: editor.setEditor(text);.
This Text is then only disposed when ENTER or ESC is pressed on it, and in no other occasions. This means that the Text will still be visible with its original content even when you select another item.
To change this behavior you could modify the widgetSelected method to, for example, dispose the Text so it would be no longer visible or update its text with the current selected item.
To remove the Text:
#Override
public void widgetSelected(SelectionEvent e) {
// get the current editor
Text text = (Text) editor.getEditor();
if (text != null && !text.isDisposed()) {
// remove the editor
text.dispose();
}
tree.setSelection(new TreeItem[] { cursor.getRow() });
}
To update the Text content:
#Override
public void widgetSelected(SelectionEvent e) {
// get the current editor
Text text = (Text) editor.getEditor();
if (text != null && !text.isDisposed()) {
// update the text in the editor
TreeItem row = cursor.getRow();
int column = cursor.getColumn();
text.setText(row.getText(column));
}
tree.setSelection(new TreeItem[] { cursor.getRow() });
}
I am using achartengine in my Android app to display a stacked bar chart (something similar to this). I use the ChartFactory.getBarChartView to get a GraphicalView object that I then place in my layout.
My objective is to provide touch feedback to the user when one of the stacked bars is touched by the user. I intend to do this by changing some of the UI properties (probably color) of the bar that the user touched. However, I want to designate the entire stacked bar as touched. This is as opposed to designating just the selected section of the bar.
Taking the SalesStackedBarChart example from the demo, I want to designate the entire bar for, say, March as selected (not just the section for March 2008).
I already know how to set up onClickListener on the GraphicalView object and to use the getCurrentSeriesAndPoint() method. However, using this approach, I have been able to only designate the same section on all bars as selected. For example, the 2008 sections on all 12 bars are highlighted.
Question:
How do I change the appearance of all sections in a single stacked bar?
Here's my code. It works as designed (i.e, sets the selected section on all 12 bars to gray), but this is not what I want.
mGraph.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
SeriesSelection seriesSelection = mGraph.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(mContext, "No chart element was long pressed",
Toast.LENGTH_SHORT).show();
return false; // no chart element was long pressed, so let
// something
// else handle the event
} else {
SimpleSeriesRenderer renderer = mMultiRenderer.getSeriesRendererAt(seriesSelection.getSeriesIndex());
Toast.makeText(mContext, "Chart element in series index "
+ seriesSelection.getSeriesIndex() + " data point index "
+ seriesSelection.getPointIndex() + " was long pressed",
Toast.LENGTH_SHORT).show();
renderer.setColor(Color.LTGRAY);
return true;
}
}
});
Setting the color of a series will change the colors of all bars in that series.
One potential solution would be to create an extra series containing only the bars you want to highlight as selected. Quite tricky.