How to let user know, an ImageView is clickable - android

I have an ImageView(see image below), which is clickable. When the user clicks on the ImageView, he gets redirected to website. This is all running fine.
My question is, how to let user know, he can click the image, how to make this feature visible?
I can add text under the image, but thats not, what i want. I would like some small icon or window(like dialog), which can be closed.
Thanks!

you can check by bellow:
imageViewObject.isClickable();

Maybe you could animate a small finger/hand icon tapping the image when the ImageView shows up to indicate the element is clickable.
Like this: http://cmsresources.windowsphone.com/windowsphone/en-us/How-to/wp7/block/gettingstarted-concept-tap-and-hold-gesture.png
Or you could use gradients and shadows to give the image a button look.

So I searched and found this. This is #kgandroid suggested.
I add a little bit of code:
private FrameLayout mFrame;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFrame = new FrameLayout(this);
mFrame.addView(LayoutInflater.from(getBaseContext()).inflate(R.layout.activity_single_list_view, null));
setContentView(mFrame);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean overlayShown = settings.getBoolean("overlayShown", false);
if(!overlayShown){
setupHelpOverlay();
}
...
}
private void setupHelpOverlay(){
final View tutView = LayoutInflater.from(getBaseContext()).inflate(R.layout.activity_detail_overlay, null);
if(mFrame != null){
mFrame.addView(tutView);
}
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("overlayShown", true);
// Commit the edits!
editor.commit();
Button btnDismiss = (Button) tutView.findViewById(R.id.button_overlay);
btnDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
mFrame.removeView(tutView);
}
});
}
And the activity_deatil_overlay.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent_gray" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:gravity="center"
android:textColor="#color/white"
android:text="#string/overlay_text"
android:paddingBottom="10dp" />
<View
android:id="#+id/textView1"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="#+id/button_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/overlay_button"
android:textColor="#color/white"
android:paddingTop="10dp" />
</RelativeLayout>
On the first start, it shows help overlay.

You could put a bar around the Image and change its color when the image is pressed. So it looks like a button.

Related

how to add buttons that appear dynamically in a dialog using android studio

