change Main UI from custom Dialog Class - java

I have 2 class, Main and DialogOrder
Main
public class Main extends Fragment{
ImageView order;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
order.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DialogOrder(getActivity()).show();
}
});
return view;
}
public void init(Bundle savedInstanceState) {
order = (ImageView) view.findViewById(R.id.order);
order.setImageResource(R.drawable.orderd);
RelativeLayout.LayoutParams orderparams = new RelativeLayout.LayoutParams(Main.screenHeight / 8, Main.screenHeight / 8);
orderparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
orderparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
orderparams.setMargins(Main.screenHeight / 80, Main.screenHeight / 80, Main.screenHeight / 80, Main.screenHeight / 30);
order.setLayoutParams(orderparams);
}
public void update_UI(){
order.setVisibility(View.INVISIBLE);
}
}
DialogOrder
public class DialogOrder extends Dialog {
Button button;
Main main;
Activity context;
public DialogOrder(Activity context) {
super(context);
this.context = context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.order_dialog);
main = new Main();
button = (Button)findviewbyid(R.id.bd);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
main.update_UI();
dismiss();
}
});
}}
what I want is to set order image INVISIBLE when the user press the button on dialog
right now the code give me
java.lang.NullPointerException
probably I try update the UI wrong
so please can someone tell me what the right way to update parent UI class from child class ?

You can update your main fragment by passing it to Dialog constructor or you can use Listener/Callback to communicate between your main fragment and dialog.
The best practice is is using Listener/Callback because a Dialog should not have access to the caller. This is also decouple Dialog from Main fragment.
First, create the listener via interface in the dialog:
public class DialogOrder extends Dialog {
...
Activity context;
private DialogListener listener;
public interface DialogListener {
void onButtonClicked();
}
public DialogOrder(Activity context, DialogListener listener) {
super(context);
this.context = context;
this.listener = listener;
}
...
}
Then, when button click call the listener:
public class DialogOrder extends Dialog {
Activity context;
private DialogListener listener;
public interface DialogListener {
void onButtonClicked();
}
public DialogOrder(Activity context, DialogListener listener) {
super(context);
this.context = context;
this.listener = listener;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listener.onButtonClicked();
dismiss();
}
});
}
}
Now, you can create the dialog with the listener. Something like this:
DialogOrder.DialogListener listener = new DialogOrder.DialogListener() {
#Ovveride
public void onButtonClicked() {
update_UI();
}
};
DialogOrder dialogOrder = new DialogOrder(getActivity(), listener);
dialogOder.show();
The main fragment will be listening for button clicked in the dialog.

Related

Custom listener with interface

I am trying to understand better how to create a custom listener with a simple example but I don't know how to start the interface so that it is not null:
public class MainActivity extends AppCompatActivity implements ListenerButton{
TextView helloToOther;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helloToOther = findViewById(R.id.helloWorldToOtherActivity);
helloToOther.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ButtonActivity.class));
}
});
}
#Override
public void onClickButton(View view) {
Toast.makeText(this, "Estoy en la primera activity", Toast.LENGTH_SHORT).show();
}
}
This is the second activity:
public class ButtonActivity extends AppCompatActivity {
Button btnInterface;
ListenerButton listenerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
btnInterface = findViewById(R.id.button_activity__btn__button_interface);
setUpButtonInterface();
}
private void setUpButtonInterface() {
btnInterface.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listenerButton.onClickButton(v);
}
});
}
}
And there's the interface:
public interface ListenerButton {
void onClickButton(View view);
}
Basically I get a null pointer exception on the second activity because the interface is null, but I don't fall right now as I can start it. Thank you very much.
the reason you are getting a null pointer exception is because you haven't assigned anything to variable listenerButton and therefor it is in fact null!!
you don't need a new interface for that you just need to do this:
public class ButtonActivity extends AppCompatActivity {
Button btnInterface;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
btnInterface = findViewById(R.id.button_activity__btn__button_interface);
setUpButtonInterface();
}
private void setUpButtonInterface() {
btnInterface.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do whatever you want to do when button is clicked!
}
});
}
}
if you want to define whatever you want to do when button is clicked you need a class and not an interface:
public class ButtonListener implements View.OnClickListener{
#Override
public void onClick(View v) {
//do whatever you want to do when button is clicked!
}
}
and then in your activity do this:
public class ButtonActivity extends AppCompatActivity {
Button btnInterface;
ListenerButton listenerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
listenerButton = new ButtonListener();
btnInterface = findViewById(R.id.button_activity__btn__button_interface);
setUpButtonInterface();
}
private void setUpButtonInterface() {
btnInterface.setOnClickListener(listenerButton);
}
}
Create a new file:
MyListener.java:
public interface MyListener {
// you can define any parameter as per your requirement
public void callback(View view, String result);
}
In your activity, implement the interface:
MyActivity.java:
public class MyActivity extends Activity implements MyListener {
#override
public void onCreate(){
MyButton m = new MyButton(this);
}
// method is invoked when MyButton is clicked
#override
public void callback(View view, String result) {
// do your stuff here
}
}
In your custom class, invoke the interface when needed:
MyButton.java:
public class MyButton {
MyListener ml;
// constructor
MyButton(MyListener ml) {
//Setting the listener
this.ml = ml;
}
public void MyLogicToIntimateOthers() {
//Invoke the interface
ml.callback(this, "success");
}
}

Creating an object and running a method

