Horizontalscrollview: imageView disappear when coming back from another Activity - java

I'm developing my very first app and I have an issue with ImageViews and Horizontalscrollview: I want to collect all the goals reached by the user adding images inside an Horizontalscrollview (goal reached --> new image displayed).
how 4 completed goals are displayed
They show properly and everything seems to work but when I open another activity and then I come back, they disappear. I tried to invalidate the layout but nothing, am I missing something?
Thank you in advance!
This is my XML file
<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main3Activity">
<HorizontalScrollView
android:id="#+id/horizontal_scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp">
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
This is my Java code
The function I use to check if a goal is completed:
<!-- language: lang-java -->
protected void checkGoal(){
int progress = calculateProgress();
if(goalPref.getInt("Goal",0)!=0){
if(progress >= goalPref.getInt("Goal", 0)) {
int id = layout.getChildCount();
addNewImage(id); // <-- here I add the image
question.setText(R.string.setgoal);
updateGoalPref(0);
if (list.size() != 0) {
list = new ArrayList<>();
}
updateProgress();
}
}
}
The function I use to add images:
<!-- language: lang-java -->
protected void addNewImage(int id){
ImageView imageView = new ImageView(this);
imageView.setId(id);
imageView.setPadding(2, 2, 2, 2);
imageView.setBackgroundResource(R.drawable.check);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
layout.addView(imageView, id);
}

When you go to another activity and come back, you should call addNewImage in onResume to show the images again:
#Override
protected void onResume() {
super.onResume();
addNewImage(id);
}
If the number of goals in your activity is limited, why don't you create those ImageViews in XML and set their Visibility in java?

Since I'm pretty stubborn, I managed to find another way to do it and this is what I did: using the code I posted I added a counter to keep track of how many goals the user reached and onResume() I display as many images as the counter value.
In this way, the user can see immediately the effect of reaching a goal and the activity doesn't lose anything.
Then, I implemented it to show a description of the goal, changing the ImageViews in TextViews with those images as backgrounds. Instead of using a counter, I saved the strings I wanted to display in an ArrayList and used its size as a counter.
Hope it can be useful for someone!
<!-- language: lang-java -->
protected void checkGoal(){
int progress = calculateProgress();
if(goalPref.getInt("Goal",0)!=0){
if(progress >= goalPref.getInt("Goal", 0)) {
String newString = goalPref.getInt("Goal", 0) + "€, " + MainActivity.currentDate();
goalsCompleted.add(newString);
updateGoalCompletedList();
//Here I clean the layout and display all the TextViews
layout.removeAllViews();
for(int i=goalsCompleted.size()-1; i>0; i--){
addNewTextView(i);
}
question.setText(R.string.setgoal);
updateGoalPref(0);
if (list.size() != 0) {
list = new ArrayList<>();
}
updateProgress();
}
}
}
//The function used to display the TextViews
protected void addNewTextView(int id){
TextView textView = new TextView(this);
textView.setBackgroundResource(R.drawable.check1);
textView.setId(id);
textView.setPadding(25,260,0,0);
textView.setTextSize(12);
textView.setText(goalsCompleted.get(id));
layout.addView(textView);
}
//The function used to update and save the array
public void updateGoalCompletedList(){
Gson gson3 = new Gson();
jsonItems3 = gson3.toJson(goalsCompleted);
SharedPreferences.Editor listEd = goalsCompletedPref.edit();
listEd.putString("GoalsCompleted", jsonItems3).apply();
}
//The function used to resume the array, you can call it on onResume() or onCreate()
public void resumeGoalsCompleted(){
Gson gson3 = new Gson();
String savedList = goalsCompletedPref.getString("GoalsCompleted", "");
goalsCompleted = gson3.fromJson(savedList, new TypeToken<ArrayList<String>>() {
}.getType());
if(goalsCompleted==null)
goalsCompleted = new ArrayList<>();
//From the most recent to the least recent
for(int i=goalsCompleted.size()-1; i>0; i--){
addNewTextView(i);
}
}

Related

Using Street View VR in my app

