When a user hit the "register" menu item it come to the register activity and after hit the button to return to the MainActivity with the menu item from "register" to "unregister" I don't know how would I go about changing the menu item text.
public class register extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Button register = findViewById(R.id.reg);
EditText name =findViewById(R.id.name);
EditText admin = findViewById(R.id.admin);
EditText email = findViewById(R.id.email);
register.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
startActivity(new Intent(register.this,MainActivity.class));
}
});
}
You can do it with onPrepareOptionsMenu:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem btn = menu.findItem(R.id.register_menu_item);
if (/* here check if user is registered or not */) {
btn.setTitle("Unregister");
} else {
btn.setTitle("Register");
}
return super.onPrepareOptionsMenu(menu);
}
Related
Good day, I am having a problem with creating an intent with a menuitem to navigate to another layout, my code is below.
I made a button that when clicked it should display the available options reading from the String.xml
This below is my code that has the layouts from the String.xml
/**********************************************************/
public class IndexActivity extends AppCompatActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Toolbar toolbar;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
ImageView homeImageView = (ImageView) findViewById(R.id.homeImageView);
homeImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Initializing the popup menu and giving the reference as current context
PopupMenu popupMenu = new PopupMenu(IndexActivity.this, homeImageView);
// Inflating popup menu from popup_menu.xml file
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
// Toast message on menu item clicked
Toast.makeText(IndexActivity.this, "You Clicked " + menuItem.getTitle(), Toast.LENGTH_SHORT).show();
// Intent x = new Intent(IndexActivity.this, menuItem.getClass());
return true;
}
});
// Showing the popup menu
popupMenu.show();
}
});
}
}
I'm trying to make a text adventure game in android studio using buttons to progress through the map. I'm trying to call a button that is in the second page of the game. I know the problem is that it can't find the button since its on the second page and the page the .java file is looking at is page one. Is there a way to have the .java file look at all .xml page at the same time?
public class textAdven extends AppCompatActivity {
boolean male;
int gender;
private Button boyButt;
private Button girlButt;
private TextView genderBox1;
private Button shedButt, fieldButt, yardButt, lookAround1, cabButt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_adven);
boyButt =(Button) findViewById(R.id.boyButt);
girlButt =(Button) findViewById(R.id.girlButt);
shedButt=(Button) findViewById(R.id.shedButt);
cabButt =(Button) findViewById(R.id.cabButt);
lookAround1 =(Button) findViewById(R.id.lookAround1);
fieldButt=(Button) findViewById(R.id.fieldButt);
yardButt =(Button) findViewById(R.id.yardButt);
boyButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven2);
male = true;
genderBox1 = findViewById(R.id.genderBox1);
genderBox1.setText("boy");
}
});
girlButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven2);
male = false;
genderBox1 = findViewById(R.id.genderBox1);
genderBox1.setText("girl");
}
});
shedButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven3);
}
});
yardButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.adven2);
}
});
}
}
I'm doing Quiz in Android Studio. The game is divided into two categories. After passing the first category, the second button (opening the second category) changes the status from setEnable "false" to "true". How to use the SharedPreferenced method in my code that the changes related to the second button (.setEnable) will be saved after closing the application.
Last level of first category
public class win extends AppCompatActivity implements View.OnClickListener{
Button win1;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_winflagi);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
win1 = (Button) findViewById(R.id.winflagi);
win1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==win1)
{
Intent myIntent = new Intent(win.this, Activity2.class);
myIntent.putExtra("isEnabled", "enabled");
startActivity(myIntent);
}
}
}
Class containing a buttons for two categories ...
button3 opened the first category
entrycity opens the second category
public class Activity2 extends AppCompatActivity implements View.OnClickListener{
Button button3;
Button entrycity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_2);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
entrycity = (Button) findViewById(R.id.entrycity);
entrycity.setOnClickListener(this);
}
#Override
public void onClick(final View v) {
final MediaPlayer mp = MediaPlayer.create(this, R.raw.menu);
if (v == button3) {
startActivity(new Intent(Activity2.this, flagi1.class));
Bungee.zoom(this);
mp.start();
}
if (v== entrycity){
Intent intent=getIntent();
String isEnabled = intent.getStringExtra("isEnabled");
if(isEnabled==null||isEnabled.equals("disabled")){
entrycity.setEnabled(false);
}
else{
entrycity.setEnabled(true);
startActivity(new Intent(this, cities1.class));
}
}
}
}
Set a boolean preference when you are enabling the Button.
else{
entrycity.setEnabled(true);
getSharedPreferences("MY_PREF", MODE_PRIVATE).edit().putBoolean("isEnabled",true).apply();
startActivity(new Intent(this, cities1.class));
}
and in your onCreate set the enabled status of the button based on the preference :
entrycity.setEnabled(getSharedPreferences("MY_PREF", MODE_PRIVATE).getBoolean("isEnabled",false));
I'm a beginner but I would like to modify an older project.
On the Preferences Activity, there are already few buttons, but I would like to add a new button, which, when clicked, opens a new Activity and shows an image only.
For this I declared this new activity in the Manifest:
<activity
android:name=".VisualHelpActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" >
</activity>
I declared the button on the associated xml layout:
<Button
android:id="#+id/button_visualhelp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="visual_help"
android:text="Visual Help" />
I created the new VisualHelp layout xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/visualhelp_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/guide" />
Declared its new VisualHelpActivity java file:
public class VisualHelpActivity extends TranslatableActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visualhelp);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.visual_help, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void refreshLanguage() {
}
}
… and now I’m supposed to connect it all together in the "main" PreferencesActivity, which looks as follows:
public class PreferencesActivity extends TranslatableActivity {
public static String url = "https://www.paypal.com/cgi-bin....";
ImageView donate;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_preferences);
donate = (ImageView) findViewById(R.id.button_donate);
donate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
donate(v);
}
});
Spinner s = (Spinner) findViewById(R.id.combo_language);
Lang[] languages = LanguageUtil.Lang.values();
s.setAdapter(new ArrayAdapter<Lang>(this,
android.R.layout.simple_dropdown_item_1line, languages));
s.setSelection(getPosition(languages, LanguageUtil.getLanguage()), true);
}
private int getPosition(Object[] objs, Object item) {
for (int i = 0; i < objs.length; i++) {
if (objs[i] == item)
return i;
}
return -1;
}
public void update(View v) {
Maintenance.downloadDB(this, new onFinishListener() {
#Override
public void onFinish() {
LanguageUtil.loadLanguage(PreferencesActivity.this);
}
});
}
public void copy(View v) {
Maintenance.copyDB(this, new onFinishListener() {
#Override
public void onFinish() {
LanguageUtil.loadLanguage(PreferencesActivity.this);
}
});
}
public void save(View v) {
LanguageUtil.saveLanguage(this,
(Lang) ((Spinner) findViewById(R.id.combo_language))
.getSelectedItem());
LanguageUtil.loadLanguage(this);
setResult(RESULT_OK);
finish();
}
public void donate(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
public void cancel(View v) {
finish();
}
#Override
protected void refreshLanguage() {
((Button) findViewById(R.id.button_update)).setText(LanguageUtil
.getDeviceLabel(LanguageUtil.dev_update));
((TextView) findViewById(R.id.language_label))
.setText(LanguageUtil.getDeviceLabel(LanguageUtil.dev_language));
}
}
According to androidbegin.com, I should add this code into the PreferencesActivity java file (code above):
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visualhelp);
button = (Button) findViewById(R.id.button_visualhelp);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(PreferencesAtivity.this,
VisualHelpActivity.class);
startActivity(myIntent);
}
});
}
The only problem is that if I try to add this new public void, multiple marker errors pop up, and if I try to include all below somewhere else, my app fails to work. So, can anyone help me out where and how to include this code part to make it work?
add the code inside oncreate method like this in your PreferencesActivity class:-
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_preferences);
donate = (ImageView) findViewById(R.id.button_donate);
donate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
donate(v);
}
});
button = (Button) findViewById(R.id.button_visualhelp);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(PreferencesAtivity.this,
VisualHelpActivity.class);
startActivity(myIntent);
}
});
Spinner s = (Spinner) findViewById(R.id.combo_language);
Lang[] languages = LanguageUtil.Lang.values();
s.setAdapter(new ArrayAdapter<Lang>(this,
android.R.layout.simple_dropdown_item_1line, languages));
s.setSelection(getPosition(languages, LanguageUtil.getLanguage()), true);
}
i have a pop up which contains edit text..how can i prevent the softkeyboard from showing up on displaying of pop up's because my pop up is moving up as soon as it is displayed.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
i used this line in the activity in which a pop up is displayed on button click. please help...!
android:windowSoftInputMode="stateHidden"
i also used this line in manifest. but of no use.
public class MainActivity extends Activity implements OnClickListener {
AlertDialog dialog;
View checkBoxView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.des_button).setOnClickListener(this);
//checkBoxView = View.inflate(this, R.layout.popup_description, null);
}
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(this);
dialog.getWindow().getAttributes().windowAnimations =
R.style.dialog_animation;
// dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.popup_description);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Button btn_Start = (Button) dialog.findViewById(R.id.ok);
btn_Start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
Button cancel = (Button) dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
#Override
public void onDestroy() {
if (dialog!=null) {
if (dialog.isShowing()) {
dialog.dismiss();
}
Try
android:windowSoftInputMode="stateAlwaysHidden"
in your manifest in that activity. one more thing you can do call the following method after opening that dialog popup.
public static void hideKeyboard()
{
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(editTextInPopup.getWindowToken(), 0);
}