Removal of override, not able to view fragment b - java

I'm going through this "Beginning Android Development" book. I get to the Fragments section and I follow the code as show, but my compiler gives me an error.
MainActivity.java
package com.example.fragmentsdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
View view = inflater.inflate(R.layout.my_frag, null, false);
return view;
}
}
The error begins at "public view" and states "The method ... of type MainActivity.java must override or implement a supertype method
The two XML files are below for completion...
activity_main.xml
<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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/fragment1"
android:name="com.example.fragmentsdemo.MyFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:layout="#layout/my_frag" />
</RelativeLayout>
my_frag.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World from Fragment!"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Now, when I remove #Override it will run, but I do not see the text "Hello World from Fragment!" there. Any suggestions?

The method onCreateView in your parent class Activity accepts the following parameters:
View, String, Context, AttributeSet
String, Context, AttributeSet
It seems you need to extend Fragment instead Activity
Fragment documentation
In your activity_main.xml, in the fragment block, the fragment name should be the name of your Fragment class.

Related

Collapsing Toolbar Layout not working in Fragment - Android

When i try to impliment collapsingbar in fragment it's not collapsing, but when i try the same code in a simple activity it works fine, and i want the same design in a fragment(ProfileFragment), there is no error occurs it just simply displayed but its not collapsing, and i want to make it working fine please help me, look on my code i want to impliment.
This is my XML
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
tools:context="com.turbo.ashish.hexon.BottomNavFragment.ProfileFragment">
<!-- TODO: Update blank fragment layout -->
<android.support.design.widget.AppBarLayout
android:id="#+id/idProfileWallappbar"
android:layout_width="match_parent"
android:layout_height="238dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/idcollapsbarlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorAccent"
app:expandedTitleMarginBottom="20dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="20dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll"
app:title="#string/title_profile">
<ImageView
android:id="#+id/idImageViewCollapsingToolbarLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.Toolbar
android:id="#+id/idtoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/custColtransparante"
app:layout_collapseMode="pin">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<Button
android:id="#+id/idFragBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="304dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
My Fragment Java File..
package com.turbo.ashish.hexon.BottomNavFragment;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.turbo.ashish.hexon.R;
import static android.widget.Toast.LENGTH_LONG;
public class ProfileFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.d("Working","Wasd");
View fragView = inflater.inflate(R.layout.fragment_profile,container,false);
fragView.findViewById(R.id.idFragBtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(),"asd",Toast.LENGTH_LONG).show();
}
});
return fragView;
}
}
But Collapsing bar layout always stay constant in Fragment, i want to make this collapsingbar working on fragment (ProfileFragment)..
My Platform activity where i load fragments like:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#SuppressLint("ResourceType")
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
switch (item.getItemId()){
case R.id.idNavContacts:
toolbar.setTitle("Contacts");
ft.replace(R.id.idFrameContainer, new ContactsFragment(), "Contacts Ftagment").commit();
return true;
case R.id.idNavPerson:
toolbar.setTitle("Profile");
ft.replace(R.id.idFrameContainer,new ProfileFragment(), "Profile Fragment").commit();
return true;
case R.id.idNavGroups:
toolbar.setTitle("Groups");
ft.replace(R.id.idFrameContainer,new GroupsFragment(), "Groups Fragment").commit();
return true;
case R.id.idNavFeeds:
toolbar.setTitle("Feeds");
ft.replace(R.id.idFrameContainer, new FeedskFragment(), "Feeds Fragment").commit();
return true;
}
return false;
}
};
There is no problem in loading fragment but the Collapsing bar not responds (No sliding), i want to happen in fragment.
Activity XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.turbo.ashish.hexon.Platform">
<FrameLayout
android:id="#+id/idFrameContainer"
android:layout_width="match_parent"
android:layout_height="454dp">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/idBottomNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:itemBackground="#color/WHITE"
android:foreground="?attr/selectableItemBackground"
app:itemIconTint="#color/deepViolate"
app:itemTextColor="#color/deepViolate"
app:menu="#menu/navigation">
</android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>
Why my Collapsing Toolbar Layout not working ?
You expect your collapsing toolbar to work in Constraint Layout ?
Firstly, to make the collapsing toolbar work make
android.support.design.widget.CoordinatorLayout
as your root layout.
make changes in xml file used below code instand of android.support.constraint.ConstraintLayout into room xml tag ..
<android.support.design.widget.CoordinatorLayout>

Update a TextView in a ViewPager from anohter Fragment

I have created a Tabbed Activity with 3 fragments. How can I update the text of a TextView in a Fragment from the MainActivity Java File?
TabStatus.java:
package com.sanderjochems.bluetoothdata;
import android.os.Bundle;
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 TabStatus extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_status, container, false);
}
}
fragment_status.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sanderjochems.bluetoothdata.MainActivity"
tools:layout_editor_absoluteY="81dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="24dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_bluetooth"
android:textAllCaps="false"
android:textColor="#color/colorPrimaryText"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="match_parent"
android:layout_height="8dp" />
<TextView
android:id="#+id/tbMacAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text_mac_address" />
<TextView
android:id="#+id/tbState"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text_state" />
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
I want to update the TextView tbState. How can I do that?
I tried to use a LayoutInflater, but that didnt work.
I used this piece of code for that:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutStatus = inflater.inflate(R.layout.fragment_status, null);
tvCurrentState = (TextView) layoutStatus.findViewById(R.id.tbState);
Regards,
Sander Jochems
Follow the documentation for communicating between fragments.
use event bus and fire the event when you perform an action in your fragment and catch that event in another fragment and update the desired textview.
follow this very simple library implementation and use
https://github.com/greenrobot/EventBus

