Android Open Source - khandroid View Utils






From Project

Back to project page khandroid.

License

The source code is released under:

Apache License

If you think the Android project khandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2012-2014 Ognyan Bankov
 *//from ww w  .j  a  va2s.c o m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.khandroid.misc;

import org.slf4j.LoggerFactory;

import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;


public class ViewUtils {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ViewUtils.class
            .getSimpleName());


    /**
     * Finds a button view
     * 
     * @param view
     * @param resourceId
     * @return ButtonView if successful or null otherwise
     */
    public static final Button findButton(View view, int resourceId) {
        Button ret = null;

        ret = (Button) view.findViewById(resourceId);
        if (ret == null) {
            logger.warn("Cannot find Button with id = " + resourceId);
        }

        return ret;
    }


    /**
     * Finds a button view or throws exception if not found
     * 
     * @param view
     * @param resourceId
     * @return ButtonView if successful or RuntimeException
     */
    public static final Button findButtonX(View view, int resourceId) {
        Button ret = null;

        ret = (Button) view.findViewById(resourceId);
        if (ret == null) {
            throw new RuntimeException("Cannot find Button with id = " + resourceId);
        }

        return ret;
    }


    public static final Button initButton(View view, int resourceId, View.OnClickListener listener) {
        Button ret = null;

        ret = findButtonX(view, resourceId);
        ret.setOnClickListener(listener);

        return ret;
    }


    public static final TextView findTextView(View view, int resourceId) {
        TextView ret = null;

        ret = (TextView) view.findViewById(resourceId);
        if (ret == null) {
            logger.warn("Cannot find TextView with id = " + resourceId);
        }

        return ret;
    }


    public static final TextView findTextViewX(View view, int resourceId) {
        TextView ret = null;

        ret = (TextView) view.findViewById(resourceId);
        if (ret == null) {
            throw new RuntimeException("Cannot find TextView with id = " + resourceId);
        }

        return ret;
    }


    public static final EditText findEditText(View view, int resourceId) {
        EditText ret = null;

        ret = (EditText) view.findViewById(resourceId);
        if (ret == null) {
            logger.warn("Cannot find EditText with id = " + resourceId);
        }

        return ret;
    }


    public static final EditText findEditTextX(View view, int resourceId) {
        EditText ret = null;

        ret = (EditText) view.findViewById(resourceId);
        if (ret == null) {
            throw new RuntimeException("Cannot find EditText with id = " + resourceId);
        }

        return ret;
    }


    public static final EditText initEditText(View view, int resourceId, TextWatcher watcher) {
        EditText ret = null;

        ret = findEditTextX(view, resourceId);
        if (watcher != null) {
            ret.addTextChangedListener(watcher);
        }

        return ret;
    }


    public static final RadioGroup findRadioGroup(View view, int resourceId) {
        RadioGroup ret = null;

        ret = (RadioGroup) view.findViewById(resourceId);
        if (ret == null) {
            logger.warn("Cannot find RadioGroup with id = " + resourceId);
        }

        return ret;
    }


    public static final RadioGroup findRadioGroupX(View view, int resourceId) {
        RadioGroup ret = null;

        ret = (RadioGroup) view.findViewById(resourceId);
        if (ret == null) {
            throw new RuntimeException("Cannot find RadioGroup with id = " + resourceId);
        }

        return ret;
    }


    public static final RadioGroup initRadioGroup(View view,
                                                  int resourceId,
                                                  RadioGroup.OnCheckedChangeListener listener) {
        RadioGroup ret = null;

        ret = findRadioGroupX(view, resourceId);
        ret.setOnCheckedChangeListener(listener);

        return ret;
    }


    public static final CheckBox findCheckBox(View view, int resourceId) {
        CheckBox ret = null;

        ret = (CheckBox) view.findViewById(resourceId);
        if (ret == null) {
            logger.warn("Cannot find CheckBox with id = " + resourceId);
        }

        return ret;
    }


    public static final CheckBox findCheckBoxX(View view, int resourceId) {
        CheckBox ret = null;

        ret = (CheckBox) view.findViewById(resourceId);
        if (ret == null) {
            throw new RuntimeException("Cannot find CheckBox with id = " + resourceId);
        }

        return ret;
    }


    public static final CheckBox initCheckBox(View view,
                                              int resourceId,
                                              CompoundButton.OnCheckedChangeListener listener) {
        CheckBox ret = null;

        ret = findCheckBoxX(view, resourceId);
        ret.setOnCheckedChangeListener(listener);

        return ret;
    }


    public static final ToggleButton findToggleButton(View view, int resourceId) {
        ToggleButton ret = null;

        ret = (ToggleButton) view.findViewById(resourceId);
        if (ret == null) {
            logger.warn("Cannot find ToggleButton with id = " + resourceId);
        }

        return ret;
    }


    public static final ToggleButton findToggleButtonX(View view, int resourceId) {
        ToggleButton ret = null;

        ret = (ToggleButton) view.findViewById(resourceId);
        if (ret == null) {
            throw new RuntimeException("Cannot find ToggleButton with id = " + resourceId);
        }

        return ret;
    }


    public static final ToggleButton initToggleButton(View view,
                                                      int resourceId,
                                                      CompoundButton.OnCheckedChangeListener listener) {
        ToggleButton ret = null;

        ret = findToggleButtonX(view, resourceId);
        ret.setOnCheckedChangeListener(listener);

        return ret;
    }


    public static final ListView findListView(View view, int resourceId) {
        ListView ret = null;

        ret = (ListView) view.findViewById(resourceId);
        if (ret == null) {
            logger.warn("Cannot find ListView with id = " + resourceId);
        }

        return ret;
    }


    public static final ListView findListViewX(View view, int resourceId) {
        ListView ret = null;

        ret = (ListView) view.findViewById(resourceId);
        if (ret == null) {
            throw new RuntimeException("Cannot find ListView with id = " + resourceId);
        }

        return ret;
    }


    public static final ImageView findImageView(View view, int resourceId) {
        ImageView ret = null;

        ret = (ImageView) view.findViewById(resourceId);
        if (ret == null) {
            logger.warn("Cannot find ImageView with id = " + resourceId);
        }

        return ret;
    }


    public static final ImageView findImageViewX(View view, int resourceId) {
        ImageView ret = null;

        ret = (ImageView) view.findViewById(resourceId);
        if (ret == null) {
            throw new RuntimeException("Cannot find ImageView with id = " + resourceId);
        }

        return ret;
    }


    public static final Spinner findSpinner(View view, int resourceId) {
        Spinner ret = null;

        ret = (Spinner) view.findViewById(resourceId);
        if (ret == null) {
            logger.warn("Cannot find Spinner with id = " + resourceId);
        }

        return ret;
    }


    public static final Spinner findSpinnerX(View view, int resourceId) {
        Spinner ret = null;

        ret = (Spinner) view.findViewById(resourceId);
        if (ret == null) {
            throw new RuntimeException("Cannot find Spinner with id = " + resourceId);
        }

        return ret;
    }


    /**
     * Finds a TimePicker view
     * 
     * @param view
     * @param resourceId
     * @return TimePicker if successful or null otherwise
     */
    public static final TimePicker findTimePicker(View view, int resourceId) {
        TimePicker ret = null;

        ret = (TimePicker) view.findViewById(resourceId);
        if (ret == null) {
            logger.warn("Cannot find TimePicker with id = " + resourceId);
        }

        return ret;
    }


    /**
     * Finds a TimePicker view or throws exception if not found
     * 
     * @param view
     * @param resourceId
     * @return TimePicker if successful or RuntimeException
     */
    public static final TimePicker findTimePickerX(View view, int resourceId) {
        TimePicker ret = null;

        ret = (TimePicker) view.findViewById(resourceId);
        if (ret == null) {
            throw new RuntimeException("Cannot find TimePicker with id = " + resourceId);
        }

        return ret;
    }
}




