How do I get the currently displayed fragment?

When you add the fragment in your transaction you should use a tag. fragTrans.replace(android.R.id.content, myFragment, “MY_FRAGMENT”); …and later if you want to check if the fragment is visible: MyFragment myFragment = (MyFragment)getSupportFragmentManager().findFragmentByTag(“MY_FRAGMENT”); if (myFragment != null && myFragment.isVisible()) { // add your code here } See also http://developer.android.com/reference/android/app/Fragment.html

How to set the font style to bold, italic and underlined in an Android TextView?

This should make your TextView bold, underlined and italic at the same time. strings.xml <resources> <string name=”register”><u><b><i>Copyright</i></b></u></string> </resources> To set this String to your TextView, do this in your main.xml <?xml version=”1.0″ encoding=”utf-8″?> <TextView xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@+id/textview” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:text=”@string/register” /> or In JAVA, TextView textView = new TextView(this); textView.setText(R.string.register); Sometimes the above approach will not … Read more

Error type 3 Error: Activity class {} does not exist

I faced a similar problem after refactoring. This is what i did to resolve this issue: Cleaned the Project Deleted the Build directory Restarted Android Studio Rebuild the Project Run Optionally (Go to the files menu on android, click on “Invalidate Caches / Restart…” uninstall the app on your phone and try again) And everything … Read more

In Android, how do I set margins in dp programmatically?

You should use LayoutParams to set your button margins: LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ); params.setMargins(left, top, right, bottom); yourbutton.setLayoutParams(params); Depending on what layout you’re using you should use RelativeLayout.LayoutParams or LinearLayout.LayoutParams. And to convert your dp measure to pixel, try this: Resources r = mContext.getResources(); int px = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, yourdpmeasure, … Read more

How can I get color-int from color resource?

You can use: getResources().getColor(R.color.idname); Check here on how to define custom colors: http://sree.cc/google/android/defining-custom-colors-using-xml-in-android EDIT(1): Since getColor(int id) is deprecated now, this must be used : ContextCompat.getColor(context, R.color.your_color); (added in support library 23) EDIT(2): Below code can be used for both pre and post Marshmallow (API 23) ResourcesCompat.getColor(getResources(), R.color.your_color, null); //without theme ResourcesCompat.getColor(getResources(), R.color.your_color, your_theme); //with … Read more

How to show soft-keyboard when edittext is focused

To force the soft keyboard to appear, you can use EditText yourEditText= (EditText) findViewById(R.id.yourEditText); yourEditText.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT); And for removing the focus on EditText, sadly you need to have a dummy View to grab focus. To close it you can use InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); This works … Read more

How to programmatically set drawableLeft on Android button?

You can use the setCompoundDrawables method to do this. See the example here. I used this without using the setBounds and it worked. You can try either way. UPDATE: Copying the code here incase the link goes down Drawable img = getContext().getResources().getDrawable(R.drawable.smiley); img.setBounds(0, 0, 60, 60); txtVw.setCompoundDrawables(img, null, null, null); or Drawable img = getContext().getResources().getDrawable(R.drawable.smiley); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)