Android, SpannableString, ArrayAdapter and Listview - android

i am newbie to android and i got a question for you. I want to add icon to a text in a listview. I'm adding the rows(textview) to listview dynamically with below code. But the text doesnt contain the icon.
.
.
.
btnicon = (Button)findViewById(R.id.button1);
txt = (TextView)findViewById(R.id.textView1);
txt.setText(" denemedene",BufferType.SPANNABLE);
list = (ListView)findViewById(R.id.listview);
dizi = new ArrayAdapter<String>(this, R.layout.text);
list.setAdapter(dizi);
btnicon.setOnClickListener(click);
.
.
.
private OnClickListener click = new OnClickListener() {
#Override
public void onClick(View v) {
if (v == btnicon){
SpannableString spans = new SpannableString(txt.getText());
Drawable dra = getResources().getDrawable(R.drawable.p1);
dra.setBounds(0, 0, dra.getIntrinsicWidth() , dra.getIntrinsicHeight());
ImageSpan span = new ImageSpan(dra,ImageSpan.ALIGN_BASELINE);
spans.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
dizi.add(spans.toString());
}
}
};
it is something about the toString method. Because when i change the
dizi.add(spans.toString());
line with
txt.setText(spans,BufferType.SPANNABLE);
it works, i can see the icon on text. I have no idea why it is not working with toString() method.
Any ideas?
Thanks..

There is an example for custom list adapter in this post: https://stackoverflow.com/a/15272092/1752867
You can create your custom adapter like that and change checkbox component with imageView or ProgressBar in the layout

Related

How to apply style to dynamically created EditText in android?

I have one Button, and on every click of that button I want to create new EditText Dynamically.
Now my question is , I have one style in style.xml for EditText. Now how can I apply that style to this dynamically created EditText?
My Code for EditText is:
List<EditText> allEdsLabReport ;
String[] edsLabReport;
EditText LabReportNm;
AddMoreLabReport.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
LabReportNm = new EditText(CaseReport.this);
allEdsLabReport.add(LabReportNm);
TableLayout tbl2 = (TableLayout)findViewById(R.id.TableLayoutLabReport);
TableRow tr1 = new TableRow(CaseReport.this);
tr1.addView(LabReportNm);
tbl2.addView(tr1);
}
});
new EditText(new ContextThemeWrapper(CaseReport.this, R.style.my_style));
LabReportNm.setTextAppearance(getApplicationContext(), R.style.style.xml)
editText.setTextAppearance(getApplicationContext(), R.drawable.demo.xml);

Create view programmatically with style

i have a problem with my application. Basically what it does, is when i click a button, it creates 3 fields (2 editText and 1 spinner) on a scrollView. The thing works well, the only problem that im having, is related with the style, the activity bgColor is white(as the rest of the app) but, when i create elements programmatically, these elements doesnt have the look of the rest of my app. The editTexts are white with white letters (impossible to read since my bgColor is white as well) and its the same thing with the spinner. What can i do? Here is a snipet of code so you can see what im doing here.
public class AddIngredients extends Activity {
public int Count = 0;
public String[] spinnerArray = {"Gr", "kg", "Cups", "ml", "L", "oz"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addingredients);
final TableLayout lm = (TableLayout) findViewById(R.id.TableMain);
TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button addMore = (Button)findViewById(R.id.addmore);
addMore.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TableRow ll = new TableRow(getApplicationContext());
//ll.setOrientation(LinearLayout.HORIZONTAL);
EditText product = new EditText(getApplicationContext());
product.setHint(" Ingredient "+Count +" ");
// Create Button
EditText amount = new EditText(getApplicationContext());
// Give button an ID
amount.setId(Count);
amount.setHint("Quantity");
final Button btn2 = new Button(getApplicationContext());
btn2.setId(Count);
btn2.setText("Remove " + Count);
Spinner spinner = new Spinner(getApplicationContext());
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
ll.addView(product);
ll.addView(amount);
ll.addView(spinner);
lm.addView(ll);
Count = Count + 1;
I know my XML is working well because if i create the 3 views on my xml, they look great. PD: Thx in advance for any help! Greetings.
you can use
amount.setTextColor(Color.BLACK);
to set colour of text to black or any other colour
same can be used for spinner
Here's how I set the colors of my edit text lines and other theme-related android views.
http://android-holo-colors.com/
I just picked the color and views I wanted, then unzipped them in my res folder, then set the theme according the android tutorials.
I recommend backing up your res folder first, in case you don't like the results.
Garret
I had a error in my code. When creating the fields, i was using
(getApplicationContext());. I fixed it using MyApplicationName.this.

How can I change Text of Textview from other funcion?

this is my first question so I hope to make it clear.
I have one textView with some numerical text and next to it one button with one click listener and what I want is that when you click on the button the numerical value (>=0) of the TextView decrements in one.
Here is part of my code:
TextView Counter = new TextView(this);
if (intSeries != 0)
Counter.setText(Integer.toString(intSeries));
else
Counter.setText("0");
Counter.setId(4);
tablaContador.addView(Counter,Tr);
Button Done = new Button(this);
Done.setText("-1");
if (intSeries != 0)
Done.setVisibility(View.VISIBLE);
else
Done.setVisibility(View.GONE);
Done.setId(6);
Done.setOnClickListener(this);
And this is the onClick funcion (part of it):
#Override
public void onClick(final View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case 6:{
TextView text = (TextView)findViewById(4);
int series = Integer.parseInt(text.getText().toString());
series--;
text.setText(series);
if (series==0){
Button boton = (Button)findViewById(6);
boton.setVisibility(View.GONE);
}
}
}
}
The error is when I try to make the setText inside the onClick function, I hope it can be fixed or maybe recieve other idea to do it.
Thank you so much.
I would avoid all this hardcoding of Ids, use resources instead.
Your call to
text.setText(series)
is passing an int. The only valid setText(int resId) overload expects a resource associated with the int value, i.e. a string resource.
Convert your series value to a string.
Something like:
text.setText(Integer.toString(series));
You should setup series as an integer. And increase/descrease it as you wish. When you want to change the button's text convert the int to String.
Instead of:
text.setText(series);
use:
text.setText(String.valueOf(series));
Variablenames in java can't start with a capital letter. That is reserved for classnames.
Counter -> counter
Done -> done
I tried this and it worked:
//Create onClickListener
OnClickListener pickChoice = new OnClickListener()
{
public void onClick(View v)
{
TextView txt = (TextView) findViewById(4);
int number = Integer.valueOf(txt.getText().toString());
txt.setText(String.valueOf(number -1));
}
};
//Create layout
LinearLayout lnLayout = new LinearLayout(this);
lnLayout.setOrientation(LinearLayout.VERTICAL);
TextView txt = new TextView(this);
txt.setId(4);
txt.setText("0");
lnLayout.addView(txt);
Button Done = new Button(this);
Done.setText("-1");
Done.setId(6);
Done.setOnClickListener(pickChoice);
lnLayout.addView(Done);
setContentView(lnLayout);
Where are you creating your button inside? an activity? the part where you pass the onClickListener to the button doesn't make sense, maybe the button is getting a wrong listener and gets you an error every time you press the button ?
The code should be easy to understand, if there is anything you need me to explain please ask :)

