I've fingers on my teeths with Java and Android. Normaly i code with Python, it easier and easier !
Java never let me do anything, or only in a unique method that i never know :(
So the only code working is copy-paste and it's hard to customize...
So, i've a simple editable text and a button, when i click on it, i go to an url. The button works : i can open my webview.
But i want to make the url with the editText and i can't :(
See that :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
final EditText Login = (EditText) findViewById(R.id.entry);
View firstButton = findViewById(R.id.ok);
firstButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.ok:
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.tatai.froyo.Webscreen.URL,
"http://192.168.1.12/index2.php");
startActivity(j);
break;
}
So, how can i have the login edittext to make the url like http://192.168.1.12/index2.php?log=toto
... where toto may is get with the login edittext.
Impossible to read a global var on the onclick, i'm lost ! :(
i trid that on the onclick listner :
String logDetail = Login.getText().toString();
but it can't see Login, it's out ! : undeclared
on your code, TRy this,
public void onClick(View v)
{
switch(v.getId())
{
case R.id.ok:
String logDetail = Login.getText().toString();
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.tatai.froyo.Webscreen.URL,"http://192.168.1.12/index2.php?log="+logDetail);
startActivity(j);
break;
}
EDIT:
EditText Login;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Add Click listeners for all buttons
Login = (EditText) findViewById(R.id.entry);
View firstButton = findViewById(R.id.ok);
firstButton.setOnClickListener(this);
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.ok:
String logDetail = Login.getText().toString();
Intent j = new Intent(this, Webscreen.class);
j.putExtra(com.tatai.froyo.Webscreen.URL,"http://192.168.1.12/index2.php?log="+logDetail);
startActivity(j);
break;
}
Related
So I have a screen where a user enters their name, on button click they are redirected to a menu where it displays their name.
In the menu screen I have a button that takes me to another screen (About Me), on that screen I have a button with an Intent to go back to the Menu Activity.
The issue is that when I click the button with the Intent to go back, the name displays as null instead of what the User entered.
This does not happen when I use the actual android navigation buttons to go to the previous screen, only the Button i created to go back to the Menu Activity.
Launcher Activity where user enters their name
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher_screen);
Button launcherNextBtn = (Button) findViewById(R.id.launcherNextBtn);
EditText launcherVisitorNameEditText = (EditText) findViewById(R.id.launcherVisitorNameEditText);
TextView launcherVisitorNameErrorTextView = (TextView) findViewById(R.id.launcherVisitorNameErrorTextView);
launcherVisitorNameErrorTextView.setText("");
launcherNextBtn.setOnClickListener((View v) -> {
String visitorName = launcherVisitorNameEditText.getText().toString();
if (visitorName.equals("")) {
launcherVisitorNameErrorTextView.setText("Please enter a value");
} else {
launcherVisitorNameErrorTextView.setText("");
Intent goToMenuActivity = new Intent(LauncherScreen.this, MenuScreen.class);
goToMenuActivity.putExtra("visitorName", visitorName);
startActivity(goToMenuActivity);
}
});
Menu activity where it displays the users name
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_screen);
Button menuHomeBtn = (Button) findViewById(R.id.menuHomeBtn);
Button menuAboutMeBtn = (Button) findViewById(R.id.menuAboutMeBtn);
TextView menuNameTextView = (TextView) findViewById(R.id.menuNameTextView);
String visitorName = getIntent().getStringExtra("visitorName");
menuNameTextView.setText("Dear " + visitorName);
menuHomeBtn.setOnClickListener((View v) -> {
Intent goToLauncherActivity = new Intent(MenuScreen.this, LauncherScreen.class);
goToLauncherActivity.putExtra("visitorName", visitorName);
startActivity(goToLauncherActivity);
});
menuAboutMeBtn.setOnClickListener((View v) -> {
Intent goToAboutMeActivity = new Intent(MenuScreen.this, AboutMeScreen.class);
startActivity(goToAboutMeActivity);
});
}
About me activity, when I click the button to back to Menu, i get "Dear null"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_me_screen);
Button aboutMeBackBtn = (Button) findViewById(R.id.aboutMeBackBtn);
String visitorName = getIntent().getStringExtra("visitorName");
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
startActivity(goToMenuActivity);
});
}
Your problem is in About me activity screen
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
startActivity(goToMenuActivity);
});
This time you are not passing an extra field, so you are getting null
Answer 1
aboutMeBackBtn.setOnClickListener((View v) -> {
onBackPressed();
});
Answer 2
aboutMeBackBtn.setOnClickListener((View v) -> {
Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
goToMenuActivity.putExtra("visitorName", visitorName)
startActivity(goToMenuActivity);
});
I'm using Firebase RealTime database, and using retrieving data, I'm asking about if I want to add If method on OnClick() for the button and get the ActionView link from real time database, and if there's no link in the database give a toast message that "There's no link available ATM"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.res_screen);
String data0= getIntent().getStringExtra("Name");
String data1= getIntent().getStringExtra("Address");
String data2= getIntent().getStringExtra("Phone1");
String data3= getIntent().getStringExtra("Phone2");
String data4= getIntent().getStringExtra("Phone3");
String data5= getIntent().getStringExtra("Offer");
final String data6= getIntent().getStringExtra("Facebook");
final String data7= getIntent().getStringExtra("Menu");
final String data8 = getIntent().getStringExtra("Menu");
TextView nm = findViewById(R.id.name);
TextView ad = findViewById(R.id.address);
TextView ph1 = findViewById(R.id.phone1);
TextView ph2 = findViewById(R.id.phone2);
TextView ph3 = findViewById(R.id.phone3);
TextView off = findViewById(R.id.closeoffer);
ImageView fb = findViewById(R.id.facebookbut);
Button menu = findViewById(R.id.menubut);
nm.setText(data0);
ad.setText(data1);
ph1.setText(data2);
ph2.setText(data3);
ph3.setText(data4);
off.setText(data5);
fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data6));
startActivity(intent);
}
});
menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent().getStringExtra("Menu")));
startActivity(i);
}
});
}
Maybe am a little confuse because my bad language.
I want to add if else into OnClick().
Mean.. if this button have link to visit ACTION_VIEW, then go. But if haven't link from realtime database, then give a Toast message "Sorry there's no link available at this moment"
Maybe you need to do this :
fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//data6 may be having link to fb ,here we check if it is not null
if(data6!=null )
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data6));
startActivity(intent);
}else{
//toast a message
Toast.makeText(Yourclass.this, "Sorry there's no link available at this moment", Toast.LENGTH_SHORT).show();
}
});
My app currently has 5 buttons (I'm going to add more later) and when each button is clicked, it'll assign a number to an item.
I'm wondering if there's a more efficient way of writing the setOnClickListner (it seems like I have to use that since I'm using this as a fragment. I found a way to do it if I was to assign an onClick in the xml but I can't apply that to this part of the code). I have it written out 5 times (and in the future it'll be more)
buttons[0] = (Button) view.findViewById(R.id.cut1Btn);
buttons[1] = (Button) view.findViewById(R.id.cut2Btn);
buttons[2] = (Button) view.findViewById(R.id.cut3Btn);
buttons[3] = (Button) view.findViewById(R.id.cut4Btn);
buttons[4] = (Button) view.findViewById(R.id.cut5Btn);
buttons[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "TESTING BUTTON CLICK 1",Toast.LENGTH_SHORT).show();
data = 1;
}
});
buttons[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "TESTING BUTTON CLICK 2",Toast.LENGTH_SHORT).show();
data = 2;
}
});
buttons[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "TESTING BUTTON CLICK 3",Toast.LENGTH_SHORT).show();
data = 3;
}
});
buttons[3].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "TESTING BUTTON CLICK 4",Toast.LENGTH_SHORT).show();
data = 4;
}
});
buttons[4].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "TESTING BUTTON CLICK 5",Toast.LENGTH_SHORT).show();
data = 5;
}
});
Could I maybe do a switch or a loop? Like assign i = 0, i < 5, i++ for the button array and then for data make that data = i + 1 ? If so, any suggestions on how I can do that?
Thanks!
THe other answers here will work, but either use the tag (generally a bad idea and prevents any other use) and aren't really object oriented. Instead you should make a class an instantiate it.
private class MyClickListener {
private int data;
public MyClickListener(int data) {
this.data = data;
}
public void onClick(View view) {
Toast.makeText(getActivity(), "TESTING BUTTON CLICK" + data,Toast.LENGTH_SHORT).show();
}
}
...
int i=0;
for(Button button : buttons) {
button.setOnClickListener(new MyClickListener(i++));
}
You could use a common method and then implement a switch case based upon R.id of your button
An example would in like this.
in onCreate(--) method
cut1Btn = (Button) view.findViewById(R.id.cut1Btn);
cut2Btn = (Button) view.findViewById(R.id.cut2Btn);
cut3Btn = (Button) view.findViewById(R.id.cut3Btn);
cut4Btn = (Button) view.findViewById(R.id.cut4Btn);
cut5Btn = (Button) view.findViewById(R.id.cut5Btn);
cut1Btn.setOnClickListener(this);
cut2Btn.setOnClickListener(this);
cut3Btn.setOnClickListener(this);
cut4Btn.setOnClickListener(this);
cut5Btn.setOnClickListener(this);
Implement View.onClickListener in your activity and override this method in your activity
public void onClick(View v){
switch(v.getId()){
case R.id.cut1Btn:
//Put Your Code Here
break;
case R.id.cut2Btn:
//Put Your Code Here
break;
case R.id.cut3Btn:
//Put Your Code Here
break;
case R.id.cut4Btn:
//Put Your Code Here
break;
case R.id.cut5Btn:
//Put Your Code Here
break;
}
}
You can implement a switch to do this things:
Your class must implement OnClickListener and override function OnClick(View view).
Then just set OnClickListener(this) like this on onCreate method
buttons[1].setOnClickListener(this)
buttons[2].setOnClickListener(this)
buttons[3].setOnClickListener(this)
do a switch-case on override function
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// button 1 do
break;
case R.id.button2:
// button 2 do
break;
case R.id.button3:
// button 3 do
break;
}
}
I am creating a Android application and the fist activity that will open will be a password entry. For know the password has already been set in a variable with the value "admit". I have created a if statement to check what value the user has entered and then compare it to the variable, and if the values match then go to the home screen, however it keeps saying it is the wrong password. Can anybody take a look at my code and advise were i am going wrong.
public class Password extends ActionBarActivity implements View.OnClickListener {
protected String password = "admin";
String getPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
Button passwordButton = (Button) findViewById(passwordbutton);
EditText passwordInput = (EditText) findViewById(R.id.password);
getPassword = (passwordInput.getText().toString());
passwordButton.setOnClickListener(this);
}
public void onClick(View v) {
if (getPassword.equals(password)) {
Intent goHome;
goHome = new Intent(this, home.class);
startActivity(goHome);
} else {
AlertDialog.Builder wrongPasswordBuilder = new AlertDialog.Builder(this);
wrongPasswordBuilder.setTitle(getString(R.string.wrongPasswordTitle));
wrongPasswordBuilder.setMessage(getString(R.string.wrongPasswordTryAgain));
wrongPasswordBuilder.setPositiveButton("ok", null);
AlertDialog dialog = wrongPasswordBuilder.show();
}
}
If you write passwordInput.getText().toString() in onCreate(), it always return empty String. So write these lines in onClick(). Code is written below
public void onClick(View v) {
getPassword = passwordInput.getText().toString()
if (getPassword.equals(password)) {
Intent goHome;
goHome = new Intent(this, home.class);
startActivity(goHome);
} else {
AlertDialog.Builder wrongPasswordBuilder = new AlertDialog.Builder(this);
wrongPasswordBuilder.setTitle(getString(R.string.wrongPasswordTitle));
wrongPasswordBuilder.setMessage(getString(R.string.wrongPasswordTryAgain));
wrongPasswordBuilder.setPositiveButton("ok", null);
AlertDialog dialog = wrongPasswordBuilder.show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
Button passwordButton = (Button) findViewById(passwordbutton);
EditText passwordInput = (EditText) findViewById(R.id.password);
passwordButton.setOnClickListener(this);
}
public void onClick(View v) {
//Place the getPassword variable in the onClick method. That way it's stored everytime you click it.
getPassword = (passwordInput.getText().toString());
if (getPassword.equals(password)) {
Intent goHome;
goHome = new Intent(this, home.class);
startActivity(goHome);
} else {
AlertDialog.Builder wrongPasswordBuilder = new AlertDialog.Builder(this);
wrongPasswordBuilder.setTitle(getString(R.string.wrongPasswordTitle));
wrongPasswordBuilder.setMessage(getString(R.string.wrongPasswordTryAgain));
wrongPasswordBuilder.setPositiveButton("ok", null);
AlertDialog dialog = wrongPasswordBuilder.show();
}
}
Place getPassword = passwordInput.getText().toString(); within your OnClickListener. Right now your variable getPassword is being set in your onCreate method, so it will always be set to what your default value is for the EditText. Instead, you need to update the variable every time the button is pressed.
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.