How do i apply a context to a textview array?
TextView[] textView = new TextView[3];
for (int cell = 0; cell < 3; cell++){
textView[cell].setText("-");
}
Apparently, this error gets thrown:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
If this was not a textview array, adding the activity context as a parameter when calling the constructor would render the object not null, solving my issue:
for (int cell = 0; cell < 3; cell++){
TextView textView = new TextView(context);
textView.setText("-");
}
Where, context = getActivity().getApplicationContext()
How do i set the context parameter for the textview array? What i want is dynamic variable names for each textview so i can call the individual textviews respectively.
You should create object before calling method.
Try this:
TextView[] textView = new TextView[3];
for (int cell = 0; cell < 3; cell++) {
textView[cell] = new TextView(context);
textView[cell].setText("-");
}
I hope below code would help you out regarding context for array of views. First you will just create array but before using it you will have to create object using new keyword.
LinearLayout rootview = (LinearLayout) findViewById(R.id.rootview);
ArrayList<String> words= new ArrayList<>();
words.add("One"); words.add("two"); words.add("three"); words.add("four"); words.add("five"); words.add("six"); words.add("seven"); words.add("eight"); words.add("nine"); words.add("ten");
int index=0;
while(index<words.size()){
TextView [] textViews = new TextView[words.size()];
textViews[index] = new TextView(this);
textViews[index].setText(words.get(index));
rootview.addView(textViews[index]);
index++;
}
Related
I already know how to create edittext programmatically but the problem is they have the same Id, How can I add random Id on each edittext generated
You don't need to set a unique id for each view. Just set different tags for them:
private static String getTag(int index) {
return "MY_TAG_" + index;
}
...
for (int i = 0; i < 3; i++) {
EditText editText = new EditText(this);
editText.setId(android.R.id.edit); // same id
editText.setTag(getTag(i)); // different tag
layout.addView(editText);
}
EditText firstField = layout.findViewWithTag(getTag(0));
System.out.println(firstField);
But, if you still want to, there's a static method View.generateViewId():
int[] fieldIds = new int[3];
for (int i = 0; i < 3; i++) {
fieldIds[i] = View.generateViewId();
EditText editText = new EditText(this);
editText.setId(fieldIds[i]); // different id
layout.addView(editText);
}
EditText firstField = layout.findViewById(fieldIds[0]);
System.out.println(firstField);
I/System.out: android.widget.EditText{ed04adc VFED..CL. ......I. 0,0-0,0 #1020003 android:id/edit}
You can use below code for each id's EditText
View.generateViewId()
This question already has answers here:
Adding Table rows Dynamically in Android
(8 answers)
How to add a row dynamically in a tableLayout in Android
(5 answers)
Closed 4 years ago.
i want to create the table dynamically , now the number of rows and columns are saved in the respective integer variables.
please tell me the way i can create the table programatically in android because number of rows and columns are not fixed .
what i tried so far is below...
is there any method to create table with given dimensions(rows and columns)?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
}
This is the solution
int yourRowCount = 12;
int yourColumnCount = 12;
private void createDynamicTable(int yourRowCount) {
TableLayout tableLayout = new TableLayout(this);
for (int i = 0; i < yourRowCount; i++) {
tableLayout.addView(createRow(yourColumnCount));
}
}
private TableRow createRow(int yourColumnCount) {
TableRow tableRow = new TableRow(this);
for (int i = 0; i < yourColumnCount; i++) {
TextView textView = new TextView(this);
textView.setText("TextView" + i);
tableRow.addView(textView);
}
return tableRow;
}
just call
createDynamicTable(yourRowCount);
I have an custom listview and textview,checkbox,edittext on it. I want to iterate my listview row and wanna take textview,checkbox and edittext values. When iteration comes up to end of the page cannot take invisible rows widget values and crashing. I have to scroll down my listview automatically i think. How can I fix this issue?
Note: I have 12 rows but it crashs after 6th row cz cannot see 7th row without scrolldown my listview.
This is my code:
public void onSave(View view) {
ArrayList<String> olcum = new ArrayList<String>();
ArrayList<String> orneklemebuyukluk = new ArrayList<String>();
EditText et;
CheckBox chx;
TextView buyukluk;
String nitel="0";
for (int i = 0; i < list_olcum_view.getCount(); i++) {
view = list_olcum_view.getChildAt(i);
et = (EditText) view.findViewById(R.id.edt_olcum);
chx=(CheckBox)view.findViewById(R.id.txt_orneklemenitel);
olcum.add(et.getText().toString());
}
}
Note: I have 12 rows but it crashs after 6th row cz cannot see 7th row without scrolldown my listview.
public void onSave(View view) {
ArrayList<String> olcum = new ArrayList<String>();
ArrayList<String> orneklemebuyukluk = new ArrayList<String>();
EditText et;
CheckBox chx;
TextView buyukluk;
String nitel="0";
for (int i = 0; i < list_olcum_view.getCount(); i++) {
view = list_olcum_view.getChildAt(i);
et = (EditText) view.findViewById(R.id.edt_olcum);
chx=(CheckBox)view.findViewById(R.id.txt_orneklemenitel);
olcum.add(et.getText().toString());
}
}
I'm trying to connect TextViews of single words and create multilpe rows of them. In order to determine when to create a new line I need to check the width of the words' TextViews.
Here is a pic that is similar to what im trying to accomplish
I get a weird errors when I try to use getWidth:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RelativeLayout.getWidth()' on a null object reference
However when I debug the code rL2 is not null and has a value.
Im guessing my View hasn't been created yet? But how can I alter it dynamically? How can I make my rows?
Here is my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_call_response, container, false);
v.measure(widthMeasureSpec, heightMeasureSpec);
int lineWidth = 0;
RelativeLayout rL2 = (RelativeLayout) v.findViewById(R.id.row1);
int lineCount = 0;
int rowWidth = rL2.getWidth();
int wordCountRow = 0;
TextView question = (TextView) v.findViewById(R.id.Question);
TextView choice1 = (TextView) v.findViewById(R.id.choice1);
TextView choice2 = (TextView) v.findViewById(R.id.choice2);
TextView choice3 = (TextView) v.findViewById(R.id.choice3);
TextView choice4 = (TextView) v.findViewById(R.id.choice4);
TextView speaker = (TextView) v.findViewById(R.id.Speaker);
TextView progress = (TextView) v.findViewById(R.id.progress);
ArrayList<String> answers = new ArrayList<>();
answers.addAll(quote.getAnswerChoice());
answers.add(quote.getWordSplitTypeA().get(quote.getAnswerIndex().get(0)));
Collections.shuffle(answers);
speaker.setText("How would " + quote.getSpeaker().get(0) + " Marx answer the above question?");
choice1.setText(answers.get(0));
choice2.setText(answers.get(1));
choice3.setText(answers.get(2));
choice4.setText(answers.get(3));
progress.setText(String.format("%.1f%% Complete", (SingletonFactory.getPlayer().getLessonProgress()[0] / 12.0) * 100));
//addTextViews(v);
for(int i = 0;i<quote.getWordSplitTypeA().size(); i++) {
if (lineWidth + 10 > rowWidth) {
lineCount++;
wordCountRow = 0;
}
int padding = getResources().getDimensionPixelOffset(R.dimen.wordBoxPadding);
TextView tv = new TextView(getActivity());
tv.setId(i);
tv.setText(quote.getWordSplitTypeA().get(i));
tv.setBackgroundColor(Color.parseColor(bGColor));
tv.setTextColor(Color.WHITE);
tv.setGravity(Gravity.CENTER);
tv.setPadding(padding, padding, padding, padding);
ViewGroup.LayoutParams LP = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(LP);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv.getLayoutParams();
if (wordCountRow == 0) {
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
} else {
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.RIGHT_OF, (i - 1));
}
tv.setLayoutParams(params);
if (quote.getAnswerIndex().contains(i)) {
tv.setBackgroundColor(Color.RED);
tv.setText(".....");
}
int width = tv.getWidth();
lineWidth += width;
if (lineCount == 0) {
RelativeLayout rL = (RelativeLayout) v.findViewById(R.id.row1);
rL.addView(tv);
} else if (lineCount == 1) {
RelativeLayout rL = (RelativeLayout) v.findViewById(R.id.row2);
rL.addView(tv);
} else if (lineCount == 2) {
RelativeLayout rL = (RelativeLayout) v.findViewById(R.id.row3);
rL.addView(tv);
} else if (lineCount == 3) {
RelativeLayout rL = (RelativeLayout) v.findViewById(R.id.row4);
rL.addView(tv);
}
choice1.setOnClickListener(mClickListener);
choice2.setOnClickListener(mClickListener);
choice3.setOnClickListener(mClickListener);
choice4.setOnClickListener(mClickListener);
question.setText(quote.getSpeaker().get(0) + ": " + quote.getQuote().get(0));
}
return v;
}
How can I measure the width of my views as I create them?
You can't. That is handled during measure and layout passes, which happen well after you create the widget.
I'm trying to connect TextViews of single words and create multilpe rows of them.
It looks like you should be using a FlowLayout, or something like that, as the container for the TextViews, where it will wrap them automatically.
If that FlowLayout does not meet your needs, here is another one.
And, if neither of those as quite what you want, you can use them for inspiration in writing your own. The UI rules that you are trying to implement should be implemented in the container, as the container handles sizing and positioning its children.
i wanna create button 1 to 9 and i want to do that in loop. But in each 3 count, i want to create a new LinearLayout.
final LinearLayout[] ll2 = new LinearLayout[10]; // create an empty array;
for(int i=1; i<=9;i++)
{
Button btnNums = new Button(this);
final LinearLayout[] ll2 = new LinearLayout[10]; // create an empty array;
for(int i=1; i<=9;i++)
{
Button btnNums = new Button(this);
btnNums.setText(i+"");
ll.addView(btnNums);
if(i%3==0){
ll2[i] = ll;
ll = null;
}
}
layout.addView(ll2[0]);
btnNums.setText(i+"");
ll.addView(btnNums);
if(i%3==0){
ll2[i] = ll;
ll = null;
}
}
layout.addView(ll2[0]);
This does not work. I get no error but when o run the app, it is stopped to work. What's the problem?
I used it in my Project and it worked for me, i used it like this. Hope it helps
Declare empty array at class level:
LinearLayout[] imageLayoutContainers = new LinearLayout[10];
And then in on onCreate methord :
for (int i = 0; i < imageLayoutContainers.length; i++) {
imageLayoutContainers[i] = new LinearLayout(this);
imageLayoutContainers[i].setOrientation(LinearLayout.VERTICAL);
imageLayoutContainers[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageLayoutContainers[i].setBackgroundResource(imagesIds[i]);
}
It worked, thanks