Created
November 4, 2014 03:39
-
-
Save codeguru42/61d309048fcc358f925f to your computer and use it in GitHub Desktop.
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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity"> | |
<codeguru.labelededittext.LabeledEditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:inputType="number" | |
app:editWeight="7" | |
app:hint="@string/hint" | |
app:label="@string/label" | |
app:labelWeight="3"/> | |
</RelativeLayout> |
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
package codeguru.labelededittext; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.util.AttributeSet; | |
import android.view.ViewGroup; | |
import android.widget.EditText; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
public class LabeledEditText extends LinearLayout { | |
private TextView label; | |
private EditText edit; | |
public LabeledEditText(Context context) { | |
this(context, null); | |
} | |
public LabeledEditText(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public LabeledEditText(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
label = new TextView(context); | |
edit = new EditText(context); | |
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LabeledEditText, | |
0, 0); | |
int[] set = {android.R.attr.inputType}; | |
TypedArray b = context.getTheme().obtainStyledAttributes(attrs, set, 0, 0); | |
try { | |
int labelWeight = a.getInt(R.styleable.LabeledEditText_labelWeight, 0); | |
int editWeight = a.getInt(R.styleable.LabeledEditText_editWeight, 0); | |
int totalWeight = labelWeight + editWeight; | |
ViewGroup.LayoutParams labelParams = new LinearLayout.LayoutParams( | |
0, LayoutParams.WRAP_CONTENT, (float)labelWeight / (float)totalWeight); | |
label.setLayoutParams(labelParams); | |
ViewGroup.LayoutParams editParams = new LinearLayout.LayoutParams( | |
0, LayoutParams.WRAP_CONTENT, (float)editWeight / (float)totalWeight); | |
label.setLayoutParams(editParams); | |
label.setText(a.getString(R.styleable.LabeledEditText_label)); | |
edit.setHint(a.getString(R.styleable.LabeledEditText_hint)); | |
edit.setInputType(b.getInt(0, 0)); | |
} finally { | |
a.recycle(); | |
} | |
this.addView(label); | |
this.addView(edit); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment