Reputation: 691
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:
Upvotes: 0
Views: 41
Reputation: 241
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!
Upvotes: 1