I have the following layout file. I want to center the text in the second textView. In Android Studio design preview the text is centered. However, on running the app it is off the center.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="74dp"
android:background="#color/white">
<com.my.app.MyCustomTextView
android:id="#+id/custom_text_1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="15dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="15dp"
android:background="#drawable/rounded_corner_green"
android:gravity="center"
android:text="Some text here"
android:textColor="#color/white"
android:textSize="16sp" />
<com.my.app.MyCustomTextView
android:id="#+id/custome_text_2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:background="#drawable/circular_red"
android:gravity="center"
android:layout_gravity="end"
android:visibility="gone"
android:includeFontPadding="false"
android:textColor="#color/white"
android:textSize="16sp" />
</FrameLayout>
</layout>
This is how it looks (0 is not in the center)
Strangely though, if I replace my custom textView with just TextView the text is perfectly centered now.
This is the MyCustomTextView
public class MyCustomTextView extends TextView {
public MyCustomTextView (Context context) {
super(context);
initView();
}
public MyCustomTextView (Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public MyCustomTextView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MyCustomTextView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initView();
}
//here the typeface is set on the TextView after creating typeface from font asset
private void initView() {
CommonUtils.setCustomFontForText(MyApplication.getAppContext(), MyCustomTextView.this);
}
}
Can someone help me? What is going wrong here? Thanks!!
Related
In TextInputLayout I want to add TextInputEditText via Custom class
Here is the code for custom class I have created :
public class CustomTextInputEditText extends TextInputLayout {
private TextInputEditText editText;
public CustomTextInputEditText(#NonNull Context context) {
this(context, null);
}
public CustomTextInputEditText(#NonNull Context context, #Nullable AttributeSet attrs) {
this(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
}
public CustomTextInputEditText(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
removeAllViews();
setWillNotDraw(false);
editText = new TextInputEditText(getContext());
createEditBox(editText);
}
private void createEditBox(TextInputEditText editText) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editText.setPadding(0,10,0,0);
editText.setLayoutParams(layoutParams);
}
}
Here is the Xml code :
<com.example.myapplication.CustomTextInputEditText
android:id="#+id/custom_view"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:boxStrokeColor="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"/>
It doesn't show any preview of the custom view
public class CustomTextInputEditText extends TextInputLayout {
private TextInputEditText editText;
public CustomTextInputEditText(#NonNull Context context) {
this(context, null);
}
public CustomTextInputEditText(#NonNull Context context, #Nullable AttributeSet attrs) {
this(context, attrs, R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox);
}
public CustomTextInputEditText(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
//removeAllViews();
setWillNotDraw(false);
editText = new TextInputEditText(getContext());
createEditBox(editText);
}
private void createEditBox(TextInputEditText editText) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
editText.setPadding(0,10,0,0);
editText.setLayoutParams(layoutParams);
addView(editText);
}
}
Instead of extending it with TextInputLayout try extending it with constraint layout and add your custom view with editText and InputField then inflate view
custom_material_edit_text.xml
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
style="#style/contentInputLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_8dp"
android:paddingStart="5dp"
android:paddingLeft="5dp"
android:layout_marginLeft="#dimen/_8dp"
android:layout_marginEnd="#dimen/_8dp"
android:layout_marginRight="#dimen/_8dp"
app:errorEnabled="false"
app:layout_constraintBottom_toTopOf="#+id/errorTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:hint="#string/EnterMobileNumber">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:gravity="start"
android:textDirection="anyRtl"
tools:text="03-xxxxxxx"
android:background="#null"
android:paddingEnd="#dimen/inputFieldPadding_36dp"
android:paddingRight="#dimen/inputFieldPadding_36dp" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
android:id="#+id/cancelButton"
android:layout_width="#dimen/inputIcon_20dp"
android:layout_height="#dimen/inputIcon_20dp"
android:layout_marginEnd="#dimen/iconMargin_16dp"
android:layout_marginRight="#dimen/iconMargin_16dp"
android:src="#mipmap/close"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#+id/textInputLayout"
app:layout_constraintEnd_toEndOf="#+id/textInputLayout"
app:layout_constraintTop_toTopOf="#+id/textInputLayout"
app:layout_constraintVertical_bias=".48" />
<ImageView
android:id="#+id/keyboardButton"
android:layout_width="#dimen/inputIcon_20dp"
android:layout_height="#dimen/inputIcon_20dp"
android:layout_marginEnd="#dimen/iconMargin_16dp"
android:layout_marginRight="#dimen/iconMargin_16dp"
android:src="#mipmap/label_keyboard"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="#+id/textInputLayout"
app:layout_constraintEnd_toEndOf="#+id/textInputLayout"
app:layout_constraintRight_toRightOf="#+id/textInputLayout"
app:layout_constraintTop_toTopOf="#+id/textInputLayout"
app:layout_constraintVertical_bias=".48" />
<TextView
android:id="#+id/errorTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_8dp"
android:layout_marginLeft="#dimen/_8dp"
android:layout_marginEnd="#dimen/_8dp"
android:layout_marginRight="#dimen/_8dp"
android:visibility="gone"
android:textColor="#color/red"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout"
tools:text="Invalid data" />
inflate(context, R.layout.custom_material_edit_text, this);
<com.example.widget.MaterialInputView
android:id="#+id/materialEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
app:floatingLabel="Amount (Rs.)"
android:inputType="number"
android:hint="00000-XXXXXX" />
I have problem in android layout,that I don't know what is the problem.
Actually I want to change the font of the text beside the image.
When I don't put any text inside the textViews in xml code and I set the text in java code and also set the typeface,I got this problem.
This is my
group_view_two_4_first_activity.xml file:
<android.support.v7.widget.CardView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/sport_complex_name"
app:layout_constraintTop_toTopOf="#+id/sport_complex_name">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/field" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/sport_complex_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textColor="#565657"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/hallNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="#7E7E7F"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="#+id/sport_complex_name"
app:layout_constraintTop_toBottomOf="#+id/sport_complex_name" />
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7E7E7F"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/hallNumber"
app:layout_constraintEnd_toStartOf="#+id/hallNumber"
app:layout_constraintTop_toTopOf="#+id/hallNumber" />
</android.support.constraint.ConstraintLayout>
And this is its java file:GroupViewTwo4FirstActivity
public class GroupViewTwo4FirstActivity extends ConstraintLayout {
View rootView;
TextView sportComplexName;
TextView hallNumber;
TextView number;
String txtNo;
String complexName;
public GroupViewTwo4FirstActivity(Context context) {
super(context);
init(context);
}
public GroupViewTwo4FirstActivity(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context);
}
public GroupViewTwo4FirstActivity(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GroupViewTwo4FirstActivity, defStyleAttr, 0);
txtNo = ta.getString(R.styleable.GroupViewTwo4FirstActivity_hallNo);
complexName = ta.getString(R.styleable.GroupViewTwo4FirstActivity_complex_name);
ta.recycle();
init(context);
}
private void init(Context context) {
rootView = inflate(context, R.layout.group_view_two_4_first_activity, this);
Typeface typeface = EnglishToPersian.createTypeFace1(context);
sportComplexName = rootView.findViewById(R.id.sport_complex_name);
hallNumber = rootView.findViewById(R.id.hallNumber);
number = rootView.findViewById(R.id.number);
sportComplexName.setTypeface(typeface);
hallNumber.setTypeface(typeface);
number.setTypeface(typeface);
number.setText(EnglishToPersian.englishToPersian(txtNo));
hallNumber.setText("سالن شماره ");
sportComplexName.setText("مجتمع ورزشی افق لاله");
}
}
Everything is OK,But when I run the program,instead of getting this picture:
I got this one:
The constructor being called while inflating from xml is this one:
public GroupViewTwo4FirstActivity(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context);
}
this(context,attrs,0) calls your third constructor, which after while calls init(context) and then you call init(context) again, so you're doing that twice.
Defining your constructors as below guarantee that your init method gets called exactly once each time.
public GroupViewTwo4FirstActivity(Context context) {
this(context, null);
}
public GroupViewTwo4FirstActivity(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GroupViewTwo4FirstActivity(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GroupViewTwo4FirstActivity, defStyleAttr, 0);
txtNo = ta.getString(R.styleable.GroupViewTwo4FirstActivity_hallNo);
complexName = ta.getString(R.styleable.GroupViewTwo4FirstActivity_complex_name);
ta.recycle();
init(context);
}
Hope that helps!
I got a bunch of form controls that all show an Icon to the left, like so:
For this I have this code:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="#dimen/icon_edittext_height"
android:layout_gravity="top"
android:layout_marginRight="#dimen/icon_edittext_marginRight"
android:layout_marginTop="#dimen/icon_edittext_margintop"
android:src="#drawable/ic_location_city_black_24dp"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/create.data.city.layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<AutoCompleteTextView
android:id="#+id/create.data.city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/create_event_city"
android:imeOptions="actionNext"
android:inputType="textCapSentences"
android:maxLines="1"
/>
</android.support.design.widget.TextInputLayout>
</FrameLayout>
</LinearLayout>
Now because I have around 20 controls, I dont want to repeat the:
<LinearLayout>
<ImageView/>
<FrameLayout>
...content....
</FrameLayout>
</LinearLayout>
schema 20 times, but I would like to do something like this:
<MycustomLayout>
<EditText .../>
</MycustomLayout>
<MycustomLayout>
<Spinner ... />
</MycustomLayout>
So basically I want to create a custom Layout that extends Linear, and I want to be able to use it in the same way that I use LinearLayout, while having an icon prepended at all times.
You can try using include tag in your layout to include the part which is repeating. Or if that doesn't work you can try something like this:
public class CustomControl extends LinearLayout {
public CustomControl (Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_layout, this);
}
}
Where custom_layout is the layout for you custom view.
Then you can just do in your xml:
<CustomControl />
UPD:
In order to add child views in xml just override onFinishInflate and add views like this:
protected void onFinishInflate() {
View[] children = detachChildren();
for (int i = 0; i < children.length; i++)
this.addView(children[i]);
}
No need to add too much layouts. Just try to use this...
<android.support.design.widget.TextInputLayout
android:id="#+id/create.data.city.layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/create.data.city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="City"
android:drawableLeft="#drawable/ic_location_city_black_24dp"
android:drawablePadding="5dp"
android:imeOptions="actionNext"
android:inputType="textCapSentences"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
Solved it myself like so:
public class IconFieldLinearLayout extends LinearLayout {
private Drawable mDefaultDrawable;
public IconFieldLinearLayout(Context context) {
super(context);
init(context, null);
}
public IconFieldLinearLayout(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public IconFieldLinearLayout(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public IconFieldLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
this.setOrientation(HORIZONTAL);
mDefaultDrawable = getResources().getDrawable(R.drawable.tv_avatar_default);
initAttr(context, attrs);
}
private void initAttr(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.IconFieldLinearLayout, 0, 0);
Drawable icon = a.getDrawable(R.styleable.IconFieldLinearLayout_icon);
a.recycle();
initIcon(context, icon);
}
else {
initIcon(context, mDefaultDrawable);
}
}
private void initIcon(Context context, Drawable icon) {
ImageView imageView = new ImageView(context);
imageView.setColorFilter(ContextCompat.getColor(context, R.color.colorAccent));
int dimension = getResources().getDimensionPixelSize(R.dimen.icon_edittext_height);
int marginRight = getResources().getDimensionPixelSize(R.dimen.icon_edittext_marginRight);
int marginTop = getResources().getDimensionPixelSize(R.dimen.icon_edittext_margintop);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dimension);
layoutParams.gravity = Gravity.TOP;
layoutParams.setMargins(0, marginTop, marginRight, 0);
imageView.setImageDrawable(icon);
addView(imageView, layoutParams);
}
}
Did not really need to put the FrameLayout in there.
In my app, I am creating views with extending relative class(For text view i am extending native textview in my java class) because I want to give custom font and don't want to apply font in every individual text view. For this I am using below code
public class LoyaltyTextView extends TextView {
private final static String DROID_SANS = "droid_sans";
public LoyaltyTextView(Context context) {
super(context);
}
public LoyaltyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
parseAttributes(context, attrs);
}
public LoyaltyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
parseAttributes(context, attrs);
}
private void parseAttributes(Context context, AttributeSet attrs) {
TypedArray values = context.obtainStyledAttributes(attrs,R.styleable.LoyaltyTextView);
String typeface = values.getString(R.styleable.LoyaltyTextView_customTypeFace);
if(typeface.equalsIgnoreCase(DROID_SANS)) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "DroidSans.ttf"));
}
values.recycle();
}
}
and i am using above custom control in xml as mentioned below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_splash">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_above="#+id/tv_logo"
android:layout_centerHorizontal="true"
android:src="#drawable/logo_m" />
<customconstrols.LoyaltyTextView
android:id="#+id/tv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="#string/loyalty_logo"
android:textSize="#dimen/logo_text_size"
android:textStyle="bold"
app:customTypeFace="DROID_SANS" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.customconstrols.LoyaltyTextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="#string/login"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
but when I use simple text views (as native android widget) it works fine but when i use above custom control preview section starts giving me error as mentioned in picture
I have tried number of solution which were mentioned in different post like clear cache/invalidate option and refresh layout and cleaning project and rebuilding it but nothing worked for me. Anything I am missing?
Replace your code as following line in your xml
xmlns:font="http://schemas.android.com/apk/res-auto"
Run your app,It will work fine.
public class MyFontTextView extends TextView {
private Typeface typeface;
public MyFontTextView(Context context) {
super(context);
}
public MyFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public MyFontTextView(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.app);
String customFont = a.getString(R.styleable.app_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
private boolean setCustomFont(Context ctx, String asset) {
try {
if (typeface == null) {
typeface = Typeface.createFromAsset(ctx.getAssets(),
"fonts/OpenSans-Regular.ttf");
}
} catch (Exception e) {
AndyUtils.generateLog("Exception in component"+e);
return false;
}
setTypeface(typeface);
return true;
}
}
And in values -> attr.xml
<declare-styleable name="app">
<attr name="customFont" format="string" />
</declare-styleable>
Try this
In fact i know how to open an activity by clicking on a button
But i can't do it in frame layout
i try this code but it didn't help me.
FrameLayout ff=(FrameLayout)findViewById(R.id.btn);
ff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(listActivity.this,bookActivity.class);
startActivity(intent);
}
});
that's all
EDITES:
this my activity_main :
<LinearLayout
android:id="#+id/linbook"
android:layout_margin="15dp"
android:layout_toRightOf="#+id/linjozve"
android:layout_width="130dp"
android:layout_height="180dp"
android:orientation="vertical">
<FrameLayout
android:id="#+id/ketabbtn"
android:layout_width="130dp"
android:layout_height="130dp">
<ImageButton
android:layout_weight="1"
android:id="#+id/btntopright"
android:layout_width="130dp"
android:layout_height="130dp"
android:background="#drawable/rgreen"/>
<ImageButton
android:background="#drawable/book"
android:layout_gravity="center"
android:layout_width="80dp"
android:layout_height="80dp" />
</FrameLayout>
<TextView
android:layout_marginTop="15dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="book"/>
</LinearLayout>
thanks
Try to set android:clickable in the xml.
If that doesn't work try to create a custom layout:
By default, onInterceptTouchEvent returns false. So include following code and use this CustomClickableFrameLayout instead of framelayout
public class CustomClickableFrameLayout extends FrameLayout {
private OnClickListener mOnClickListener;
#Override
public void setOnClickListener(OnClickListener l) {
super.setOnClickListener(l);
mOnClickListener = l;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mOnClickListener != null;
}
// Standard constructors — just pass everything
public CustomClickableFrameLayout (final Context context) {
super(context);
}
public CustomClickableFrameLayout (final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public CustomClickableFrameLayout (final Context context, final AttributeSet attrs, final int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomClickableFrameLayout (final Context context, final AttributeSet attrs, final int defStyleAttr, final int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
The problem is it is quite hard to click on a FrameLayout if it is covered by other views. In your case, the framelayout and the imagebutton have the same size 130x130. This explains why the touch cannot reach the layout.
To make sure you can click it, add some padding between the layout and its childs. (I reduced the size of its child)
<FrameLayout
android:padding="10dp"
android:id="#+id/ketabbtn"
android:layout_width="130dp"
android:layout_height="130dp">
<ImageButton
android:layout_weight="1"
android:id="#+id/btntopright"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#drawable/rgreen"/>
<ImageButton
android:background="#drawable/book"
android:layout_gravity="center"
android:layout_width="80dp"
android:layout_height="80dp" />
</FrameLayout>
Also, you used wrong ID. So change this:
FrameLayout ff=(FrameLayout)findViewById(R.id.btn);
To this:
FrameLayout ff=(FrameLayout)findViewById(R.id.ketabbtn);
So you can click the padding space to trigger the onClick event.