Dynamically adding a ViewGroup to an active Layout - android

I'm looking to insert items into lists in the View below when any of the add buttons are clicked. What I'd like is to display all the information that is being collected, but I'm not really sure how to add a ViewGroup programatically and insert it into the ConstraintLayout in the correct locations while bumping all the corresponding items below.
I've updated the button code and now I'm running into a mess of other problems, currently when the button is clicked a TextView that reads "Name: " appears and is properly inserted into the Layout, but it seems that something isn't going right when I set the id for textView name and it either isn't being recorded or I'm not using the methods correctly.
Button Code
protected void addWeap(View view)
{
int id = 0;
int weapons = 0;
String weaponAdder = "";
int weaponPointer = 0;
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.conLayout);
EditText weapN = (EditText) findViewById(R.id.weapName);
EditText weapAB = (EditText) findViewById(R.id.weapattbns);
EditText weapDMG = (EditText) findViewById(R.id.weapDmg);
EditText weapInfo = (EditText) findViewById(R.id.weapInfo);
TextView proItem = (TextView) findViewById(R.id.proItem);
String weapName = "";
String weapAttBns = "";
String weapdmg = "";
String weapinfo = "";
weapName += weapN.getText();
weapAttBns += weapAB.getText();
weapdmg += weapDMG.getText();
weapinfo += weapInfo.getText();
weapons++;
weaponAdder += weapName + "|";
weaponAdder += weapAttBns + "|";
weaponAdder += weapdmg + "|";
weaponAdder += weapinfo + "|";
weapN.getText().clear();
weapAB.getText().clear();
weapDMG.getText().clear();
weapInfo.getText().clear();
TextView name = new TextView(this);
params.topToBottom = weaponPointer;
params.leftToLeft = R.id.parent;
name.setText(R.string.namePrint);
name.setTextColor(R.color.black);
name.setLayoutParams(params);
name.setPadding(8,8,0,0);
name.setId(id);
id++;
weaponPointer = id;
constraintLayout.addView(name);
params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
TextView weaponName = new TextView(this);
params.baselineToBaseline = id;
params.leftToRight = id;
params.rightToRight = R.id.parent;
weaponName.setText(weapName);
weaponName.setTextColor(R.color.black);
weaponName.setLayoutParams(params);
weaponName.setPadding(8,8,8,0);
constraintLayout.addView(weaponName);
params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
params.leftToLeft = R.id.parent;
params.rightToRight = R.id.parent;
params.topToBottom = id;
proItem.setPadding(8,8,8,0);
proItem.setLayoutParams(params);
}
Pre-Button Push
Post-Button Push

Related

Dynamically adding data to more TextView

Is it possible to dynamically add text to several textview that are defined in xalm? For example, in a loop:
var textView1 = FindViewById<TextView>(Resource.Id.textView1);
var textView2 = FindViewById<TextView>(Resource.Id.textView2);
var textView3 = FindViewById<TextView>(Resource.Id.textView3);
var content = "Add me to more at one textView"
for (i = 1; i = 3; i++)
{
// add me;
}
I can easily do it as it adds textview programmatically by calling GetChildAt () in layout. Is it possible to add dynamic if textView is defined in xalm?
Say your texts ids are text1, text2, tex3, etc. Use:
for (int i=1; i<numberOfTextsViews; i++) {
int id = this.getResources().getIdentifier("text" + i, "id", getPackageName());
TextView temp = findViewById(id);
temp.setText("text you want to add to all your texts");
}
I write like this:
public TextView FindId(string resourceId)
{
var type = typeof(Resource.Id);
var field = type.GetField(resourceId);
int res = (int)field.GetRawConstantValue();
TextView tx = FindViewById<TextView>(res);
return tx;
}
and:
for (int i = 0; i <= 34; i++)
{
TextView tx = FindId($"textView_day{i}");
tx.Text = day[i];
tx.Tag = tx.Text;
tx.Click += Day_Click;
}
THX a lot Mike087 for getting me on the right track.

Android - Loop to collect all editText values

