I have a lot of textviews in my program .
I want the numbers inside these textviews to be separated by 3 to 3 .
Should I write code for any textview?
Is there a way to write code once and use it for the whole program?
Thank You .
I am not a Java developer, but I'm thinking you could write a class that inherits from the textview class and override the method that sets the text to separate the numbers. Then you'd just need to replace the class of your textview objects with your new class, which should be quite easy.
It dependes on the type of Layout. For example in a simple LinearLayout you can proceed like this :
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
for(int k = 0 ; k < n ; k++){
TextView tView = new TextView(context);
tView.setText(k*3)
linearLayout.addView(tView);
}
Or another way could be iterating on all Views that are TextView setting the text. For example, assuming that mylayout is the parent view :
for(int k=0; k < mylayout.getChildCount(); k++ ){
if( mylayout.getChildAt(k) instanceof TextView ){
((TextView)mylayout.getChildAt(k)).setText(k*3);
}
}
In order to use your own TextView style, you can creat one by extend the TextView class and then use it in the xml activity
See example how to extend:
How to make a custom TextView?
In the constractor of your new TextView, you can add TextWacher by addTextChangedListener method to perform your number seperation.
See example how:
android on Text Change Listener
You have to use DecimalFormat and the grouping pattern
Related
I need to add 5 Input fields (EditText) dynamically one by one on button click and want to take values from them and store them into database using Room Persistence with MVVM.
Here I'm adding the view dynamically
private void addEditTextView() {
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
}
Any suggestion would be very helpful.
Thank you in advance.
Add view based on child count
private void addEditTextView() {
if (binding.layoutList.getChildCount() <= 5) {
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
}
}
"When I clicked Add button it is adding input field one by one, this code is working but I just want to limit for 5 fields not more not less and take values from them."
If you want to add exactly 5 fields on button click I recommend designing a fragment with the 5 fields in place, then when the button is clicked, inflate the fragment into your parent view. Then code the fragment appropriately with the data you're working with.
Then if you wanted, you could deflate the fragment on button click to clear the view or add some other way to clear the fragment when you want. Much easier than what you're doing currently in my own opinion.
You might as well include a submit button in your fragment assuming this is some kind of form.
You can simply define an integer and increase it every time you add the EditText but you should check if your integer is less than 5 everytime the method is called.
Example
private void addEditTextView() {
int count = 0;
if (count < 5){
View inputView = getLayoutInflater().inflate(R.layout.row_edit_text, null, false);
EditText val1 = inputView.findViewById(R.id.input_value_1);
binding.layoutList.addView(inputView);
count++;
}
}
I was wondering if it was possible to ouput xml elements such as editText and textView based off user input. For example I'm making a simple game app and in the app you get taken to a new activity where it asks the amount of players. Based off the input (e.g. 5) I woulld like to display editText's and textViews so the players can be given names. Is this possible with Java and if so, how?
Sure is, though without having to go through XML.
Pass the number of players to the next activity as an argument. Then make sure you have a ViewGroup (LinearLayout for example) in the second activity's layout.
Then loop through and create the Views dynamically:
ViewGroup container = findViewById(R.id.linearLayout);
for( int i = 0; i < numberOfPlayers; i++ ){
EditText et = new EditText(this);
et.setHint(R.string.new_player_hint);// String providing a hint to the user
container.addView(et);
}
You can put the EditTexts in a List or if that's all you'll have you can directly cycle over the ViewGroup's children to get their values later on:
for( int i = 0; i < container.getChildCount(); i++ ){
String playerName = ((EditText) container.getChildAt(i)).getText().toString();
}
If you're going to have many possible players you should wrap the LinearLayout in a ScrollView.
Because you'll likely use this in onCreate remember that you can't rely on the EditText to save its own data in case the Activity is destroyed, so save them yourself and feed them back in when re-creating.
See this picture:
I am designing a sudoku in android, have done the layout like this, so 81 Editext is there. I want to get all the values in the Editext so its difficult to declare and mange 81 views in activity. Can you suggest me a method which is effective for processing my problem?
Create edittexts dynamically.
In every iteration you are rewriting the ed variable, so when loop is finished ed only points to the last EditText instance you created.
You should store all references to all EditTexts:
EditText ed;
List<EditText> allEds = new ArrayList<EditText>();
for (int i = 0; i < count; i++) {
ed = new EditText(Activity2.this);
allEds.add(ed);
ed.setBackgroundResource(R.color.blackOpacity);
ed.setId(id);
ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linear.addView(ed);
}
Now allEds list hold references to all EditTexts, so you can iterate it and get all the data.
getting data
String[] strings = new String[](allEds.size());
for(int i=0; i < allEds.size(); i++){
string[i] = allEds.get(i).getText().toString();
}
NOTE:
This is not results your design there may be some changes.but follow these procedure to get exact results. I think you must you use two for loops to get your edittexts in a gridview.
I would suggest you to use array of Edittext's like this
EditText text1,text2,text3,text4,text5,text6,text7,text8,text9, text21,text22,text23,text24....text 99;
EditText[] fields={text1,text2,text3,text4,text5,text6,text7,text8,text9, text21,text22,text23,text24....text 99};
//Give your edittext ids in ids array like this
int ids[]={R.id.text1,R.id.text2,R.id.text3.....R.id,text99};
int values[9][9]={};
for(int i=0;i<fiends.length;i++)
{
fields[i]=(EditText)findViewById(ids[i]);
}
To get values use the same way take an array and save those values in array like this.
use for loop and get the values from the textfields and save it in the 2d array as i have declared in values array. I suppose you know how to code the rest of the thing you really want to know please comment, I'll post the rest.thank u.. I hope this will help you.
You can try out this way:
public static ArrayList<EditText> m_arr_edit= = new ArrayList<EditText>();
RelativeLayout p_rl_layout=new RelativeLayout(m_context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout m_ll_timelayout = new LinearLayout(m_context);
m_ll_timelayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int i = 0; i < p_intervals; i++)
{
EditText m_edit = new EditText(m_context);
m_edit.setLayoutParams(new LinearLayout.LayoutParams(90, LayoutParams.WRAP_CONTENT));
m_edit.setText("Set Text");
m_edit.setId(i);
m_arr_edit.add(m_btn);
m_ll_timelayout.addView(m_btn);
}
layoutParams.addRule(RelativeLayout.BELOW);
p_rl_layout.addView(m_ll_timelayout, layoutParams);
I hope it will help you.
Basically, I have a LinearLayout that holds a random amount of horizontal LinearLayouts, and in each of the horizontal LinearLayouts there's a TextView and an EditText. I want to be able to get the value of each EditText children of the master LinearLayout.
Sorry if it's confusing, I'm no good at explaining things!
Could I just set the same ID for each of the EditTexts then use findViewById, or would that only return the first instance of an EditText?
Thanks,
Alex.
LinearLayout ll = //Your Layout this can be any Linear or Relative layout
//in which you added your spinners at runtime ;
int count = ll.getChildCount();
for(int i =0;i<count;i++)
{
View v = ll.getChildAt(i);
if(v instanceof Spinner)
{
// you got the spinner
EditText s = (EditText) v;
Log.i("Item selected",s.getText().toString());
}
}
findViewById returns only the first view with the given id. You're going to have to traverse the view hierarchy yourself, at least until you get down to each horizontal linear layout. You'll find the methods ViewGroup.getChildCount() and ViewGroup.getChildAt(int) useful for this.
You would need to call findViewById on each of the LinearLayouts. If you do this, you can set the same ID for each EditText.
I started my own android app a few days ago since I needed a mobile application to store a bunch of data I collect in the hospital.
I'm pretty new to Java and android environment, although it seems easy to understand and very similar to C++.
Anyway, my application has a bunch of "EditText" and radio buttons and my question is:
How can I iterate through all those widgets (EditTexts and radio buttons)?
In .NET you could do a "for each element in container " loop but I can't seem to find a way to do this in Java/android environment.
Note: I don't know how many "widgets" exist in the activity, since some are created dinamicaly, others are hardcoded and some others show if some user preferences are set
Any help or hint would be appreciated.
for (int i = 0; i < rootView.getChildCount(); i++)
rootView.getChildAt(i)
Note that this will return View-s, you will have to check at runtime exactly what type of View you are currently looking at
It works.
Regarding the View type (ie Spinner, radioButton, EditText, etc) we can tag each type we want to parse in the Layout XML file and then add a conditional, ie:
if (Widget_Tag != null){
View Current_Widget = (View) rootView.getChildAt(i);
String Widget_Tag = (String) Current_Widget.getTag();
if (Widget_Tag.equals("MyEdiText")) {
//do something
}
}
the if (Widget_Tag != null){ is to prevent NullPointReferences. You can also doi it with a Try / Catch.
You can try this code:
LayoutInflater inflater = getLayoutInflater();
LinearLayout l = (LinearLayout) inflater.inflate(R.layout.main, null);
ViewGroup Current_Widget = (ViewGroup)l.getRootView();
for (int i = 0; i < Current_Widget.getChildCount(); i++)
Current_Widget.getChildAt(i);