I'm using Android Studio and trying to show some chosen Street View paths in VR. I already have Street View running well and now I'm trying to show it in VR.
I have put the com.google.vr.sdk.widgets.pano.VrPanoramaView in the layout and, inside onCreate in my class, referenced it to a VrPanoramaView variable through findViewById. Now I'm trying to show an image calling a method which I've defined in this class, loadPanoImage. This method loads an image from the storage and shows it through loadImageFromBitmap.
The problem is that it isn't able to show anything, even though I've followed a guide and I've done everything as showed. I've even tryed calling it in different parts of the code (before doing any other action, on clicking a button, before and after showing streetview) but I can't understand why it isn't working and how will I be able to use it to show images taken from StreetView (I don't know if I will be able to do it dinamically or I should download them and put them in the storage).
I'm putting part of the code for reference:
public class VrExperience extends FragmentActivity {
Button buttonCitta;
Button buttonMare;
Button buttonMontagna;
TextView titleTextView;
// George St, Sydney
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
// LatLng with no panorama
private static final LatLng INVALID = new LatLng(-45.125783, 151.276417);
//VrPanoramaView is inserted in the layout
private VrPanoramaView panoWidgetView;
//StreetViewPanorama is another class in my project which shows Street View
private StreetViewPanorama mStreetViewPanorama;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vrexperiences);
panoWidgetView = (VrPanoramaView) findViewById(R.id.pano_view);
panoWidgetView.setEventListener(new VrPanoramaEventListener());
//download image and show it, but it doesn't show anything
loadPanoImage();
titleTextView = (TextView) findViewById(R.id.titleTextView);
buttonCitta = (Button) findViewById(R.id.buttonCitta);
buttonCitta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!checkReady()) {
return;
}
titleTextView.setVisibility(View.GONE);
buttonCitta.setVisibility(View.GONE);
buttonMare.setVisibility(View.GONE);
buttonMontagna.setVisibility(View.GONE);
loadPanoImage(); //it doesn't show anything
mStreetViewPanorama.setPosition(SYDNEY);
loadPanoImage(); //it doesn't show anything
}
}};
//code for buttonMontagna and buttonMare as well, it's identical
SupportStreetViewPanoramaFragment streetViewPanoramaFragment =
(SupportStreetViewPanoramaFragment)
getSupportFragmentManager().findFragmentById(R.id.streetviewpanorama);
streetViewPanoramaFragment.getStreetViewPanoramaAsync(
new OnStreetViewPanoramaReadyCallback() {
#Override
public void onStreetViewPanoramaReady(StreetViewPanorama panorama) {
mStreetViewPanorama = panorama;
// Only set the panorama to INVALID on startup (when no panoramas have been
// loaded which is when the savedInstanceState is null).
if (savedInstanceState == null) {
mStreetViewPanorama.setPosition(INVALID);
}
}
});
}
/**
* When the panorama is not ready the PanoramaView cannot be used. This should be called on
* all entry points that call methods on the Panorama API.
*/
private boolean checkReady() {
if (mStreetViewPanorama == null)
return false;
return true;
}
/**
* Called when the Animate To Invalid button is clicked.
*/
public void onGoToInvalid(View view) {
if (!checkReady()) {
return;
}
mStreetViewPanorama.setPosition(INVALID);
}
//retrieves image from the assets folder and loads it into the VrPanoramaView
private void loadPanoImage() {
VrPanoramaView.Options options = new VrPanoramaView.Options();
InputStream inputStream = null;
AssetManager assetManager = getAssets();
try {
inputStream = assetManager.open("demo2.jpg");
options.inputType = VrPanoramaView.Options.TYPE_MONO;
panoWidgetView.loadImageFromBitmap(
BitmapFactory.decodeStream(inputStream), options
);
inputStream.close();
} catch (IOException e) {
Log.e("Fail", "Exception in loadPanoImage" + e.getMessage());
}
}
#Override
protected void onPause() {
panoWidgetView.pauseRendering();
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
panoWidgetView.resumeRendering();
}
#Override
protected void onDestroy() {
panoWidgetView.shutdown();
super.onDestroy();
}
}
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/vrExperienceActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.vr.sdk.widgets.pano.VrPanoramaView
android:id="#+id/pano_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_weight="5"
android:scrollbars="none" />
<TextView
android:id="#+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:text="VR Experience"
android:textAlignment="center"
android:textAppearance="#android:style/TextAppearance.Large"
android:textColor="#0000F0"
android:visibility="visible" />
<Button
android:id="#+id/buttonCitta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Città" />
<fragment
class="com.google.android.gms.maps.SupportStreetViewPanoramaFragment"
android:id="#+id/streetviewpanorama"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
EDIT: #LucioB
a) those are the places I've tried to call loadPanoImage, but neither of them showed anything. It acts as nothing happens calling that method, the program keeps going to the other tasks. I'd like for images to be shown directly in VR when a button is clicked, or if that isn't possible to add the classic cardboard button in Street View mode to pass to VR view.
b) I mean the code isn't doing what I expected it to do. I thought that once I created VrPanoramaView in the layout and used it to show an image through .loadImageFromBitmap it would have shown the image I loaded from asset (I have an image saved on the virtual SD), and that once I was able to do that for a single image I would have found a way to do it for a whole path.
The code doesn't give any exception, I think I'm making a logic mistake or I didn't understand how VR api work.
EDIT: I've found that the java code is working, the problem was in the layout which didn't permit to see VrPanoramaView because it was obscured by StreetViewPanorama

