Create view programmatically with style - android

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.

Related

Android - Create spinner programatically display issue

Basically, my program is prompting user to choose subjects from MainActivity's checkbox (maximum 5 from 10) and send its isChecked boolean value to NextActivity. With each true value receive, I need to create a spinner for user to choose grades between from A-F.
However, the issue is that the spinner in layout shows like that.
I created a class for creating spinner
public void createSpinner(String spinnerTitle, Spinner spinnerNumber){
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add(spinnerTitle);
spinnerArray.add("A");
spinnerArray.add("B");
spinnerArray.add("C");
spinnerArray.add("D");
spinnerArray.add("F");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinnerNumber.setAdapter(spinnerArrayAdapter);
}
Then, to get value from previous activity and check whether its true, for creating a spinner.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grade);
LinearLayout layout = new LinearLayout(this);
Spinner spinner1 = new Spinner(this);
Spinner spinner2 = new Spinner(this);
.....
Spinner spinner10 = new Spinner(this);
Boolean check01 = getIntent().getExtras().getBoolean("C001");
Boolean check02 = getIntent().getExtras().getBoolean("C002");
.....
Boolean check10 = getIntent().getExtras().getBoolean("C010");
if (check01 == true) {
createSpinner("Subject C0001",spinner1);
layout.addView(spinner1);
}
if (check02 == true) {
createSpinner("Subject C0002",spinner2);
layout.addView(spinner2);
}
.....
if (check10 == true) {
createSpinner("Subject C0003",spinner3);
layout.addView(spinner3);
}
Please advice how to let the spinner to display vertically and placed in the centre.
An also, how can I detect which spinner is created, so that I can get the value user choose from the spinner.
To display it vertically you need to set LinearLayout's orientation to vertical. You can do it like this in code.
layout.setOrientation(LinearLayout.VERTICAL);
To make Spinner appear in center set layout's gravity to CENTER_HORIZONTAL.
LayoutParams layout_params = new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT, Gravity.CENTER_HORIZONTAL);
layout.setLayoutParams(params);

filling in text views from shared preferences in a scroll view

Im a bit stuck on the best way to implement this so here I am.
I have a list of shared preferences with a label and a price. My app, a sort of calculator if you will, I would like to load the shared preferences in this way but stuck on how to implement it properly so ill wrote it down in a very literal way
Load shared preferences "item1"
Load into textView1 if empty
if it is full then try textview2,
if it is full then try textview3,
etc etc
I would like a function which loads preference 1, attempts to load it into a view and if its full, try the next textview and so on until it finds and empty slot. and then try the next shared preference item
Maybe I'm looking at this wrong but without writing out tons of if/else statements to get the job done, I cant see a way that makes sense.
I've seen somewhere some code to increment the textview numbers in a loop until a case is met but cant seem to recall it anywhere. this would reduce the code a lot if this was implemented properly I'm sure.The app I'm creating will display a list of selected items from another screen,save selected to shared preferences and then load the list on the results screen to give a total based on the sums attached to the items loaded.
I have no actual code to give you as im still prototyping this section and have nothing solid to show you.
any pointers welcome or any nod in the right direction would be handy.
thanks guys
You could add TextViews dynamiclly through java.
public class DynamicAvtivity1 extends Activity {
TextView tv;
EditText et;
Button btn;
LinearLayout ll;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
i = 1;
ll = new LinearLayout(this);
ll.setOrientation(1);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT);
final LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(lp);
tv = new TextView(this);
tv.setText("Enter Name");
tv.setLayoutParams(lp2);
ll.addView(tv);
et = new EditText(this);
et.setHint("Name");
et.setLayoutParams(lp2);
ll.addView(et);
btn = new Button(this);
btn.setLayoutParams(lp2);
btn.setText("Add Button");
btn.setLayoutParams(lp2);
ll.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("NewApi") #Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button b = new Button(getApplicationContext());
b.setLayoutParams(lp2);
b.setText("Button " + i++);
b.setTextColor(Color.BLACK);
b.setBackground(btn.getBackground());
ll.addView(b);
}
});
setContentView(ll);
}
}

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

List Of Text And Clickable

I want to ask how to make a list of text that we can tap in each of the text and then get the selected text to editText.
I just added the screenshot
http://i.stack.imgur.com/ddZSg.png
I have been searching it since yesterday, but I can not find exact solution. I also tried with listview, but I don't know how it's possible with horizontal, and flow list item.
I am also new in android. But I can think of the logic what you want. You can try this out if you want.
First of all, you can make your list of texts EditText by list of buttons Buttons
You can dynamically add as many Buttons with their respective text as you want to show.
set their corresponding onClickListener
In their onClickListener , create an object of the EditText you are using to add texts.
Store the value of EditText into a String Variable first.
Add the text of the clicked Button to the variable.
and now again set the text in EditText with the variable you created to store the values.
Your task will be done.
Try referring this code.
and change the code accordingly as your needs.
// Adding EditText and a button in a new linear layout and then adding
// the new linearLayout to the main layout
String[] valuesToBeAdded={"A","B","C","D"};
String selectedValues=null;
LinearLayout mainLayout=(LinearLayout) findViewById(R.id.mainLayout);
LinearLayout localLayout = new LinearLayout(context);
localLayout.setOrientation(LinearLayout.VERTICAL);
localLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
EditText editText=new EditText(context);
editText.setText(selectedValues);
editText.setId(5000);
localLayout.addView(editText);
for(int i=0;i<valuesToBeAdded.length();i++){
Button button = new Button(context);
button.setText(R.string.scanDocument);
button.setId(i);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed=(EditText) findViewById(5000);
selectedValues=ed.getText();
selectedValues=selectedValues +" " + this.getText();
ed.setText(selectedValues);
}
});
localLayout.addView(button);
}
mainLayout.addView(localLayout);
Thank You
Could you not make as many buttons as you need and then in the button_Click method for all buttons add:
Buttonwithtext_Click(object sender, EventArgs e)
{
editTextBox.Text = editTextBox.Text + Buttonwithtext.text + ", ":
}

Android, SpannableString, ArrayAdapter and Listview

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

Categories

Resources