videoview not showing on device nor xml design,

I am new in java and xml programming, i am making tabbed activity with fragment, there is no error when running or debug the code but VideoView is not showing, i am using Android Studio,
the idea is i want to make tabbed activity, with textView and videoview in fragment as a content that scrollable(possible ?),one more thing is the API level somehow have influence ? my phone still on KitKat
here is the (fragment) code :
package com.panduanberwudhu;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.VideoView;
public class MencuciTangan extends Fragment {
private VideoView player;
private String videopath;
private MediaController mediacon;
public View rootView;
//#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.mencucitangan_layout, container, false);
mediacon = new MediaController(getActivity());
player = (VideoView) rootView.findViewById(R.id.videoplayer);
videopath = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.washhand;
player.setVideoURI(Uri.parse(videopath));
player.setMediaController(mediacon);
mediacon.setAnchorView(player);
player.start();
return rootView;
//return inflater.inflate(R.layout.mencucitangan_layout,container,false);
}
}
and the layout code :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mencucitangan_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.panduanberwudhu.MencuciTangan">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
/>
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/videoplayer"
android:layout_gravity="bottom"
/>
</LinearLayout>
</ScrollView>
Use this layout, where the height Relativelayout is wrap_content which will increase/wrap according to content because it is inside the ScrollView .
I removed Linearlayout as hard coded some value:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mencucitangan_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:id="#+id/ap"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<VideoView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="50dp"
android:foregroundGravity="center"
android:id="#+id/videoplayer" />
</RelativeLayout>
</ScrollView>
Output:

App Theme(Actionbar) is not respecting your space

I'm using a Navigation Drawer, but every time I put something in the fragment, the action bar does not respect the layout.
The image itself explains:
The fragment_main :
<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"
tools:context="com.example.leonel.picollo.MainFragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAIN"
android:padding="8dp"
android:textColor="#fff"
android:background="#color/colorPrimary"
android:textSize="28sp"
android:id="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
And the MainFragment.java :
package com.example.leonel.picollo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
Using custom toolbar has some consequences. Custom toolbar has height and other elements don't respect it in default, we must set them padding or margin. Add to RelativeLayout marginTop on height of Toolbar.
<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"
android:marginTop="?attr/actionBarSize" <!-- here is marginTop -->
tools:context="com.example.leonel.picollo.MainFragment">
</RelativeLayout>

Android list with ListAdapter layout Android Studio

I'm working with android studio and I have this list and I can't get the layout to work.(You'll notice I have a listener that opens another activity when you click on a single item, and that works just fine).
Here's my activity_estoque.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
and this is the estoque.java class, that uses the .xml file above:
package com.example.asus.mingausfashionmoda;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class estoque extends ListActivity {
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//string que pega do query
String[] adobe_products = {"SANSUMG", "LG", "MOTOROLA", "Apple"};
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_estoque, R.id.label, adobe_products));
ListView lv = getListView();
//listening to single item in list view
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//selected item
LinearLayout ll = (LinearLayout) view;
TextView tv = (TextView) ll.findViewById(R.id.label);
final String product = tv.getText().toString();
//String product = ((TextView) view).getText().toString();
//lauching new activity to single item
Intent i = new Intent(getApplicationContext(), estoqueSingle.class);
//sending data to new activity
i.putExtra("product", product);
startActivity(i);
}
});
}
}
I added some pictures to help you understand what I want and what I'm getting(sorry for bad paiting):
1 - The screen on the left is what I should see, according to the Android Studio activity_estoque.xml Design session, and the right one is what I actually get(Notice the "Mingau's fashion moda" thing missing, I really want this blue thing): (What I should get vs what I'm getting)
2 - Now, the left screen is what I want, a top single button and the list bellow. The right one is what I'm receiving after adding a button on the activity_estoque.xml file(code bellow): (What I want vs what I get)
this is the activity_estoque.xml file after I add the button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnttt"
android:text="This is a button"
/>
</LinearLayout>
First I thought that I should add a setContentView(R.layout.activity_estoque); on the onCreat method, like this:
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_estoque);
//string que pega do query
String[] adobe_products = {"SANSUMG", "LG", "MOTOROLA", "Apple"};
but if I do that I get the following error, and I'm not sure if it makes any sense to add this setContentView...:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.mingausfashionmoda/com.example.asus.mingausfashionmoda.estoque}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
and this:
at com.example.asus.mingausfashionmoda.estoque.onCreate(estoque.java:20)
this is what I have on estoque.java.20:
setContentView(R.layout.activity_estoque);
Anyone can help? Thanks.
What you want to achieve is explained here as example here. And the blue bar above you see is actually achievable using Action Bar example here. This action bar is configurable for color, icon, title, sub title and the menus(show/not to show).
You are extending a ListActivity, so when you start the activity it will use the default layout. If you try to do setContentView(R.layout.activity_estoque); in the code then the activity will expect your layout to hava list view with the id "list".
Edit--
Change your layout to
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnttt"
android:text="This is a button"
/>
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
and
and do setContentView(R.layout.activity_estoque) in onCreate
You will need to create a new layout for your list item view, which you need to pass to the ArrayAdapter
For eg:
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
then change this this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_estoque, R.id.label, adobe_products)); to this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_test, R.id.label1, adobe_products));

Categories

Resources