radio group is generated dynamically

This commit is contained in:
Roumen Georgiev 2018-04-25 11:03:33 +03:00
parent eef4423109
commit dda7ca3f95
2 changed files with 55 additions and 33 deletions

View file

@ -1,11 +1,15 @@
package info.nightscout.androidaps.startupwizard;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
@ -15,8 +19,14 @@ import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.NumberFormat;
import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import static info.nightscout.androidaps.startupwizard.SWItem.Type.RADIOBUTTON;
@ -49,12 +59,30 @@ public class SetupWizardActivity extends AppCompatActivity {
private View mContentView;
private LinearLayout linearLayout;
private TextView radioLabel;
private int numberOfButtons = 0;
private String[] labels;
private Context context;
private View view;
private int resLayoutId;
private String label;
private JSONArray data1;
private JSONArray data2;
private double step;
private double min;
private double max;
private NumberFormat formatter;
private Runnable save;
private LinearLayout layout;
private TextView textlabel;
private TextView screenName;
private TextView label1;
private EditText editText1;
private TextView label2;
private EditText editText2;
private Button skipButton;
//logiing
private static Logger log = LoggerFactory.getLogger(SetupWizardActivity.class);
private int currentWizzardPage = 0;
public static final String INTENT_MESSAGE = "WIZZARDPAGE";
private final Runnable mHidePart2Runnable = new Runnable() {
@ -153,7 +181,6 @@ public class SetupWizardActivity extends AppCompatActivity {
}
for(int i = 0; i < currentScreen.items.size(); i++){
SWItem currentItem = currentScreen.items.get(i);
if(currentItem.type == URL){
label1 = (TextView) findViewById(R.id.textLabel1);
editText1 = (EditText) findViewById(R.id.editText1);
@ -169,11 +196,9 @@ public class SetupWizardActivity extends AppCompatActivity {
editText2.setText(currentItem.getComment());
editText2.setVisibility(View.VISIBLE);
} else if(currentItem.type == RADIOBUTTON){
((LinearLayout) findViewById(R.id.radio_group_layout)).setVisibility(View.VISIBLE);
radioLabel = (TextView) findViewById(R.id.radio_group_label);
radioLabel.setText(currentScreen.getHeader());
// generate layout dynamically
SWRadioButton radioGroupItems = (SWRadioButton) currentItem;
addRadioButtons(radioGroupItems.labels().length, radioGroupItems.labels(), radioGroupItems.values());
addRadioButtons(currentScreen.getHeader(), radioGroupItems.labels().length, radioGroupItems.labels(), radioGroupItems.values());
}
}
@ -240,22 +265,40 @@ public class SetupWizardActivity extends AppCompatActivity {
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
public void addRadioButtons(int number, String[] labels, String[] values) {
private int itemsCount() {
return data1.length();
}
public void addRadioButtons(String label, int number, String[] labels, String[] values) {
layout = (LinearLayout) findViewById(R.id.fullscreen_content_fields);
layout.removeAllViews();
textlabel = new TextView(this);
textlabel.setText(label);
textlabel.setGravity(Gravity.START);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llp.setMargins(10, 0, 0, 0); // llp.setMargins(left, top, right, bottom);
textlabel.setLayoutParams(llp);
textlabel.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.linearBlockBackground));
TextViewCompat.setTextAppearance(textlabel, android.R.style.TextAppearance_Medium);
RadioGroup rg = new RadioGroup(this);
for (int row = 0; row < 1; row++) {
RadioGroup ll = new RadioGroup(this);
ll.setOrientation(LinearLayout.VERTICAL);
rg.setOrientation(LinearLayout.VERTICAL);
rg.setVisibility(View.VISIBLE);
for (int i = 0; i < number; i++) {
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId((row * 2) + i);
// rdbtn.setText("Radio " + rdbtn.getId());
rdbtn.setText(labels[i]);
ll.addView(rdbtn);
rg.addView(rdbtn);
}
((RadioGroup) findViewById(R.id.radiogroup)).addView(ll);
((RadioGroup) findViewById(R.id.radiogroup)).setVisibility(View.VISIBLE);
}
layout.addView(textlabel);
layout.addView(rg);
}

View file

@ -65,27 +65,6 @@
android:text="Enter your setting here"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/radio_group_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="@+id/radio_group_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="radio button group label" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
</LinearLayout>
<LinearLayout