Android viewing an arraylist in text view

I have a program that has a text input and a button. When I type something into the input and press the button I want that String to be added to a String Arraylist and have that Arraylist displayed in a TextView. Right now I have a method:
public void addString(View view)
{
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
}
"edit-choices" is the name of the text input and "choices" is the name of the array list. First of all am I doing this correctly? Second, how to I get the text view to display the contents of "choices". Right now my TextView id is just textView1
Please keep in mind that it is not the best way to show list items in a TextView. You can do this using a ListView. Anyhow, see pseudo code below (didn't test that in Eclipse, however, it should show how it is basically going to work):
public class YourActivity extends Activity {
Vector<String> choices = new Vector<String>();
public void onCreate(Bundle ....) {
(Button) myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(new OnClickListener() {
#Override
public boolean button.onClick() {
addString();
TextView textView = (TextView) findViewById(R.id.text_view);
String listRepresentation = "";
for (String choice : choices)
if ("".equals(listRepresentation))
listRepresentation = choice; else
listRepresentation = ", " +choice;
textView.setText(listRepresentation );
}
});
}
public void addString(View view)
{
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
}
}
So simply assign an OnClickListener to your button that does what you need.
The question is how you want the Text to be displayed...
Either like a list view or just as a normal text.
If you want to show the text as a normal text in the text view you can simply do something like this.
for(String msg : choices)
{
textView1.setText(textView1.getText()+msg);
}
If you want the choices to be displayed in list view you need to set an adapter to the list view using the choices that you have.
First of all am I doing this correctly?
If it works for you, sure. I would maybe cache the EditText so you don't have to "find" it every time you want to access it's content.
Your only "problem" here is, that a TextView has no method that accepts a List<String>. So, you'll need to make a single string out of your list of strings.
You can simply iterate over the list and con-cat them together:
StringBuilder b = new StringBuilder();
for (String s : choices){
b.append(s+"\n");
}
textview.setText(b.toString());
This will simply build one string from all the items in your list, adding line-breaks after every item.
You'll need to set your TextView's android:inputType-attribute to textMultiLine, so it will actually show you multiple lines.

Select text by ContextMenu

I have a looong text in TextView in ScrollView and I want to make a function to tab on text and select current paragraph to add it in bookmarks, but I haven't any ideas how to do it, please somebody help me. I'm trying to get current positions, but I don't know how to slect text.
You can just use new TextView for each paragraph, and let it impliment a click Listener
the code should look like this
#Override
public void onCreate(Bubdle bundle) {
super.onCreate(bundle);
ScrollView view = (ScrollView) findViewById(R.id.scrollView);
// String txt = getText from Somewhere
String[] text = txt.split(".");
for (String t : text) {
TextView view = new TextView();
view.setText(t);
view.setOnClickListener(clickListener);
view.addView(view);
}
}

Categories

Resources