First off this question has been asked multiple times, however, none of these questions have been answered to any extent. I have one example that works in the main activity class:
final Button button = (Button) findViewById(R.id.viewcatalog);
button.setFocusable(true);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.find_item);
}
});
But all of my other attempts to replicate this in sequential pages has resulted in failure. I know the reason that they won't work the same way is that my buttons are instantiated in other classes and not in the host class. What is the correct way to fix this error?
The method that doesn't work for reference:
public void OnClickSearch(View view) {
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText text = (EditText)findViewById(R.id.editText);
String value = text.getText().toString();
setContentView(R.layout.search_results);
}
});
}
It sounds like you are mis-understanding how the UI works in Android.
It is not normally expected that you will change an Activity's view on the fly as your are doing in your OnClickListener.
Instead, you should do one of two things. Either switch to a new Activity, using an Intent and the Activity's startActivity method, or use Fragments, and replace a Fragment in your Activity with a new Fragment.
Related
If the showChatList() function is not being called in the code, and the dialog is displayed normally.
When the listView is called via showChatList() function, it does not work.
To the original custom dialog
Is it impossible to bring up the listView?
public void callFunction() {
final Dialog dlg = new Dialog(context);
dlg.setContentView(R.layout.room_list);
dlg.show();
final Button okButton = (Button) dlg.findViewById(R.id.okButton);
final Button backbtn = (Button) dlg.findViewById(R.id.backbtn);
**final ListView chat_list = (ListView) dlg.findViewById(R.di.chat_list);**
- or
**chat_list = dlg.findViewById(R.id.chat_list);**
backbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dlg.dismiss();
}
});
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dlg.dismiss();
}
});
showChatList();
}
Your adapter depends on firebase.
It will wait until it gets data,
then I believe you need to call
adapter.notifyDataSetChanged();
after
adapter.add();
When dealing with views like dialogs, it is important to load all the views needed in the dialog before calling dialog.show().
Alternatively, I'll suggest the use of DialogFragment with this example: https://blog.mindorks.com/implementing-dialog-fragment-in-android.
DialogFragments allow you to manage your dialogs just like any other fragment.
Let me know which one you're able to use.
I'm having trouble taking in text from a few text boxes with one button. I can't seem to get OnClick() to work. I have setContentView(R.layout.activity_load_xactivity); as a test I know it doesn't have anything to do with input.
Button button = (Button)findViewById(R.id.create);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setContentView(R.layout.activity_load_xactivity);
}
});
Is in my protected void onCreate(Bundle savedInstanceState). Doesn't work. No errors, just doesn't do anything.
Button button = (Button)findViewById(R.id.create);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
setContentView(R.layout.activity_home2);
}
});
On It's own Doesn't resolve setOnClickListener. I have import android.view.View.OnClickListener set. I've tried entering different code where it says setContentView(R.layout.activity_home2);
Try adding an ID to the Layout holding the Views, Buttons (RelativeLayout, ContrasintLayout ect...) in XML. Then in java make a new Layout, and use layout.addContentView(XML) in your button:
Layout a = findViewById(R.id.layout_name);//Use whatever layout TYPE is used in XML
Button button = (Button)findViewById(R.id.create);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
a.addContentView(R.layout.activity_load_xactivity);
}
});
Also, In you XML, you should give an ID to your layout then define it in java.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_name">
<!--You can use any Layout Type -->
...
</RelativeLayout>
EDIT:
I know this has nothing to with your question, but if you want to switch activity's use this instead:
Intent b = new Intent(this, JavaActivtyName.class);
startActivity(b);
I want to make buttons once I click on the button I go to another activity?
and the problem is only the first button is working!
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), PageOne.class);
v.getContext().startActivity(myIntent);
Button PageTwo = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), PageTwo.class);
v.getContext().startActivity(myIntent);
}
{}
});
}
});
}
}
Think it is because most of your code is closed inside the scope of the first onClickListener, try something like this.
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(Main.this, PageOne.class);
startActivity(myIntent);
});
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(Main.this, PageTwo.class);
startActivity(myIntent);
});
Using v.getContext() should be ok, this is just how I usually would do as the Activity itself is indeed a valid context. I guess it just seems more readable to me.
Edit:
Just as a clarification to the current state of your code. The second button is assigned a onClickListener only after the first button is pressed. But since the first button takes the app to a new Activity, inherently destroying the Main Activity, the second button will never have a chance to reach it's onClickListener.
Hope it makes sense, nevertheless the code above should fix the issue.
There are a couple of issues currently in your code. The first issue is that your second button is being defined inside the first button's declaration. The next issue is that you're setting the second OnClickListener to the wrongly named button. You've made a typo and instead of PageTwo, which you've called the Button (presumably you wanted to call it PageTwoButton in accordance with the first Button) and then set the OnClickListener to PageTwoButton instead. Seeing as you're also using multiple Buttons, it's a lot cleaner and more efficient to use a GroupOnClickListener. I'd probably also suggest using 'this' instead of 'v.getContext()' as well when setting up your Intents. Change your code to be like so:
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageOneButton.setOnClickListener(addGroupOnClickListener);
PageTwoButton.setOnClickListener(addGroupOnClickListener);
private OnClickListener addGroupOnClickListener = new OnClickListener() {
public void onClick(View v) {
if (v == PageOneButton) {
Intent myIntent = new Intent(Main.this, PageOne.class);
startActivity(myIntent);
} else if (v == PageTwoButton) {
Intent myIntent = new Intent(Main.this, PageTwo.class);
startActivity(myIntent);
}
}
};
Hope this helps!
Two words: Code Indentation
Were you to indent your code properly, you would have noticed that you're setting OnClickListener INSIDE your first buttons' listener. Move it outside your first listener, as has already been advised by others.
There's also an extra pair of {}, which is redundant.
Also, #edwoollard noticed that for the second button, you're using two different names, PageTwo and PageTwoButton. Keep that in mind, unless it's a typo.
In my app i have to use same dialog box on all the activities but then on to the click of button on dialog box i need to perform different operations for different activities, i have kept a common code for dialog but then how to call different functions, here is my code:
final Dialog dialog = new Dialog(mContext,R.style.Theme_Levels);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_alert);
TextView title = (TextView)dialog.findViewById(R.id.title);
title.setText("Network Error");
TextView msg = (TextView)dialog.findViewById(R.id.msg_txt);
msg.setText("The system is down, please check after some time ");
ImageView cancel = (ImageView)dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
TextView continue_btn = (TextView)dialog.findViewById(R.id.continue_btn);
continue_btn.setBackgroundResource(R.drawable.feedback_button_purple);
continue_btn.setText("Retry");
continue_btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//TODO perform different operation depending upon from where this function has been called
dialog.dismiss();
}
});
dialog.show();
Create an interface, say DialogActivity, with one method "handlePositiveButton". Let all your Activities implement this interface. From the Dialog.onClick you do this:
DialogActivity activity = (DialogActivity) getActivity();
activity.handlePositiveButton();
Put the code you have specified in a function of a Utils file.
Then pass the positive button onclick listener in that function.
Refer the below code.
public static void showAlertDialog(OnClickListener listener) {
// enter your code here
continue_btn.setOnClickListener(listener);
// more code here
}
I'm creating a dialog box and using the (this) isnt working. Up until now its just been a button calling a dialogbox but now the button within the called dialogbox needs to call another dialog. The Dialog dialogdelcon is the one with problem.
Here is the code:
case R.id.delappt:
//rmvall();
final Dialog dialogdelsel = new Dialog(this);
dialogdelsel.setContentView(R.layout.delsel);
dialogdelsel.setTitle("What would you like to do?");
dialogdelsel.setCancelable(true);
Button btndelsel = (Button) dialogdelsel.findViewById(R.id.btndelsel);
btndelsel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete selected code here.
}
});
Button btndelall = (Button) dialogdelsel.findViewById(R.id.btndelall);
btndelall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// delete all code here.
final Dialog dialogdelcon = new Dialog();
dialogdelcon.setContentView(R.layout.delcon);
dialogdelcon.setTitle("Deletion Confirmation");
dialogdelcon.setCancelable(true);
Button buttoncnclok = (Button) dialogdelcon.findViewById(R.id.btndelcon);
buttoncnclok.setOnClickListener(new OnClickListener() {
// on click for cancel button
#Override
public void onClick(View v) {
dialogdelcon.dismiss();
}
});
dialogdelcon.show();
}
});
dialogdelsel.show();
break;
getApplicationContext() or use YourActictyName.this Because this refers the button click listner ,not your class Object
If this code is in the onCreate() method, or similiar, add getApplicationContext() instead of this and you should be fine. That's because this in a Button-context will refer to the button environment.
To improve the isolation between the two dialogs, it would be best to call showDialog(R.id.delapptcon) from the onClick handler. Then load the new dialog in the onCreateDialog of your activity. In this way, you can create more reusable dialogs and avoid the scoping issue you have now.