ListPreferences without any radio buttons? - android

I want to create a ListPreference in my PreferenceActivity.
When a ListPreference is clicked, I get a dialog box with a listview. Each row in list view has a text field and a radio button.
I do not want this radio button and also on clicking list item, I want to fire an intent that opens browser? Any idea how to go about it?
If i extend DialogPreference then how to handle onClicks? Like onListClickListener will work?
OR
If i extend ListPreference what are the functions i need to override?

This is possible when you are customizing preferences.When you are using only Preference ,it works like a button.And later you have to implement whatever you want.The Following example simply shows as your requirement.When you click preference,it shows list dialog without radio buttons .But i am not implemented to store the data in Shared preferences.If you want to do that,you have to implement your own.I just post some code here.
prefereces=findPreference("intent");
// prefereces.setIntent(new Intent(Intent.ACTION_VIEW,Uri.parse("https://market.android.com/")));
// prefereces.setIntent(new Intent(getApplicationContext(), DynamicPreference.class));
prefereces.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
createListPreferenceDialog();
return true;
}
});
}
private void createListPreferenceDialog()
{
Dialog dialog;
final CharSequence str[]={"Android","Black Berry","Symbian"};
AlertDialog.Builder b=new AlertDialog.Builder(PreferenceActivities1Activity.this);
b.setTitle("Mobile OS");
b.setItems(str, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int position)
{
showToast("I am Clicked "+str[position]);
// switch (position)
// {
// case 0:
// showToast("I am Clicked "+str[position]);
// break;
//
// default:
// break;
// }
}
});
dialog=b.create();
dialog.show();
}
public void showToast(String msg)
{
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}

Related

one Button for multiple thing

I just want to implement one button to do multiple actions like on first click make Textview1 visible and on second click make Textview2 visible and so on.
here is my code it works but for 2 actions only i want to set more visible component in one button i hope its clear and Thanks for any help
final TextView textView_r4 = findViewById(R.id.tv_r4);
final EditText editText_r4 = findViewById(R.id.input_R4);
final TextView textView_r5 = findViewById(R.id.tv_r5);
final EditText editText_r5 = findViewById(R.id.input_R5);
findViewById(R.id.Addbtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView_r4.setVisibility(View.VISIBLE);
editText_r4.setVisibility(View.VISIBLE);
}
});
findViewById(R.id.Addbtn).setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
textView_r5.setVisibility(View.VISIBLE);
editText_r5.setVisibility(View.VISIBLE);
return true;
}
});
You can add an enum State to keep track of which state your button is in. Create a class field in the same class (activity) that these methods are in, and change the state every time you click. Then in the .setOnClickListener method you can check which state the button is in, and depending on that do different actions.
private State state = INITIAL;
findViewById(R.id.Addbtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (state) {
case INITIAL:
// do first action
state = State.CLICKED_ONCE;
break;
case CLICKED_ONCE:
// do second action
state = State.CLICKED_TWICE;
break;
default:
// clicked too many times, no action taken
break;
}
}
});
private enum State { INITIAL, CLICKED_ONCE, CLICKED_TWICE }

How to code a button with differents effects when pressed multiple times

I may be asking a basic question, but to be honest, I have no real developement or code knowledge. I've been requested to make a prototybe of some basic app, that is supposed mainly to be buttons on screens, activable or desactivable. I've written some kind of TL;DR in case my explanations are bad
I've been coding this on Android Studio 3.0, I (hardly) managed to place PNGs files on the screen, making it looking like a button.
Thing is, while some parts of the app are mainly constituted with independants togglable button. There a part where pressing a button must deselect the others. AND, if this button is pressed a second time open another activities.
Here's part of my code I'm using.
This one for independants buttons
indbutton1.setOnTouchListener(new View.OnTouchListener(){
// track if the image is selected or not
boolean isSelected = true;
public boolean onTouch(View v, MotionEvent event) {
if(isSelected) {
indbutton1.setImageResource(R.drawable.indbutton1slct);
} else {
indbutton1.setImageResource(R.drawable.indbutton1nosl);
}
// toggle the boolean
isSelected = !isSelected;
return false;
}
});
And this one for going into other activities
movements.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent gestesActivity = new Intent (getApplicationContext(), movements.class);
startActivity(movementsActivity);
finish();
}
});
TL;DR
How should I proceed to have a mix of pressing button, disabling others enabled, then, when pressed a second time, I go to another activity.
Thank you for any help :) -Pliskin
Here is how I would do that. Let's say you have 4 buttons.
// your class fields
boolean [] alreadyTouched = new boolean[4];
For each of the 4 buttons :
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!alreadyTouched[0]){
setAlreadyTouched(0);
// one click actions here
}else{
// second click's action
}
}
});
Now you make a private method in your class :
private void setAlreadyTouched(int index){
for (int i = 0; i< alreadyTouched.length; i++)
alreadyTouched[i] = false;
if(index != -1)
alreadyTouched[index] = true;
}
And to reset your boolean array when the button looses the focus :
button0.setOnFocusChangeListener(new View.OnFocusChangeListener(){
#Override
public void onFocusChange(View view, boolean hasFocus){
if(!hasFocus)
setAlreadyTouched(-1);
}
});
You can do exactly the same thing but with an array of int if you want more than two clicks with some slight modifications. For example :
// your class fields
int[] alreadyTouched = new int[4];
Your privat method :
private void setAlreadyTouched(int index){
if(index == -1){
for (int i = 0; i< alreadyTouched.length; i++)
alreadyTouched[i]=0;
}else
alreadyTouched[index] = alreadyTouched[index] +1 ;
}
Then just add some if in your onClickListeners :
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setAlreadyTouched(0);
switch(alreadyTouched[0]){
case 1:
// one click actions here
break:
case 2:
// second click's action
break:
// ... and so on
default:
// action for max number of clicks here.
}
}
});

