How to display 2 different classes (activities) in a fragment in ANDROID? - java

I have an already working android app, but I need to update it for better layout on tablets. I can rewrite it no-problem, but what I want to ask is;
I will have 2 fragments on screen (one for list, and one for details). When I select a customer from list, details will update itself (I have already done this without a problem). BUT, when I select an item from details fragment, it must change itself (it must be opened on details fragment, but of course it will be a different class). For example first details will be a list of details, but when I select an item from details list, it will be a different layout, maybe WebView or so..
To better represent the idea,
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
Where "L" is list fragment, and "D" is details fragment. But I want to show 2 (or more) different classes (activities) (Not at the same time btw) in the details fragment. How do I do that? I was searching this for 2 full days, there is nice tutorials but I couldn't find any example for this behaviour.
Thanks in advance.

You can use a viewFlipper inside your details fragment. Of this way you can have two clases there, and navigate between then. For example:
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/example_view_flipper"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<include android:id="#+id/first" layout="#layout/first_layout" />
<include android:id="#+id/second" layout="#layout/second_layout" />
</ViewFlipper>

Related

BottomNavbar breaks when you add more than 3 elements

I am working on an android application and it was agreed to only have 3 fragments in its bottom navigation bar. However, things have expanded and we decided to add another fragment. When adding the 4th fragment, the dimensions of the container break making it look like this.
This is my xml code.
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#drawable/selector"
app:itemTextColor="#drawable/selector"
app:menu="#menu/menu_navigation" />
I have tried different properties to make it stretch but they end up putting it only to the right and not expanding left or vice versa. For instance if I was to try using wrap_content on the layout_width, the container would glue itself to the left.
What is the needed property that might be a solution to the problem?
It's an automatic measure in order to not have labels overlap.
You can make a quick fix by adding app:labelVisibilityMode="labeled" to your xml.
Note that you should use Use Support Library 28 for this.

How to create custom button onClick() event Android(Studio)?

I'm creating my first Android App, and as part of it, I need to add new buttons after a click on another button.
Then I need to create() a button after a onClick() event on another button.
How to do it?
Things you should know:
I am using Android Studio to develop the app,
I have already imported these:
import android.view.*;
import android.widget.Button;
import android.widget.TextView;
And I'd like to know where to get the whole Library list for android, or better, I'd like you to say what's your best android library to develop on such these things, I need to find and get experienced on a library so I'll need suggestions!
Thanks a lot in advance :)
P.S. Just typed those create() and onClick() to make it clear that I was talking about methods, but not that both of 'em are real ones or the ones I'll need ;)
P.P.S. I don't know how many buttons will be ther becuase I do NOT choose that, the user does, it's somethig like: I want to create a new thing, then I press the button '+' and create a new button (the new thing), for this reason, the amount of buttons isn't known, that's why I can't use the visibility trick, but yeah I first thought about that aswell!
rockfight's answer is correct, but I would recommend you to use the button in your layout with android:visibility="gone" and then show it when the user taps on the first button using button2.setVisibility(View.Visible).
It's always easier to create your layouts and views in xml instead of code.
Personally, I'm using support-v4 library, recyclerview, recently-released design library and some of the Google Play Services libraries.
EDIT: if you're going to add a lot of buttons, and you don't know how many, I'd recommend you to use ListView or RecyclerView. I personally prefer RecyclerView, but it might be more difficult to set up. Anyway, you'll be adding items to your list. That items are basically buttons, so your item layout would look like this:
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a new button!"
android:layout_margin="16dp"
android:gravity="center_vertical|center_horizontal"
android:elevation="2dp"
android:translationZ="6dp" />
<!-- Material Design. See http://www.google.com/design/spec/what-is-material/elevation-shadows.html#elevation-shadows-elevation-android- -->
<!-- The attributes you need...... -->
That's all. You don't have to mess with ugly code. If you want to get an idea of the advantages of RecyclerView, see http://antonioleiva.com/recyclerview/.
Create a button object
Button myButton = new Button(this);
Then add button to your layout
LinearLayout layout = (LinearLayout) findViewById(R.id.my_layout);
layout.addView(myButton);
I am not sure what library are you talking about. To learn about android go to android developer website and click search button and search what you want to know. You will get all libraries there.
You do not need libraries for making onClick methods. When you have your button, set onClick method like this: button.setOnClickListener(new onClickListener...) if you press Ctrl+Space you will get possible choices of what to choose.
How many buttons do you wish to add programmatically? Maybe it would be better if you create buttons in xml and use Visibility.GONE and Visibility.VISIBLE if there are not many buttons and you know the end number of them.

How to use Fragments in Android properly

