When the application is restarted, dynamically generated Buttons are not present? - android

I'm using a predefined Button to add new Buttons dynamically.
But when I restart the application, the dynamically generated Buttons are not present in layout in which they are created.
Code to generate the Buttons.
if(v == btnaddnew) //Button to new buttons in layout Dynamically
{
final Button btn1 = new Button(this);
btn1.setText("New");
btn1.setId(btncount);
LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(btn1, lp);
btncount++;
}
In main activity
int btncount = 15;

Everytime you close your application and restart it the onCreate method is invoked for your Activity! I suspect you are loading your base layout.xml file, your dynamically added buttons are not part of this layout and are only defined in your code. I am assuming the code to add these dynamic buttons are not in the onCreate, they are probably some button click callback?
My point is this, the dynamically added artifacts will not be present if you close the application for the reason mentioned above!
If you want to resume to the last state after closing it, you need to figure a way to store your current layout before the onDestroy method is invoked for the activity! This stored layout can be reloaded in your onCreate. Let me clear that, in your onCreate you can check if there is a stored layout if yes load from it! Else load from your layout file.
EDIT:
Look at this : http://developer.android.com/guide/components/activities.html#SavingActivityState

Related

Creating Activity Dynamically

I am working now on app, and I want to do the following:
The user creates a new page (new activity and xml layout).
Saving the user's page to database.
Adding the page to ListView as an item, and launch it when the user
will click it
on the ListView.
I saw many answers here about "Creating Activity Dynamically" and I understand it's
impossible, so i don't know how to do that.
The number of the pages the user can create is unlimited so it must be done dynamically.
Each layout of a page in the ListView is the same.
Thank you very much!!!
Indeed there is no way for dynamically creating new activities.
But you can create multiple instances of the same activity. This will require setting your activity's launchMode to "standard" or "singleTop" .
Additionally, you may use initialization flags to have each of these instances use its own specific layout, creating a user experience which is literally identical as having multiple activities:
Intent intent = new Intent(this, MyDynamicActivity.class);
Bundle b = new Bundle();
b.putInt("LayoutIndex", mode);
intent.putExtras(b);
startActivity(intent);
And the activity:
class MyDynamicActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
int layoutIndex = b.getInt("LayoutIndex");
// and here populate the activity differently based on layoutIndex value
}
}
But how can you dynamically populate different instances of an activity?
Well, there is no easy way. You cannot for example create an ad-hoc XML layout file and store it in
filesystem, because XML layouts must be compiled in a specific format to be loadable by Android.
The only thing you can do is from your Java code dynamically set the layout widgets following the rules
set by the user. Below is an example of how Java layout generation code looks like:
LinearLayout layout = new LinearLayout(this);
layout.setGravity(Gravity.CENTER);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button button = new Button(this);
button.setText("My Button");
layout.addView(button, params);
setContentView(layout);
Have no doubt, creating such a dynamic mechanism will be a lot of work.
Perhaps instead of an Activity you could use Dialog or PopupWindow?

How can I setText() (TextView) when the TextView is in another layout?

My app has 2 layouts (main layout) and (preference (prefs) layout).
When the MainActivity loads, I set setContentView(R.layout.main); - main layout
I need to then set text for a TextView in the preference layout, but it never gets set.
LayoutInflater factory = getLayoutInflater();
View inflate = factory.inflate(R.layout.prefs, null);
TextView eSerial = (TextView) inflate.findViewById(R.id.editTextSerial);
mSerial = "Test";
eSerial.setText(mSerial);
The way I get to the preference page is with a menu and then the page loads up with no change to TextView
I have searched and not found an answer yet.
Please help.
Thank you.
When the menu kicks off your prefs activity, you can populate the view with the values. user1853479 points out one way of doing this, which is to add the values to an intent. Assuming you want to store these prefs for future runs, you can also set any that for that specific run and save them in your local store. Another method is to create a singleton to store your settings, load it when your app starts, modify and save as needed, and access it from any of your activities.
Short answer: You can't do this.
Long answer:
If you are launching the preference page yourself, you must be creating an Intent to do so. Call putExtra() to store your text inside that intent. In your PreferenceActivity, call getIntent().getStringExtra() to get the text, then put it in your TextView.

Refer activity layout from another activity

I have been developing web apps for a few years now, and just decided to start my way with android development using this book: Beginning Android Application Development of wrox.
At some point the book explains how to get a result from an intent (under the title "Returning results from an intent").
These are the steps:
The book describes how to change the main.xml layout file by adding some new controls.
The book describes how to change the activity that will be the second one (​Activity2), it says that this activity should refer to the layout of a different activity by calling
​Button btn = (Button) findViewById(R.id.btn_OK);
since btn_OK was defined in the layout of the main activity (and not the one of ​Activity2, it is in main.xml) the method returns null.
The official documentation describes:
public View findViewById (int id)
Since: API Level 1
Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
So am I missing something here or is this book incorrect?
Using findViewById() in an Activity searches in the layout xml file that was used in setContentView() in onCreate().
If the two activities use different layout.xml files the button inside those files may have the same id and will not be confused (by findViewById()).
In Activity1
public static Button btn;
void onCreate(...) {
btn = (Button) findViewById(R.id.btn_OK);
}
In Activity2:
​Button btn = Activity1.btn;
Note: when you use Intent to navigate to Activity2, do not call activity1.finish(), as this will destroy btn;

calling bringChildToFront() from a surfaceView within a frameLayout doesn't work?

Can you add a surfaceView child to a frameLayout, and then call bringChildToFront() to bring another child view in front of the surfaceView?
This is my first android app I've made, so please let me know if my setup makes any sense or if i'm doing the wrong things in the wrong places.
I have one activity, which has a FrameLayout.
I have 2 views (named pauseMenuView and mainMenuView) that are simply Views that have 4 buttons. These menu buttons are handled by overriding onClick() inside my activity.
I have a 3rd custom view (called gameView) that extends SurfaceView. This is where I am currently processing almost everything about my gameplay (handling touch events, drawing graphics, animation, collision detection). I have a seperate thread for controlling the state (paused, unpaused, etc.) and framerate of the game.
So basically, while playing the game, I have a pause icon in the top left corner of the screen. I'm trying to make it so that when they touch that icon, my gameView's onTouchEvent calls bringChildToFront(pauseMenuView) to display the pause menu. However, currently when I call bringChildToFront() or bringToFront(), nothing happens as if they have no effect. My gameView is still visible.
Here's the relevant code from my activity:
public static final int INDEX_MAIN_MENU_VIEW = 0;
public static final int INDEX_PAUSE_MENU_VIEW = 1;
public static final int INDEX_GAMEVIEW_VIEW = 2;
fl = new FrameLayout(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
fl.setLayoutParams(lp);
mainMenuView = View.inflate(this, R.layout.main_menu,null);
pauseMenuView = View.inflate(this, R.layout.pause_menu, null);
fl.addView(mainMenuView, INDEX_MAIN_MENU_VIEW);
fl.addView(pauseMenuView, INDEX_PAUSE_MENU_VIEW);
setContentView(fl);
View onePlayerButton = findViewById(R.id.onePlayer_button);
onePlayerButton.setOnClickListener(this);
/* then i set up a bunch of pauseMenu and mainMenu buttons the same way, so I won't put them here */
fl.bringChildToFront(mainMenuView);
So when the app starts, the mainMenuView is brought to the front, and when the player clicks the onePlayerButton, a gameView gets created and added to the frameLayout, brought to the front, and the user starts playing the game.
gv = new GameView(this, 1);
fl.addView(fv, INDEX_GAMEVIEW_VIEW);
fl.bringChildToFront(gv);
fl.invalidate();
Then, when in game, when the user clicks the pause icon in my gameView, the onTouchEvent() method catches it and I do this. (I just made my pauseMenuView public so my gameView could see it)...It seems like my program goes straight to the mThread.pause() line and doesn't do anything with the first 2 lines... I've tried setting the gameView to invisible here, and when I do that, the screen is completely black, but I can click where the pause buttons WOULD be if the pause menu was visible, and they actually do get detected by my activity's onClick()! What is going on here?
((TestGame)getContext()).fl.bringChildToFront(((TestGame)getContext()).pauseMenuView);
((TestGame)getContext()).pauseMenuView.bringToFront();
mThread.pause();

Create new "screens" dynamically

I have an application that gets information from a database and it creates ImageButtons dynamically. What I want is, when I click a dynamically created ImageButton, I want it to make a search in my database (which I know how to do) and then create a new activity (or screen) with new ImageButtons created dynamically, too.
How can I do this?
You cannot create activities without declaring them in the manifest file. You can either call setContentView after creating the new layout(I do not recomend this.).
Or use the viewflipper and add the the different layouts as childs to it. So you can use the viewflipper to switch between layouts.
Best option is to read the android developer documentation on how Intents can be used to launch a New Activity
An Intent object is passed to Context.startActivity() or
Activity.startActivityForResult() to launch an activity or
get an existing activity to do something new.
(It can also be passed to Activity.setResult() to return
information to the activity that called startActivityForResult().)
ImageButton imagebutton=new ImageButton(this);
imagebutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
getDataFromDataBase();//In this method you get data from database
Intent intent=new Intent(currentactivity.this,newactivity.class);//start new screen.. in this you can create imagebutton dynamically
startActivity(intent);
finish();
}
});
just execute the code that you have inside your onCreate (wrap it in an init(Params param) method), and call that inside your onClickListener. of course exclude the setContentView and findViewById's.

Categories

Resources