How to split the screen in more then one part - java

I am trying to implement an app that plays RTMP url which I have done already using libvlc sdk.
Now I want to split the screen more than one part and want to play rtmp url I am facing problem with spliting the screen any suggestions will be appreciated thanks in advance

To Split Android activity , You should use fragment class which allows you to show multiple task dynamically. You can use fragment class .
Steps :
1. Create Fragment class
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class yourFragmentName extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}
}
Create FrameLayout in your activity where you want to show fragment.
<FrameLayout
android:id="#+id/your_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
In your Main Activity , add this layout to fragment
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder, new FooFragment());
// or ft.add(R.id.your_placeholder, new FooFragment());
// Complete the changes added above
ft.commit();
For the second fragment also repeat same.
For more explanation refer , https://guides.codepath.com/android/Creating-and-Using-Fragments

With the help of fragments you can slit the screen into multiple part and each one will have its own UI

Related

Where do I change this TextView to use a WebView?

OK, I'm working on the chapter 9 tutorial in Android Boot Camp and I'm having...actually a few issues. The book was written for the older versions of Android Studio but my class is using the latest version. I've done my best to look up tutorials for the latest version but they've become quite rare.
Chapter 9 covers a Master/Detail flow tutorial that some things have worked in and others have not.
Where I stand now is a TextView/WebView issue.
I tried simply converting the WebView to match TextView but then .loadUrl won't work and when I use WebView I get an "unexpected cast error. Layout tag was TextView." And Android studio won't tell me where the layout tag was declared so I'm currently combing through all the files line by line. I'm not certain if this source layout tag is in an .xml, a .java or if I should be looking in the manifest.
I believe this means I have to change the source layout to WebView though I can't find anything in the chapter itself about it except ensuring I have the correct import.
package com.example.bikeandbarge;
import android.app.Activity;
import android.support.design.widget.CollapsingToolbarLayout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;
import com.example.bikeandbarge.dummy.DummyContent;
public class ItemDetailFragment extends Fragment {
public static final String ARG_ITEM_ID = "item_id";
private DummyContent.DummyItem mItem;
public ItemDetailFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
Activity activity = this.getActivity();
CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
if (appBarLayout != null) {
appBarLayout.setTitle(mItem.content);
}
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem.id.equals("1")) {
rootView = inflater.inflate(R.layout.photos, container, false);
}
if (mItem.id.equals("2")) {
rootView = inflater.inflate(R.layout.tour, container, false);
}
// Can not get this to update to WebView-not certain where the layout tab is textView
//if (mItem.id.equals("3")) {
// ((WebView) rootView.findViewById( R.id.item_detail )).loadUrl( mItem.item_url ); }
//Can replace WebView with TextView but won't recognize .loadUrl without WebView
if (mItem.id.equals("3")) {
((WebView) rootView.findViewById( R.id.item_detail )).loadUrl( mItem.item_url );
}
return rootView;
}
}
I would love for this to run with loadUrl actually working.
Neither WebView nor TextView will allow me to run the program. The apk file simply can't be compiled for me to even test it. I'd like to at least get to a point where I can compile the apk file and attempt to run it.
Open the layout file item_detail.xml(or is it fragment_item_detail.xml inn the book?) and change the <TextView>-element to <WebView>. Then the "Unexpected cast error" should go away.
In your webView and manifest add the following code:
AndroidManifest.xml
<application
....
android:usesCleartextTraffic="true"
android:hardwareAccelerated="true"
.....
>
In Your Layout where the webView is
<WebView
....
android:usesCleartextTraffic="true"
....
></WebView>

Fragment is not showing up in suggestion when I try to inflate

I am trying to inflate one of my fragments. However, the fragment does not show up in the autosuggest. So I manually entered the name of the fragment. By doing that the fragment name was showing in red text color and also red squiggly lines showing below all of the class names. However, when I run the application, it runs perfectly without any errors.
I have three fragments: fragment_dynamic_android.xml, fragment_dynamic_windows.xml and fragment_dynamic_ios.xml.
I can able to inflate the fragment_dynamic_android.xml in AndroidFragment.java class on onCreateView method.
But when I try to inflate the fragment_dynamic_windows.xml on WindowsFragment.java's onCreateView method.
The red textcolor is showing on
View rootView = inflater.inflate(R.layout.fragment_dynamic_windows, container, false);
The applications run without any error.
package app.tab.simple.fragmenttut;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class WindowsFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dynamic_windows, container, false);
return rootView;
}
}
After some frustration, Switched off the pc and went to do some stuff. Came again and opened the project, the error was gone. Not sure why android studio behaved like that.