I asked a question a while back:
Replacing Fragments isn't working/Am I executing this the proper way?
Because I was having a really tough time wrapping my head around the entire concept. Past Google I/O videos have also helped, but I still didn't really have a reason to use fragments. I have encountered my problem again, and with more searching and Google I/O 2013 videos, I've hit another fork in the road. After starting a bounty to get a more expert opinion on this subject, I was guided to use FrameLayouts, yet in this I/O 13 video at 19:05 into the video it shows <fragment>. Can someone clear up any discrepencies or misconceptions I have?
Do I use <fragment> or <frameLayout>? Bonus points if you look at my original question to know where I'm coming from.
There are 2 seperate questions I see in here; (1) what is the <fragment> element? (2) am I (meaning you) doing what I tried to do in my previous question in the best way possible by using a FrameLayout?.
I will try to answer both.
The <fragment> XML tag is just another way to implement a fragment. As a refresher, a Fragment is like a part of an activity. It is a stand alone class, however, referring back to the fragment documentation, because a Fragment is intended to be modular, all fragments must be housed within a seperate FragmentActivity. Think of this as FragmentActivity as a container. By loading your Fragment into the FragmentActivity the FragmentActivity provides other valuable functions that the Fragment class does not have access to on its own. It also offers you the chance to swap out fragments dynamicly, allowing for a more flexable user experience.
How you load your Fragment into its containing FragmentActivity depends on what you are trying to acomplish. One way to do this is to use the <fragment> tag. With this approach, rather than declaring the Fragment in the activity java code and then loading it with a FragmentTransaction as you did in your earlier question, the Fragment class is loaded via the XML layout for the containing FragmentActivity.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:name="com.example.android.fragments.HeadlinesFragment"
android:id="#+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
You can see the fragment declared here in the example taken from the android docs. The fragment is then loaded by the FragmentActivty when setContentView() is called onCreate.
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
}
}
The other way to display fragments is as you have done it in your prior example. Using a FragmentManager you can create a new instance of your Fragment class and load it programatically within the java code.
So which technique makes sense when? As the example from the android docs demonstraits, the XML fragment method makes sense when you are using stationary fragments, i.e. you are not dynamically swapping fragments in and out. This is because you cannot edit the XML layout you create on the fly the same way you can the java code. Also, from what I can tell, using the XML approach treats the fragments more like layout elements alowing for different control. In the android example, they use the XML approach to display 2 fragmens side by side on a tablet. By controling the fragments more like layout elements, e.g. having the ability to adjust layout_weight you can achieve a better result for that purpose.
However, if you are designing a highly dynamic UX, say with a ViewPager or some other feature where fragments will be rotated regularly, it makes more sense to use seperate Fragment classes and replace those Fragments as they become necessary.
Based on your prior question and the need to swap fragments dynamicly I think you made the right choice for that implementation.

Android multiple options menu for multiple screen with no code repetition

My android app has several screens, and in each one of them I would like to have a similar options menu with a slight different.
For example screen A would have items 1,2,3,4 and screen B would have items 1,3,4,5
As you see they have items in common which make it stupid to have a different xml menu for each one, because I will have to define the items again and again.
Is there a smart way of doing that?
you could try to use this code in your various layouts
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/your_layout_with_options" />
where your_layout_with_options is an xml file containing all your options.
so you have always the same identifiers in your screens and you can chose witch items to show and witch items to hide..
hope this help

What is difference between #+id/android:list and #+id/list

I am wondering what's the difference between #+id/android:list and #+id/list. I know the last one which is a regular id assignment but the first looks different. What makes it special?
Where I saw it:
I was studying on ListView, ListAdapter and things like that and the author define the ListView in layout xml file as below :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/main_no_items"/>
</LinearLayout>
and also let me mention #+id/android:empty id as well.
And he also extends ListActivity class.
Here is the source of the article.
And also what's in my mind as questions are :
Should we extend ListActivity? Maybe I want an Activity which also contains other Views.
We use #+id/android:list just because we extend ListActivity or we can use the same convention if we extend Activity?
Thanks.
Resource IDs in Android are specific to a package (which is good, or else you'd have lots of conflicts if your app is dealing with several packages at the same time).
#+id/list will create a resource ID in your app (=your package) with the name "list" and give it a unique ID. In code, that would be R.id.list.
#android:id/list will use the ID "list" from the package android (which, in code, would be android.R.id.list.
EDIT: Need to add the corrections David Hedlund pointed out: The proper reference would be #android:id/list. Also, + indicates you're defining a new ID - you obviously don't need that when you're referencing something that was defined in the Android API.
I think the example code you posted has a typo, so it should be #android:id/list (without the +). From the ListActivity javadoc:
your own view MUST contain a ListView object with the id "#android:id/list"
#android:id/list is specific to ListActivity, so you do not need it if you are adding a ListView into any other kind of Activity. You should extend ListActivity if you want the user to do more than view the list. For example, you can override ListActivity.onListItemClick to respond to clicks on an item in the list.
Similarly, #id/android:empty (again, without the +), is a special case for ListActivity. This allows you to specify an alternative view that should be displayed when your list is empty. That View will not be displayed when the list is populated.
in android,
In XML: #[package:]layout/filename
like
android:id="#+id/android:list"
This is the standard way to refer to a list view when refering to listFragment or listActivity
so filename is android:list is a reference to ListView.
navigate to res/values/ids.xml
you will find <item type="id" name="list" />
ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

Categories

Resources