I have problems with a menu systems that I made. four buttons the all work correctly but for one button that i have made it loads the wrong activity when I click on it, but when I click on the emulator's back button then it loads the correct activity and don't want this i want it to load the correct activity
This is the code that I have write for the menu
the check_view loads the question_view where it should load the check_view
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
appointment_image_view=(ImageView)findViewById(R.id.appointment_image_view);
check_view=(ImageView)findViewById(R.id.check_view);
questions_view=(ImageView)findViewById(R.id.questions_view);
survey_view =(ImageView)findViewById(R.id.survey_view);
appointment_image_view.setOnClickListener(this);
check_view.setOnClickListener(this);
questions_view.setOnClickListener(this);
survey_view.setOnClickListener(this);
}
public void onClick(View view){
switch (view.getId())
{
case R.id.appointment_image_view:
// open
Intent appointment_intent = new Intent(this,apppointmentactivity.class);
this.startActivity(appointment_intent);
break;
case R.id.check_view:
// open
Intent Health_intent1 = new Intent(this,HealthActivity.class);
this.startActivity(Health_intent1);
case R.id.questions_view:
//open
Intent Question_Intent = new Intent(this,QuestionsActivity.class);
this.startActivity(Question_Intent);
break;
case R.id.survey_view:
Intent Survery_intent = new Intent(this,SurveyActivity.class);
this.startActivity(Survery_intent);
break;
}
}
Try to add break; at your case R.id.check_view.
You are lacking a break; after this:
case R.id.check_view:
// open
Intent Health_intent1 = new Intent(this,HealthActivity.class);
this.startActivity(Health_intent1);
Related
I am developing an app for Quick settings Tile. I want to open a layout or a custom dialog when user click on the tile, whichever is a good option. Assume this as a custom dialog. I tried many examples, but I got no luck!
Thank you in advance.
You just have to #Override method of Tile service.
#Override
public void onClick() {
super.onClick();
Tile tile = getQsTile();
switch (tile.getState()) {
case Tile.STATE_INACTIVE:
// write code for start service or open activity according to your prefrrance
StaticUtils.closeNotificationTopPanel(this);
tile.setLabel(getString(R.string.service_running));
updateTileState(Tile.STATE_ACTIVE);
break;
case Tile.STATE_ACTIVE:
updateTileState(Tile.STATE_INACTIVE);
break;
default:
updateTileState(Tile.STATE_INACTIVE);
break;
}
}
when you Click of tile service button you have to close the notification panel below code will help for that.
public static void closeNotificationTopPanel(Context context) {
Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(closeIntent);
}
below method will help you to change the tile state and name of a button according to state.
private void updateTileState(int state) {
Tile tile = getQsTile();
if (tile != null) {
tile.setState(state);
Icon icon = tile.getIcon();
switch (state) {
case Tile.STATE_ACTIVE:
icon.setTint(Color.WHITE);
break;
case Tile.STATE_INACTIVE:
case Tile.STATE_UNAVAILABLE:
default:
icon.setTint(Color.GRAY);
break;
}
tile.updateTile();
}
}
Try to use this link: http://wintechtutorials.com/blog/android-customize-quick-setting-tiles-7-0-nougat/
Here in this link they use default Alert dialog. Try to create custom dialog as per need. Hope this will work for you.
I have 10 cardview in a fragment, when user click on one it'll open a specific html file within the application. When I try to open this class, the apps always crash .
My MainActivity is here
2nd Activity is here
What you can do to solve your issues is, in your MainActivity's onclick method change like :
Intent i;
switch (v.getId()) {
case R.id.indonesia_card:
i = new Intent(this, Indonesia.class);
i.putExtra("isFrom", "Indonesia");
startaCtivity(i);
case R.id.mathematika_card:
i = new Intent(this, Indonesia.class);
i.putExtra("isFrom", "Mathematika");
startaCtivity(i);
case R.id.ipa_card:
i = new Intent(this, Indonesia.class);
i.putExtra("isFrom", "IPA");
startaCtivity(i);
}
Now, In your other Activity receive the intent and set data appropriately.
String from = getIntent.getStringExtra("isFrom");
if (isFrom != null) {
if (isFrom.equals("Indonesia") {
webview.loadUrl("file:///android_asset/yourfile.html");
} else if (isFrom.equals("Mathematika") {
webview.loadUrl("file:///android_asset/yourfile.html");
}else if (isFrom.equals("IPA") {
webview.loadUrl("file:///android_asset/yourfile.html");
}
}
Hope this will help you.
I am developing an android game in which there are near about 50 levels and each level represents a single activity . E.g. Level1, Level2...Level50.
After completion of every level I am switching to LevelUpActivity where I am showing the current score and level of player along with Level Up message. On button click I am switching to next level of the game. Now my question is that how can I switch to next activity (level) when I am having the current level number?
E.g. Suppose I have cleared Level2 then I want to jump on Level3 from LevelUpActivity what are the efficient ways to do that? Currently I am getting success for this task by using Switch Case statements. But will it be working efficiently for 50 or more than that numbers to check and then switch activities accordingly?
Following code snippet shows the class definition of LevelUpActivity
public class LevelUpActivity extends Activity implements View.OnClickListener {
Button btn_continue;
TextView scoreview, levelview;
int score, level;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level_up);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
score = bundle.getInt("score");
level = bundle.getInt("levelno");
System.out.println("Current Score = " + score);
levelview = (TextView) findViewById(R.id.levelView);
levelview.setText("Level " + level + " Completed");
scoreview = (TextView) findViewById(R.id.score);
scoreview.setText(score + "");
btn_continue = (Button) findViewById(R.id.levelbtn);
btn_continue.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.levelbtn) {
Intent i;
switch (level) {
case 1:
i = new Intent(this, Level2.class);
i.putExtra("score", score);
startActivity(i);
break;
case 2:
i = new Intent(this, Level3.class);
i.putExtra("score", score);
startActivity(i);
break;
case 3:
i = new Intent(this, Level3.class);
i.putExtra("score", score);
startActivity(i);
break;
}
}
}
}
The performance of the switch statement won't be significantly decrease with the number of statements. The switch statement will be incredibly long though.
Assuming that every level Activity is following the same interface, (i.e. they accept a "score" and nothing else), then you can instead just put all your levels in an array.
private static final Class<Activity>[] levels = [
Level1.class,
Level2.class,
Level3.class,
...
Level4.class
]
Then just retrieve them like so:
int levelNumber = someMethodToGetLevelNumber();
Class<Activity> levelClass = levels[levelNumber];
Intent i = new Intent(this, Level2.class);
i.putExtra("score", score);
startActivity(i);
I'm working on a music player for android.
In it I have an activity which contains ListView that displays songs/artists/albums etc.
I keep track of the lists the user views (For example all album --> artist x --> album y)
In order to provide a way for the user to go back in the lists, I override onBackPressed() so it will pick the previous list the user viewed (handled by a Stack)
#Override
public void onBackPressed()
{
if (ListActivity.onScreenList == ListActivity.ALL_SONGS && viewStack.empty())
super.onBackPressed();
else if (viewStack.empty())
navListView.performItemClick(navListView.getChildAt(0), 0, navListView.getChildAt(0).getId());
else
{
TypeAndName tan = viewStack.pop();
if (tan.getName() == null)
{
int num = tan.getType();
navListView.performItemClick(navListView.getChildAt(num), num, navListView.getChildAt(num).getId());
}
else
{
switch (tan.getType())
{
case ARTISTS:
break;
case ALBUMS:
{
Album alb = albumListAdapter.getAlbumAtPosition(position);
c = db.rawQuery(DatabaseHandler.qryGetSongsByAlbum, new String[] { alb.getAlbumName() });
songListAdapter.setPlaylist(DatabaseHandler.convertCursorToSongsArray(c));
lv.setAdapter(songListAdapter);
ListActivity.onScreenList = ListActivity.ALL_SONGS;
}
break;
case GENRES:
break;
case PLAYLISTS:
break;
case YEAR:
break;
// TODO add other options here
}
}
}
}
the weird part is, that the back sound (the one that is played whenever you press the back button) is played twice.
However, the onBackPressed() method is not called twice and the code works as expected.
I am building a login system in android and i need to accomplish the following:
save userdata on the local storage
when a user is created and i press on the button it needs to dynamically create a radiobutton with the user name and short name in it.
I have already managed to accomplish these things, but the only problem i have now is that i want to to have 2 classes, one where you register your account and the other where the radiobuttons with the user data is displayed. So my question: how can i program the calss in such a way that when i press on the register button that it creates the radiobuttons in a different class.
beneath is the code of the classes:
enter code here
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.Save:
Intent I = new Intent();
I.putExtra(
I.setClass(this, ShowUsers.class);
startActivity(I);
break;
}
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Start other Activity
switch (v.getId()) {
case R.id.create:
Intent intent = new Intent();
intent.setClass(Main.this, Register.class);
startActivity(intent);
break;
}
Bundle extras = getIntent().getExtras();
String userName;
if (extras != null) {
userName = extras.getString("editTextName,editTextPassword,editTextshortname");
// and get whatever type user account id is
}
}
}
when i click the save button it needs to create the radio button in the main class, now the question is how can i accomplish this?
If the second class is an Activity, then one solution is to send it an Intent with the data it needs to create the radio buttons. You do this by creating an Intent object, calling any of its setExtra() methods, then calling startActivity() with the Intent. Check out the Android API docs for more details.