List of usage examples for android.content.res Resources getDrawable
@Deprecated public Drawable getDrawable(@DrawableRes int id) throws NotFoundException
From source file:com.conferenceengineer.android.iosched.ui.tablet.TracksDropdownFragment.java
private void loadTrack(Cursor cursor, boolean triggerCallback) { final int trackColor; final Resources res = getResources(); if (cursor != null) { trackColor = cursor.getInt(TracksAdapter.TracksQuery.TRACK_COLOR); mTrackId = cursor.getString(TracksAdapter.TracksQuery.TRACK_ID); String trackName = cursor.getString(TracksAdapter.TracksQuery.TRACK_NAME); mTitle.setText(trackName);/*from w w w. j a v a 2 s . c o m*/ mAbstract.setText(cursor.getString(TracksAdapter.TracksQuery.TRACK_ABSTRACT)); int iconResId = res.getIdentifier("track_" + ParserUtils.sanitizeId(trackName), "drawable", getActivity().getPackageName()); if (iconResId != 0) { BitmapDrawable sourceIconDrawable = (BitmapDrawable) res.getDrawable(iconResId); Bitmap icon = Bitmap.createBitmap(sourceIconDrawable.getIntrinsicWidth(), sourceIconDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(icon); sourceIconDrawable.setBounds(0, 0, icon.getWidth(), icon.getHeight()); sourceIconDrawable.draw(canvas); BitmapDrawable iconDrawable = new BitmapDrawable(res, icon); mIcon.setImageDrawable(iconDrawable); } else { mIcon.setImageDrawable(null); } } else { trackColor = res.getColor(R.color.all_track_color); mTrackId = ScheduleContract.Tracks.ALL_TRACK_ID; mIcon.setImageDrawable(null); switch (mViewType) { case VIEW_TYPE_SESSIONS: mTitle.setText(R.string.all_tracks_sessions); mAbstract.setText(R.string.all_tracks_subtitle_sessions); break; case VIEW_TYPE_OFFICE_HOURS: mTitle.setText(R.string.all_tracks_office_hours); mAbstract.setText(R.string.all_tracks_subtitle_office_hours); break; case VIEW_TYPE_SANDBOX: mTitle.setText(R.string.all_tracks_sandbox); mAbstract.setText(R.string.all_tracks_subtitle_sandbox); break; } } mRootView.setBackgroundColor(trackColor); mCallbacks.onTrackNameAvailable(mTrackId, mTitle.getText().toString()); if (triggerCallback) { mHandler.post(new Runnable() { @Override public void run() { mCallbacks.onTrackSelected(mTrackId); } }); } }
From source file:com.androzic.MainActivity.java
private void initializeDrawerItems() { if (mDrawerItems == null) return;// w w w . jav a 2s . c om mDrawerItems.clear(); Resources resources = getResources(); Drawable icon; Intent action; Fragment fragment; FragmentManager fm = getSupportFragmentManager(); // add main items to drawer list icon = resources.getDrawable(R.drawable.ic_map_white_24dp); fragment = fm.findFragmentByTag(getString(R.string.menu_map)); if (fragment == null) fragment = Fragment.instantiate(this, MapFragment.class.getName()); mDrawerItems.add(new DrawerItem(icon, getString(R.string.menu_map), fragment)); icon = resources.getDrawable(R.drawable.ic_place_white_24dp); fragment = fm.findFragmentByTag(getString(R.string.menu_waypoints)); if (fragment == null) fragment = Fragment.instantiate(this, WaypointList.class.getName()); mDrawerItems.add(new DrawerItem(icon, getString(R.string.menu_waypoints), fragment)); icon = resources.getDrawable(R.drawable.ic_directions_white_24dp); fragment = fm.findFragmentByTag(getString(R.string.menu_routes)); if (fragment == null) fragment = Fragment.instantiate(this, RouteList.class.getName()); mDrawerItems.add(new DrawerItem(icon, getString(R.string.menu_routes), fragment)); icon = resources.getDrawable(R.drawable.ic_gesture_white_24dp); fragment = fm.findFragmentByTag(getString(R.string.menu_tracks)); if (fragment == null) fragment = Fragment.instantiate(this, TrackList.class.getName()); mDrawerItems.add(new DrawerItem(icon, getString(R.string.menu_tracks), fragment)); // add plugins to drawer list mDrawerItems.add(new DrawerItem()); icon = resources.getDrawable(R.drawable.ic_my_location_white_24dp); action = new Intent(this, HSIActivity.class); mDrawerItems.add(new DrawerItem(icon, getString(R.string.menu_hsi), action).makeMinor()); java.util.Map<String, Pair<Drawable, Intent>> plugins = application.getPluginsViews(); for (String plugin : plugins.keySet()) { mDrawerItems .add(new DrawerItem(plugins.get(plugin).first, plugin, plugins.get(plugin).second).makeMinor()); } // add supplementary items to drawer list mDrawerItems.add(new DrawerItem()); icon = resources.getDrawable(R.drawable.ic_settings_white_24dp); fragment = fm.findFragmentByTag(getString(R.string.menu_preferences)); if (fragment == null) fragment = Fragment.instantiate(this, Preferences.class.getName()); mDrawerItems.add(new DrawerItem(icon, getString(R.string.menu_preferences), fragment).makeMinor() .makeSupplementary()); icon = resources.getDrawable(R.drawable.ic_info_white_24dp); fragment = fm.findFragmentByTag(getString(R.string.menu_about)); if (fragment == null) fragment = Fragment.instantiate(this, About.class.getName()); mDrawerItems.add( new DrawerItem(icon, getString(R.string.menu_about), fragment).makeMinor().makeSupplementary()); if (exitConfirmation == 3) { icon = resources.getDrawable(R.drawable.ic_open_in_new_white_24dp); Runnable exit = new Runnable() { @Override public void run() { Log.e(TAG, "Exit"); MainActivity.this.finish(); } }; mDrawerItems .add(new DrawerItem(icon, getString(R.string.menu_exit), exit).makeMinor().makeSupplementary()); } mDrawerAdapter.notifyDataSetChanged(); }
From source file:org.mozilla.gecko.AwesomeBar.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(LOGTAG, "creating awesomebar"); mResolver = Tabs.getInstance().getContentResolver(); setContentView(R.layout.awesomebar); if (Build.VERSION.SDK_INT >= 11) { RelativeLayout actionBarLayout = (RelativeLayout) GeckoActionBar.getCustomView(this); mGoButton = (ImageButton) actionBarLayout.findViewById(R.id.awesomebar_button); mText = (AwesomeBarEditText) actionBarLayout.findViewById(R.id.awesomebar_text); } else {// w w w . ja v a 2s. c o m mGoButton = (ImageButton) findViewById(R.id.awesomebar_button); mText = (AwesomeBarEditText) findViewById(R.id.awesomebar_text); } TabWidget tabWidget = (TabWidget) findViewById(android.R.id.tabs); tabWidget.setDividerDrawable(null); mAwesomeTabs = (AwesomeBarTabs) findViewById(R.id.awesomebar_tabs); mAwesomeTabs.setOnUrlOpenListener(new AwesomeBarTabs.OnUrlOpenListener() { public void onUrlOpen(String url) { openUrlAndFinish(url); } public void onSearch(String engine) { openSearchAndFinish(mText.getText().toString(), engine); } }); mGoButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { openUserEnteredAndFinish(mText.getText().toString()); } }); Resources resources = getResources(); int padding[] = { mText.getPaddingLeft(), mText.getPaddingTop(), mText.getPaddingRight(), mText.getPaddingBottom() }; GeckoStateListDrawable states = new GeckoStateListDrawable(); states.initializeFilter(GeckoApp.mBrowserToolbar.getHighlightColor()); states.addState(new int[] { android.R.attr.state_focused }, resources.getDrawable(R.drawable.address_bar_url_pressed)); states.addState(new int[] { android.R.attr.state_pressed }, resources.getDrawable(R.drawable.address_bar_url_pressed)); states.addState(new int[] {}, resources.getDrawable(R.drawable.address_bar_url_default)); mText.setBackgroundDrawable(states); mText.setPadding(padding[0], padding[1], padding[2], padding[3]); Intent intent = getIntent(); String currentUrl = intent.getStringExtra(CURRENT_URL_KEY); mType = intent.getStringExtra(TYPE_KEY); if (currentUrl != null) { mText.setText(currentUrl); mText.selectAll(); } mText.setOnKeyPreImeListener(new AwesomeBarEditText.OnKeyPreImeListener() { public boolean onKeyPreIme(View v, int keyCode, KeyEvent event) { // We only want to process one event per tap if (event.getAction() != KeyEvent.ACTION_DOWN) return false; if (keyCode == KeyEvent.KEYCODE_ENTER) { openUserEnteredAndFinish(mText.getText().toString()); return true; } // If input method is in fullscreen mode, we want to dismiss // it instead of closing awesomebar straight away. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (keyCode == KeyEvent.KEYCODE_BACK && !imm.isFullscreenMode()) { // Let mAwesomeTabs try to handle the back press, since we may be in a // bookmarks sub-folder. if (mAwesomeTabs.onBackPressed()) return true; // If mAwesomeTabs.onBackPressed() returned false, we didn't move up // a folder level, so just exit the activity. cancelAndFinish(); return true; } return false; } }); mText.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { // do nothing } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // do nothing } public void onTextChanged(CharSequence s, int start, int before, int count) { String text = s.toString(); mAwesomeTabs.filter(text); updateGoButton(text); } }); mText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_ENTER) { if (event.getAction() != KeyEvent.ACTION_DOWN) return true; openUserEnteredAndFinish(mText.getText().toString()); return true; } else { return false; } } }); mText.setOnFocusChangeListener(new View.OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } } }); registerForContextMenu(mAwesomeTabs.findViewById(R.id.all_pages_list)); registerForContextMenu(mAwesomeTabs.findViewById(R.id.bookmarks_list)); registerForContextMenu(mAwesomeTabs.findViewById(R.id.history_list)); GeckoAppShell.registerGeckoEventListener("SearchEngines:Data", this); GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:Get", null)); }
From source file:com.appunite.scroll.ScaleImageView.java
/** * Set image drawable//from ww w . j av a2s . c o m * @param drawable set image to display */ @SuppressWarnings("UnusedDeclaration") public void setSrcResource(int drawable) { if (drawable == 0) { setSrcDrawable(null); return; } final Resources resources = getResources(); assert resources != null; setSrcDrawable(resources.getDrawable(drawable)); }
From source file:com.hippo.vectorold.drawable.AnimatedVectorDrawable.java
public void inflate(Context context, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException { Resources res = context.getResources(); int eventType = parser.getEventType(); float pathErrorScale = 1; while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { final String tagName = parser.getName(); if (ANIMATED_VECTOR.equals(tagName)) { final TypedArray a = res.obtainAttributes(attrs, R.styleable.AnimatedVectorDrawable); int drawableRes = a.getResourceId(R.styleable.AnimatedVectorDrawable_drawable, 0); if (drawableRes != 0) { VectorDrawable vectorDrawable = (VectorDrawable) res.getDrawable(drawableRes).mutate(); vectorDrawable.setAllowCaching(false); pathErrorScale = vectorDrawable.getPixelSize(); mAnimatedVectorState.mVectorDrawable = vectorDrawable; }/* ww w .j a v a2s . com*/ a.recycle(); } else if (TARGET.equals(tagName)) { final TypedArray a = res.obtainAttributes(attrs, R.styleable.AnimatedVectorDrawableTarget); final String target = a.getString(R.styleable.AnimatedVectorDrawableTarget_name); int id = a.getResourceId(R.styleable.AnimatedVectorDrawableTarget_animation, 0); if (id != 0) { Animator objectAnimator = AnimatorInflater.loadAnimator(context, id, pathErrorScale); setupAnimatorsForTarget(target, objectAnimator); } a.recycle(); } } eventType = parser.next(); } }
From source file:com.android.systemui.statusbar.phone.NavigationBarView.java
public NavigationBarView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context;/* ww w.j av a 2 s . c o m*/ mHidden = false; mDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); mBarService = IStatusBarService.Stub.asInterface(ServiceManager.getService(Context.STATUS_BAR_SERVICE)); final Resources res = mContext.getResources(); mBarSize = res.getDimensionPixelSize(R.dimen.navigation_bar_size); mVertical = false; mShowMenu = false; mDelegateHelper = new DelegateViewHelper(this); mBackIcon = res.getDrawable(R.drawable.ic_sysbar_back); mBackLandIcon = res.getDrawable(R.drawable.ic_sysbar_back_land); mBackAltIcon = res.getDrawable(R.drawable.ic_sysbar_back_ime); mBackAltLandIcon = res.getDrawable(R.drawable.ic_sysbar_back_ime); mContext.getContentResolver().registerContentObserver( Settings.System.getUriFor(Settings.System.NAV_BAR_COLOR), false, new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { updateColor(false); } }); }
From source file:com.cssweb.android.trade.stock.StockTrading.java
@Override public void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); sc = new ServiceControl(this);//?? ////from w w w. j av a2s. com HandlerThread mHandlerThread = new HandlerThread("CSSWEB_THREAD"); mHandlerThread.start(); priceHandler = new PriceDataHandler(mHandlerThread.getLooper()); setContentView(com.cssweb.android.main.R.layout.zr_trade_stock_trade); Bundle bundle = getIntent().getExtras(); type = bundle.getInt("type"); bsname = bundle.getString("bsname"); String stockCode = bundle.getString("stkcode"); initTitle(R.drawable.njzq_title_left_back, 0, bsname); LinearLayout localLinearLayout = (LinearLayout) findViewById(R.id.zrtradelayout); localLinearLayout.setOnFocusChangeListener(setOnEditFocusListener); this.m_vklayout = localLinearLayout; View localView1 = this.m_vklayout; localView1.setOnFocusChangeListener(setOnEditFocusListener); View localView2 = this.m_vklayout; localView2.setOnClickListener(setOnEditClickListener); viewFlipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01); preView = (ImageView) findViewById(R.id.previous_screen); leftIn = AnimationUtils.loadAnimation(this, R.anim.push_left_in_layout); leftOut = AnimationUtils.loadAnimation(this, R.anim.push_left_out_layout); rightIn = AnimationUtils.loadAnimation(this, R.anim.push_right_in_layout); rightOut = AnimationUtils.loadAnimation(this, R.anim.push_right_out_layout); priceView = (PriceMini) findViewById(R.id.zrviewprice); trendView = (TrendView) findViewById(R.id.zrviewtrend); trendView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }); klineView = (KlineMini) findViewById(R.id.zrviewkline); klineView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }); financeView = (FinanceMini) findViewById(R.id.zrviewfinance); stockHolder = (Spinner) findViewById(R.id.zrtxtaccount); queryMethod = (Spinner) findViewById(R.id.zrtxtbsflag); // availableNum = (SeekBar) findViewById(R.id.zrcanbs); // lblMaxNumber = (TextView)findViewById(R.id.txtMaxNumber); stkcode = (EditText) findViewById(R.id.zredtstockcode); stkname = (TextView) findViewById(R.id.zrtxtstockname); price = (EditText) findViewById(R.id.zredtprice); number = (EditText) findViewById(R.id.zredtcount); lblNumberUnit = (TextView) findViewById(R.id.lblNumberUnit); btn0 = (ShadowButton) findViewById(R.id.zr_surebutton); btn0.setOnClickListener(myShowProgreeBar); layoutprice0 = (LinearLayout) findViewById(R.id.LinearLayout03); layoutprice = (LinearLayout) findViewById(R.id.LinearLayout11); //layoutAvaiNumber = (LinearLayout)findViewById(R.id.LinearLayout04); //layoutAvaiAsset = (LinearLayout)findViewById(R.id.LinearLayout06); lblAvaiAsset = (TextView) findViewById(R.id.lblAvaiAsset); AvaiAsset = (TextView) findViewById(R.id.AvaiAsset); lblPrice = (TextView) findViewById(R.id.lblPrice); lblPrice1 = (TextView) findViewById(R.id.lblPrice1); lblNumber = (TextView) findViewById(R.id.lblNumber); avaiAsset = (TextView) findViewById(R.id.AvaiAsset); lblNumberUnit = (TextView) findViewById(R.id.lblNumberUnit); setDynamic(); adjustDownPrice = (ImageView) findViewById(R.id.AdjustDownPrice); adjustDownPrice.setTag(0); adjustDownPrice.setOnClickListener(adjustIconListener); adjustUpPrice = (ImageView) findViewById(R.id.AdjustUpPrice); adjustUpPrice.setTag(1); adjustUpPrice.setOnClickListener(adjustIconListener); adjustDownNumber = (ImageView) findViewById(R.id.AdjustDownNumber); adjustDownNumber.setTag(2); adjustDownNumber.setOnClickListener(adjustIconListener); adjustUpNumber = (ImageView) findViewById(R.id.AdjustUpNumber); adjustUpNumber.setTag(3); adjustUpNumber.setOnClickListener(adjustIconListener); stkcode.setText(stockCode); if (stkcode.getText().length() == 6) { showProgress(); } stkcode.setInputType(InputType.TYPE_NULL); stkcode.setFocusable(true); stkcode.setTag("STOCK"); stkcode.setOnClickListener(setOnEditClickListener); stkcode.setOnFocusChangeListener(setOnEditFocusListener); price.setInputType(InputType.TYPE_NULL); price.setFocusable(true); price.setTag("NUMDOT"); price.setOnClickListener(setOnEditClickListener); price.setOnFocusChangeListener(setOnEditFocusListener); number.setInputType(InputType.TYPE_NULL); number.setFocusable(true); number.setTag(""); number.setOnClickListener(setOnEditClickListener); number.setOnFocusChangeListener(setOnEditFocusListener); stkcode.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable arg0) { //queryStock(queryCode.getText()); OnTextChanged(arg0); } public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } }); holder = TradeUser.getInstance().getHolder(); stockHolderAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, (String[]) holder.toArray(new String[holder.size()])); stockHolderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); stockHolder.setAdapter(stockHolderAdapter); String[] arrayOfString = getResources().getStringArray(R.array.other_market_bs_methods); for (int i = 0; i < arrayOfString.length; i++) { Log.e("arrayOfString", arrayOfString[i]); } queryMethodAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrayOfString); queryMethodAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); queryMethod.setAdapter(queryMethodAdapter); queryMethod.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (position != 0) { layoutprice0.setVisibility(View.GONE); layoutprice.setVisibility(View.VISIBLE); } else { layoutprice0.setVisibility(View.VISIBLE); layoutprice.setVisibility(View.GONE); } } public void onNothingSelected(AdapterView<?> arg0) { } }); int h = R.drawable.forminput; Resources localResources = getResources(); Drawable localDrawable = null; localDrawable = localResources.getDrawable(h); int spinnerheight = localDrawable.getIntrinsicHeight() - 4; // Log.e("<<<<<<<<<<<<<<<<<<<eeeeeeeeeeeeeeeeeeeeeeeeeee>>>>>>>>>>>>>>", String.valueOf(spinnerheight)); LinearLayout.LayoutParams linearParams1 = (LinearLayout.LayoutParams) stockHolder.getLayoutParams();//?stockHolder? linearParams1.height = spinnerheight;//?? stockHolder.setLayoutParams(linearParams1); LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) queryMethod.getLayoutParams();//?queryMethod? linearParams.height = spinnerheight;//?? queryMethod.setLayoutParams(linearParams); }
From source file:org.level28.android.moca.ui.schedule.SessionListFragment.java
@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); final Resources res = view.getResources(); // Get a hold on all view elements mListView = (ListView) view.findViewById(android.R.id.list); mProgressBar = (ProgressBar) view.findViewById(R.id.progressLoading); mEmptyView = (TextView) view.findViewById(android.R.id.empty); // Backward compatibility sucks if (!isHoneycomb) { // Force the Holo Light background color for this fragment on // gingerbread and lower view.setBackgroundColor(res.getColor(R.color.background_holo_light)); // If we're running on Gingerbread or lower we have to override the // divider drawable for consistency mListView.setDivider(res.getDrawable(R.drawable.list_divider)); }/*from w ww .j a v a 2s .com*/ // Set divider height to 2dp mListView.setDividerHeight(2); // Set item click listener mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { onListItemClick((ListView) parent, view, position, id); } }); mListView.setAdapter(mAdapter); }
From source file:com.neka.cordova.inappbrowser.InAppBrowser.java
/** * Display a new browser with the specified URL. * * @param url The url to load. * @param jsonObject/* w w w . ja v a2 s .co m*/ */ public String showWebPage(final String url, HashMap<String, Boolean> features) { // Determine if we should hide the location bar. showLocationBar = true; openWindowHidden = false; if (features != null) { Boolean show = features.get(LOCATION); if (show != null) { showLocationBar = show.booleanValue(); } Boolean hidden = features.get(HIDDEN); if (hidden != null) { openWindowHidden = hidden.booleanValue(); } Boolean cache = features.get(CLEAR_ALL_CACHE); if (cache != null) { clearAllCache = cache.booleanValue(); } else { cache = features.get(CLEAR_SESSION_CACHE); if (cache != null) { clearSessionCache = cache.booleanValue(); } } } final CordovaWebView thatWebView = this.webView; // Create dialog in new thread Runnable runnable = new Runnable() { /** * Convert our DIP units to Pixels * * @return int */ private int dpToPixels(int dipValue) { int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) dipValue, cordova.getActivity().getResources().getDisplayMetrics()); return value; } public void run() { // Let's create the main dialog dialog = new InAppBrowserDialog(cordova.getActivity(), android.R.style.Theme_NoTitleBar); dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog; dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCancelable(true); dialog.setInAppBroswer(getInAppBrowser()); // Main container layout LinearLayout main = new LinearLayout(cordova.getActivity()); main.setOrientation(LinearLayout.VERTICAL); // Toolbar layout RelativeLayout toolbar = new RelativeLayout(cordova.getActivity()); //Please, no more black! toolbar.setBackgroundColor(android.graphics.Color.LTGRAY); toolbar.setLayoutParams( new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44))); toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2)); toolbar.setHorizontalGravity(Gravity.LEFT); toolbar.setVerticalGravity(Gravity.TOP); // Action Button Container layout RelativeLayout actionButtonContainer = new RelativeLayout(cordova.getActivity()); actionButtonContainer.setLayoutParams( new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); actionButtonContainer.setHorizontalGravity(Gravity.LEFT); actionButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL); actionButtonContainer.setId(1); // Back button Button back = new Button(cordova.getActivity()); RelativeLayout.LayoutParams backLayoutParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); backLayoutParams.addRule(RelativeLayout.ALIGN_LEFT); back.setLayoutParams(backLayoutParams); back.setContentDescription("Back Button"); back.setId(2); /* back.setText("<"); */ Resources activityRes = cordova.getActivity().getResources(); int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName()); Drawable backIcon = activityRes.getDrawable(backResId); if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { back.setBackgroundDrawable(backIcon); } else { back.setBackground(backIcon); } back.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { goBack(); } }); // Forward button Button forward = new Button(cordova.getActivity()); RelativeLayout.LayoutParams forwardLayoutParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); forwardLayoutParams.addRule(RelativeLayout.RIGHT_OF, 2); forward.setLayoutParams(forwardLayoutParams); forward.setContentDescription("Forward Button"); forward.setId(3); //forward.setText(">"); int fwdResId = activityRes.getIdentifier("ic_action_next_item", "drawable", cordova.getActivity().getPackageName()); Drawable fwdIcon = activityRes.getDrawable(fwdResId); if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { forward.setBackgroundDrawable(fwdIcon); } else { forward.setBackground(fwdIcon); } forward.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { goForward(); } }); // Edit Text Box edittext = new EditText(cordova.getActivity()); RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); textLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1); textLayoutParams.addRule(RelativeLayout.LEFT_OF, 5); edittext.setLayoutParams(textLayoutParams); edittext.setId(4); edittext.setSingleLine(true); edittext.setText(url); edittext.setInputType(InputType.TYPE_TEXT_VARIATION_URI); edittext.setImeOptions(EditorInfo.IME_ACTION_GO); edittext.setInputType(InputType.TYPE_NULL); // Will not except input... Makes the text NON-EDITABLE edittext.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { navigate(edittext.getText().toString()); return true; } return false; } }); // Close button Button close = new Button(cordova.getActivity()); RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); close.setLayoutParams(closeLayoutParams); forward.setContentDescription("Close Button"); close.setId(5); //close.setText(buttonLabel); int closeResId = activityRes.getIdentifier("ic_action_remove", "drawable", cordova.getActivity().getPackageName()); Drawable closeIcon = activityRes.getDrawable(closeResId); if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { close.setBackgroundDrawable(closeIcon); } else { close.setBackground(closeIcon); } close.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { closeDialog(); } }); // WebView inAppWebView = new WebView(cordova.getActivity()); inAppWebView.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView)); WebViewClient client = new InAppBrowserClient(thatWebView, edittext); inAppWebView.setWebViewClient(client); WebSettings settings = inAppWebView.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setBuiltInZoomControls(true); settings.setPluginState(android.webkit.WebSettings.PluginState.ON); //Toggle whether this is enabled or not! Bundle appSettings = cordova.getActivity().getIntent().getExtras(); boolean enableDatabase = appSettings == null ? true : appSettings.getBoolean("InAppBrowserStorageEnabled", true); if (enableDatabase) { String databasePath = cordova.getActivity().getApplicationContext() .getDir("inAppBrowserDB", Context.MODE_PRIVATE).getPath(); settings.setDatabasePath(databasePath); settings.setDatabaseEnabled(true); } settings.setDomStorageEnabled(true); if (clearAllCache) { CookieManager.getInstance().removeAllCookie(); } else if (clearSessionCache) { CookieManager.getInstance().removeSessionCookie(); } inAppWebView.loadUrl(url); inAppWebView.setId(6); inAppWebView.getSettings().setLoadWithOverviewMode(true); inAppWebView.getSettings().setUseWideViewPort(true); inAppWebView.requestFocus(); inAppWebView.requestFocusFromTouch(); // Add the back and forward buttons to our action button container layout actionButtonContainer.addView(back); actionButtonContainer.addView(forward); // Add the views to our toolbar //toolbar.addView(actionButtonContainer); //toolbar.addView(edittext); toolbar.addView(close); // Don't add the toolbar if its been disabled if (getShowLocationBar()) { // Add our toolbar to our main view/layout main.addView(toolbar); } // Add our webview to our main view/layout main.addView(inAppWebView); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); lp.copyFrom(dialog.getWindow().getAttributes()); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.MATCH_PARENT; dialog.setContentView(main); dialog.show(); dialog.getWindow().setAttributes(lp); // the goal of openhidden is to load the url and not display it // Show() needs to be called to cause the URL to be loaded if (openWindowHidden) { dialog.hide(); } } }; this.cordova.getActivity().runOnUiThread(runnable); return ""; }
From source file:com.dtworkshop.inappcrossbrowser.WebViewBrowser.java
/** * Display a new browser with the specified URL. * * @param url The url to load. *//*from www . j a v a2s .c o m*/ public String showWebPage(final String url, HashMap<String, Boolean> features) { // Determine if we should hide the location bar. showLocationBar = true; openWindowHidden = false; if (features != null) { Boolean show = features.get(LOCATION); if (show != null) { showLocationBar = show.booleanValue(); } Boolean hidden = features.get(HIDDEN); if (hidden != null) { openWindowHidden = hidden.booleanValue(); } Boolean cache = features.get(CLEAR_ALL_CACHE); if (cache != null) { clearAllCache = cache.booleanValue(); } else { cache = features.get(CLEAR_SESSION_CACHE); if (cache != null) { clearSessionCache = cache.booleanValue(); } } } final CordovaWebView thatWebView = this.webView; // Create dialog in new thread Runnable runnable = new Runnable() { /** * Convert our DIP units to Pixels * * @return int */ private int dpToPixels(int dipValue) { int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) dipValue, cordova.getActivity().getResources().getDisplayMetrics()); return value; } @SuppressLint("NewApi") public void run() { // Let's create the main dialog dialog = new InAppBrowserDialog(cordova.getActivity(), android.R.style.Theme_NoTitleBar); dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog; dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCancelable(true); dialog.setInAppBroswer(getInAppBrowser()); // Main container layout LinearLayout main = new LinearLayout(cordova.getActivity()); main.setOrientation(LinearLayout.VERTICAL); // Toolbar layout RelativeLayout toolbar = new RelativeLayout(cordova.getActivity()); //Please, no more black! toolbar.setBackgroundColor(android.graphics.Color.LTGRAY); toolbar.setLayoutParams( new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44))); toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2)); toolbar.setHorizontalGravity(Gravity.LEFT); toolbar.setVerticalGravity(Gravity.TOP); // Action Button Container layout RelativeLayout actionButtonContainer = new RelativeLayout(cordova.getActivity()); actionButtonContainer.setLayoutParams( new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); actionButtonContainer.setHorizontalGravity(Gravity.LEFT); actionButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL); actionButtonContainer.setId(1); // Back button Button back = new Button(cordova.getActivity()); RelativeLayout.LayoutParams backLayoutParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); backLayoutParams.addRule(RelativeLayout.ALIGN_LEFT); back.setLayoutParams(backLayoutParams); back.setContentDescription("Back Button"); back.setId(2); Resources activityRes = cordova.getActivity().getResources(); int backResId = activityRes.getIdentifier("ic_action_previous_item", "drawable", cordova.getActivity().getPackageName()); Drawable backIcon = activityRes.getDrawable(backResId); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { back.setBackgroundDrawable(backIcon); } else { back.setBackground(backIcon); } back.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { goBack(); } }); // Forward button Button forward = new Button(cordova.getActivity()); RelativeLayout.LayoutParams forwardLayoutParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); forwardLayoutParams.addRule(RelativeLayout.RIGHT_OF, 2); forward.setLayoutParams(forwardLayoutParams); forward.setContentDescription("Forward Button"); forward.setId(3); int fwdResId = activityRes.getIdentifier("ic_action_next_item", "drawable", cordova.getActivity().getPackageName()); Drawable fwdIcon = activityRes.getDrawable(fwdResId); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { forward.setBackgroundDrawable(fwdIcon); } else { forward.setBackground(fwdIcon); } forward.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { goForward(); } }); // Edit Text Box edittext = new EditText(cordova.getActivity()); RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); textLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1); textLayoutParams.addRule(RelativeLayout.LEFT_OF, 5); edittext.setLayoutParams(textLayoutParams); edittext.setId(4); edittext.setSingleLine(true); edittext.setText(url); edittext.setInputType(InputType.TYPE_TEXT_VARIATION_URI); edittext.setImeOptions(EditorInfo.IME_ACTION_GO); edittext.setInputType(InputType.TYPE_NULL); // Will not except input... Makes the text NON-EDITABLE edittext.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // If the event is a key-down event on the "enter" button if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { navigate(edittext.getText().toString()); return true; } return false; } }); // Close/Done button Button close = new Button(cordova.getActivity()); RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); close.setLayoutParams(closeLayoutParams); forward.setContentDescription("Close Button"); close.setId(5); int closeResId = activityRes.getIdentifier("ic_action_remove", "drawable", cordova.getActivity().getPackageName()); Drawable closeIcon = activityRes.getDrawable(closeResId); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { close.setBackgroundDrawable(closeIcon); } else { close.setBackground(closeIcon); } close.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { closeDialog(); } }); // WebView inAppWebView = new WebView(cordova.getActivity()); inAppWebView.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView)); WebViewClient client = new InAppBrowserClient(thatWebView, edittext); inAppWebView.setWebViewClient(client); WebSettings settings = inAppWebView.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setBuiltInZoomControls(true); settings.setPluginState(WebSettings.PluginState.ON); //Toggle whether this is enabled or not! Bundle appSettings = cordova.getActivity().getIntent().getExtras(); boolean enableDatabase = appSettings == null ? true : appSettings.getBoolean("InAppBrowserStorageEnabled", true); if (enableDatabase) { String databasePath = cordova.getActivity().getApplicationContext() .getDir("inAppBrowserDB", Context.MODE_PRIVATE).getPath(); settings.setDatabasePath(databasePath); settings.setDatabaseEnabled(true); } settings.setDomStorageEnabled(true); if (clearAllCache) { CookieManager.getInstance().removeAllCookie(); } else if (clearSessionCache) { CookieManager.getInstance().removeSessionCookie(); } inAppWebView.loadUrl(url); inAppWebView.setId(6); inAppWebView.getSettings().setLoadWithOverviewMode(true); inAppWebView.getSettings().setUseWideViewPort(true); inAppWebView.requestFocus(); inAppWebView.requestFocusFromTouch(); // Add the back and forward buttons to our action button container layout actionButtonContainer.addView(back); actionButtonContainer.addView(forward); // Add the views to our toolbar toolbar.addView(actionButtonContainer); toolbar.addView(edittext); toolbar.addView(close); // Don't add the toolbar if its been disabled if (getShowLocationBar()) { // Add our toolbar to our main view/layout main.addView(toolbar); } // Add our webview to our main view/layout main.addView(inAppWebView); LayoutParams lp = new LayoutParams(); lp.copyFrom(dialog.getWindow().getAttributes()); lp.width = LayoutParams.MATCH_PARENT; lp.height = LayoutParams.MATCH_PARENT; dialog.setContentView(main); dialog.show(); dialog.getWindow().setAttributes(lp); // the goal of openhidden is to load the url and not display it // Show() needs to be called to cause the URL to be loaded if (openWindowHidden) { dialog.hide(); } } }; this.cordova.getActivity().runOnUiThread(runnable); return ""; }