How to go to a specific tab from a different class? - java

I have a TabHost Activity with three tabs. On the second tab I have a button, clicking which would open a new Activity class (not part of the TabHost). On clicking a button in the new Activity class, it should return to the Tab 2 of the TabHost class but it returns to Tab 1. How should I fix it?
Here is the code in my new Activity class:
ImageButton btn1 = (ImageButton)findViewById(R.id.close);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ES_pic2.this, Work.class);
startActivity(intent);
ES_pic2.this.finish();
}
});
The code of my TabHost class:
private void tabs(){
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("WORK 1");
tabs.addTab(spec);
spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("WORK 2");
tabs.addTab(spec);
spec=tabs.newTabSpec("tag3");
spec.setContent(R.id.tab3);
spec.setIndicator("WORK 3");
tabs.addTab(spec);
}
I did have look at other questions with similar problems, but they didn't help me.

In the onResume of the activity in which tabs are, use setCurentTab function to set the current tab. If you want to set the tab to the same index which was selected before you started the new activity, then you can save the index in SharedPreference or something. Then use that to set the current tab.

Related

Creating a different activity within main activity triggered by a button

I have rather simple task to complete, but I can't get my head around it, I have my main class here, I choosing between screens using buttons, My task is to create a About page just explaing the rules of the game (my application).
public class Hashi_Main extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
// click handling
public void onClick(View view) {
switch (view.getId()) {
case R.id.exit_button:
finish();
break;
case R.id.new_button:
NewGame();
break;
case R.id.about_button:
NewGame();
break;
}
}
And here i create a my NewGame activity this all works.
public void NewGame() {
// We first ask for the difficulty level.
new AlertDialog.Builder(this)
.setTitle(R.string.new_game_title)
// we provide a char array with the on click listener.
.setItems(R.array.difficulty,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int hardness) {
Intent intent = new Intent(Hashi_Main.this, HashiGame.class);
intent.putExtra(HashiGame.KEY_DIFFICULTY, hardness);
startActivity(intent);
}
})
.show();
}
What I want to do is the same thing but to use it for about page, I want to use TextView for the rules, this activity will have nothing else except text and a back to main menu button. I tried something like this.
public void About() {
LinearLayout lheader = new LinearLayout(this);
lheader.setOrientation(LinearLayout.HORIZONTAL);
TextView about_rules = new TextView(this);
about_rules.setId(about_id);
lheader.addView(about_rules);
}
But I am stuck for a while now, how can i trigger this activity?
Create an about activity and use an intent to launch your newly created activity. like so:
Intent intent = new Intent(Hashi_Main.this, AboutActivity.class);
startActivity(intent);
I cannot see any Activity in the About() method. It is just a local LinearLayout with a TextView.
You need to learn more about Android development before making any app.

ANDROID: launch new activity from inflated pop up view

I have a main_activity in which by pressing a button I launch a form to be completed:
popup= getLayoutInflater().inflate(R.layout.pop_up, null);
signup = new SignUp(popup);
register = (Button) findViewById(R.id.sign_up);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MyLocalBartender.this);
alertBuilder.setView(popup);
final AlertDialog dialog = alertBuilder.create();
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
dialog.show();
}
});
By what you can see I'm using a second activity class (SignUp) to manage the form and not the root class from which it was launched (main_activity).
In this new class I set all the click listeners etc to verify the inputs through a third class that implements an OnClickListener.
Everything works fine until this point. But now I want to test the page/activity called HomePage in which the user should land to if the form is filled.
So what I don know is I remove the click listener from the previous handler and I create an anonymous one to simply open the new activity on register button pressed:
// signup_registerButton.setOnClickListener(new SignupListener(signup_emailField,signup_passwordField1,
// signup_passwordField2, signup_textTemp,signup_organiserRadio, signup_staffRadio,signup_alertMessage));
////*************************TEST******************* START
signup_registerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent menu = new Intent(getApplicationContext(), HomePage.class);
startActivity(menu);
}
});
////*************************TEST******************* END
but this returns a NullPointerException.
I've tried to launch the HomePage.class from the main_activity directly and it works and also I've tried to launch the main activity from this REGISTER button, which didn't work, so this tells me that the problem it is somewhere here.
You need to pass an Activity Context to the Intent constructor. Activities context and Applications context are not the same. Activities context hold much more inforamtions.
In your case you can do like this:
signup_registerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent menu = new Intent(yourActivity, HomePage.class);
startActivity(menu);
}
});
where yourActivity is your activity instance. You can pass it as variable or access it via main_activity.this from inner classes (Listeners) on anywhere inside your class.

