How to switch between layouts in android? - 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));

Related

Android layout doesn't "refresh" until I start a new activity

I have this problem. I wrote a program and I have this code in the MainActivity class:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent activity = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(activity);
output.setText(object.toString());
}
});
In AnotherActivity I modify the object and after that activity ends (with finish()) i want to "refresh" the textView of the MainActivity.
At the end of AnotherActivity the object is actually modified, but the text is not refreshed. If I click again on button, before new Activity is started (with his layout) the text is refreshed as it should, and if I close the AnotherActivity layout, the text is well refreshed. But if I don't click again that button, the text remains the old one. How can I do? Sorry for bad English or bad explanation.
You can use onResume() in the first activity to update your UI when the second activity is finished.
Either that or start the second activity with startActivityForResult which will call onActivityResult for you when the second activity is finished.

How can i load another activity under tab - android

i created one tab menu.when i click the tab it opens one activity under the tab that activity contains two buttons.when clicking the buttons it open the new activity it does not open a activity under that tab itself.how should i open a another activity under the same menu.please help friends.
i used this code to open a another activity when button was clicked
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(SongsActivity.this,FiveActivity.class);
startActivity(intent);
finish();
} });
button2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent obj=new Intent("com.layout.Contactregistration");
startActivity(obj);
}
});
If i understood your question then You can use simple logic for your button click.
First you can change your layout on button click that you are trying to open in a new activity.for this you can use
setContentView(R.layout.SecondLayout);
again on button click and initialize your FiveActivity's resources and use them.
this saves your memory and comfusion of activities.

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.

Creating custom back button in android

I have an application that has a menu and depending on what button you press in the menu a new activity is opened. I want to have a back button on every screen that will bring you to the previous screen so I'm wondering how do I go about this?
Here is some code I have used that works:
backButton = (ImageButton) findViewById(R.id.back_button);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
However its not good programming practice for me to put this code in all my activities. How do I go about creating some kind of stack that saves all the pages viewed and using that to return to the previous page?
I have to put a back button in my application so I cannot use the existing one in the ActionBar.
just an Idea
Create a baseClass which extends Activity. In there declare
#Override
public void onClick(View v) {
super.onBackPressed(); // or super.finish();
}
In all activity extend this Base Class. And in every layout in the button put
android:onClick="onClick"
And to make the xml design of button reusable create it in a separate xml. and add it using <include/>
Have you tried using action bar in your activity ? use in every activity
ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.setTitle(getResources().getString(R.string.app_name));
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.drawable.app_icon);
}
and handle in
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I would suggest against having a custom back button. Android has a hardware back button. Pressing the haradware back button will navigate to the previous.
I don't think you need a custom back button. I don't think its a good programming practice to override the default behaviour.
You create a backbutton in your activity and implementing the functionality as you have done above. Still the user can use the hardware back button for the same functionality. So you would be providing the same functionality which is redundant.
There is a hardware back button on all android devices, and it does exactly what your lines of codes do, unless overridden to do something else.
You can refer to this answer as well.
I had a similar issue where I was using a back button "Cancel" and a home screen "homeActivity". I ended up solving it using this:
CancelButton = (Button) findViewById(R.id.cancel_button);
CancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent homeActivity = new Intent(getApplicationContext(), DeviceListActivity.class);
startActivity(homeActivity);
finish();
}
});
In kotlin
backButton!!.setOnClickListener(View.OnClickListener {
onBackPressed()
})

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