Fragment Transaction not recognizing my fragment as a fragment

So i'm implementing a FragmentActivity and am trying to add a fragment, however I'm running into multiple problems. I've done this before, and am actually using the same code as the last project (where it worked), but for some reason it's not working here. Here's my code:
package silversphere.eyeon;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.LinearLayout;
public class AlarmsActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setId(40000);
FragmentManager man = getFragmentManager();
FragmentTransaction trans = man.beginTransaction();
Alarm_Landing_Page_Fragment frag = new Alarm_Landing_Page_Fragment();
trans.add(40000, frag);
}
}
I'm getting an error on the layout.setId() call and also the trans.add(). The layout.setId() is saying "expected resource of type id" and the transaction is saying "cannot resolve method add(int, silversphere.eyeon.alarm_landing_page_fragment)", which to me suggests that for some reason it's not recognizing the Alarm_LandingPage_Fragment as a fragment, but it definitely should be. Here's the declaration for Alarm_Landing_Page_Fragment:
public class Alarm_Landing_Page_Fragment extends Fragment {
Any suggestions are appreciated!
You are mixing Fragment related classes from the support library with the ones shipped with SDK. Use either only support library classes or only SDK's built-in classes when working with Fragments (I mean only classes related to Fragments).
If your class Alarm_Landing_Page_Fragment extends Fragment from the support library then change:
import android.app.FragmentManager;
import android.app.FragmentTransaction;
// ...
FragmentManager man = getFragmentManager();
to:
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
// ...
FragmentManager man = getSupportFragmentManager();
Otherwise, do not extend FragmentActivity and use only Fragment related classes from SDK.
Edit:
See also this answer:
Error inflating class fragment (As ListFragment)
The problem is related to the setId. You have to create an <id> resource:
/res/values/ids.xml
<resources>
<item type="id" name="myId" />
</resources>
An then, on your file:
LinearLayout layout = new LinearLayout(this);
layout.setId(R.id.myId);
...
trans.add(R.id.myId, frag);

Fragment Tabs Strange Crashes

Does anyone know what to do with this Fragment Tabs Problem?
I am a very novice programmer with not much experience, especially in the world of Android application developing. I recently started a project with fragment tabs and I find that when I try to access a object (for lack of a better word, but I mean like a TextView, Spinner, EditText, etc.) by something like
TextView textBox = (TextView) getView().findViewById(R.id.scouter_name_box);
the compiler finds no errors, but the app crashes. I have tried putting this line of code in a function, in the body of the fragment by itself (after onCreate()), and at the end of onCreate(). This doesn't help. The Error Log also doesn't say anything.
Furthermore, when I comment out this line, the rest of the app runs seamlessly. The app doesn't have any other activities except MainActivity and the three tab Fragments.
WHAT CAN I DO TO ACCESS THE TEXTVIEWS, SPINNERS, ETC?
also, as I mentioned I am very new. If you need a better explanation please ask, and sorry if I don't understand what you are saying at first.
We all have to start somewhere
Please help,
and Thank you in Advance
Tab XML File:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/input_scouter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="Scouter Name:"
android:textSize="25sp"
android:textStyle="bold" />
<Spinner android:id="#+id/choose_scouter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/input_scouter_name"
android:entries="#array/scouter_name_list" />
</RelativeLayout>
MainActivity Java File:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
// Declare Tab Variable
Tab tab;
ScoutingData ScoutData;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the actionbar
ActionBar actionBar = getActionBar();
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.drawable.red_and_blue_frisbees);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(true);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create Home Tab
tab = actionBar.newTab().setTabListener(new FragmentsTab0());
// Set Tab Title
tab.setText("General");
actionBar.addTab(tab);
// Create first Tab
tab = actionBar.newTab().setTabListener(new FragmentsTab1());
// Set Tab Title
tab.setText("Autonomous");
actionBar.addTab(tab);
// Create Second Tab
tab = actionBar.newTab().setTabListener(new FragmentsTab2());
// Set Tab Title
tab.setText("Teleop");
actionBar.addTab(tab);
// Create Third Tab
tab = actionBar.newTab().setTabListener(new FragmentsTab3());
// Set Tab Title
tab.setText("Endgame");
actionBar.addTab(tab);
}
}
Tab Java File:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.Spinner;
public class FragmentsTab0 extends Fragment implements ActionBar.TabListener {
private Fragment mFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from tab0fragment.xml
getActivity().setContentView(R.layout.tab0fragment);
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
mFragment = new FragmentsTab0();
// Attach tab0fragment.xml layout
ft.add(android.R.id.content, mFragment);
ft.attach(mFragment);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
// Remove tab0fragment.xml layout
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
//Heeere's the Problem
Spinner scouterName = (Spinner) getView().findViewById(R.id.choose_scouter_name);
}
First off, remove Spinner scoutername = (Spinner) getView().findViewById(R.id.choose_scouter_name). Secondly, remove getActivity().setContentView(R.layout.tab0fragment); from your onCreate(). You want to inflate your xml within onCreateView and then use that view to get the id of your other views. For example:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
if (container == null) {
return null;
}
view = inflater.inflate(R.layout.tab0fragment, container, false);
Spinner scouterName = (Spinner) view.findViewById(R.id.choose_scouter_name);
}