I have a dialog that shows an error message,
i know i can take that error message and just make it invisible, and visible in the code,
but then the Dialog would still save room in it and it will just show as a white space.
I want the error message to be added dynamical so if no error message will be shown the dialog will be sized accordingly.
how do i do that ?
here is my Dialog >
<?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:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="#+id/textContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="14">
<EditText
android:id="#+id/barcode_activity_input_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:ems="10"
android:hint="Enter Serial Number"
android:inputType="number"
android:maxLength="14" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/barcode_activity_button_cancel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/barcode_activity_manual_input_check_box"
android:text="Cancel" />
<Button
android:id="#+id/barcode_activity_button_ok"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#+id/barcode_activity_manual_input_check_box"
android:layout_toEndOf="#+id/barcode_activity_button_cancel"
android:text="Ok" />
<TextView
android:id="#+id/barcode_activity_text_view_warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textContainer"
android:layout_toRightOf="#+id/barcode_activity_image_view_warning"
android:paddingTop="5dp"
android:text="Serial doesn't match Workorder"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#f00000"
android:textSize="13dp" />
<ImageView
android:id="#+id/barcode_activity_image_view_warning"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="#+id/textContainer"
android:background="#drawable/warning" />
<CheckBox
android:id="#+id/barcode_activity_manual_input_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/barcode_activity_image_view_warning"
android:text="Allow Serial In Workorder"
android:textSize="13dp" />
</RelativeLayout>
and here is my main file >
inputFieldDialog = new Dialog(this); // CASTING
//inputFieldDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
inputFieldDialog.setContentView(R.layout.aligment_manager_manual_input_layout); // INFLATING VIEW
Button cancelInputButton, confirmInputButton; //declaring BOXES
final EditText inputField;
TextView warningTextView;
ImageView warningImageView;
CheckBox warningCheckBox;
cancelInputButton = (Button) inputFieldDialog.findViewById(R.id.barcode_activity_button_cancel); //casting
confirmInputButton = (Button) inputFieldDialog.findViewById(R.id.barcode_activity_button_ok); //casting
inputField = (EditText) inputFieldDialog.findViewById(R.id.barcode_activity_input_editText); //casting
warningTextView = (TextView) inputFieldDialog.findViewById(R.id.barcode_activity_text_view_warning); //casting
warningImageView = (ImageView) inputFieldDialog.findViewById(R.id.barcode_activity_image_view_warning); //casting
warningCheckBox = (CheckBox) inputFieldDialog.findViewById(R.id.barcode_activity_manual_input_check_box); //casting
warningTextView.setVisibility(TextView.INVISIBLE); //sets warnings invisible
warningImageView.setVisibility(ImageView.INVISIBLE); //sets warnings invisible
warningCheckBox.setVisibility(CheckBox.INVISIBLE); //sets warnings invisible
cancelInputButton.setOnClickListener(new View.OnClickListener() { //listeners for the buttons
#Override
public void onClick(View v) {
inputFieldDialog.hide();
}
});
confirmInputButton.setOnClickListener(new View.OnClickListener() { //listeners for the buttons
#Override
public void onClick(View v) {
scanResults = String.valueOf(inputField.getText());
scanBarcodeText.setText(inputField.getText());
editor.putString("boxID", String.valueOf(inputField.getText())); //saves the data as antenaNamein shared prefrences
editor.commit();
if (checkIfIdMatched(String.valueOf(inputField.getText())) == true) { //checkks if id is maching the one in the workorder
aligmentManagerClass.scanFromBarcodeApproved(); //ID MACHED
if (mainDialog != null) {
mainDialog.show();
}
loaderScreenMainText.setText("initlizing WiFi");//shows the user on screen message
wifiWrapper myWifiWrapper = new wifiWrapper(); //- INIZILIZING WIFI
myWifiWrapper.checkWifiState(getApplication(), callbackFunctionForisWifiOn);
} else {
loaderScreenMainText.setText("Scan Doesn't Match Data In Workoder"); // notify onScreen User
if (mainDialog != null) { // hides the loading dialog
mainDialog.hide();
}
AlertDialog wrongNumberDialog = getAlertDialogForWrongId(); //shows to user alert dialog for Wrong Number
wrongNumberDialog.show(); // show it
}
}
For remove white space...
Use "View.GONE" property of setVisibility() method rather than "TextView.INVISIBLE".
Set below code for hide views
warningTextView.setVisibility(View.GONE);
warningImageView.setVisibility(View.GONE);
warningCheckBox.setVisibility(View.GONE);
And for visible view just user
warningTextView.setVisibility(View.VISIBLE);
warningImageView.setVisibility(View.VISIBLE);
warningCheckBox.setVisibility(View.VISIBLE);
You can add buttons dynamically by creating buttons in Java file
Add this XML to dialog like dialog.addView(...)
Then Create Dynamically Buttons by according to values like
for(){
Create Buttons and add to above layout
layout.addView(button);
}
Hope it will help.

Android: Change background colour of specific activity using SharedPreferences

I am trying to change the background colour of a activity where the user can change the colour using 3 radio buttons in an android app. I am doing this using sharedPreferences.
So I have the activity page where the colour is chosen with the radio group and the code to use sharedPrefernces looks like this (i know the switch statement works aswell as I have a toast message apperring when the colour is supposed to change):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
String colourSelected = "";
switch (checkedId) {
case R.id.radioButton1 :
colourSelected = "YELLOW";
editor.putString("colour", colourSelected);
editor.commit();
break;
case R.id.radioButton2 :
colourSelected = "YELLOW";
editor.putString("colour", colourSelected);
editor.commit();
break;
case R.id.radioButton3 :
colourSelected = "BLUE";
editor.putString("colour", colourSelected);
editor.commit();
break;
}
}
});
}
The XML looks like this
<LinearLayout 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:orientation="vertical"
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.practical3_10327751_donnacha_holmes.Preferences" >
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radiogroup"
>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yellow" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
Then there is the activity that changes the background colour, which looks like this when trying to change the colour:
public class Activity2 extends ActionBarActivity {
RelativeLayout rl;
String colour;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE);
colour = prefs.getString("Colour", "WHITE");
rl = (RelativeLayout)findViewById(R.id.RelativeLayout);
if(colour=="GREEN"){
rl.setBackgroundColor(Color.GREEN);
}
else if(colour=="YELLOW"){
rl.setBackgroundColor(Color.YELLOW);
}
else if(colour=="BLUE"){
rl.setBackgroundColor(Color.BLUE);
}
else{
rl.setBackgroundColor(Color.RED);
}
I know that the background colour is being changed because it is being set to red every time I go to this page.
Thanks for any help!
The == operator checks to see if the two strings are exactly the same object.
The .equals() method will check if the two strings have the same value.
Therefore to compare Strings use .equals(),i.e. rewrite your comparison as
if(colour.equals("GREEN")){
rl.setBackgroundColor(Color.GREEN);
}
else if(colour.equals("YELLOW")){
rl.setBackgroundColor(Color.YELLOW);
}
else if(colour.equals("BLUE")){
rl.setBackgroundColor(Color.BLUE);
}
else{
rl.setBackgroundColor(Color.RED);
}
and you are saving value as colour and try to retrieving as Colour,so change
colour = prefs.getString("Colour", "WHITE");
to
colour = prefs.getString("colour", "WHITE");

