I'm a newbie to Android. I'm learning about PreferenceActivity. I need some guidance on How to display current value of EditTextPreference at right side of them. Like this:
--------------------------------------
|EditTextPreference value |
|"Summary" |
--------------------------------------
This is may code:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="com.example.prefereceproject.MainActivity" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog"/>
</RelativeLayout>
setting_preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:title="Your Age"
android:key="yourAge"
android:summary="Please provide your age"
android:inputType="numberDecimal">
</EditTextPreference>
</PreferenceScreen>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intentSetting = new Intent(MainActivity.this, Setting.class);
startActivityForResult(intentSetting, 1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int k = 0;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I would really be grateful for the help!
You need to create a custom view for that.
public class EditTextPreferenceWithValue extends EditTextPreference {
private TextView textValue;
public EditTextPreferenceWithValue(Context context) {
super(context);
setLayoutResource(R.layout.preference_with_value);
}
public EditTextPreferenceWithValue(Context context, AttributeSet attrs) {
super(context, attrs);
setLayoutResource(R.layout.preference_with_value);
}
public EditTextPreferenceWithValue(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.preference_with_value);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
textValue = (TextView) view.findViewById(R.id.preference_value);
if (textValue != null) {
textValue.setText(getText());
}
}
@Override
public void setText(String text) {
super.setText(text);
if (textValue != null) {
textValue.setText(getText());
}
}
}
preference_with_value.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<LinearLayout
android:id="@+id/preference_first_line"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dip"
android:layout_weight="1"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="@+id/preference_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="10"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:gravity="right"
android:ellipsize="end" />
</LinearLayout>
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/preference_first_line"
android:layout_alignLeft="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="2" />
</RelativeLayout>
</LinearLayout>
Great post! I "optimized" your xml a bit like this, a bit simpler.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="?android:attr/scrollbarSize">
<TextView
android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dip"
android:layout_weight="1"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:hint="title"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/preference_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="10"
android:ellipsize="end"
android:gravity="right"
android:hint="value"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@android:id/title"
android:layout_below="@android:id/title"
android:hint="summary"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceSmall"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With