How to load class at runtime in android app? - android

In my android project I've two layouts in that first layout having one button if i click that button I need to display the second layout in the same layout for this I created a view by using LayoutInflater and attached it to "Table Layout" which is present in first layout.
Everything should be fine but the corresponding class file for the second layout is not loading. Without loading I'm not able to call events like click and some other loader events so any one help me how can I load the corresponding class file when i click button in first layout?

It's difficult to understand what you mean by class not loading.
If you want some layout objects to be hidden at a particular time, look into the visibility parameter in xml or setVisibility(true/false) in code.
If you want to display a whole different screen, create a second activity and call it:
Intent i = new Intent(CallingActivity.this, ActivityToStart.class);
startActivity(i);

Your question is difficult to understand, but you appear to be trying to achieve an effect like embedding one activity inside another. You may want to look at the android developer docs about the "fragments" API, which is the currently-recommended way of achieving this kind of effect.

Related

How to handle dynamic view created in Recycler View

Actually, I am working on RecyclerView that shows some details along with images and all data comes from a database.
In my RecyclerView, I have used three diffs layout and each layout is used according to data coming from the database that means if only one image comes from database then one_image.xml layout comes to play, if two then two_imag.xml layout comes to play and if more than three then the third layout I am using.
My apps working fine for some extents but when i am trying to scroll down further, it get crashed. I come to know that the problem is on OnCreateViewHolder, meaning it doesn't get called when I go down further in Recycler View.
I searched alots on google but nothing work for me. Is it possible to call OnCreateViewHolder every time or some other way to solve this problem?
Thanks in advance
Sounds like the best option would be to use one layout file that has all three image views but just default them to visibility "gone" so they dont take up space. Then when you need to use them, set them to visibility "visible" when you are creating your viewholder.
Without seeing any actual code it hard to be more specific.

Android Studio Single activity acting as many

I have looked everywhere but don't seem to be able to find what I am looking for. I have an activity with multiple buttons, each button opens a new activity with an identical recyclerview layout, but different data. I am wondering if it is possible to use one activity and layout instead of multiple? this way instead of having 10+ activities (one for each button) I only have to manage one when a button is clicked and simply pass the necessary list data to it.
I believe you could set the intents for each button to call the same activity but with an integer such as 1-10, in the activity it takes the value and decides which list should be presented? If anyone thinks of how this could be done or a simpler way I would greatly appreciate it!
Yes, there are various ways to do that.
You could use multiple fragments on the same activity. Then add/remove fragments on each button click.
You can have multiple layouts within your activity. Say you have two buttons, and you have three layouts layout1, layout2, layout3, sequentially one after the another. So if initially, layout1 is visible and the rest are gone using layout.setVisiblity(View.GONE), if you click button1, ypu can do layout1.setVisiblity(View.GONE); layout3.setVisiblity(View.GONE); layout2.setVisiblity(View.VISIBLE) and vice-versa for pressing button2.
Are all the activities opened by the buttons similar? If so, you could only take care of the changes in the elements of the layout & specify conditions.
For instance, if you click a button, instead of changing the whole thing, you only go into the buttons & change their texts with btn.setText("..."). You could define different conditional statements inside the onClickListener of that button.
It could be something like:
if(btn.getText().equals("a certain text that you set to the button")){
doSomething();
else if(btn.getText().equals("another option")){
doSomethingElse();
Following this logic, you could continually update the elements in your layout & your code will decide what to do depending on what's stored in these elements.
The second option that comes to my mind would be creating different xml layout files & simply changing the layout of your MainActivity to the appropriate one depending on what stage of your process you are at.
I hope this helps,

Fragment vs Activity with LinearLayout in Android

I have a dynamic UI that I need to generate and I would like to know what the best approach would be to do this and why? The application that I need to make will either way only have a single activity in which to display various different views which are generated by code, so not in an XML file.
So basically I want to draw one set of views and have a user interact with them (textview, button, radio button, edittexts etc). Then save the data he generated, clear the canvas or screen and within the same activity generate the next set of views for the user to interact with.
I have done extensive research and I know that I can use either a Fragment or an Activity with a LinearLayout to achieve this, but I am not sure which would be better and why?
Thanks,
Wihan

Android: is using setContentView multiple times bad while changing layouts?

is using setContentView multiple times bad while changing layouts?
Some people say that it's bad and they never say why.
and is there some other thing to change layout using button?
Let's take a look at the Android Documents:
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.
So, setContentView will overwrite the layout, and replace it with a new one. Usually, you only want to do this once in onCreate. Theoretically, you could do it more, but it involves re-drawing the entire layout, and this could take some time. There are a few alternatives, depending on exactly what you want:
ViewAnimator: This is useful for showing a quick animation, if you want to change the view multiple times in quick succession.
Fragments- Instead of re-drawing the entire view, you can switch out fragments. Each fragment is a kind of mini activity, and overall this will contain the code much better.
Pass Intent Arguments- Pass information to an activity to help it set up. The first activity passes information to a common second activity, which knows how to set itself up based off of the information it receives from the first activity.
As for your specific application, here's what I would do:
Each band follows a specific layout. There is only 1, or maybe a few, possible layouts.
When the Band activity starts, the appropriate layout is chosen, and populated, knowing what's in there.
The Android SDK shows how to pass data from one activity to another. Just pass the data that the second activity needs from the first, using something like this:
Intent intent=new Intent(...);
intent.putExtra("Album","Some Album")
startActivity(intent);
The second activity will do this:
Intent intent=getIntent();
String albumName=intent.getExtraString("Album");
//Does something with albumName, maybe get a TextView and .setText()
Yes this is bad, because it inflates your activity with your layout, and if your layout has a lot of views, it may take time.
To avoid that you should use a ViewAnimator, where you put all your layouts and you switch by showNext() and showPrevious(), i.e:
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ViewAnimator"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout> </RelativeLayout>
<RelativeLayout> </RelativeLayout>
</ViewAnimator>
And in your code:
// Don't forget the setContentView
//
// Load the ViewAnimator and display the first layout
ViewAnimator va = (ViewAnimator) findViewById(R.id.ViewAnimator);
// Switch to the second layout
va.showNext();
// Add another layout at the third position
LinearLayout fooLayout = new LinearLayout(this);
va.addView(fooLayout, 3, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
i cant show example because i'm waiting for answer to do it. okay i
have the app of lyrics(it will show lyrics of band) and albums are new
activities but i dont want many activities and thats why i want to
make songs only layouts and change views with button press
It sounds like you're going about this the wrong way. If you want to change the UI an Activity contains, then Fragments would be the better approach. There's a bit of a learning curve there, but it's good android design, and well documented.
Further, you seem to be confusing formatting and content. If you're displaying song lyrics, you don't need a new layout for each song. You just need to change the lyrics and keep them in the same activity. What you're doing is akin to creating a new web-browser for each web-page you intend to visit. Instead, find a way to store the lyrics and display them on a single activity (or in a fragment) to display those lyrics. The same would apply to each album: One activity would display the album cover in the corner (or as background), the title, release date, etc, as text, and then a list of songs below. The actual content of the TextViews can change, but the layout ought to be the same.

Multiple layouts for the Fragment

Can I choose different layout for the Fragment based on the Button that has been clicked on the Activity, or should I create one Fragment class for each Button?
I know that Fragments exist to accommodate different UI but in my case I have many buttons that on click display a FrameLayout and I was thinking if it is possible to save time from copy/paste ?
I'd say that depends on how your layouts/fragments look like. If they are huge and very similar it could be better to have one layout. You could also think about using <merge /> or <include/> in that case.
This way you can maintain equal parts in (sub-)layouts without the need of code repetition.
Have a look at Googles site here.

Categories

Resources