Multiple activities and buttons?

I want to make buttons once I click on the button I go to another activity?
and the problem is only the first button is working!
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), PageOne.class);
v.getContext().startActivity(myIntent);
Button PageTwo = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), PageTwo.class);
v.getContext().startActivity(myIntent);
}
{}
});
}
});
}
}
Think it is because most of your code is closed inside the scope of the first onClickListener, try something like this.
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
PageOneButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(Main.this, PageOne.class);
startActivity(myIntent);
});
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageTwoButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(Main.this, PageTwo.class);
startActivity(myIntent);
});
Using v.getContext() should be ok, this is just how I usually would do as the Activity itself is indeed a valid context. I guess it just seems more readable to me.
Edit:
Just as a clarification to the current state of your code. The second button is assigned a onClickListener only after the first button is pressed. But since the first button takes the app to a new Activity, inherently destroying the Main Activity, the second button will never have a chance to reach it's onClickListener.
Hope it makes sense, nevertheless the code above should fix the issue.
There are a couple of issues currently in your code. The first issue is that your second button is being defined inside the first button's declaration. The next issue is that you're setting the second OnClickListener to the wrongly named button. You've made a typo and instead of PageTwo, which you've called the Button (presumably you wanted to call it PageTwoButton in accordance with the first Button) and then set the OnClickListener to PageTwoButton instead. Seeing as you're also using multiple Buttons, it's a lot cleaner and more efficient to use a GroupOnClickListener. I'd probably also suggest using 'this' instead of 'v.getContext()' as well when setting up your Intents. Change your code to be like so:
Button PageOneButton = (Button) findViewById(R.id.btnPageOne);
Button PageTwoButton = (Button) findViewById(R.id.btnPageTwo);
PageOneButton.setOnClickListener(addGroupOnClickListener);
PageTwoButton.setOnClickListener(addGroupOnClickListener);
private OnClickListener addGroupOnClickListener = new OnClickListener() {
public void onClick(View v) {
if (v == PageOneButton) {
Intent myIntent = new Intent(Main.this, PageOne.class);
startActivity(myIntent);
} else if (v == PageTwoButton) {
Intent myIntent = new Intent(Main.this, PageTwo.class);
startActivity(myIntent);
}
}
};
Hope this helps!
Two words: Code Indentation
Were you to indent your code properly, you would have noticed that you're setting OnClickListener INSIDE your first buttons' listener. Move it outside your first listener, as has already been advised by others.
There's also an extra pair of {}, which is redundant.
Also, #edwoollard noticed that for the second button, you're using two different names, PageTwo and PageTwoButton. Keep that in mind, unless it's a typo.

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.

Show information based on what button that was pressed

I'm making a information app for Android and I can't figure out how to change a button depending on which button was pressed in a previous class.
I made this picture that shows how the app works:
After clicking the first button you get to a new class with 2 new buttons that sends you to the same class but the 3 buttons there is supposed to change depending on which one of the two buttons you pressed, the three buttons you get sent to sends you to the same class but there is a TextView that changes text depending on what button you pressed.
So I need to add some kind of information to the button (an intent?) so it knows what you pressed earlier.
I'm kinda new to Java and Android so I'm sorry if I explained in a weird way.
Thanks in advance
Each time you start a new Activity, but some extras in that intent telling the next activity what the buttons should display. In the next activity, read the extras from the intent you got, and programatically set your buttons' text accordingly.
Here's some psuedo code.
public class FirstActivity extends Activity{
//setup button 1 and two first, then set their onClickLiseners like so
View.OnClickListener clickListener = new View.OnClickListener(){
public void onClick(View view){
Intent newActivity = new Intent(FirstActivity.this, SecondActivity.class);
if(view == button1){
//add extra to intent indicating button1 was clicked
}
else{
//add extra to intent indicating button2 was clicked
}
startActivity(newActivity);
}
};
// other stuff in your activity
}
public class SecondActivity extends Activity{
protected void onCreate(Bundle icicle){
Intent startedBy = getIntent();
if(started by has the extra indicating button 1 was clicked){
//do button1 stuff
}
else{
//do button2 stuff
}
}
}
}

Categories

Resources