Android app: 2 problems with layout - java

After ionic and angular i'm trying to develop apps in native java. After setting up a toolbar and a drawer-menu i wasn't able to make it work:
First problem: How can i use myitems in my drawer to show different views (in separate .xml files) in the main view.
Second problem: How can i create a view, similar to a full-height snackbar, whitin a text input field?
thank you for your time.

You should use fragment replacement. So in your main activity layout file create Frame Layout and onNavigationDrawerClick change fragments. Take a look at this as example.
2.I recommend you to use Bottom Sheet in it. Sample
If you need some code recommendations comment here. Good luck

Related

Parse different data through the activity in the same activity

I am building an E-Commerce App and I have an activity showing image slider, description and related items, and in image slider I have put an arrow to change the product on the arrow click I want to be in the same activity but data should be different.How should I do that need your help guys.
You should try doing it by urself in one of the many ways its possible to. Then we could provide some help with your idea or show you better solution. I think you are looking for RecyclerView (https://developer.android.com/guide/topics/ui/layout/recyclerview). It lets you show some similar views but with different data inside. Also as an item inside RecyclerView you can try CardView.
Using only RecyclerView you can also use:
https://stackoverflow.com/a/46084182/8713068
You can also search for a library that has everything you want inside already implemented. For example the first library that came out after i typed it in google : https://github.com/Ramotion/cardslider-android.

Android navigation through multiple layouts

i have this app that i want to have a specific way of navigation. And so i made a research but i got confused. And i am begginer in android development.
i want to ask what kind of layout or anything i can use to achieve that. I dont search for super specific answer, just what is the thing that is doing the job i want. So there it is:
the blue block("choose a brand" view) has to be on that position at all times and only changing the text if needed. I want when one image button is clicked the whole green block with the image grid change into another xml layout. I want to call multiple layouts in the green block when i interact with the buttons of those layouts. Atm the green block is an <include layout ="layout.xml"/>
I really apreciate any answer. Sorry if is basic but i really tried to find the thing i need but so far i see solutions that prevent me from using simple inflaters. Thanks in advance
You have to use fragments for this scenario.
You will have a LinearLayout where the first element will be your blue block.
The second element will be a FrameLayout that you will change into the Fragment you need (usually it will have the ID container).
Create a Fragment and set the layout your layout.xml file.
Create a second Fragment with the desired layout you would like to change the green block.
As you click on a imageButton you will have to change the current Fragment with the desired one. Here you will see how to send objects to fragments.
You can find here a way to switch between fragments. In the ft.replace method the first id is the FrameLayout you will use as a container(see above).
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();
Be careful when you import fragments. If you use the support package you will have to use getSupportFragmentManager();
Read more on the android developer about fragments
I think what you are looking for is Fragment class, which can be used as reusable code that can be linked to a UI/layout.
From android documentation:
To create a dynamic and multi-pane user interface on Android, you need to encapsulate UI components and activity behaviors into modules that you can swap into and out of your activities. You can create these modules with the Fragment class, which behaves somewhat like a nested activity that can define its own layout and manage its own lifecycle.
Read more here

execute class from included layout

Within my layout I have set a Include Other Layout. this layout schould in his turn show a listview with items loaded from the web (my webserver)
the only problem is: the items don't load in because the class that holds the code to load the items isnt called on because im using a Include Other Layout meaning only the layout is called and not the functional code from any .java files (classes)
leaving me with a blank page...
is there a way to make a call upon the class containing the code for the layout i have included?
<include/> tag is only for including only the view into another layout. It can be very useful if you use a common view everywhere. A ProgressBar can be an example. You can include ProgressBar everywhere you want. But it is just a UI.
If you also want the functionality you should use Fragments. Actually Fragments are exactly for what you describe.
Here is the tutorial from Android Developers official website
http://developer.android.com/training/basics/fragments/index.html

Android App Development : How do change content on screen depending on event

Just starting out developing some android apps. Coming from a web development background I'm wondering if the idea behind changing whats displayed on screen is similar to linking html files.
Say I had a button that once clicked would then display a completely new page, button gone and completely new content in its place. I had thought at first that this was done just by having conditional statements in the main activity class. But I don't see how this would work with the xml layout file.
So I have come to the conclusion that you have to define multiple xml files and switch between them using logic in the main class.
If this is correct whats the best way to go about this, and if not could some suggest how this is achieved normally?
Thanks.
I think it wise to follow the following tutorial: http://developer.android.com/training/basics/firstapp/index.html
Have you tried visiting Android developers' website?.The solution to your question can be obtained taking the Android training module in that website. You have said you want to go to a new page, you can use Activities here.
Let me explain you this in simple terms.
In Android for every page(Activity) you need to make a separate xml file. for example main_activity.xml.
And for each page(Activity) there is a java class. For ex MainActivity.java. This class may contain event handling and business logic part.
Now let's go to your question about switching between multiple pages.
Suppose you have 2 activities: MainActivity and SecondActivity.
Now in MainActivity you have a button then you set its onClick attribute to its event handling method. This can be done in xml file.
android:onClick="goToSecond"
Now in MainActivity.java you need to create a method which looks like this.
public void goToSecond(View v)
{
Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
}
This is a code snippet for switching to second activity.
And I also agree with other answers that you should check out developers.android.com
Hope it helps.
There is no need to switch between the XML files for portrait and landscape mode. Android does that for you. If you are writing an app that uses both orientation, then you have to write separate layout files and keep them in layout(for portrait), layout-land(for landscape) folders. This is not at all necessary if your design looks same in both orientation.

Group of Views (controls) on multiple screens

I am working on an Android project where a group of buttons needs to show on the bottom of every screen (activity) in the application. The group of buttons are basically a navigation bar. I want to know the best way to do this without creating new buttons for every activity. I have been around programming (C++/C#) for many years but am pretty new to Android and Java so if someone can point me in a general direction, it would be appreciated.
Thanks.
I bet you need to use "include" tag for xml layouts. It's the best when you need to reuse some UI components. See http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/ for the examples and description.
To elaborate on Konstantin's answer, after you've used include, you'll need to bind actions to these buttons.
If the buttons should have the same action regardless of the activity they are in, use the include tag to create their layout and then create a parent NavigationActivity (or whatever else you want to call it) class from which all your other activites will inherits.
In the parent NavigationActivity class' onCreate method, you can set up the onClickListener (and other needed stuff) for the buttons.

Categories

Resources