Create an object and run a method:
It compiles just fine but the App crashes instantly upon launch :(
I implemented the interface and all..
What's going on?
MainActivity.java
public class MainActivity extends AppCompatActivity {
public interface MyInterfcae {
void test0();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Test t = new Test();
t.test0();
}
Test.java
public class Test extends MainActivity implements MainActivity.MyInterfcae {
public Button b = (Button)findViewById(R.id.button0);
#Override
public void test0(){
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast toast = Toast.makeText(getApplicationContext(), "Implemented!" , Toast.LENGTH_SHORT);
toast.show();
}
});
}
It compiles just fine but the App crashes instantly upon launch :(
I implemented the interface and all..
What's going on?
public Button b = (Button)findViewById(R.id.button0);
This link gives an error. You haven't inflated the layout file related to this activity and you are trying to access button0. So, in order to resolve this error pass the layout file instance in the method and then do findViewById() using that instance.
Do like this:-
MainActivity.java
public class MainActivity extends AppCompatActivity
{
public interface MyInterfcae
{
void test0(Context context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Test t = new Test();
t.test0(this);
}
}
Test.java
public class Test extends MainActivity implements MainActivity.MyInterfcae
{
Context context;
#Override
public void test0(final Context context)
{
this.context = context;
Button b = (Button) ((Activity)context).findViewById(R.id.button0);
b.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
Toast toast = Toast.makeText(context, "Implemented!", Toast.LENGTH_SHORT);
toast.show();
}
});
}
}

Registration with two steps, next fragment by button

I want to handle user registration in my app. I red that using fragments is the best way to handle it.
I prefer using button to move to registration finish fragment, than swype.
I have to send few field to another and finally send all of data by Retrofit.
Any solution?
You can implement a Callback to save the values on your activity.
First create the interface:
public interface ActivityCallback {
void sendData(String data1,String data2);
}
Then make your activity implements this interface:
public class MyActivity extends AppCompatActivity implements ActivityCallback{
private ArrayList<String> arrayData = new ArrayList<>();
...
#Override
void sendData(String data1, String data2){
this.arrayData.add(data1);
this.arrayData.add(data2);
}
...
}
Then on your fragment, when you decide to go to the next step:
public class YourFragment extends Fragment{
private Button nextButton;
private EditText text1;
private EditText text2;
protected ActivityCallback callback;
...
#Override
public void onAttach(Context context) {
super.onAttach(context);
callback = (ActivityCallback) context;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
...
nextButton = (Button) findViewById(R.id.next_button);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
callback.sendData(text1.getText().toString(),text2.getText().toString());
}
});
}
}

Can i declare and use the same button in an Activity and a class?

I want to use the button for different calls in the different classes. However, when I declare and try to call the Button click method in the Activity class, it throws a null exception. In my Class I want to do this:
public class CustomFeedListViewAdapter extends BaseAdapter {
holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);
holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
private class ViewHolder {
Button feedUpVoteButton;
}
And in my main activity I want to do this:
public class Feed extends AppCompatActivity {
Button upVoteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
upVoteButton = (Button) findViewById(R.id.feedUpVoteButton);
feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
}
}
You are getting NullPointerException because you are trying to access feedUpVoteButton which is present in the item layout used in CustomFeedListViewAdapter and not in the layout used by Feed activity.
Do you want to have access to the ClickListener whenever you create an adapter of type CustomFeedListViewAdapter? If yes then do this...
Your Activity class
public class Feed extends AppCompatActivity {
CustomFeedListViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
adapter = new CustomFeedListViewAdapter( new OnClickListener() {
#Override
public void onClick(View v) {
// Handle click here
}
});
}
}
And your custom adapter class
public class CustomFeedListViewAdapter extends BaseAdapter {
View.OnClickListener clickListener;
// Constructor
public CustomFeedListViewAdapter(View.OnClickListener listener) {
clickListener = listener;
}
#Override
void getView(){
holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);
holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
}
}
This way you can set a different listener for each instance of the adapter you create.

on click interface callback gives null in Android

I have Android module library which will create the Touch Keypad UI and set event listener for done and backbutton.
Also have main activity with implement eventCallback interface
MainActivity.java (Appication)
public class MainActivity extends AppCompatActivity implements eventCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = new touchkey(this);
setContentView(v);
}
#Override
public void onClick() {
Log.i("test","complete");
Toast.makeText(this, "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
}
}
eventCallback.java (Android Module Library)
public interface eventCallback {
void onClick();
}
touchkey.java (Android Module Library)
public class touchkey extends RelativeLayout{
private static touchkey INSTANCE;
TextView bclear;
ImageView bdone;
eventCallback eventCall;
public touchkey(Context context) {
super(context);
initialize(context);
}
public touchkey(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public void test() {
Log.i("test","test");
}
private void initialize(Context context) {
inflate(context, R.layout.touchkey, this);
bclear = (TextView) findViewById(R.id.anti_theft_t9_key_clear);
bdone = (ImageView) findViewById(R.id.anti_theft_t9_key_done);
bdone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (eventCall != null) {
eventCall.onClick();
}
}
});
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
but iam getting nullpointer exception on the touchkey.java
eventCall.onClick(); (eventCall is null)
I dont know where i am doing things wrong. can anybody help on this. Requirement: i need to handle the click event happening on the Library in Main Activity
You must create setter for eventCall (in touchkey.java):
public void setEventCall(eventCallback eventCall) {
this.eventCall = eventCall;
}
And, use it (in MainActivity):
View v = new touchkey(this);
setContentView(v);
((touchkey) v).setEventCall(this);

Categories