addView adds the view but it's not showing it

I know this has already been asked, but I tried everything and I couldn't solve my problem.
When I create the views programmatically, they are definitely added. I checked in the debugger and everything is in it's place, even the parent view gets bigger in height because they are using space. But I can't see them. It's like they are below other views or invisible (but they are not. I checked many times...).
This is the xml code where I'm trying to insert the views. I want to insert them where the cursor is (where it's tagged information). I only have it there to show you how it will look like in the end, but this part will be added programmatically.
<LinearLayout
android:id="#+id/llhTestItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:orientation="horizontal">
<TextView
android:id="#+id/tvInformationTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
fontPath="fonts/OpenSans-Regular.ttf"
android:text="Sub title: "/> <!-- tvInformationTitle -->
<TextView
android:id="#+id/tvInformation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
fontPath="fonts/OpenSans-Light.ttf"
android:text="information"/> <!-- tvInformation -->
</LinearLayout> <!-- information -->
Below you can see the code that I'm using to add the views just like in the xml above.
#Override
public void onBindViewHolder(SetupViewerHolder holder, int position) {
CardViewItem cardViewItem = cardViewItemList.get(position);
holder.tvTitle.setText(cardViewItem.getCardTitle());
for (int i = 0; i < cardViewItem.getInformationList().size(); i++){
//region Create llhItem
LinearLayout.LayoutParams llhItemParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
llhItemParams.topMargin = dipToPixels(6);
LinearLayout llhItem = new LinearLayout(context);
llhItem.setLayoutParams(llhItemParams);
llhItem.setOrientation(LinearLayout.HORIZONTAL);
//endregion
LinearLayout.LayoutParams tvInformationsParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//region Create tvInformationTitle
TextView tvInformationTitle = new TextView(context);
tvInformationTitle.setLayoutParams(tvInformationsParams);
tvInformationTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
if (Build.VERSION.SDK_INT < 23){
tvInformationTitle.setTextAppearance(context, R.style.OpenSansRegular);
} else {
tvInformationTitle.setTextAppearance(R.style.OpenSansRegular);
}
tvInformationTitle.setText(cardViewItem.getInformationList().get(i)[0]);
//endregion
//region Create tvInformation
TextView tvInformation = new TextView(context);
tvInformation.setLayoutParams(tvInformationsParams);
tvInformation.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
if (Build.VERSION.SDK_INT < 23){
tvInformation.setTextAppearance(context, R.style.OpenSansLight);
} else {
tvInformation.setTextAppearance(R.style.OpenSansLight);
}
tvInformation.setText(cardViewItem.getInformationList().get(i)[1]);
//endregion
llhItem.addView(tvInformationTitle);
llhItem.addView(tvInformation);
holder.llvInformation.addView(llhItem);
}
Basically what I'm trying to achieve is to have a recycler view, and each item has only one title, one overflow button, but can have multiple information rows.
Here is a print of this, which I had hard coded in xml previously as a prototype.
I know of some alternative ways of doing this that might work, but for now I would like to have it like this, since everything is working like it should, the views are just "not visible".
Had to use layout.post
holder.llvInformation.post( new Runnable() {
#Override
public void run() {
holder.llvInformation.addView(llhItem);
}
});
Have you tried calling invalidate() after adding the view? Like this:
holder.llvInformation.addView(llhItem);
holder.llvInformation.invalidate();

How to make phone calls programmatically in Android?

I have the following layout defined in useful_numbers_item_fragment.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/call_linear_layout">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/useful_nums_item_name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/useful_nums_item_value"/>
</LinearLayout>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#drawable/call"
android:id="#+id/call_btn"
android:onClick="callNumber"/>
</LinearLayout>
I dynamically populate the two text views in a class called UNItemListFragment.java
in the onCreate method:
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
if (getArguments().containsKey(Constants.UNItem.GROUP_ID)) {
simpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.useful_numbers_item_fragment, null,
new String[]{Constants.UNItem.NAME, Constants.UNItem.VALUE},
new int[]{R.id.useful_nums_item_name, R.id.useful_nums_item_value}, 0);
setListAdapter(simpleCursorAdapter);
getLoaderManager().initLoader(0, getArguments(), this);
}
}
For each number if i click on the button i want to make a phone call by
calling the callNumber method when the user clicks the button:
public void callNumber(View view) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);
String phoneNumber = unItemVal.getText().toString();
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
}
It is ok when I click the first button in the list, but when I click on the other buttons
it continues calling the number defined in the first row...
Any idea how to resolve this?
The problem is that this line:
TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);
is executed on the activity, so the findViewById will always return the first item with that id, which is likely the first item in the list.
The best way to fix this would be to override the adapter and add a tag containing the phone number to the view. A quick way to fix this would be to tag along in the view hierarchy, like so:
public void callNumber(View view) {
if( view != null ) { // view is the button tapped
View parent = view.getParent(); // this should be the LinearLayout
if( parent instanceof LinearLayout ) {
TextView unItemVal = (TextView) ((LinearLayout)parent).findViewById(R.id.useful_nums_item_value);
if( unItemVal != null ) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String phoneNumber = unItemVal.getText().toString();
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
}
}
}
}
This would find the parent for the button that was clicked, and then find the text-view containing the number within that ViewGroup.
Using findViewById() will return the first view in the activity or fragment with the specified id. If this is a ListView, it will correspond to the first row.
There are many ways to work around this problem. The quickest one (but certainly not the prettiest one, since it depends on the layout) would be to use findViewById() relative to the LinearLayout that contains the list item. Assuming that view is the ImageButton, it would be somthing like:
((View)view.getParent()).findViewById(R.id.useful_nums_item_value)
A more elegant solution would be to set a tag in the adapter's getView(), containing the data you need (in this case, the phone number to call).

Android: Linking Edit Text field to button

I am creating a times tables app, in which one of the activities allows the user to enter which times tables they would like to view, then the app will bring up that times tables.(e.g. 6x5=30) etc.
Below is the xml layout I have created for the activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="#+id/tvTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I want to see the: "
android:textSize="25dp" />
<EditText
android:id="#+id/etEnterNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Number..."
>
</EditText>
<TextView
android:id="#+id/tvBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Times tables!"
android:textSize="25dp" />
<Button
android:id="#+id/btnGo"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Go"
android:layout_gravity="center"/>r
</LinearLayout>
And this it the java class I have created thus far for the classes functionalitiy:
public class ViewTimesTables extends Activity implements View.OnClickListener {
// Declaring Vars
Button go;
EditText enterNumber;
TextView top;
TextView bottom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setting equal to text layout View
setContentView(R.layout.view);
// calling method to intialise vars
initialiseVars();
}// on create end
/**
* method to initialise all of the buttons, textviews etc used to clean up
* the onCreate.
*/
private void initialiseVars() {
// Setting up (initialising) all the buttons text views etc from the xml
// (vid 25)
go = (Button) findViewById(R.id.btnGo);
enterNumber = (EditText) findViewById(R.id.etEnterNumber);
top = (TextView) findViewById(R.id.tvTop);
bottom = (TextView) findViewById(R.id.tvBottom);
}
/**
* Method with on click listener that adds functionality for all of the
* buttons, text views etc
*
* #param v
*/
public void onClick(View view) {
// switch statement which determines what is clicked
switch ((view).getId()) {
case R.id.etEnterNumber:
// code to read user number (i.e. between 1 and 12)
//And possibly link to go button
break;
case R.id.btnGo:
// code to bring up new activity/screen with times table
// of the number that was entered in edit text
break;
}
}
}
I am unsure how to add the correct functionality (probably within switch statement) so that when e.g. "6" is entered in the edit text box and the "go" button is pressed then the 6 times tables will be brought up in a new activity?
I would begin by looking at Intents to start a new activity and pass data to it.
A relevant tutorial is this Android Intents Tutorial
Getting the text from a edit text is a simple as enterNumber.getText().getString()
You could then use a conditional statement to call the designated class.
Something like this would allow you to pass two values to the SixTimesTables class with the values 5 and 6 passed in.
if(enterNumber.getText().getString().equals("6")){
Intent i = new Intent(this, SixTimesTables.class);
i.putExtra("Value1", 5);
i.putExtra("Value2", 6);
// set the request code to any code you like,
// you can identify the callback via this code
startActivityForResult(i, REQUEST_CODE);
}
You probably want a dynamic layout for next activity.
It may help you.
http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/
Then you can switch between activities as AndyGable mentioned.
Hopefully it'll help you.
You really dont need the onClick for the editText you can handle if data is entered in the editText or not from the button click only like this:
public void onClick(View view) {
// switch statement which determines what is clicked
switch ((view).getId()) {
case R.id.btnGo:
// code to bring up new activity/screen with times table
// of the number that was entered in edit text
// check if editText has values or not
if(TextUtils.isEmpty(mEditText.getText().toString())) {
mEditText.setError("Please enter a number");
}else {
int number = Integer.parseInt(mEditText.getText().toString());
Intent intent = new Intent(YourCurrentActivity.this, NextActivity.class);
intent.putExtra("value", number);
startActivity(intent);
// it is always good to check if the value entered is a number only or not
// add inputType tag in the xml
// android:inputType="number" for the editText.
}
break;
}
}
Now, in order to get value in the next activity do this:
// write this inside the onCreate of the Activity.
int number;
if(getIntent().getExtras() != null) {
number = getIntent().getIntExtra("value");
}
// use the number then to display the tables