I have around 50 EditText fields and I want to get their values using a loop. Getting the values individually as below works fine:
SharedPreferences settings = getSharedPreferences(filename, 0);
SharedPreferences.Editor editor = settings.edit();
editor.clear(); //not sure if this is required
final EditText t1 = (EditText) findViewById(R.id.text1Value);
String T1 = t1.getText().toString();
editor.putstring("text1",T1);
final EditText t2 = (EditText) findViewById(R.id.text2Value);
String T2 = t2.getText().toString();
editor.putstring("text2", T2);
................................ and so on till EditText t50.
I have tried achieving this through the loop below but couldn't get it to work.
for(int x=1; x<50; x++)
{
EditText et[x] = (EditText) findViewById(R.id.text[x]Value);
String t[x] = et[x].getText().toString();
String Ref = "text" + x;
editor.putString(Ref, t[x]);
}
You can try this by creating an array of the ids of the textview and looping through the array.
Try this:
int[] ids = new int[]{R.id.text1Value,R.id.text2Value,R.id.text3Value};//and so on
int i =1;
for(int id : ids){
EditText t = (EditText) findViewById(id);
String Ref = "text" + i;
editor.putString(Ref, t.getText().toString());
i++;
}
If I assume you have all of these EditTexts inside a Layout called ll (should work for structures other than Layout too, such as a ViewGroup etc.):
ArrayList<String> values = new ArrayList<String>();
for (int i = 0; i < ll.getChildCount(); i++) {
if (ll.getChildAt(i) instanceof EditText) {
EditText et = (EditText)ll.getChildAt(i);
values.add(et.getText().toString());
}
}
My Android is a little rusty so apologies if there is something wrong with that, but should give you the general idea anyway
int[] ids = new int[]{R.id.text1Value,R.id.text2Value,R.id.text3Value};//and so on
int i =1;
for(int id : ids){
EditText t = (EditText) findViewById(id);
String Ref = "text" + i;
editor.putString(Ref, t.getText().toString());
i++;
}
private void clearScreen() {
int[] _ids = null;
_ids = new int[]{R.id.etIdTAtividade, R.id.etNomeTAtividade, R.id.etNmAtividade};
for (int i = 0; i < (_ids.length); i++) {
EditText t = (EditText) findViewById(_ids[i]);
t.setText("");
}
}

How to get checked RadioButtons according to IDs of dynamically generated RadioGroups

I have dynamically generated multiple RadioGroups for questions which are fetched from the database and i have also assigned IDs to the RadioGroups serially.
Now, I want to get all checked RadioButtons from all the RadioGrouips so I may store them in the database.
How can I get all these RadioGroups and their checked RadioButtons pertaining to each RadioGroup ID?
Any attempt at a solution would be appreciated!
if (c.moveToFirst()) {
do {
String question = c.getString(1);
String option1 = c.getString(2);
String option2 = c.getString(3);
String option3 = c.getString(4);
// add text view
qtxt = new TextView(getApplicationContext());
counter4q += 1;
qtxt.setId(counter4q);
qtxt.setText(question);
qtxt.setTextSize(15);
qtxt.setTextColor(getResources().getColor(R.color.black, null));
ll.addView(qtxt);
questions.add(qtxt.getText().toString());
Log.d("arrayq",questions.toString());
Log.d("question", qtxt.getText().toString());
String qid = String.valueOf(counter4q);
Log.d("questionid", qid);
//create the RadioGroup
rg = new RadioGroup(getApplicationContext());
rg.setOrientation(RadioGroup.VERTICAL);
counter4rg += 1;
rg.setId(counter4rg);
rgIDs.add(String.valueOf(rg.getId()));
Log.d("arrayrgid", rgIDs.toString());
Log.d("rg", String.valueOf(rg.getId()));
String rgid = String.valueOf(counter4rg);
Log.d("rgid", rgid);
//add radio buttons
o1 = new RadioButton(getApplicationContext());
o2 = new RadioButton(getApplicationContext());
o3 = new RadioButton(getApplicationContext());
counter4rb += 1;
o1.setId(counter4rb);
Log.d("o1", o1.getText().toString());
String o1id = String.valueOf(counter4rb);
Log.d("o1id", o1id);
counter4rb += 1;
o2.setId(counter4rb);
Log.d("o2", o2.getText().toString());
String o2id = String.valueOf(counter4rb);
Log.d("o2id", o2id);
counter4rb += 1;
o3.setId(counter4rb);
Log.d("o3", o3.getText().toString());
String o3id = String.valueOf(counter4rb);
Log.d("o3id", o3id);
o1.setText(option1);
o2.setText(option2);
o3.setText(option3);
o1.setTextColor(getResources().getColor(R.color.black, null));
o2.setTextColor(getResources().getColor(R.color.black, null));
o3.setTextColor(getResources().getColor(R.color.black, null));
//the RadioButtons are added to the radioGroup instead of the layout
rg.addView(o1);
rg.addView(o2);
rg.addView(o3);
//you add the whole RadioGroup to the layout
ll.addView(rg);
} while (c.moveToNext());
}
Just do this:
int o1_Id = o1.getId();
int o2_Id = o2.getId();
int o3_Id = o3.getId();
For getting all the ids of RadioGroups create a list:
List<Integer> ids = new ArrayList<>();
then add the ids:
ids.add(counter4rg);
while getting :
for( int i=0; i< ids.size(); i++) {
System.out.println(ids.get(i));
}
To get checked RadioButtions:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int selectedId) {
RadioButton radioButton = (RadioButton) findViewById(selectedId);
//add to your list
}
});

