onClick Button for different int event - java

I have a method with int parameter to getResources for a listView. I want to create a button to call one of each int one by one. How can I do that?
The method:
public void changeData(int i){
Resources res = getResources();
String[] descriptions = res.getStringArray(R.array.tips_description_details);
text.setText(descriptions[i]);
String[] titles = res.getStringArray(R.array.text_titles);
textTitle.setText(titles[i]);
imageTips.setImageResource(images[i]);
}
and this is the button's onClick method:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}

You could try something like:
#Override
public void onClick(View v) {
int tagId = (int)v.getTag();
changeData(tagId);
}
Check out the docs for getTag() and android:tag.

Related

Getting java.lang.NullPointerException on customDialogBox

I am getting java.lang.NullPointerException for this method. I have check the adapter and lists are just fine and the list is ofcourse under fragment_phonelist xml file.
private void showCustomDialog() {
final Dialog dialog = new Dialog(SetupProfile.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.fragment_phonelist);
Button done = (Button)dialog.findViewById(R.id.btn_done);
Button canCel = (Button)dialog.findViewById(R.id.btn_cancel);
ListView PhoneListView = (ListView)findViewById(R.id.list_phone);
MyCustomAdapter tst = new MyCustomAdapter(this,ContactName,ContactNumb);
PhoneListView.setAdapter(tst);
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
canCel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
Please any help..
This line
ListView PhoneListView = (ListView)findViewById(R.id.list_phone);
Shouldn't that be like this:
ListView PhoneListView = (ListView)dialog.findViewById(R.id.list_phone);

Set focus to the EditText upon clicking a button

int[] etColorId = new int[] {R.id.etColor1, R.id.etColor2, R.id.etColor3, R.id.etColor4, R.id.etColor5,
R.id.etColor6, R.id.etColor7, R.id.etColor8, R.id.etColor9, R.id.etColor10, R.id.etColor11,
R.id.etColor12, R.id.etColor13, R.id.etColor14, R.id.etColor15};
for (int editText : etColorId) {
findViewById(editText).setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
v.setEnabled(false);
}
});
}
int[] btnEditId = new int[] {R.id.btnEdit1, R.id.btnEdit2, R.id.btnEdit3, R.id.btnEdit4, R.id.btnEdit5,
R.id.btnEdit6, R.id.btnEdit7, R.id.btnEdit8, R.id.btnEdit9, R.id.btnEdit10, R.id.btnEdit11,
R.id.btnEdit12, R.id.btnEdit13, R.id.btnEdit14, R.id.btnEdit15};
for (int button : btnEditId) {
findViewById(button).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setEnabled(true);
v.requestFocus();
}
});
}
How can I gain focus to EditText upon clicking a button? I have a bunch of EditTexts and Buttons. Each button corresponds to a specific EditText. What I want is, for example: if I clicked the button1, the EditText1 will be focused. And then when I click other button, the EditText corresponds to that button will be focused and then the other EditTexts will be disabled.
This code for OnFocusChangeListener has to be modified little bit, for anonymous classes you need to have final reference of any object which you wish to modify, since you want to modify EditText
final int[] etColorId = new int[] {R.id.etColor1, R.id.etColor2, R.id.etColor3, R.id.etColor4, R.id.etColor5,
R.id.etColor6, R.id.etColor7, R.id.etColor8, R.id.etColor9, R.id.etColor10, R.id.etColor11,
R.id.etColor12, R.id.etColor13, R.id.etColor14, R.id.etColor15};
Now for OnClickListener do like this
for (int i=0;i<btnEditId.length;i++)
{
final int counter=i;
int button=btnEditId[i];
findViewById(button).setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// HERE v refers to button itself on which you clicked,
// you need to update get edit text so
// based on ith position accessing the same edit as the button correspond too
EditText edt=etColorId[counter];
edt.setEnabled(true);
edt.requestFocus();
// and you are DONE!!!
}
});
}
UPDATE:
new OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if(!hasFocus)
{ // lost focus
v.setEnabled(false);
}
else
{
//you are already enabling on button click
}
}
});
Inside
onClick()
{
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
}
onFocusChange called every time when lost focus and gain focus.
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
v.setEnabled(false);
}
So, first you need to change code
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus) {// gain focus
// Code block
}
if(!hasFocus) { // lost focus
// Code block
}
}
You EditText is disabled every time, when press Button

Closing Custom Dialog Box

