How to make a button permanently unclickable - java

I am making a log-in system on Android. And I want the register Button to be unclickable when it has been clicked. I am using this code:
final Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
register.setEnabled(false);
Intent register = new Intent(getApplicationContext(), register.class);
startActivity(register);
}
});
This is working great, but I want the Button to remain unclickable even when the application or phone has been restarted. Does anyone know a way to make the Button unclickable permanently even when the application has been shut down?

As I already said in the comments section something like this may work:
public class MyActivity extends Activity {
private static final String KEY_IS_BUTTON_CLICKABLE = "key_clickable";
#Override
public void onCreate(Bundle savedInstanceState) {
...
final Button register = (Button) findViewById(R.id.register);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isClickable = sharedPreferences.getBoolean(KEY_IS_BUTTON_CLICKABLE, true);
register.setEnabled(isClickable);
if(isClickable) {
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
register.setEnabled(false);
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit()
.putBoolean(KEY_IS_BUTTON_CLICKABLE, false);
Intent register = new Intent(getApplicationContext(), register.class);
startActivity(register);
}
});
}
}
...
}

In this case you could take a pessimistic approach and disable the button in the layout (by default) with android:clickable="false" and enable it in the condition where registration is required.

Related

Is it impossible to have ListvVew in Custom Dialog?

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.

Saving Activity State when switching activities

I'm currently trying to create an activity, which should be creating a new TextView on my main activity, everytime a Button is clicked on the first activity. However, instead of creating a new TextView everytime the button is clicked, it just changes the values of the first created TextView, so that there is always only one TextView. Is there a way to make it so that my first activity will not only create one single textview?
Here's the code from my "NewSubjectActivity":
**public class NewSubjectActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_subject);
Button SaveBtn = findViewById(R.id.SaveBtn);
nsaSaveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Click();
}
});
}
protected void Click(){
Intent intent = new Intent(NewSubjectActivity.this, MainActivity.class);
Boolean createnewTextView = true;
intent.putExtra("createnewTextView",createnewTextView);
startActivity(intent);
}
}
And here's the (relevant) code from my MainActivity:
protected void ReceiveValue (){
//getting Extras
Intent nsareceivedvalues = getIntent();
boolean createTextView = false;
createTextView = nsareceivedvalues.getExtras().getBoolean("createnewTextView");
//declaring fixed Views
final LinearLayout mainLinearLayout = (LinearLayout)findViewById(R.id.mainLinearLayout);
//Params for TextView
RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(1000, 200);
Params.setMargins(0, 10, 0, 10);
while(createTextView) {
//creating a TextView
TextView newsubject = new TextView(MainActivity.this);
//applying values to the TextView
newsubject.setLayoutParams(Params);
newsubject.setGravity(CENTER);
newsubject.setBackgroundColor(Color.GRAY);
mainLinearLayout.addView(newsubject);
createTextView = false;
}
}
As I said, this only create one text view, everytime I press the button on my "NewSubjectActivity" I think this might be, because the previous text view is not saved and the MainActivity is reset everytime I switch between the activities.
Every help and advise is much appreciated <3
Maybe when you are going back to your main activity you should call to onBackPressed() function, so it will reset nothing at all. Here's the example in kotlin
val back_button:Button = findViewById(R.id.button2)
back_button.setOnClickListener { v -> run {
//Change activity
onBackPressed()
} }
in Java should be:
Button back_button = findViewById(R.id.button2);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Change activity
onBackPressed()
}
});
Let's hope it works!

Custom dialog disappears when clicking next to it

I want to show a custom dialog and force the user to click on whether button one or two.
The problem is that users can use the back button AND if they click on the view that is shown in the background my dialog also disappears.
Why? And how could I prevent this?
final Main t = this;
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.prompt_input_access);
dialog.setTitle("Title");
Button cmdLoginAccount = (Button) dialog.findViewById(R.id.cmdLoginAccount);
Button cmdLoginBank = (Button) dialog.findViewById(R.id.cmdLoginBank);
cmdLoginAccount.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
loginToBank = true;
dialog.dismiss();
Intent intent = new Intent(t, UserMenu.class);
startActivity(intent);
}
});
cmdLoginBank.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
loginToBank = false;
dialog.dismiss();
Intent intent = new Intent(t, UserMenu.class);
startActivity(intent);
}
});
dialog.show();
You just need to use the setCanceledOnTouchOutside method :
dialog.setCanceledOnTouchOutside(false);