How to trigger Actionbar menu items in android app

I have webview in my app, which loads some url's. In this webview, I am seeing Actionbar menu (Cut, Copy, Paste, SelectAll, Settings) on top of the app, while long pressing on the texts in the loaded webpages.
I am trying to make an alert for this menus, while click on them. If user touch's cut I need to show an alert of You clicked Copy, Are you sure to Copy this?.
For this, I am overriding onOptionsItemSelected method, but not sure that this is right way. There is no onActionBarItemSelected method.
My mainActivity extends ActionBarActivity
Here my code, I used for this triggering,
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("MenuItem clicked - inside onOptionsItemSelected");
if (item.getItemId() == R.attr.actionModeCopyDrawable) {
Log.d("MenuItem clicked----", "Copy");
ShowAlert("You clicked Copy, Are you sure to Copy this?");
}
return true;
}
public void ShowAlert(String str) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setMessage(str);
builder1.setCancelable(true);
builder1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder1.create();
alert.show();
}
But there is no alert and not even a log message of MenuItem clicked - inside onOptionsItemSelected, while click on the Copy menu.
What I am missing here, hope someone can assist here.

Show dialog of "android-feedback.com" library

I am following the tutorial given here http://www.android-feedback.com/library for sending feedback. But I am unable to show dialog in onOptionsItemSelected.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//menu.add("Email");
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
// do whatever
FeedbackSettings feedbackSettings = new FeedbackSettings();
//SUBMIT-CANCEL BUTTONS
feedbackSettings.setCancelButtonText("No");
feedbackSettings.setSendButtonText("Send");
//DIALOG TEXT
feedbackSettings.setText("Hey, would you like to give us some feedback so that we can improve your experience?");
feedbackSettings.setYourComments("Type your question here...");
feedbackSettings.setTitle("Feedback Dialog Title");
//TOAST MESSAGE
feedbackSettings.setToast("Thank you so much!");
feedbackSettings.setToastDuration(Toast.LENGTH_SHORT); // Default
feedbackSettings.setToastDuration(Toast.LENGTH_LONG);
//RADIO BUTTONS
feedbackSettings.setRadioButtons(false); // Disables radio buttons
feedbackSettings.setBugLabel("Bug");
feedbackSettings.setIdeaLabel("Idea");
feedbackSettings.setQuestionLabel("Question");
//RADIO BUTTONS ORIENTATION AND GRAVITY
feedbackSettings.setOrientation(LinearLayout.HORIZONTAL); // Default
feedbackSettings.setOrientation(LinearLayout.VERTICAL);
feedbackSettings.setGravity(Gravity.RIGHT); // Default
feedbackSettings.setGravity(Gravity.LEFT);
feedbackSettings.setGravity(Gravity.CENTER);
//SET DIALOG MODAL
feedbackSettings.setModal(true); //Default is false
//DEVELOPER REPLIES
feedbackSettings.setReplyTitle("Message from the Developer");
feedbackSettings.setReplyCloseButtonText("Close");
feedbackSettings.setReplyRateButtonText("RATE!");
//DEVELOPER CUSTOM MESSAGE (NOT SEEN BY THE END USER)
feedbackSettings.setDeveloperMessage("This is a custom message that will only be seen by the developer!");
feedBack = new FeedbackDialog(this, "AF-548BD4EFE07D-89", feedbackSettings);
feedBack.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I am unable to show the dialog on click of item !
Please help me to identify my mistake !
Thanks in advance !

Android single button multiple functions

I am beginner to Android development. I have 3 edit boxes and one "Edit" button. When I launch the activity all the edit boxes should be disabled. When I click on the Edit button all the 3 edit boxes should get enabled and button text should change to "Save". After updating the data in the edit boxes, when I click on the "Save" button, I should be able to send the updated data to the backend.
My problem is how can I make use of a single button for two function "Edit" and "Save".
Please help me.
You can do it this way:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String ButtonText = button.getText().toString();
if(ButtonText.equals("Save"){
//code for save
button.setText("Edit");
}
else{
//code for edit
button.setText("Save");
}
}
});
If I were you I would actually use two buttons one for edit, and one for save. Make them the same size and in the same position, when you want to switch between them make one invisible, and the other visible. Doing it that way would let you keep your onClickListeners separate which would make your code more understandable in my mind.
That being said you could technically achieve it with a single button as well. Just change the text on the button when you want to switch between them, and add an if statement into your click listener to check which "mode" your button is currently in to determine which action it should take.
I am not sure there is an easy way to do this or not. but you can sure use different behaviors of button clicks like
// When you press it for long time.
dummyButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true; // Can do lot more stuff here I am just returning boolean
}
});
// Normal click of button
dummyButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do lot more stuff here.
}
});
Do it this way :
Make a public boolean variable
public boolean isClickedFirstTime = true;
make your 3 editTexts enabled false in xml and
onClick of your button
#Override
public void onClick(View v) {
if (v.getId() == R.id.edit_button_id) { //whatever your id of button
Button button = (Button) findViewById(R.id.edit_button_id);
if(isClickedFirstTime)
{
edit1.setEnabled(true);
edit2.setEnabled(true);
edit3.setEnabled(true);
butt.setText("Save");
isClickedFirstTime = false;
}
else
{
....//Get your values from editText and update your database
isClickedFirstTime = true;
}
}

Categories

Resources