I am Using the following code for my custom dialog box.
Code is here
I am using a new layout by setCustomView Method.That layout contains a 'Ok' button and a 'Cancel' button.
I need to close the dialog box when click on cancel.
buttonCancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick" , "YYYYY");
//up to this comes , here what I can wright
}
});
dialogObject.dismiss();
You can use this method
Why not you create custom dialog from here:
http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
Very clearly explained and easy to implement too.
try this :
buttonCancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick" , "YYYYY");
qustomDialogBuilder.dismiss();//this line will close the dialog
}
});
Replace your TestDialogActivity like below,
public class TestDialogActivity extends Activity {
private static final String HALLOWEEN_ORANGE = "#FF7F27";
private AlertDialog alertDialog;
private OnClickListener mShowDialogClickListener = new OnClickListener() {
public void onClick(View v) {
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(
v.getContext())
.setTitle("Set IP Address")
.setTitleColor(HALLOWEEN_ORANGE)
.setDividerColor(HALLOWEEN_ORANGE)
.setMessage("You are now entering the 10th dimension.")
.setCustomView(R.layout.example_ip_address_layout,
v.getContext())
.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
alertDialog=qustomDialogBuilder.create();
qustomDialogBuilder.setAlertDialog(alertDialog);
alertDialog.show();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(mShowDialogClickListener);
}
and replace setCustomView of QustomDialogBuilder like below
public QustomDialogBuilder setCustomView(int resId, final Context context) {
View customView = View.inflate(context, resId, null);
((TextView)customView.findViewById(R.id.ip_text)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
((FrameLayout)mDialogView.findViewById(R.id.customPanel)).addView(customView);
return this;
}
finally add below line into your QustomDialogBuilder
private AlertDialog alertDialog;
public void setAlertDialog(AlertDialog alertDialog)
{
this.alertDialog=alertDialog;
}
To close your dialog click on IP Address text.
Using the QustomDialog Source here in your activity class (TestDialogActivity), you can set the "Ok" and "Cancel" button by setting the Negative and Positive button of the dialog like this:
private OnClickListener mShowDialogClickListener =new OnClickListener(){
public void onClick(View v){
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()).
setTitle("Set IP Address").
setTitleColor(HALLOWEEN_ORANGE).
setDividerColor(HALLOWEEN_ORANGE).
setMessage("You are now entering the 10th dimension.").
setCustomView(R.layout.example_ip_address_layout, v.getContext()).
setIcon(getResources().getDrawable(R.drawable.ic_launcher));
qustomDialogBuilder.setNegativeButton("Cancel", new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
qustomDialogBuilder.setPositiveButton("Ok", new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
/**
* Do something here...
*/
}
});
qustomDialogBuilder.show();
}
};
And it will look like this:
Hope you'll find this helpful. Thanks!

how i should need to write method to use it anywhere in a class

Still i am calling startUpload(position); method in getView(...) {} method, but now i wanna call it in
loginbutton.setOnClickListener(new View.OnClickListener() {....}
Like this, but always i am getting position cannot be resolved to a variable
so here i want to know how i should need to write method to use it anywhere in a class:
loginbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SaveData();
alertDialog.dismiss();
startUpload(position);
}
below is the method, startUpload(...){...}
//Upload
public void startUpload(final int position) {
Runnable runnable = new Runnable() {
public void run() {
handler.post(new Runnable() {
public void run() {
View v = lstView.getChildAt(position - lstView.getFirstVisiblePosition());
// Show ProgressBar
ProgressBar progress = (ProgressBar)v.findViewById(R.id.progressBar);
progress.setVisibility(View.VISIBLE);
// Status
TextView status = (TextView)v.findViewById(R.id.ColStatus);
status.setText("Uploading..");
new UploadFileAsync().execute(String.valueOf(position));
}
});
}
};
new Thread(runnable).start();
}
Try using the following sintax.
loginbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SaveData();
alertDialog.dismiss();
startUpload(OldUploadActivity.this.position);
}
}
You need to add final int position as a parameter to onPrepareDialog() as well; just like you're passing it to the getView() method. Also, update the line of code from where you're actually invoking this method from by passing the actual position value.
public void onPrepareDialog(final int position, int id, Dialog dialog) {

how to assign different activities to each item of the spinner?

I have a page which consist of a spinner and a submit button. What I want to achieve is when user selects an item in the list and click on submit, it should take him to an other layout having a webview. Each item in the spinner should open different .html page in the layout.
What I have now is the item is being selected from the spinner, but I'm not sure how to perform onclick listener to it...
code for main activity is here
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beef);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
//get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
btnSubmit = (ImageButton) findViewById(R.id.imageButton1);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*Nothing as of now*/
//I need to call the ID of the selected item from the spinner here and start new activity
}
});
}
}
code of CustomOnItemSelectedListener is here
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if (arg2 == 0) // First item selected
{
//Here I need to give an id for the .html file
}
else if (arg2 == 1) // Second
{
//Here I need to give an id for the .html file
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selItem = arg0.get(arg2); // String representation of the selected item
if (arg2 == 0) // First item selected
{
}
else if (arg2 == 1) // Second
{
}
// etc
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}

Categories