I have created a project with custom button class as below.
package app.kibbeh.widget;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;
public class MyFontButton extends Button {
private static final String TAG = "TextView";
private Typeface typeface;
public MyFontButton(Context context) {
super(context);
}
public MyFontButton(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, "");
}
public MyFontButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, "");
}
private boolean setCustomFont(Context ctx, String asset) {
try {
if (this.typeface == null) {
Log.i(TAG, "asset:: fonts/" + asset);
this.typeface = Typeface.createFromAsset(ctx.getAssets(), "fonts/avenirroman.ttf");
}
setTypeface(this.typeface);
return true;
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "Could not get typeface: " + e.getMessage());
return false;
}
}
}
Now i have to add ripple effect from here. so is it possible or i have to add rippelview in all xml file ? please guide me.
Thank you..
try this link for set the ripple effect on button:
Material effect on button with background color
Android L's Ripple Effect - Touch Feedback for Buttons - Using XML
Related
My app creates different answers (radio buttons) to select based on what I have in arrays, but I can't figure out how to change the font of these answers. Usually I would just change the font in the layout.xml file, but I only have the radio group there, not the actual buttons.
Here is my code:
private void createRadioButton(View view) {
RadioGroup group = (RadioGroup)view.findViewById(R.id.radioGroup);
String[] answers = getResources().getStringArray(R.array.question_1_answers);
for (int i=0;i<answers.length;i++){
String answer = answers[i];
RadioButton button = new RadioButton(getActivity());
button.setText(answer);
group.addView(button);
}
}
I wanted to do something like this:
Typeface typeface = getResources().getFont(R.font.myfont);
button.setTypeface(typeface);
but that requires API 26 and I'm working with API 16
Try this :
RadioButton rb = (RadioButton) findViewById(R.id.radiobutton);
Typeface font = Typeface.createFromAsset(getAssets(), "MyFont.ttf");
rb.setTypeface(font);
Just create a CustomRaioButton and add it to your view:
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
public class CustomRadioButton extends android.support.v7.widget.AppCompatRadioButton {
public CustomRadioButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public CustomRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomRadioButton(Context context) {
super(context);
init(context);
}
private void init(Context context) {
Typeface typeface = getResources().getFont(R.font.myfont);
setTypeface(typeface);
a.recycle();
}
}
I need to set my shape as default background for all of my texts and I don't know how to do this, I want to add many TextView to my project so it's really important for me to do this, I've tried searching the internet but no luck :(
try this code
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView{
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomTextView(Context context) {
super(context);
init();
}
public void init() {
if(!isInEditMode()){
setBackgroundResource("Your shape file id ");// like r.drawable.shape_textview
}
}
}
I have a TextView that I'm looking to allow the user to search in for a specific string. If the string is found, it should highlight. Using a background span is too slow and awkward, so I am trying to figure out if I can just have it select the string. I know with EditText this would be possible using setSelection(), but I don't want the user to be able to edit the text, while still being able to manually highlight text, which I can't seem to do manage with an EditText.
I guess, then it's an either or; is it either possible to programmatically select text in a TextView or possible to allow text selection without allowing editing in an EditText?
Note: I'm actually using a custom view extending TextView, so I'm assuming it's either that or extend EditText; I'm just not sure which (if either) will work.
Not sure whether the question is still actual, I will provide my solution. Maybe will be useful for people coming from search engines.
So the purpose, as I understood, is to select all text in TextView without being able to modify its content. I didn't check how effective it is against very large text, but hope that not so bad.
Please note, API version should be >=11
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.Selection;
import android.text.Spannable;
import android.util.AttributeSet;
public class SelectableTextView extends TextView
{
public SelectableTextView(Context context)
{
super(context);
init();
}
public SelectableTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public SelectableTextView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SelectableTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init()
{
if (Build.VERSION.SDK_INT > 10)
setTextIsSelectable(true);
}
#Override
public boolean onTextContextMenuItem(int id)
{
switch (id)
{
case android.R.id.cut:
return true;
case android.R.id.paste:
return true;
case android.R.id.shareText:
{
String selectedText = getText().toString().substring(getSelectionStart(), getSelectionEnd());
if (selectedText != null && !selectedText.isEmpty())
{
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, selectedText);
sendIntent.setType("text/plain");
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(sendIntent);
}
return true;
}
case android.R.id.selectAll:
{
selectAllText();
return true;
}
}
return super.onTextContextMenuItem(id);
}
public void selectAllText()
{
if (Build.VERSION.SDK_INT > 10)
Selection.setSelection((Spannable) getText(), 0, length());
}
}
I'm using the new SlidingPaneLayout available in the latest support library, which provides the openPane() and closePane() methods to smooth open and close the panel. Unfortunately, there are no public methods to do so without animation.
Is there a way to still do this? I have a feeling reflection may be necessary.
P.S. The file is available under sdk/extras/android/support/v4/src/java/android/support/v4/widget/.
I ended up writing a subclass that provides two methods, openPaneNoAnimation() and closePaneNoAnimation(). Yes, it's reflection, and may stop working with future support libraries, but for now it does the job. Worst case, the methods fall back to using openPane() and closePane().
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.content.Context;
import android.support.v4.widget.SlidingPaneLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class AnimationlessSlidingPaneLayout extends SlidingPaneLayout {
private boolean mSlideEnabled = true;
private Field mSlideOffsetField = null;
private Field mSlideableViewField = null;
private Method updateObscuredViewsVisibilityMethod = null;
private Method dispatchOnPanelOpenedMethod = null;
private Method dispatchOnPanelClosedMethod = null;
private Field mPreservedOpenStateField = null;
private Method parallaxOtherViewsMethod = null;
public AnimationlessSlidingPaneLayout(Context context) {
this(context, null, 0);
}
public AnimationlessSlidingPaneLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AnimationlessSlidingPaneLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
try {
mSlideOffsetField = SlidingPaneLayout.class.getDeclaredField("mSlideOffset");
mSlideableViewField = SlidingPaneLayout.class.getDeclaredField("mSlideableView");
updateObscuredViewsVisibilityMethod = SlidingPaneLayout.class.getDeclaredMethod("updateObscuredViewsVisibility",
View.class);
dispatchOnPanelClosedMethod = SlidingPaneLayout.class.getDeclaredMethod("dispatchOnPanelClosed", View.class);
dispatchOnPanelOpenedMethod = SlidingPaneLayout.class.getDeclaredMethod("dispatchOnPanelOpened", View.class);
mPreservedOpenStateField = SlidingPaneLayout.class.getDeclaredField("mPreservedOpenState");
parallaxOtherViewsMethod = SlidingPaneLayout.class.getDeclaredMethod("parallaxOtherViews", float.class);
mSlideOffsetField.setAccessible(true);
mSlideableViewField.setAccessible(true);
updateObscuredViewsVisibilityMethod.setAccessible(true);
dispatchOnPanelOpenedMethod.setAccessible(true);
dispatchOnPanelClosedMethod.setAccessible(true);
mPreservedOpenStateField.setAccessible(true);
parallaxOtherViewsMethod.setAccessible(true);
} catch (Exception e) {
Log.w("ASPL", "Failed to set up animation-less sliding layout.");
}
}
public void openPaneNoAnimation() {
try {
View slideableView = (View) mSlideableViewField.get(this);
mSlideOffsetField.set(this, 1.0f);
parallaxOtherViewsMethod.invoke(this, 1.0f);
requestLayout();
invalidate();
dispatchOnPanelOpenedMethod.invoke(this, slideableView);
mPreservedOpenStateField.set(this, true);
} catch (Exception e) {
openPane();
}
}
public void closePaneNoAnimation() {
try {
View slideableView = (View) mSlideableViewField.get(this);
mSlideOffsetField.set(this, 0.0f);
parallaxOtherViewsMethod.invoke(this, 0.0f);
requestLayout();
invalidate();
updateObscuredViewsVisibilityMethod.invoke(this, slideableView);
dispatchOnPanelClosedMethod.invoke(this, slideableView);
mPreservedOpenStateField.set(this, false);
} catch (Exception e) {
closePane();
}
}
}
For an Android widget I've been developing, I'm trying to use a custom font. I've looked up some tutorials on the internet, and I found several ways to do it. I chose one and tried to implement it, but I get an error and I can't figure out why. The technique uses a seperate class (of which I've posted the code below), to set the font. In this code I get an error at the customFont line in setCustomFont. It says that customFont cannot be resolved to a variable. Can anybody help me out as to why this is happening?
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TypefacedTextView extends TextView {
private static final String TAG = "TextView";
public TypefacedTextView(Context context) {
super(context);
}
public TypefacedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TypefacedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
String customFontl = a.getString(R.styleable.TypefacedTextView_typeface);
setCustomFont(ctx, customFont); // get an error here
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: " + e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
String customFontl = a.getString(R.styleable.TypefacedTextView_typeface);
setCustomFont(ctx, customFont); // get an error here
Your variable is declared as customFontl (note final l), and you are trying to use it as customFont. I would guess the error is that customFont is not defined or something alike, as it is the case.
i have try this may it help you in this code my font is in asset/fonts folder
if it is only in asseet than replace in code fonts/
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
rotate();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
rotate();
}
public MyTextView(Context context) {
super(context);
init();
rotate();
}
private void rotate() {
// TODO Auto-generated method stub
setSelected(true);
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/TrajanPro-Regular.otf");
setTypeface(tf);
}
}
}