Java Source Code List

com.github.khandroid.JsonFunctionality.java
com.github.khandroid.http.functionality.HttpFunctionalityImpl.java
com.github.khandroid.http.functionality.HttpFunctionalityWCookiesImpl.java
com.github.khandroid.http.functionality.HttpFunctionalityWCookies.java
com.github.khandroid.http.functionality.HttpFunctionality.java
com.github.khandroid.http.misc.FileDownloader.java
com.github.khandroid.http.misc.KhandroidBasicResponseHandler.java
com.github.khandroid.http.misc.SynchronizedCookieStore.java
com.github.khandroid.http.request.GetRequestBuilder.java
com.github.khandroid.http.request.PostRequestBuilder.java
com.github.khandroid.http.request.RequestBuilder.java
com.github.khandroid.http.ssl.DefaultSslHttpClient.java
com.github.khandroid.http.ssl.KhandroidX509TrustManager.java
com.github.khandroid.http.ssl.SslSocketFactory.java
com.github.khandroid.kat.ActivityKatExecutorFunctionality.java
com.github.khandroid.kat.FragmentKatExecutorFunctionality.java
com.github.khandroid.kat.KatExecutor.java
com.github.khandroid.kat.KhandroidAsyncTask.java
com.github.khandroid.misc.ActivityUtils.java
com.github.khandroid.misc.DateTimeHelper.java
com.github.khandroid.misc.ImageUtils.java
com.github.khandroid.misc.LocationUtils.java
com.github.khandroid.misc.NetUtils.java
com.github.khandroid.misc.StringUtils.java
com.github.khandroid.misc.ViewUtils.java
com.github.khandroid.rest.InvalidJsonString.java
com.github.khandroid.rest.InvalidResponseException.java
com.github.khandroid.rest.KhRestExchangeBuilder.java
com.github.khandroid.rest.KhRestFunctionalityImpl.java
com.github.khandroid.rest.KhRestFunctionality.java
com.github.khandroid.rest.KhRestResult.java
com.github.khandroid.rest.MalformedResponseException.java
com.github.khandroid.rest.RawRestResult.java
com.github.khandroid.rest.RestAsyncTask.java
com.github.khandroid.rest.RestExchangeBuilder.java
com.github.khandroid.rest.RestExchangeFailedException.java
com.github.khandroid.rest.RestExchangeOutcome.java
com.github.khandroid.rest.RestExchange.java
com.github.khandroid.rest.RestFragmentFunctionality.java
com.github.khandroid.rest.RestFunctionalityImpl.java
com.github.khandroid.rest.RestFunctionality.java
com.github.khandroid.rest.RestPersistFragment.java
com.github.khandroid.rest.RestResponse.java
com.github.khandroid.rest.RestResult.java
com.github.khandroid.rest.UnexpectedResponseException.java
com.github.khandroid.state.StateEvent.java
com.github.khandroid.state.StateMachineImpl.java
com.github.khandroid.state.StateMachine.java
com.github.khandroid.state.StateSkeleton.java
com.github.khandroid.state.State.java
com.github.khandroid.state_app.AppStateContextImpl.java
com.github.khandroid.state_app.AppStateContext.java
com.github.khandroid.state_app.AppState.java
com.github.khandroid.state_app.InvalidAppStateException.java
com.github.khandroid.state_app.SkeletonAppState.java
com.github.khandroid.state_app.StateEvent.java
com.github.khandroid.ui_functionality.ActivityAttachable.java
com.github.khandroid.ui_functionality.ActivityAttachedFunctionality.java
com.github.khandroid.ui_functionality.ActivityUniqueAttachedFunctionality.java
com.github.khandroid.ui_functionality.FragmentAttachable.java
com.github.khandroid.ui_functionality.FragmentAttachedFunctionality.java
com.github.khandroid.ui_functionality.FragmentUniqueAttachedFunctionality.java
com.github.khandroid.ui_functionality.HostActivity.java
com.github.khandroid.ui_functionality.HostFragment.java
com.github.khandroid.ui_functionality.SuperNotCalledException.java
com.github.khandroid.views.ImageViewWithContextMenuInfo.java
com.github.khandroid.volley.GetJsonObjectRequestBuilder.java
com.github.khandroid.volley.GetStringRequestBuilder.java
com.github.khandroid.volley.GetVRestRequestBuilder.java
com.github.khandroid.volley.PostJsonObjectRequestBuilder.java
com.github.khandroid.volley.PostRequestBuilderImpl.java
com.github.khandroid.volley.PostRequestBuilder.java
com.github.khandroid.volley.PostStringRequestBuilder.java
com.github.khandroid.volley.PostVRestRequestBuilder.java
com.github.khandroid.volley.RequestBuilder.java
com.github.khandroid.volley.VRestRequestBuilder.java
com.github.khandroid.volley.VRestRequest.java