Last active
August 29, 2015 14:02
-
-
Save extralam/47f9ffc3185bf2a552f1 to your computer and use it in GitHub Desktop.
Adjust the TextView TextSize immediately for any Android device.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.AlertDialog; | |
import android.app.Dialog; | |
import android.app.DialogFragment; | |
import android.app.FragmentManager; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.res.Resources; | |
import android.os.Bundle; | |
import android.util.DisplayMetrics; | |
import android.util.Log; | |
import android.util.TypedValue; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.LinearLayout; | |
import android.widget.SeekBar; | |
import android.widget.TextView; | |
/** | |
* ResizeTextHelper | |
* | |
* Adjust the TextView TextSize immediately for any Android device. | |
* Mostly for designer use. | |
* | |
* Copyright (c) 2014 @author extralam @ HongKong (http://ah-lam.com) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
* | |
* Usage: | |
* TextView textview = (TextView) fidnViewById(R.id.yourtextview); | |
* ResizeTextHelper.attach(getFragmentManager(), textview); | |
* | |
*/ | |
public class ResizeTextHelper { | |
/** | |
* | |
* @param fm Get the Current FragmentActivity Fragment Manager to generate the DialogFragment | |
* @param v V is the TextView or EditText , you want to adjust the text size. | |
*/ | |
public static void attach(FragmentManager fm , View v){ | |
if(v instanceof TextView){ | |
v.setOnClickListener(new ViewClick(fm)); | |
}else{ | |
return; | |
} | |
} | |
/** | |
* private function add on Click Listener | |
*/ | |
private static class ViewClick implements View.OnClickListener{ | |
FragmentManager fm; | |
private ViewClick(FragmentManager fm){ | |
this.fm = fm; | |
} | |
@Override | |
public void onClick(View v) { | |
ResizeTextHelper.showResizeTextDialog(fm, (TextView) v); | |
} | |
} | |
/** | |
* Create the DialogFragment | |
* @param fm | |
* @param orgTextView | |
*/ | |
private static void showResizeTextDialog(FragmentManager fm , TextView orgTextView) { | |
DialogFragment newFragment = new ResizeTextDialog(orgTextView); | |
newFragment.show(fm, "showResizeTextDialog"); | |
} | |
/** | |
* Main DialogFragment Class | |
*/ | |
private static class ResizeTextDialog extends DialogFragment{ | |
private final int FIXED_MAX_SIZE = 65; | |
private final int FIXED_TEXT_SIZE = 5; | |
private TextView mOrginalTextView; | |
private TextView mText; | |
private TextView mTextSizeView; | |
private SeekBar mSeekBar; | |
private float mOrgTextSize; | |
private ResizeTextDialog( TextView view) { | |
mOrginalTextView = view; | |
mOrgTextSize = mOrginalTextView.getTextSize(); | |
setCancelable(true); | |
} | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | |
Log.e("atest","mOrgTextSize L " + mOrgTextSize + "," ); | |
builder.setTitle("ID: " + | |
getResources().getResourceEntryName(mOrginalTextView.getId()) + | |
" - Text Size Adjustment"); | |
builder.setView(createView()); | |
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
mOrginalTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, | |
convertPixelsToDp(mOrgTextSize, getActivity())); | |
dialog.dismiss(); | |
} | |
}); | |
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
dialog.dismiss(); | |
} | |
}); | |
return builder.create(); | |
} | |
@Override | |
public void onCancel(DialogInterface dialog) { | |
mOrginalTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, | |
convertPixelsToDp(mOrgTextSize, getActivity())); | |
super.onCancel(dialog); | |
} | |
private View createView(){ | |
LinearLayout mContainer = new LinearLayout(getActivity()); | |
mContainer.setOrientation(LinearLayout.VERTICAL); | |
mTextSizeView = new TextView(getActivity()); | |
mTextSizeView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22); | |
mTextSizeView.setText("Size : " + convertPixelsToDp(mOrgTextSize, getActivity())); | |
mText = new TextView(getActivity()); | |
mText.setText(mOrginalTextView.getText()); | |
mText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, convertPixelsToDp(mOrgTextSize, getActivity())); | |
mSeekBar = new SeekBar(getActivity()); | |
mSeekBar.setMax(FIXED_MAX_SIZE); | |
mSeekBar.setProgress((int)convertPixelsToDp(mOrgTextSize , getActivity()) - FIXED_TEXT_SIZE); | |
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { | |
mTextSizeView.setText("Size : " + (progress + FIXED_TEXT_SIZE) + "dp"); | |
mText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, progress + FIXED_TEXT_SIZE); | |
mOrginalTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, progress + FIXED_TEXT_SIZE); | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar) { | |
} | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) { | |
} | |
}); | |
mContainer.addView(mText); | |
mContainer.addView(mTextSizeView); | |
mContainer.addView(mSeekBar); | |
return mContainer; | |
} | |
} | |
/** | |
* This method converts dp unit to equivalent pixels, depending on device density. | |
* | |
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels | |
* @param context Context to get resources and device specific display metrics | |
* @return A float value to represent px equivalent to dp depending on device density | |
*/ | |
private static float convertDpToPixel(float dp, Context context){ | |
Resources resources = context.getResources(); | |
DisplayMetrics metrics = resources.getDisplayMetrics(); | |
float px = dp * (metrics.densityDpi / 160f); | |
return px; | |
} | |
/** | |
* This method converts device specific pixels to density independent pixels. | |
* | |
* @param px A value in px (pixels) unit. Which we need to convert into dp | |
* @param context Context to get resources and device specific display metrics | |
* @return A float value to represent dp equivalent to px value | |
*/ | |
private static float convertPixelsToDp(float px, Context context){ | |
Resources resources = context.getResources(); | |
DisplayMetrics metrics = resources.getDisplayMetrics(); | |
float dp = px / (metrics.densityDpi / 160f); | |
return dp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment