Create new "screens" dynamically - android

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.

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?

Too many Activities in Android App. Any alternatives?

I am creating my first app. In this app, I have an expandable list with many items. When I select any of these items, I want several paragraphs to be displayed. Do I need to create an Activity for each of these items if text is the only thing I want displayed? I know that there has to be an easier way. I did create it like this at first and it seemed very bulky (30+ activities), so now I have it set up so that when an item is selected, the setContentView opens the corresponding layout with the text that needs to be displayed. This works but there is a catch, whenever I hit the back button, it takes me back to my main activity class and not my expandable list class. I want the user to be able to go back and select something else from the list. Any guidance as to what I need to do would be appreciated.
I would suggest creating string resources for each item you would like to display, then creating one activity with a TextView. Then, instead of creating new intents for each activity, create an intent that goes to the new activity, and add an extra that contains the text for the TextView. For example:
Activity1:
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(this, ParagraphView.class);
intent.putExtra("textData", getResources().getString(R.string.myText));
getBaseContext().startActivity(intent);
}
});
In the onCreate of the viewer, add this to get your TextView:
Intent intent = getIntent();
String textData = intent.getStringExtra("text");
Now, we need to write the text into the TextView:
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(textData);
All you have to to is set up your string resources and button click listeners. You may consider this easier than having lots of activities (it's definitely easier to manage entries this way) but does require a bit of setup.
Edit: Thanks to #ianhanniballake for pointing out a much better way (I don't even know what I was thinking at the time...)
Edit2: Wow, I REALLY messed up my code. (Hopefully) Fixed now

how to make a button redirect to another xml page

I'm making a button in xml, like this:
<Button
android:id="#+id/buttondp"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/thisisstringtext" />
and I want it to direct it to another page coded in xml. Can anyone help me out?
Make another activity and use
setContentView(R.layout.your_other_layout);
inside of it.
Then in the onClickListener for your button put this:
Intent i = new Intent(YourActivity.this, YourOtherActivity.class);
startActivity(i);
You can add the onclick listener to your button, so: android:onclick="method_in_your_activity".
In your activity added the method (method_in_your_activity) and add startActivity(NewActivity).
If you dynamically want to change the Content of your activity you can always call setContentView(my_layout) and change the content. However; its best practice to use another Activity.
You can use different activities for different layout. But if you want to use same activity for different layout then you should go for ViewFlipper . You can also get some animation when switching from one view to another. Tutorial regarding the same can be found here.
using this code in java file you will click button to redirect next page
public void onClick(View v)
{
Intent ia=new Intent(getApplicationContext(),second.class);
startActivity(ia);
}
In your button XML, add :
android:onCLick="myRedirectFunction"
In your MyMainActivity.java, add function named myRedirectFunction and inside that function :
Intent homepage = new Intent(MyMainActivity.this, MySubActivity.class);
startActivity(homepage);

How to move to another activity when a button inside the ListView is clicked

How to go to another Activity when a Button inside the ListView is clicked
I have a custom list which I'm inflating it in getview() and I'm also made the buttons clickable but how to move to another Activity when button pressed.
add this
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick() {
Intent i = new Intent(CurrentActivity.this, OtherActivity.class);
startActivity(i);
}
});
replace OtherActivity with the activity class name that you want to switch to.
The trick to doing this is to add an OnClickListener to your button inside the adapter's getView() method and to use getTag() and setTag() to communicate the necessary data to the OnClickListener. There's a detailed tutorial here about this and also this thread that talks about how it works for multiple buttons in a row.
register your list activity under application tag in Android Manifest.xml file.

Android development - CurrentActivity and LatestActivity classes not recognized by Eclipse

I ran the HelloWorld android app and now I moved on to making buttons and stuff like that. I am able to create the buttons in the layout xml and all that, but I ran into some confusion over Eclipse not recognizing my Intent declarations.
Here is a snippet of code:
addProblemButton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
The CurrentActivity and NextActivity classes do not seem to be recognized and Eclipse and it doesn't give me the option to automatically create the import statements for it.
What is the package that these classes are in? Is it an issue of some things not recognized? Or some package that needs to be installed/downloaded? Whats the best practice way to handle such a situation?
Also, do I need to add listeners if I already added the buttons to the layout?
Thanks!
I believe CurrentActivity and NextActivity are just being used as example names for classes for launching an activity in whatever code snippet you were looking at.
CurrentActivity should be the name of whatever the Activity class is that you're launching the new activity from, and NextActivity would be the name of some new Activity class that you want to navigate to next.
It seems you are trying a tutorial. In your project, you should create your own classes extends Activity, named CurrentActivityand NextActivity, so Eclipse will know what they are.
2.If you just declare a button in the layout xml file, the app only show it, but doesn't know how to handle the click event on it, so you still have to register the listener for it. You can:
a. Set the android:onClick attribute for the button in the layout file, and then implement the method to handle the click event. I.e. android:onClick="click" in the xml, and add a function with that name in your code:
public void click(View v){
//Process click event here
}
b. register the listener fully in code:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Process click event here
}
});

Categories

Resources