Fragment and view

My application is divided into fragments, each with its own layout (.xml file).
When I start visualize the first fragment in the onCreate() method of my activity, I set the appropriate layout with setContentView(fragment_first). How do I change, for example, a TextView contained inside the second fragment (fragment_second)?
Generally speaking, a Fragment, or any Activity or View for that matter, should update its own internal UI controls. It's easier, and it's good design. Other classes/events may update the state of the Fragment's data, but it handles how that state is displayed.
Edit to answer commented question:
This is how to load a content view in a Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.pump_info, container, false);
return view;
}
in your onCreateView() method, you should assign all of the views you will need to access later to fields in your fragment class, like this,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
view = inflater.inflate(R.layout.xxx, container, false);
this.aTextView = (TextView)view.findViewById(R.id.a_text_view);
this.anImageView = (ImageView)view.findViewById(R.id.an_image_view);
...
}
you can then use aTextView, etc. at other places in the execution of your fragment.
It seems you set the xml file you intended for your first fragment in your activity.
What you should do in short is create a completely new class and have it extend android.support.v4.app.Fragment, also have your activity extend FragmentActivity instead of just Activity.
Then in your android.support.v4.app.Fragment class (which I shall call your fragment from now on) you should override the onCreateView(LayoutInflater inflate, ViewGroup container, Bundle savedInstanceState){} method and in this method you should put a line like this:
View view = inflater.inflate(R.layout.the_xml_layout_for_this_fragment, container, false); which inflates the layout of the fragment and plants it in the proper place in your activity's layout.
After this your need to return view;, but before you return this view you can do view.findViewById(R.id.id_of_a_view_from_the_xml_layout_file); in order to find an element and manipulate it.
You should create such a fragment class for each fragment you need in your app and have it inflate it's own xml layout file.
For more detailed instructions you can see http://www.youtube.com/watch?v=4BKlST82Dtg or other videos or written tutorials.
EDIT: here is a basic fragment class:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFrag extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate the view:
View view = inflater.inflate(R.layout.myfrag_layout, container, false);
// manipulate widgets, for example:
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText("read me!!!");
// return the view:
return view;
}
}
and it's parenting activity:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
public class MyFragmentActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// notice that there is a different layout file for the activity and for
// the fragment!
setContentView(R.layout.xml_layout_for_the_activity);
// to start the fragment and stick it into your activity (not needed if
// you use ViewPager)
FragmentManager fragMan = getSupportFragmentManager();
fragMan.beginTransaction()
.add(R.id.the_visual_element_that_will_contain_your_fragments_layout, fragMan)
.commit();
}
}

Categories