run time layout manipulation - android

I have a grid type of layout. When the app first loads, there are 3 buttons. I am using "Adapter" for the layout.
ONCLICK of the button, I want to refresh the same activity but with different set of 9 buttons.
Do I start a new Activity in all? OR Make a temporary activity to start the previous activity (and how)?
Since the ONCLICK event is written in the "Adapter" part of the code, starting new activity on click of the button is difficult. (is out of my knowledge).

if you are using adapter i.e., like baseadapter then you can try:
adapter.notifyDataSetChanged();
directly without starting activity again.

If you want the user to go back to the 3 button view on click of Back button, it will be easier to have the 9 buttons in a different Activity.
Otherwise, you can have the 3 buttons and 9 buttons within two different LinearLayouts in the same activity and hide the second layout using setVisibility(LinearLayout.GONE);
On Click of the button, you can hide the first layout and enable the second one using setVisibility(LinearLayout.VISIBLE);

In the adapter class, we can start an activity using context.startActivity(intent) I did not know that we can access "start Activity" from adapter...
but now it's working just fine!!
Thanks a lot for your recommendation...

Related

How can i update an activity in android studio

I've created an app with two activities. On the second activity i can create new buttons by pressing a floating action bar button inside the activity; but the problems is when i return to the main activity and come back all the added buttons are gone.
I was wondering how can i save these added views so whenever i exit that particular activity or the app entirely, views won't perish.
I know i can save the views' values by "Preferences" commands but that only can save the views' parameters not themselves and if i go that way i have to recreate each view again each time i enter the activity.
if i could update the activity whenever views are added to it that would be excellent.
I would appreciate any help.
You can save information about views in preference like number of buttons created. for example you clicked button 4 times then save numOfButtons value as 4. In onCreate method you can check values of numOfButtons and execute that block of code 4 times to generate buttons.

Android: Using fragments

Is it possible to disable an activity elements when it loads a fragments?
I have a program which has an activity and two fragments. I put a container in activity. When I put two buttons in activity and load each fragment by clicking the button, fragment loads on the activity, but when I click in the position of buttons which are under fragment(or in the large screen next to it), they do some actions, however I don't like it. The buttons should not be clickable.
As a simple solution I create a third fragment and put my buttons in it and load it as a default view in the activity.
I was wondering is it possible to do this without using third fragment.
If you do not want clicks to propagate to below layers you can specify android:clickable="true".
In your case define android:clickable="true" in the bottom layout of your fragments layout xml file to stop any clicks to the activity below.
mach's solution is great, but i can suggest a solution that will be helpful if you want to do more actions in the future than just disabling buttons.
You can simply have your activity implement an Interface "OnFragmentLoaded" for example which has a single method onLoaded()
and in your fragment in your onAttach(Activity ac) method you can do the following
((OnFragmentLoaded) ac).onLoaded()
and you activity would implement onLoaded() to do what ever you want

Whether it is possible to use two activities with same layout

Whether it is possible one layout can be called by two activities.
Say i have a textview and a button.
1) Using one activity i set the textView with a name
2) Using another activity, is it possible to display popup message when i click the button.
first activity should set the textview alone and second activity should be called when i click the button. Because, i will get the text from api webservice call in first activity and will display message in popup window using another activity using some other api webservice call.
yes you can use one layout for two activities,but in that case where you use same controls for both activites:
like activity one:
uses both one textView and Button
like activity two:
uses both one textView and Button
Yes. Two activities don't care if they are using the same layout, and in fact don't really know about it. You can use view.setVisibility to make certain views invisible or visible, and as long as you are using the ids associated with the views can make different calls on them in the activities.
So the answer is yes, you can manipulate that layout however you like in two different activities.
No problem, you can easily use same layout file for more then one activity.
First try to implement this thing in your project, and then see, what happening.
Thanks,
Sumeet.

Without creating activity open new screen in android

I just want to know it is possible to open new screem without creating activity. (Means I don't want to create activity and open screen). I don't want to use dialog.
EX.
I have Two class. 1st is my main activity with one button, from where I want to open new screen.
And 2nd is my custom class which extends linear layout and add two textview controls and back button.
So now in my main activity, I click on button to show my 2nd class in new screen. And when click on back from 2nd screen it will appear 1st screen again.
Please any have any idea please share his/her knowledge.
Thanks in advance.
In your main activity you can remove the views added and add the new views to your layout. If you don't want to remove your main activity views you can still do it by addcontent view.
use a ViewPager for controlling which screen is displayed. also override onBackPressed() to control the back button.
follow this post for a ViewPager example.
Store your layouts into hashmap.When you click the button get the respective view from the hashmap and set that view to the setContentView(yourView);

Android set constant Button in more than 5 activity

Hi I am new to Android I want to Set One Button named (Click) in More than Five Activity like Title of the Application at the Top.I did not want it to add on all the 5 activity Manually is there any simple way in Android?
This is simple if you have one layout for all 5 activities, but you probably don't. So the solution is to create 5 buttons in each layout with same id. Then you can create a static method in one of your activities called, for example, initializeButton(). This method should inflate your button. You can call this method from every activity, and it should do the job. Hope this helps.

Categories

Resources