I need to Create RadioButton Dynamically in a Radio Group, radio Group is already defined in the XML, after I made my research I found some topics to put the android:drawableRight="#android:drawable/btn_radio" and android:button="#null" in the XML how can I do them programmatically?
Here is my code :
final RadioGroup RG =(RadioGroup) vi.findViewById(R.id.RG);
RG.removeAllViews();
String[] answers= null;
answers=item.Answers.split(";");
final TextView txtTitle = (TextView) vi.findViewById(R.id.QuestionRow);
txtTitle.setText(item.Question);
for (int i=0;i<answers.length;i++){
RadioButton bt=null;
bt = new RadioButton(parent.getContext());
bt.setId(i+1);
bt.setText(answers[i]);
bt.setGravity(Gravity.RIGHT);
RG.addView(bt);
}
And here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Questionlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#color/White" >
<TextView
android:id="#+id/QuestionRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right"
android:textColor="#color/black_overlay"
android:textSize="30sp"
android:textStyle="bold" />
<RadioGroup
android:id="#+id/RG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_below="#+id/QuestionRow"
android:layout_alignRight="#+id/QuestionRow"
></RadioGroup>
<Button
android:id="#+id/Vote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/RG"
android:text="Vote" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/RG"
android:text="See Statistics" />
</RelativeLayout>
protected void onPostExecute(HashMap<String,String> mylist) {
// TODO Auto-generated method stub
super.onPostExecute(mylist);
for(Integer z=0;z<mRG.getChildCount();z++){
View o =mRG.getChildAt(z);
((RadioButton)o).setText(text[z]);
}
for(Integer z =0;z< mRG.getChildCount();z++){
Set set = mylist.entrySet();
Iterator i = set.iterator();
View o = mRG.getChildAt(z);
NumberFormat formatter = new DecimalFormat("#0.00");
if (o instanceof RadioButton){
int counted[] = new int[ mRG.getChildCount()];
int j=0;
while(i.hasNext()) {
enter code here
Map.Entry me = (Map.Entry)i.next();
String Value=String.valueOf(formatter.format(Double.valueOf(me.getValue().toString())*100/count))+"%";
if (Integer.valueOf(me.getKey().toString())==((RadioButton) o).getId())
{
((RadioButton)o).setText(text[z]+" "+ Value);
}
}
}
}
progressDialog.hide();
}
Related
I try to Add new Table rows into TableLayout in my android app. I successfully added new table rows with multiple edittexts, by following an android tutorial. Now what I try to do is, I need to get user values for those edittexts using TextWatcher. I searched a lot of tutorials and tried a lot, but i was failed.
This is how my app looks like before I add a button to this.
What I need is to Open Second row by clicking a button If user wants, and if user enters values to edittext fields they need to use and , final Result should display accordingly.
This is my previous code, I posted with a question and solved it in here .
Using Android TextWatcher with multiple user input values and Update thses values
If someone could help me, I highly appreciate your help. Thanks in advance.
Now this is my new XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Add Line" />
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Button1"
android:layout_marginTop="97dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
<TableRow
android:id="#+id/TableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="4"
android:padding="2dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="4"
android:padding="2dp" />
<EditText
android:id="#+id/editText3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="4"
android:padding="2dp" />
<TextView
android:id="#+id/TextViewsub1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:hint="$ 0.00"
android:textSize="18dip" />
</TableRow>
<TableRow
android:id="#+id/TableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="4"
android:padding="2dp" />
<EditText
android:id="#+id/editText5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="4"
android:padding="2dp" />
<EditText
android:id="#+id/editText6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="4"
android:padding="2dp" />
<TextView
android:id="#+id/TextViewsub2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:hint="$ 0.00"
android:textSize="18dip" />
</TableRow>
</TableLayout>
</RelativeLayout>
</ScrollView>
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/scrollView2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="87dp"
android:layout_marginRight="87dp"
android:hint="Rs: 0.00" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView12"
android:layout_marginEnd="86dp"
android:layout_marginRight="86dp"
android:layout_toLeftOf="#+id/textView12"
android:layout_toStartOf="#+id/textView12"
android:text="Total Value" />
</RelativeLayout>
This is now How my Java File looks like
public class NewClass extends AppCompatActivity implements View.OnClickListener {
Button btnAdd;
int counter = 0;
EditText edit1, edit2, edit3;
TextView textViewSub1, textViewResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
/*First row variables*/
edit1 = (EditText) findViewById(R.id.editText1);
edit2 = (EditText) findViewById(R.id.editText2);
edit3 = (EditText) findViewById(R.id.editText3);
textViewSub1 = (TextView) findViewById(R.id.TextViewsub1);
textViewResult = (TextView) findViewById(R.id.textView12);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_class);
//add line button
btnAdd = (Button) findViewById(R.id.Button1);
btnAdd.setOnClickListener(this);
edit1.addTextChangedListener(new LashCustomTextWatcher1());
edit2.addTextChangedListener(new LashCustomTextWatcher1());
edit3.addTextChangedListener(new LashCustomTextWatcher1());
}
public class LashCustomTextWatcher1 implements TextWatcher {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
textViewResult.setText(lashCalculate());
}
}
public void onClick(View view) {
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
TableRow tr = new TableRow(this);
//TextView tv = new TextView(this);
//tv.setText("text ");
edit1 = new EditText(this);
//edit1.setText("Hello " + counter++);
edit2 = new EditText(this);
//edit2.setText("Item no " + counter++);
edit3 = new EditText(this);
//edit3.setText("Qty no " + counter++);
textViewSub1 = new TextView(this);
tr.addView(edit1);
tr.addView(edit2);
tr.addView(edit3);
tr.addView(textViewSub1);
//tr.addView(cb);
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
public String lashCalculate() {
//declaring variables
double row1_value = 0;
DecimalFormat df = new DecimalFormat("0.00");
//calculate first row
if (!edit1.getText().toString().equals("") && !edit2.getText().toString().equals("")) {
double num1 = Double.parseDouble((edit1.getText().toString()));
double num2 = Double.parseDouble((edit2.getText().toString()));
row1_value = num1 * num2;
double num3 = 0;
if (!edit3.getText().toString().equals("")) {
num3 = Double.parseDouble((edit3.getText().toString()));
row1_value = (((100 - num3) * num2) * num1) / 100;
}
textViewSub1.setText(df.format(row1_value));
}
return df.format(row1_value);
}
}
1) First of all in parent layout xml put linearlayot as below:
<LinearLayout
android:id="#+id/parent_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
2) Than Make a layout that is want to add in every click.
3) You can now add your view in parent like below :
LinearLayout parent_layout = (LinearLayout)findViewById(R.id.parent_layout);
for(i=0;i<3;i++)
{
parentlayout.addView(myViewReview(data.get(i)));
}
public View myViewReview() {
View v; // Creating an instance for View Object
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row_facility_review, null);
TextView row_reviewer_name = (TextView) v.findViewById(R.id.row_reviewer_name);
TextView row_review_text = (TextView) v.findViewById(R.id.row_review_text);
return v;
}
As they look in the picture: http://es.tinypic.com/r/mug8b8/8
In the month of having a single column, have several with botons
The genre buttons dynamically by a query
The function that generates the buttons:
private void createEmpresas() {
Button b;
JSONObject json = null;
int count = 0;
LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
JSONObject jobj = obj_sqlite.get_descripcion_empresas();
try {
count = Integer.parseInt(jobj.getString("cont"));
json = new JSONObject(jobj.getString("json"));
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
for (int x = 1; x <= count; x++) {
try {
JSONObject json_row = new JSONObject(json.getString("row" + x));
b = new Button(this);
b.setText(json_row.getString("descripcion"));
b.setId(json_row.getInt("empresa"));
b.setTextSize(10);
b.setPadding(8, 3, 8, 3);
b.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
lm.addView(b);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Yipee.." +v.getId(), Toast.LENGTH_SHORT)
.show();
}
});
} catch (Exception e) {
Log.e("getParams", e.getMessage());
}
}
}
my xml is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg_main"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.php_mysql_sqlite.MainActivity" >
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button6"
android:layout_alignBottom="#+id/button6"
android:layout_alignParentRight="true"
android:layout_marginRight="17dp"
android:background="#null"
android:onClick="loginOut"
android:text="#string/btLogOut"
android:textColor="#color/Red" />
<Button
android:id="#+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_centerHorizontal="true"
android:onClick="syncSqliteMysql"
android:text="#string/syn" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="borrarTabla"
android:text="#string/delTable" />
<ScrollView
android:layout_width="285dp"
android:layout_height="330dp"
android:layout_marginTop="1dp" >
<LinearLayout
android:id="#+id/linearMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="#+id/button5"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button4"
android:background="#color/White"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Thanks for the help
PS: If you give me negative points, leave a comment of reason, as has happened before and not why
here are some answers that may help
1-
In android how do I add buttons into a TableLayout from an array of buttons programmatically?
2-
How do I programmatically add buttons into layout one by one in several lines?
3-
Add an array of buttons to a GridView in an Android application
Im having some issues, i have two Groups of Radiobuttons in my XML which has handling in an activity. It just crashes with nullpoint exception on line 48:
int languangeId = languageGroup.getCheckedRadioButtonId();
I have set a "default" checked button in the XML to one of the buttons in the buttongroup,
So why doesnt it get an valid value?:(
public class FirstTimeSelectMenu extends Activity{
private RadioGroup languageGroup;
private RadioGroup storageGroup;
private Button okButton;
private String getStorage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_time_run);
languageGroup = (RadioGroup)this.findViewById(R.id.languageGroup);
storageGroup = (RadioGroup)this.findViewById(R.id.storageGroup);
okButton = (Button)findViewById(R.id.okBtn);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int languangeId = languageGroup.getCheckedRadioButtonId();
int storageId = storageGroup.getCheckedRadioButtonId();
RadioButton languageb = (RadioButton)findViewById(languangeId);
RadioButton storageb = (RadioButton)findViewById(storageId);
if(languageb.equals("Norwegian")){
//Need to fix this!
}
if(languageb.equals("English")){
//Need to fix this!
}
if(storageb.equals("SD Card")){
String sdStoragePath = Environment.getExternalStorageDirectory().toString() + "/PictureTalk/";
FileInteraction fi = new FileInteraction();
fi.firstTimeFillPath(getResources(), "PictureTalk/Food", sdStoragePath +"PictureTalk/Food");
fi.firstTimeFillPath(getResources(), "PrivatePictures", Environment.getExternalStorageDirectory().toString() +"PrivatePictures");
Intent intent = new Intent(getApplicationContext(),MainMenuActivity.class);
intent.putExtra("dataStorePath",sdStoragePath);
startActivity(intent);
}
}
});
}}
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/languagetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="236dp"
android:text="Choose language" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/languagetext"
android:layout_alignRight="#+id/languagetext"
android:layout_below="#+id/languageGroup">
<RadioButton
android:id="#+id/norwegianRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Norwegian" />
<RadioButton
android:id="#+id/englishRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English" />
</RadioGroup>
<TextView
android:id="#+id/internal_sd"
android:layout_width="464dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="236dp"
android:text="Do you want to store the data to:" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="#+id/storageGroup">
<RadioButton
android:id="#+id/sdcardRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:checked="true"
android:text="SD card" />
<RadioButton
android:id="#+id/internalRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Internal storage" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/okBtn"
android:layout_gravity="bottom" />
To start off with, what element is assigned the id "#+id/languageGroup" since it doesn't appear to be declared in your XML
I need to have a series of text views wrap within a single cell on a table but I cannot figure out how to make the text views stay in one cell and wrap depending on if the whole text view will fit on the same line. Each category name is a link to another activity and each event text view is a link to another activity. This table is built dynamically also so that may provide some restrictions.
Here is a test layout I have tried to make to figure out what settings need to be set.
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Category"
android:id="#+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Events"
android:id="#+id/textView4" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="A"
android:id="#+id/textView5" />
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Event 1"
android:id="#+id/textView6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=", Event 2"
android:id="#+id/textView7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView8"
android:text=", Event 3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=", Event 4"
android:id="#+id/textView9" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=", Event 5"
android:id="#+id/textView12"
android:singleLine="true" />
</TableRow>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="B"
android:id="#+id/textView10" />
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Event 1"
android:id="#+id/textView11" />
</TableRow>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="C"
android:id="#+id/textView"
/>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tableRow">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Event 1"
android:id="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=", Event 2"
android:id="#+id/textView13" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView14"
android:text=", Event 3"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=", Event 4"
android:id="#+id/textView15" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=", Event 5"
android:id="#+id/textView16"
android:singleLine="true" />
</TableRow>
</TableRow>
</TableLayout>
Update:
Still after a week of fiddling I am still not able to get my table to display correctly. I am getting all of my individual eruptions parsed out correctly but still can't get them to wrap to a new line like they do on our webpage when the number of eruptions for a single geyser exceeds the number that can fit on that line. Here is the link to that table that we use on the web interface (if you change your browser size you will understand what I am after with the android app.) http://geysertimes.org/retrievedailylog.php?date=2013-06-24 . and here is my code that builds the table currently for the app.
List<Eruption> alleruptionssum = null;
try {
alleruptionssum = db.getDaySummary(CurDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
String GeyserOLD = "";
String timeSTR = "";
long GeyserIDOLD = 0;
long numEruptions = 0;
if (alleruptionssum != null) {
if(alleruptionssum.size() !=0){
for (Eruption eruption : alleruptionssum) {
int Live;
long GeyserID;
long EruptionID;
long EruptionUnixTime;
long EruptionUnixTimeMilli;
String EruptionRegTime;
String EruptionTimeDisplay;
String TimeInfo;
String Geyser;
Live = eruption.getLive();
GeyserID = eruption.getGeyserID();
EruptionID = eruption.getId();
Geyser = eruption.getGeyser();
EruptionUnixTime = eruption.getEruptionUnixtimeTime();
EruptionUnixTimeMilli = EruptionUnixTime * 1000;
Date EruptionDate = new Date(EruptionUnixTimeMilli);
SimpleDateFormat EruptionTimeFormatter = new SimpleDateFormat("HHmm");
EruptionTimeFormatter.setTimeZone(TimeZone.getTimeZone("US/Mountain"));
EruptionRegTime = EruptionTimeFormatter.format(EruptionDate);
TimeInfo = eruption.getTimeInfo();
EruptionTimeDisplay = EruptionRegTime + TimeInfo;
if(!GeyserOLD.equals(Geyser)){
TableRow trGeyser = new TableRow(this);
//Set geyser text
TextView tvGeyser = new TextView(this);
tvGeyser.setPaintFlags(tvGeyser.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
tvGeyser.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
tvGeyser.setTextColor(Color.parseColor("#0000EE"));
tvGeyser.setBackgroundResource(R.drawable.cell_left_middle);
tvGeyser.setPadding(4, 2, 4, 2);
tvGeyser.setText(GeyserOLD);
final long finalGeyserID = GeyserIDOLD;
tvGeyser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("GeyserID", String.valueOf(finalGeyserID));
link_recent_geyser_info(finalGeyserID);
}
});
trGeyser.addView(tvGeyser);
if(!timeSTR.equals("")){
LinearLayout llEruptions = new LinearLayout(this);
llEruptions.setBackgroundResource(R.drawable.cell_right_middle);
llEruptions.setPadding(4,2,4,2);
String[] eruptions = timeSTR.split(";");
for ( long j = 0; j < numEruptions; j++ ) {
String[] ParsedEruptions = eruptions[(int) j].split(",");
long PostErupionID = Long.parseLong(ParsedEruptions[0]);
String PostEruptionDisplay = ParsedEruptions[1];
int PostLive = Integer.parseInt(ParsedEruptions[2]);
TextView comma = new TextView(this);
comma.setText(", ");
comma.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
if(j != 0){
llEruptions.addView(comma);
}
TextView tvTime = new TextView(this);
tvTime.setText(PostEruptionDisplay);
tvTime.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
tvTime.setPaintFlags(tvTime.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
if(PostLive == 0){
tvTime.setTypeface(null, Typeface.ITALIC);
tvTime.setTextColor(Color.parseColor("#FFBD00"));
}else{
tvTime.setTextColor(Color.parseColor("#0000EE"));
}
final long finalEruptionID = PostErupionID;
tvTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("EruptionID", String.valueOf(finalEruptionID));
link_eruption_detail(finalEruptionID);
}
});
llEruptions.addView(tvTime);
}
trGeyser.addView(llEruptions);
SummaryDataView.addView(trGeyser);
}
GeyserOLD = Geyser;
GeyserIDOLD = GeyserID;
timeSTR = EruptionID+","+EruptionTimeDisplay+","+Live;
numEruptions = 1;
}else{
//add 1 to eruption counter for current geyser and add the eruption to the string
numEruptions = numEruptions+1;
timeSTR = timeSTR + ";" + EruptionID+","+ EruptionTimeDisplay+","+Live;
}
}
TableRow trGeyser = new TableRow(this);
//Set geyser text
TextView tvGeyser = new TextView(this);
tvGeyser.setTextColor(Color.parseColor("#0000EE"));
tvGeyser.setBackgroundResource(R.drawable.cell_left_bottom);
tvGeyser.setPadding(4, 2, 4, 2);
tvGeyser.setPaintFlags(tvGeyser.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
tvGeyser.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
tvGeyser.setText(GeyserOLD);
trGeyser.addView(tvGeyser);
LinearLayout llEruptions = new LinearLayout(this);
llEruptions.setBackgroundResource(R.drawable.cell_right_bottom);
llEruptions.setPadding(4,2,4,2);
String[] eruptions = timeSTR.split(";");
for ( long j = 0; j < numEruptions; j++ ) {
String[] ParsedEruptions = eruptions[(int) j].split(",");
long PostErupionID = Long.parseLong(ParsedEruptions[0]);
String PostEruptionDisplay = ParsedEruptions[1];
int PostLive = Integer.parseInt(ParsedEruptions[2]);
//add comma between eruptions
TextView comma = new TextView(this);
comma.setText(", ");
comma.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
if(j != 0){
llEruptions.addView(comma);
}
TextView tvTime = new TextView(this);
tvTime.setText(PostEruptionDisplay);
tvTime.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
if(PostLive == 0){
tvTime.setTypeface(null, Typeface.ITALIC);
tvTime.setTextColor(Color.parseColor("#FFBD00"));
}
final long finalEruptionID = PostErupionID;
tvTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
link_eruption_detail(finalEruptionID);
}
});
llEruptions.addView(tvTime);
}
trGeyser.addView(llEruptions);
SummaryDataView.addView(trGeyser);
}
}
quiz_list.xml : My xml file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#43bd00"
android:textSize="20sp"
android:textStyle="bold"
android:paddingTop="4dip"
android:paddingBottom="1dip" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Radio Group"
>
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="bottom|center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/loadPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:text="Load Previous"
android:visibility="invisible" />
<Button
android:id="#+id/loadNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:text="Load Next" />
</LinearLayout>
I have a list of questions and radio buttons have the options for it.
The problems that I face are
1. When i load the next list a button is already selected.
2. When i load the previous list a different button is selected instead of the one i selected.
This is my code containing the questions as a json.
String json = "[{\"question\": \"What?\", \"option1\": \"alpha\" , \"option2\": \"beta\" , \"option3\": \"gamma\" , \"option4\": \"alpha\"},{\"question\": \"What is your name ?\", \"option1\": \"Dinesh\" , \"option2\": \"Boopesh\" , \"option3\": \"Srinath\" , \"option4\": \"JK\"},{\"question\": \"What is the capital of India?\", \"option1\": \"Delhi\" , \"option2\": \"Bombay\" , \"option3\": \"Calcutta\" , \"option4\": \"Chennai\"}]";
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(json).getAsJsonArray();
for(JsonElement obj : array) {
Quiz quiz = gson.fromJson(obj,Quiz.class);
String question = quiz.getQuestion();
System.out.println("Question: "+question);
quizList.add(quiz);
}
I'm adding the questions using a customadapter.
How can i change it to make it work ??
Any Ideas ??? Pls let me know...
Thanks Nishit.
try this
RadioGroup radioGroup;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
boolean hack = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioButton1 = (RadioButton) findViewById(R.id.r1);
radioButton2 = (RadioButton) findViewById(R.id.r2);
radioButton3 = (RadioButton) findViewById(R.id.r3);
OnClickListener radioClickListener = new OnClickListener()
{
public void onClick(View v)
{
if (v.getId() == radioGroup.getCheckedRadioButtonId() && hack)
{
radioGroup.clearCheck();
}
else
{
hack = true;
}
}
};
OnCheckedChangeListener radioCheckChangeListener = new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
hack = false;
}
};
radioButton1.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton2.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton3.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton1.setOnClickListener(radioClickListener);
radioButton2.setOnClickListener(radioClickListener);
radioButton3.setOnClickListener(radioClickListener);
}