Android: Unable to Prevent buttons from increasing size and overlapping with other buttons

I am new to Android Programming, what I am trying to do in this Android Application is to create a xml page filled with buttons.
When I click the button, the button would change to light green color and when I click it again, it would change to light grey
The error: I am getting is when I click the button, it increases in size and overlaps with the other buttons, please help me out here, it is not user friendly in this case
attached below is the code:
lockerbooking.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/sisButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="28dp"
android:text="#string/sis"
/>
<Button
android:id="#+id/solButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/soeButton"
android:layout_alignBottom="#+id/soeButton"
android:layout_alignParentRight="true"
android:text="#string/sol" />
<Button
android:id="#+id/soeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/sisButton"
android:layout_alignBottom="#+id/sisButton"
android:layout_centerHorizontal="true"
android:text="#string/soe" />
</RelativeLayout>
Code:
makeBooking.java
public class makeBooking extends Activity {
Button sisButton;
Button solButton;
Button soeButton;
Button sobButton;
super.onCreate(savedInstanceState);
// Get the message from the intent
setContentView(R.layout.lockerbookpage);
Intent intent = getIntent();
// Initialize TextViews
sisButton = (Button) findViewById(R.id.sisButton);
solButton = (Button) findViewById(R.id.solButton);
soeButton = (Button) findViewById(R.id.soeButton);
sobButton = (Button) findViewById(R.id.sobButton);
}
public OnClickListener solButtonListener = new OnClickListener(){
boolean flag = true;
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(flag){
solButton.setBackgroundColor(Color.GREEN);
}
else{
solButton.setBackgroundColor(Color.LTGRAY);
}
flag=!flag;
}
};
...The code goes on
Please help me out here, I am eager to learn
to avoid overlapping of buttons, use fixed width and height for buttons:
change this:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to some this like this:
android:layout_width="100dp" //what ever size suits your layout
android:layout_height="50dp" //fixing this,will not overlap the buttons

Android toggle button display wrong value after orientation change

Here is my problem.
I've got a simple activity which set a layout, and add rows in a table-layout(itself in a scroll view).
Those table-rows have a custom layout with a text-field and a toggle button.
Each toggle button has a value taken from a database, and when I first create the activity, the values are OK. But when I turn the device and then change the orientation, all the toggles-button take "false" value. I printed the values that I set in the Logcat, and the values are the good ones (those in the database).
I thought something like the layout I want is hidden behind another layout, but I made some tests and the text-fields change with new values, so I really don't understand why the toggle buttons don't work.
Here is the code :
TableRow layout :
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="#+id/relativelayout_row_parametres"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1.0">
<TextView
android:id="#+id/textview_row_parametres"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginLeft="2dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
/>
<ToggleButton
android:id="#+id/togglebutton_row_parametres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="2dp"
android:textOn="#string/togglebutton_on"
android:textOff="#string/togglebutton_off" />
</RelativeLayout>
</TableRow>
Activity Layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView style="#style/header" />
<LinearLayout
android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="0.8"
android:background="#drawable/gradient_bg"
android:padding="10dip"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="0.85"
android:fillViewport="true"
android:padding="10dip"
android:layout_gravity="center">
<TableLayout
android:id="#+id/tablelayout_parametres"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/cornered_bg"
android:paddingTop="5dip"
android:paddingBottom="5dip">
</TableLayout>
</ScrollView>
<Button
android:id="#+id/button_parametres_accept"
android:gravity="center"
android:text="#string/accept_changes"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.15"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
And the activity code:
public class Parameters extends Activity {
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Map<String, Boolean> changes = new HashMap<String, Boolean>();
final Context ctx = getApplicationContext();
LanguageManager.updateConfig(this);
setContentView(R.layout.parametres);
CountryDB[] countries = Database.instance(getApplicationContext()).getCountries();
TableLayout tabLayout = (TableLayout) findViewById(R.id.tablelayout_parametres);
for(int i =0; i<countries.length; i++){
TableRow newRow = (TableRow) getLayoutInflater().inflate(R.layout.row_parametres, null);
TextView textView = (TextView) newRow.findViewById(R.id.textview_row_parametres);
ToggleButton toggleButton = (ToggleButton) newRow.findViewById(R.id.togglebutton_row_parametres);
toggleButton.setChecked(countries[i].isToSynchronize());
toggleButton.setTag(countries[i]);
Log.e("setChecked",""+toggleButton.getId()+"/"+countries[i].isToSynchronize());
textView.setText(countries[i].getLabel());
toggleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CountryDB countryTemp = (CountryDB) v.getTag();
changes.put(countryTemp.getLabel(), ((ToggleButton)v).isChecked());
}
});
tabLayout.addView(newRow);
TableRow rowDivider = (TableRow) getLayoutInflater().inflate(R.layout.row_divider, null);
tabLayout.addView(rowDivider);
}
Button buttonValidation = (Button) findViewById(R.id.button_parameters_accept);
buttonValidation.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Iterator<String> iterator = changes.keySet().iterator();
while(iterator.hasNext()){
String stringTemp = iterator.next();
Database.instance(ctx).updateCountry(stringTemp, changes.get(stringTemp));
}
Intent intent = new Intent(ctx, Splash.class);
String result = "restart";
String from = "parameters";
Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
returnIntent.putExtra("from", from);
setResult(RESULT_OK, returnIntent);
startActivity(intent);
}
});
}
}
In the Log.e, I print the values, and they are good, the display on togglebuttons is wrong, they are just all "false".
Thanks for your help.
Between onCreate() and onResume() , Android tries to restore the old state of the toggle Buttons. Since they don't have unique ID's , Android wont succeed and everything is false again. Try to move your setChecked() calls into onResume() ( maybe onStart() works too).
Here is a pretty good answer to the same Question:
ToggleButton change state on orientation changed
you have to save data before Orientation.
Android have method onSaveImstamceState(Bundle outState) and onRestoreInstanceState(BundleInstaceState)
override these method in Activity.
There's a simpler solution: you only need to add configChanges property to your activity declaration, like stated here. This way you can prevent Activity restart when orientation changes. So in your manifest you should have something like
<activity android:name=".Parameters"
android:configChanges="orientation|keyboardHidden">
or if your buildTarget>=13
<activity android:name=".Parameters"
android:configChanges="orientation|keyboardHidden|screenSize">
Edit: Like reported on comments below, this is not an optimal solution. The main drawback is reported in a note of the document linked above:
Note: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.