How to set ArrayList<HashMap<String, String>> arrayList values to TextView using for loop.I am try below code

I want to set arrayList values to TextView using for loop.I am try below code....
ArrayList> getStatusFlag_values = db.getStatusFlag();
Log.i("", "getStatusFlag_values----->" + getStatusFlag_values);
int count;
for (count = 0; count < getStatusFlag_values.size(); count++) {
final LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View jobView = layoutInflater.inflate(R.layout.job_item_list_layout, null);
TextView item_name_txt = (TextView) jobView.findViewById(R.id.item_name_txt);
final TextView qnty = (TextView) jobView.findViewById(R.id.qnt);
final TextView actual_qnt = (TextView) jobView.findViewById(R.id.actual_qnty);
String _ItemName = getStatusFlag_values.get(count).get(DatabaseStaticVariable.KEY_ItemName);
String _QtyExpected = getStatusFlag_values.get(count).get(DatabaseStaticVariable.KEY_QtyExpected);
String _QtyActual = getStatusFlag_values.get(count).get(DatabaseStaticVariable.KEY_QtyActual);
Log.i("", "_ItemName_bd_values------>" + _ItemName);
Log.i("", "_QtyExpected_bd_values------>" + _QtyExpected);
Log.i("", "_QtyActual_bd_values------>" + _QtyActual);
item_name_txt.setText(_ItemName);
actual_qnty_txt.setText(_QtyExpected);
qnty_txt.setText(_QtyActual);
}
I think your code is correct, but are you adding the View you created (jobView) in your apps view?
also make sure this is done on main (Ui) thread.

how to display results that getting from json to gridview

Am getting the results as in dynamic buttons, but i want to display that in gridview.here is my code
JSONArray jsonMainNode1 = jsonResponse.getJSONArray("menu");
// JSONObject e = jsonMainNode1.getJSONObject(position);
int lengthJsonArr = jsonMainNode1.length();
for(int i=0; i <lengthJsonArr; i++)
{
JSONObject jsonChildNode = jsonMainNode1.getJSONObject(i);
String Pid = jsonChildNode.optString("pid".toString());
String Name = jsonChildNode.optString("name").toString();
String Refid=jsonChildNode.optString("refid".toString());
String resName = jsonChildNode.getString("image_url");
OutputData = Name;
str_id = Pid;
// OutputData += "pid : "+ Pid +" "+ "content : "+ Name+" ";
LinearLayout buttonContainer=(LinearLayout)findViewById(R.id.btn_container);
Button button = new Button(buttonContainer.getContext());
button.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ViewGroup.LayoutParams params = button.getLayoutParams();
button.setLayoutParams(params);
//Button new width
params.width = 64;
params.height = 64;
button.setText(OutputData);
int resId = getResources().getIdentifier(resName, "drawable", getPackageName());
button.setBackgroundResource(resId);
button.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);
button.setTextSize(13);
}
button.setTag(Refid);
button.setTextColor(Color.parseColor("#FF0000"));
buttonContainer.addView(button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout buttonContainer = (LinearLayout)findViewById(R.id.btn_container);
buttonContainer.removeAllViews();
What is the amount of data in json?
Parse json and Get your data into a model class using Gson, refer:
Unable to Loop through dynamic json string recursively in android
And pass the ArrayList of the class objects to a custom adapter for the gridview, refer:
http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html
http://www.mkyong.com/android/android-gridview-example/
http://www.technotalkative.com/android-gridview-example/

Categories

Resources