What i would like is when i select a date it filters the rss feed to display the roadworks on the day that is selected. Only for now i start an intent to take me to the next activity which displays all the roadworks and not the ones on the day selected. How would i go about this?
public class ChooseDate extends Activity {
RelativeLayout rl;
Button pick;
DatePicker date;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_picker);
rl = (RelativeLayout) findViewById(R.id.rl);
pick = (Button) findViewById(R.id.button1);
date = new DatePicker (ChooseDate.this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
date.setLayoutParams(params);
rl.addView(date);
pick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Date is : " +
date.getDayOfMonth() + "-"+(date.getMonth()+1) +
"-"+ date.getYear(), Toast.LENGTH_SHORT).show();
startActivity(new Intent("com.mobilerwts.robert.MainActivityOther"));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If I understand this right, you want to pass the date from the first activity to the second. If you are going to do that, you need to include it in the Intent. Basically, you have to include the data in the intent. See How do I pass data between Activities in Android application? for how to do that.
Related
I am trying to allow people to input hypothetical new loan information in to editText fields. Upon clicking on button "Calculator" I want it to calculate the monthly payment.
Below is the code on the calculator.java page I have.
public class AddDebt extends Activity implements OnClickListener {
Button Home;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_debt);
}
This part is for my Menu to switch between my pages (Activities):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.default_menu, menu);
MenuItem newDebt = menu.findItem(R.id.newDebt);
Intent intent1 = new Intent(this, AddDebt.class);
newDebt.setIntent(intent1);
MenuItem About = menu.findItem(R.id.about);
Intent intent2 = new Intent(this, About.class);
About.setIntent(intent2);
MenuItem Calcu = menu.findItem(R.id.Calcu);
Intent intent3 = new Intent(this, Calculator.class);
Calcu.setIntent(intent3);
MenuItem Manu = menu.findItem(R.id.manu);
Intent intent4 = new Intent(this, ManuPage.class);
Manu.setIntent(intent4);
return true;
}
I have "android:onClick="ButtonOnClick"" declared in my .xml on the Calculator button:
public void ButtonOnClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.calculate:
EditText myEdit = (EditText) findViewById(R.id.editBalance);
String myEditValue = myEdit.getText().toString();
int myEditBal = Integer.parseInt(myEditValue);
EditText myEdit2 = (EditText) findViewById(R.id.editRate);
String myEditValue2 = myEdit2.getText().toString();
int myEditRate = Integer.parseInt(myEditValue2);
EditText myEdit3 = (EditText) findViewById(R.id.editTerm);
String myEditValue3 = myEdit3.getText().toString();
int myEditTerm = Integer.parseInt(myEditValue3);
Also FYI: I know this is not a valid loan calculation, I'm using it to ensure I get a valid number:
int editMnthlypmt = myEditBal + myEditRate + myEditTerm;
TextView textMnthlypmt = new TextView(this);
textMnthlypmt.setText("" + String.valueOf(editMnthlypmt));
break;
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
I can input the data; however, it does not populate the answer in to my TextView field "textMnthlypmt". Any ideas?
textMnthlypmt.setText("" + String.valueOf(editMnthlypmt));
Try this...
check out you xml once again and
use a already declared text view
TextView textMnthlypmt = new TextView(this);
with this code you are creating a new text view but not putting it on activity.
You have created an instance of TextView but you haven't added to parentView...
try like this..
TextView textMnthlypmt = new TextView(this);
textMnthlypmt.setText("" + String.valueOf(editMnthlypmt));
parentView.addView(textMnthlypmt);//where parentView is parent layout defined in layout.activity_new_debt
Hope this helps..
im having problems with a simple task.
I have 2 Buttons and i want only one to be visible at same time, so when you touch one it hides and the other appears.
This is my code:
fromAnex = new Button(this);
fromAnex.setText("from");
fromAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "from anex");
returnFromAnex();
}
});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF,plano.getId());
params.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(fromAnex,params);
fromAnex.setVisibility(View.GONE);
toAnex = new Button(this);
toAnex.setText("to");
toAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "Show anex");
showAnex();
}
});
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.RIGHT_OF,plano.getId());
params2.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(toAnex,params2);
private void showAnex()
{
fromAnex.setVisibility(View.VISIBLE);
toAnex.setVisibility(View.GONE);
private void returnFromAnex()
{
fromAnex.setVisibility(View.GONE);
toAnex.setVisibility(View.VISIBLE);
I really cant understand why but that first call to fromAnex.setVisibility(View.GONE); is working as expected, the first call to showAnex() at fromAnex.setVisibility(View.VISIBLE);
is working too but toAnex.setVisibility(View.GONE); right under doesnt work.
And after that nothing happens with buttons visibility when i touch.
Someone can see whats wrong with my code?
Sorry for my bad english and thanks.
EDIT:
That two methods arent complete, the rest are not related with this buttons visibility problem.
When i set visibility to gone just after creating the button it works but then i cant set visibility to gone again, thats the problem.
Taking your snippet I tried run the following and it works perfectly.I guess you are making some small mistake some where as DevTest said.
public class MainActivity extends Activity {
Button fromAnex,toAnex;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rl=new RelativeLayout(getApplicationContext());
RelativeLayout.LayoutParams relPra=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rl.setLayoutParams(relPra);
fromAnex = new Button(this);
fromAnex.setText("from");
fromAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "from anex");
returnFromAnex();
}
});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(fromAnex,params);
fromAnex.setVisibility(View.GONE);
fromAnex.setId(1);
toAnex = new Button(this);
toAnex.setId(2);
toAnex.setText("to");
toAnex.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONClickListener", "Show anex");
showAnex();
}
});
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.RIGHT_OF,fromAnex.getId());
params2.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(toAnex,params2);
setContentView(rl);
}
private void showAnex()
{
fromAnex.setVisibility(View.VISIBLE);
toAnex.setVisibility(View.GONE);
}
private void returnFromAnex()
{
fromAnex.setVisibility(View.GONE);
toAnex.setVisibility(View.VISIBLE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I'm working on rendering a text on textview. I'm trying to add two buttons to zoom in and out the text inside text view and the code goes like this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter_view);
String chapter;
chapter=getIntent().getExtras().getString("ChapterName");
final StringBuffer buffer = new StringBuffer();
int id= getResources().getIdentifier(chapter,"raw",getPackageName());
try{
DataInputStream dataIO= new DataInputStream(getResources().openRawResource(id));
String strLine= null;
while((strLine = dataIO.readLine())!=null){
buffer.append(strLine);
buffer.append("\n");
}
dataIO.close();
}catch(Exception e){
}
final TextView tv=(TextView) findViewById(R.id.chapter);
tv.setText(buffer.toString());
Button zoomIn = (Button) findViewById(R.id.zoomin);
zoomIn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textSize = tv.getTextSize();
tv.setTextSize((float) (textSize+0.25));
}
});
Button zoomOut = (Button) findViewById(R.id.zoomout);
zoomOut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textSize = tv.getTextSize();
tv.setTextSize((float) (textSize-0.25));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.chapter_view, menu);
return true;
}
}
But the problem I'm getting is the even upon clicking zoom out button, it still increasing the font size of the text. Please help me out in this. Also once I close one chapter and opens another, the text size will reset to its default value. Are there any solutions regarding this. I'm thinking of using the namevalue pair for this solution.
The problem is that the unit passed to setTextSize(float size) is in scaled pixels, whereas getTextSize() reports in pixels. Try using setTextSize(int unit, float size) instead, setting unit to TypedValue.COMPLEX_UNIT_PX.
I've tried a sample at my machine. Check the code below. You have to place the variable at the right place with the scope.
public class MainActivity extends Activity {
float textSize;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView)findViewById(R.id.textView1);
textSize = tv.getTextSize();
Button btnPlus = (Button)findViewById(R.id.button1);
btnPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.textView1);
Log.v("TextSizeP", String.valueOf(textSize));
textSize = (float) (textSize+0.25);
tv.setTextSize(textSize);
Log.v("TextSizeP", String.valueOf(textSize));
}
});
Button btnMinus = (Button)findViewById(R.id.button2);
btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.textView1);
Log.v("TextSize", String.valueOf(textSize));
textSize = (float) (textSize-0.25);
tv.setTextSize(textSize);
Log.v("TextSize", String.valueOf((float) (textSize-0.25)));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
For example I have my textSize variable declared at the class level. And the textSize variable is increased and decreased when the relevant button is clicked.
This works fine for me.
As for the size that returns to its default , you will have to use shared preferences to save the value of the last text size .
As for the text size , why don't you increment the value one by one , for instance tv.setTextSize(textsize++) when zoomOut is clicked
or tv.setTextSize(textsize--) when zoomIN is clicked
public class MainActivity extends Activity {
TextView tv;
Button in,out;
static float textSize;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,30);
textSize = tv.getTextSize();
in = (Button) findViewById(R.id.zoomin);
out = (Button) findViewById(R.id.zoomout);
in.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textSize = (float)(textSize + 2);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
}
});
out.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textSize = (float)(textSize - 2);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Firstly, this is my first question here, Thanks for any reply:)
I have a fragment extends SherlockListFragment that has a list of (events) saved in database, i want when i click on any item in the list , to get all event data and fill in another activity that has editText(s) ....
this is the frgment.java code
public class EventsFragment extends SherlockListFragment {
EditText _eventName_;
EditText next_activty_text;
List<String> presidents = new ArrayList<String>();
public DBAdapter db;
String buf = "";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_events, container, false);
_eventName_ = (EditText) v.findViewById(R.id.editText11);
next_activty_text = (EditText) v.findViewById(R.id.editText12);
// The most Important part in action bar menus :)
setHasOptionsMenu(true);
/*
//This is how to link a button in fragment by its .xml file :Ds
Button create_btn = (Button) v.findViewById(R.id.button1);
create_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//startActivity(new Intent("com.nazmy.CreateEventActivity"));
}
});*/
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
db = new DBAdapter(getActivity());
db.open();
int i = 1;
while(db.getEvent(i).isLast()){
Cursor c = db.getEvent(i);
presidents.add(c.getString(1));
i++;
}
db.close();
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, presidents));
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, presidents));
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_events_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.add:
startActivity(new Intent("com.nazmy.CreateEventActivity"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
//startActivity(new Intent("com.nazmy.CreateEventActivity"));
db = new DBAdapter(getActivity());
db.open();
Cursor c = db.getEvent(id+1);
//populate(c.getString(1));
_eventName_.setText(c.getString(1));
db.close();
}
}
`
and this is the activity code
public class CreateEventActivity extends FragmentActivity {
EditText mEdit;
EditText mEdit2;
EditText mEditFromTime;
EditText mEditToTime;
int whichDateBtn;
int whichTimeBtn;
DBAdapter db = new DBAdapter(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
db.open();
Cursor c = db.getAllEvents();
if (c.moveToFirst())
{
do {
DisplayEvent(c);
} while (c.moveToNext());
}
db.close();
Button cancel_btn = (Button) findViewById(R.id.button4);
Button save_btn = (Button) findViewById(R.id.button1); //if clicked -> save in data-base
save_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
db.open();
EditText event_name = (EditText)findViewById(R.id.editText12);
EditText event_place = (EditText)findViewById(R.id.editText13);
EditText from_date = (EditText)findViewById(R.id.editText14);
EditText from_time = (EditText)findViewById(R.id.editText15);
EditText to_date = (EditText)findViewById(R.id.EditText16);
EditText to_time = (EditText)findViewById(R.id.EditText17);
CheckBox share = (CheckBox)findViewById(R.id.checkBox1);
CheckBox alarm = (CheckBox)findViewById(R.id.checkBox2);
long id2 = db.insertEvent(event_name.getText().toString(), event_place.getText().toString(),
from_date.getText().toString(), to_date.getText().toString(),
from_time.getText().toString(),
to_time.getText().toString(),
share.isChecked(),alarm.isChecked());
db.close();
event_name.setText("");
event_place.setText("");
from_date.setText("");
to_date.setText("");
from_time.setText("");
to_time.setText("");
if (share.isChecked()) {
share.setChecked(false);
}
if (alarm.isChecked()) {
alarm.setChecked(false);
}
startActivity(new Intent("com.nazmy.HomeActivity"));
}
});
cancel_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.nazmy.HomeActivity"));
}
});
}
//This method shows the dialog when Button is clicked
public void selectFromtDate(View view) {
DialogFragment mDialog = new SelectDateFragment();
mDialog.show(getFragmentManager(), "DatePicker");
whichDateBtn = 0;
}
public void selectToDate(View view) {
DialogFragment mDialog2 = new SelectDateFragment();
mDialog2.show(getFragmentManager(), "DatePicker");
whichDateBtn = 1;
}
//This method will update the EditText field with the following code.
public void populateSetDate(int year, int month, int day) {
mEdit = (EditText)findViewById(R.id.editText14); //brbt el editText (mEdit) be el text ely hayzhar fel text Field
mEdit2 = (EditText)findViewById(R.id.EditText16);
if(whichDateBtn==0) {
mEdit.setText(month+"/"+day+"/"+year);
}
else if (whichDateBtn == 1) {
mEdit2.setText(month+"/"+day+"/"+year);
}
}
//This subclass includes method to display the datepicker fragment to the user.
//It also has the method to handle the event on setting the date.
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
//The onCreateDialog() method creates a calendar object.
//Using this object the current day, month and year that will be retrieved.
//This current instance will return to the activity to display the date picker with the current date by default.
//onDateSet() method calls the populateSetDate() with the selected date parameters.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm+1, dd);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Time Pickers
////////////////////////////////////////////////////////////////////////////////////////////////////////
public void selectFromTime(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "TimePicker");
whichTimeBtn = 0;
}
public void selectToTime(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "TimePicker");
whichTimeBtn = 1;
}
public void populateSetTime(int hour, int min) {
mEditFromTime = (EditText)findViewById(R.id.editText15); //brbt el editText (mEdit) be el text ely hayzhar fel text Field
mEditToTime = (EditText)findViewById(R.id.EditText17);
if(whichTimeBtn==0) {
mEditFromTime.setText(hour+":"+min);
}
else if (whichTimeBtn == 1) {
mEditToTime.setText(hour+":"+min);
}
}
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
populateSetTime(hourOfDay, minute);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_create_event, menu);
return true;
}
public void DisplayEvent(Cursor c)
{
Toast.makeText(this,
"id : " + c.getString(0) + "\n" +
"Event Name: " + c.getString(1) + "\n" +
"Place : " + c.getString(2) + "\n" +
"From Date : " + c.getString(3)+ "\n" +
"From Time : " + c.getString(5)+ "\n" +
"To Date : " + c.getString(4)+ "\n" +
"To Time : " + c.getString(6),
Toast.LENGTH_LONG).show();
}
}
`
Thanks in advance :)
You have two choices. Either fetch the data from your database in EventsFragment.onListItemClick(), and pass that data to your second Activity as Intent extras. Or you can pass the id of the clicked item, and do the database operations in your second Activity. Either way you have to pass data using Intents
public class EventsFragment extends SherlockListFragment {
.
.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(this, CreateEventActivity.class);
// OPTION 1: Fetch data here, and pass it with the intent
db = new DBAdapter(getActivity());
db.open();
Cursor c = db.getEvent(id+1);
intent.putExtra("id", c.getString(1));
intent.putExtra("event_name", c.getString(2));
// Do this for all your values.
db.close();
startActivity(intent);
// OPTION 2: Pass the id of the selected item, and fetch the data in second activity
intent.putExtra("selected_id", id+1);
startActivity(intent);
}
}
In CreateEventActivity you then have to get the extras you chose to pass with the Intent.
#Override
protected void onCreate(Bundle savedInstanceState) {
.
.
Intent intent = getIntent();
// If you chose option 1, you have to get all the extras you attached to the Intent.
long id = intent.getLongExtra("id", -1)
String eventName = intent.getStringExtra("event_name", "defValue");
// Fetch all the values here..
// If you chose option 2, you fetch the id and do the database operations to retrive
// the data
long id = intent.getLongExtra("selected_id", -1);
// DB stuff here..
}
I need to create a number of check boxes programmatically using Java, check if one of them is checked and when I click Next Button the control will move to next page. If none of them is checked a toast message has to be displayed. Please suggest as I am new to Android.
Here is my code:
public class TestValidCheckboxActivity extends Activity implements OnCheckedChangeListener{
private RelativeLayout layoutMiddle = null;
private TableLayout layout1;
private CheckBox chk;
private String[] resarr = {"silu", "pinky", "meera"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layoutMiddle = (RelativeLayout)findViewById(R.id.layoutMiddleUp);
layout1 = (TableLayout)findViewById(R.id.tableId1);
final Button btnNext = (Button)findViewById(R.id.btnNext);
int i = 0;
for(String res: resarr){
TableRow tr=new TableRow(this);
chk=new CheckBox(this);
chk.setId(i);
chk.setText(res);
chk.setTextColor(Color.BLACK);
tr.addView(chk);
i++;
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
lay.addRule(RelativeLayout.BELOW, layoutMiddle.getId());
layout1.addView(tr, lay);
}
chk.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (arg1)
{
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(final View view) {
Log.e("1111111111","111111111");
if(chk.isChecked()){
Intent intent = new Intent(view.getContext(),NextPage.class);
startActivityForResult(intent, 0);
}else{
Toast msg = Toast.makeText(view.getContext(), "please choose at least one option", Toast.LENGTH_LONG);
msg.show();
}
}
});
}else{
Toast msg = Toast.makeText(TestValidCheckboxActivity.this, "please choose at least one option", Toast.LENGTH_LONG);
}
}
});
}
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
}
}
this can be easily done
take a boolean flag and set true it on checkbox.isChecked();(inbuilt)
then check if flag is set then navigate to next else TOAST
Can you show us some of your layout code, meaning, are the checkboxes defined in an xml file and do they have IDs?
If they do, you can access each of them with findViewByID and check their status one by one.