Which context do i need?

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.

How to navigate from one screen to another screen

How to navigate from one Activity screen to another Activity screen? In the first screen I'm having one button if I click the button it has to move to another Activity screen.
The most trivial case (called from activity):
startActivity(new Intent(this, ActivityToLaunch.class));
More details here: http://developer.android.com/guide/topics/fundamentals.html
OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(action));
}
};
Button button = (Button) findViewById(id);
button.setOnClickListener(onClickListener);
Button x.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent i = new Intent(y.this, Activity.class);
startActivity(i);
}
});
Here we've defined a listener for Button x. The OS will call this method and start the Activity referenced in Intent i.
Here's the official tutorial example:
http://developer.android.com/guide/tutorials/notepad/notepad-ex2.html
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(TestActivity.this,second.class));
}
});
public void onClick(View v)
{
Intent myintent = new Intent(currentclass.this, nextactivity.class);
startActivity(myintent);
}
This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html
public void startActivity (Intent intent) -- Used to launch a new activity.
So suppose you have two Activity class and on a button click's OnClickListener() you wanna move from one Activity to another then --
PresentActivity -- This is your current activity from which you want to go the second activity.
NextActivity -- This is your next Activity on which you want to move.
So the Intent would be like this
Intent(PresentActivity.this, NextActivity.class)
Finally this will be the complete code
public class PresentActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);
// currentContext.startActivity(activityChangeIntent);
PresentActivity.this.startActivity(activityChangeIntent);
}
});
}
}
This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener() at any place where you want to switch between your activities.
final Context cont = this;
Button btnClickABC =(Button)findViewById(R.id.btnClickABC);
btnClickABC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(cont, NextActivity.class));
}
});
Use following code..I hope this will help you.
Button button = (Button)findViewById(R.id.xxx);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
startActivity(intent);
}
});
xxx is id from your xml of your Button.
startActivity(new Intent(this,newActivity.class));
Switching from one activity to another is really simple, but tricky for a new one.
Your next class must be defined in AndroidManifest.xml. This is tester class:
<activity
android:name=".Tester"
android:label="#string/title_activity_tester" >`enter code here`
</activity>
final Button button = (Button) findViewById(R.id.btnGo);// btnGo is id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(CurrentClass.this, Tester.class);
startActivity(i);
}
You can navigate to the next screen using these code snippets:
Kotlin
startActivity(Intent(this, LoginActivity::class.java))
Java
startActivity(new Intent(this, LoginActivity.class))
Here's a reference: Android Developers - Starting another activity
Intent intentobj=new Intent(FromActivity.this,ToActivity.class);
startActivity(intentobj);
or you can simply use
startActivity(new Intent(FromActivity.this,ToActivity.class));
For Kotlin (if you are in an activity)
buttonToClick.setOnClickListener{ startActivity(this,YourDestinationActivity::class.java)
}
If you are in a fragment
buttonToClick.setOnClickListener{
startActivity(requireActivity, YourDestinationActivity::class.java)
}
In your method fun onCreate(savedInstanceState: Bundle?) add this.
your_btn_id.setOnClickListener{
val intent = Intent(this, yourpagename::class.java)
startActivity(intent)
}
Until now if it doesn't work then, check if these two files are added or not,
import android.content.Intent
import kotlinx.android.synthetic.main.activity_otp.*
Try this code:
Button my_btn;
my_btn = findViewById(R.id.submit_btn);
my_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_2);
}
});
Button navigate;
navigate = findViewById(R.id.button);
navigate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Navigate another Activity",Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
});
Just go to XML file and add Onclick = "opennewactivity" in button xml.
Then go to java code and create class opennewactivity you can just make it my clciking alt+Enter in xml code's "opennewactivity". in that just write
Intent intent = new Intent(this, newacivity.class);
startActivity(intent);
and if you want user to not get back to first activity again then just write this
Intent intent = new Intent(this, newactivity.class);
startActivity(intent);
finish();
Cartoon_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
newActivity();
}
});
}
public void newActivity()
{
Intent selectClass= new Intent(getApplicationContext(), com.example.fyp.videoplayer.class);
startActivity(selectClass);
}

Categories

Resources