Automatic Checkbox list with 5 sec. delay in android

I want to show automatic checklist in android. For example I can have 4 list of item with unchecked check box adjutant to that.
After 5 sec. delay I want to check the first item then move on to second. At the end of 20 sec. I should have all 4 check box checked one by one.
Any idea how should I go on doing this ? I can use list view with checkbox opeion on ..right ??
I am a .net programmer and this is new experience for me so any comment / advise is appreciated.
Here is a basic example I've just written:
public class AutoChecker extends Activity {
private CheckBox checkbox1;
private CheckBox checkbox2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkbox1 = (CheckBox) findViewById(R.id.checkbox_1);
checkbox2 = (CheckBox) findViewById(R.id.checkbox_2);
new CheckerAsync(AutoChecker.this).execute(checkbox1, checkbox2);
}
private class CheckerAsync extends AsyncTask<CheckBox, CheckBox, Void>{
private Activity mActivity;
private CheckerAsync(Activity activity) {
mActivity = activity;
}
#Override
protected Void doInBackground(final CheckBox... checkboxes) {
try {
for(int i = 0, j = checkboxes.length; i < j; i++ ){
Thread.sleep(5000);
publishProgress(checkboxes[i]);
}
} catch (InterruptedException e) {}
return null;
}
#Override
public void onProgressUpdate(final CheckBox... checkboxes){
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
checkboxes[0].setChecked(true);
}
});
}
}
}
This is the XML layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="#+id/checkbox_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Checkbox 1"
/>
<CheckBox
android:id="#+id/checkbox_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Checkbox 2"
/>
</LinearLayout>
Explanation
It uses a class that extends AsyncTask which allows you to use threading in a easy to use/understand way. The way this class is executed is kind of simple:
new CheckerAsync(AutoChecker.this).execute(checkbox1, checkbox2);
You can add as many checkboxes as you want. For instance:
new CheckerAsync(AutoChecker.this).execute(checkbox1, checkbox2, checkbox4, checkbox3);
By the way, I don't know why you are trying to do this. But, if it's for testing purposes, you better take a look at the instrumentation framework.
For the automatic checking with delay, you should use an AsyncTask, like a thread where you can modify things within the Android main UI thread, i.e. like checking the checkboxes.
http://developer.android.com/resources/articles/painless-threading.html
http://www.xoriant.com/blog/mobile-application-development/android-async-task.html
http://developer.android.com/reference/android/os/AsyncTask.html
The checkboxes can be part of a listview, but don't have to be. You can also add them into a LinearLayout with vertical orientation one by one.

Categories

Resources