Android pages Activity or Layout? - android

My app Consists an intro page with "Start", "About", etc..
I managed to create a functional version of the app by having the "Start" button call a new layout in which a new onclick listener is defined.
This doesn't seem clean to me that I#m defining a new onClick listener for each Layout I use and wonder how the correct way would be to create individual pages (including my "About" and any other screens I implement).
Any advice is appreciated, thanks.

You should create an Activity for each "screen". All the buttons will launch the same event (onClick) and you will start the right activity looking at the ID of the event's source view.
Something like this:
public void onButtonClick(View target) {
System.out.println("Button clicked '" + target + "'");
switch (target.getId()) {
case R.id.a_button:
Intent intent = new Intent(this, AboutActivity.class);
this.startActivity(intent);
break;
}
}

Related

android tablayout - how to use a button instead of tab indicators to trigger click event?

so i have a standard tabLayout that is in a viewpager.
But the very last tab needs to open a new activity. its only purpose is to open a new activity. and when the user returns from the activity by pressing the back button the last tab the user was at should be selected.
my tabs are custom views and there are 4 of them but the last one should be a button to trigger an event. so it will look just like a tab but really will be a button with a onclick listener. how can i acheive this ? i am wondering if a framelayout could be used and activated for the very last tab icon . thats the only way i can see to do it is to get the custom view and set its parent to be clickable that way it absorbs the click event. then i can set a onclick listener on the parent to open the activity. but then how to handle swipping to that tab ? swipping should open that activity as well.
Assuming you're using TabLayout.setupWithViewPager(), you can accomplish what you want by simply adding an extra tab and changing the OnTabSelectedListener.
// normal setup
tabs.setupWithViewPager(pager);
// our extra tab
tabs.addTab(tabs.newTab().setText("extra"));
// remove the `OnTabSelectedListener` created by `setupWithViewPager()`
tabs.clearOnTabSelectedListeners();
// add our own `OnTabSelectedListener`
tabs.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(pager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == pager.getAdapter().getCount()) {
// special case for the last tab in the list
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
else {
// otherwise, handle as normal
super.onTabSelected(tab);
}
}
});

How to add a page in Android Studio that connects to a button?

This is my first time using Android Studio, so please let me know if there is a very simple way to add a new page using buttons for my android app. If you click the button it should lead you to a new file with other code. Thank You so much!
Create a layout with a button in it
Create two activities
setContentView(R.layout.your_xml) on one of the activities
Set up the buttons onClickListener
Code for step 4:
//initialize buttons
button = (Button) findViewById(R.id.button_location);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
});
Just create a new intent, and start it inside of the onclickListener of that button. Don't forget that on the intent you need to put the main activity from where you want to launch the new activity and the class (new page) that you want to launch.
I think that this link can help you.
Starting new Activity
I hope this helped you :)

Switching activity layout according to the button pressed in previous activity

I am a beginner in java and android.
I have a categories activity with two buttons & the game activity with two layouts.I want to let the game activity detect which button is pressed in categories and set its corresponding layout by this way:
if (Case_A)
setContentView(R.layout.layout1);
else if (Case_B)
setContentView(R.layout.layout2);
With what code should I replace "Case_A" and "Case_B" to detect the button press ?
When you create the Intent you can pass a value to GameActivity
Intent i = new Intent(CategoryActivity.this, GameActivity.class);
i.putExtra("layout", "layout1");
startActivity(i);
then in your GameActivity in your onCreate()
Intent intent = getIntent();
String layout = intent.getStringExtra("layout");
if (layout.equals("layout1")
setContentView(R.layout.layout1);
else if (Case_B)
setContentView(R.layout.layout2);
This is one way to just pass any value you want depending on which Button was pressed. You also could get the Button text, id, or whatever you want and pass that.

How to switch between layouts in android?

I am making a contact list application, and i have a main screen that helps you save a contact. I have a search button there. Then i created another UI that i call it search screen. When i click on the search button on main screen, i want to be redirected to the other UI, i.e. search screen. I am planning to add setOnClickListener method for search button but how can i switch from main screen to search screen when i click the button?
btn_goSearch.setOnClickListener(new OnClickListener() {
?????????
}
});
Thanks
#Override
public void onClick(View v) {
Intent intent = new Intent(Currentactivity, YourSearcgActivity.class);
intent.putExtra("EXIT", false);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
you should create another Activity, maybe call it SearchActivity, and when the user clicks the button, just start SearchActivity with a simple intent, that will load its own layout and everything:
startActivity(new Intent(context,SearchActivty.class));

Start another Activity inside the FrameLayout of TabActivity

I am trying to make an application which have 4 tabs at the bottom of the screen.
All of them contain Activity (Intent).
And I want to navigate any of the Activity to another activity. But want to keep the TabWidget visible.
Let me know as quickly as possible if you know about it.
Shaiful
The problem of error occuring due to the replacement of activities can be solved in the following manner.
First Let us understand the flow:
We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now
When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)
The code can be as follows..
For going from list to details...
(This can be in the onClickListener)
private OnClickListener textListener = new OnClickListener() {
#Override
public void onClick(View v) {
Constants.SHOW_DETAILS = true;
Intent intent = new Intent(context, DetailsActivity.class);
replaceContentView("activity3", intent);
}
};
public void replaceContentView(String id, Intent newIntent) {
View view = ((ActivityGroup) context)
.getLocalActivityManager()
.startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
((Activity) context).setContentView(view);
}
When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
if (MathHelper.SHOW_DETAILS) {
Log.e("back", "pressed accepted");
Constants.LIST_ACTIVITY = 1;
Constants.SHOW_DETAILS = false;
Intent intent = new Intent(this, Tab_widget.class);
startActivity(intent);
finish();
}
}
The most important part here is
Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc
Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs
tabHost.setCurrentTab(Constants.LIST_ACTIVITY);
This is implemented in Tabs with multiple activities in a single tab.
However when multiple times activities are called StackOverFlow error arises. Tried very hard but unable to solve it.. Please someone tell a method to solve this problem
Also need to Replace an activity in a tab, However from child activity. How is that to be done?
At any one moment there may only be one activity. Docs about this here

Categories

Resources