How to attach OnClickListener to a button in a custom preference layout?

I have a preference screen that is populated with items from a database. I have this working by creating my own PreferenceActivity. In the activity I create DialogPreference items and add them to my PreferenceCategory To style to preference item on the screen I use a custom layout and apply it using setLayoutResource(R.layout.custom_pref_row)
This basically adds an ImageButton to the view aligned to the right of the layout. This all works fine and my preference screen shows the custom view with the button. My question is how do I attach a click listener to the button in the custom view? I was not able to find a way to get at View for the row from the PreferenceActivity. If my items were not created dynamically I might be able to do this all from XML and then reference the id or the button, but I can do that because I am creating the list dynamically.
Any suggestions on how to get a handle on the ImageButton for each item? In the end I want to configure the button to launch a delete confirmation dialog.
R.layout.custom_pref_row:
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="#+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignLeft="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="2" />
<ImageButton android:id="#+id/pref_delete_station" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/ic_trash_can" android:layout_alignParentRight="true" android:background="#null"></ImageButton>
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="#+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout>
Related part of my PreferenceActivity:
DialogPreference diaPref;
for (Station mStation : sList) {
diaPref = new StationEditor(this.getPreferenceScreen().getContext(), null, this, mStation);
diaPref.setLayoutResource(R.layout.custom_pref_row);
diaPref.setTitle(mStation.getName());
diaPref.setKey(STATION_PREFIX + mStation.getId());
// add new preference
stationTypesCategory.addPreference(diaPref);
}
You can extend DialogPreference and override the onBindDialogView(View view). Inside this method you can do:
#Override
protected void onBindDialogView(View view) {
((ImageButton) view.findViewById(R.id.pref_delete_station)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
super.onBindDialogView(view);
}
Your sublcass of DialogPreference can hold any state/value related to the item it represents.
Take a look at this question about general guidelines to extend DialogPreference.
Hope this helps!
OK, Chopin got me thinking in a different direction. I did not realize that the Preference object is also responsible for how its selector appears in a Preference screen.
The setLayoutResouce() function sets the resource for the Dialog itself not the row seen in a Preference screen. This was confusing and I was incorrectly trying to use this in the preference screen to adjust the selector layout there.
The solution is to override onCreateView and return a custom layout there. To me this is counterintuitive because that method usually controls the final view in most other situations.
I alraedy subclassed my Preference (DialogPreference) so all I had to do was add the following...
#Override
protected View onCreateView (ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customRow = inflater.inflate(R.layout.preferences_station_list_row, null);
((ImageButton) customRow.findViewById(R.id.pref_delete_station)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("c","clicked");
}
});
customRow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(null);
}
});
customRow.setClickable(true);
return customRow;
}
One problem I ran into was that at first the row itself was no longer clickable but the button was. I had to add a listener on the whole view and manually call ShowDialog(). The only thing missing now is that when clicked from the Preference screen the item no longer shows a highlight. Any idea what styles I should apply so the list shows the highlight like it normally does?

Categories

Resources