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:
Related
Here is my MainActivity.java class
package com.example.bottomnavigationdrawer;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView=findViewById(R.id.bottomNavigationView);
NavController navController= Navigation.findNavController(this,R.id.nav_host_fragment);
NavigationUI.setupWithNavController(bottomNavigationView,navController);
}
}
Here is my activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:itemTextColor="#00FAF7F7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my HomeFragment.java class:
package com.example.bottomnavigationdrawer;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.smarteist.autoimageslider.SliderView;
/**
* A simple {#link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
public HomeFragment() {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
SliderView imageView = (SliderView) view.findViewById(R.id.imageSlider);
return view;
}}
Here is my fragment_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="3dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:gravity="center"
android:textSize="70sp"
android:textColor="#FAF8F8"
android:background="#25212E"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home" />
</RelativeLayout>
I am new in Android, and I have searched a lot about it. But every tutorial I get they do this with main activity, model,adapter. But I have to do it on my home fragment in main activity.
Actually I don't understand your question. If you want to learn about recyclerview and card view and bottom navigation view, follow this tutorial:
https://www.androidhive.info/2016/01/android-working-with-recycler-view/
https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/
It's very easy. If you want to set scroll horizontal use LinearLayoutManager like this
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myRecyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
myRecyclerView.setLayoutManager(layoutManager);
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
i'm building an android app using Android Studio and I want to display my items(images-using imageViews) in rows(4 in each one) but when I add more than 4 items, instead of the next item been taken to the next row, it just doesn't show up at all. What am I missing? I tried tweaking multiple things but none of it worked. Keep in mind that i'm new to android app development so i'm not sure if my layout choices are "optimal".Below you will find my xml file that handles all of the layout parts:
<?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">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:padding="20dp">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#+id/toolbar">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="70dp"
android:text="Cards List"
android:textColor="#android:color/holo_blue_light"
android:textSize="40sp" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"></android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
<android.support.v7.widget.LinearLayoutCompat
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:verticalSpacing="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:paddingBottom="500dp"
android:stretchMode="columnWidth"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
android:verticalSpacing="10dp">
</GridView>
</LinearLayout>
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
MAINACTIVITY.JAVA
package com.example.steli.hellogridview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(MainActivity.this,"" + position,Toast.LENGTH_SHORT).show();
}
});
}
}
IMAGEADAPTER.JAVA
package com.example.steli.hellogridview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
/**
* HelloGridView was
* Created by Stelios Papamichail on 11/5/2017.
*
* This file belongs to the com.example.steli.hellogridview package.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// Create a new ImageView for each item referenced by the Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView == null) {
// If it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(180,180));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.ssj4_gogeta,
R.drawable.cell_jr_n,
R.drawable.mrsatan_n,
R.drawable.tien,
R.drawable.vegito,
R.drawable.videl
};
}
There is nothing wrong with your Java code. Your XML, however, is pretty messed up.
Why, for example, do you cascade a LinearLayout inside a LinearLayoutCompat inside a NestedScrollView? For scrolling the GridView, you don't need any of this. So take it out.
Also, android:paddingBottom="500dp" is too much. This line pushes part of your GridView outside the top of the screen (at least on the emulator I use). So get rid of that, too (or make the padding smaller).
I don't understand what you want with the AppBarLayout, so I leave it untouched.
Since you probably don't want the GridView to render on top of it, I put the AppBarLayout and the GridView inside a RelativeLayout with android:layout_below="#id/appbar" inside the GridView's XML.
So the XML now looks like this:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#+id/toolbar">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="70dp"
android:text="Cards List"
android:textColor="#android:color/holo_blue_light"
android:textSize="40sp" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"></android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/appbar"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
android:verticalSpacing="10dp">
</GridView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
I am using this code to make an Image Slider, but the Application stops on startup. Help !!
You might suggest any other way to make an Image Slider in a Fragment also.
This is my java code.
gallery.java
import android.content.Context;
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.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;
public class gallery extends Fragment{
View myView;
Animation fade_in, fade_out;
ViewFlipper viewFlipper;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.activity_gallery, container, false);
viewFlipper = (ViewFlipper) myView.findViewById(R.id.backvf);
fade_in = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in);
fade_out = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out);
viewFlipper.setInAnimation(fade_in);
viewFlipper.setOutAnimation(fade_out);
viewFlipper.setAutoStart(true);
viewFlipper.setFlipInterval(5000);
viewFlipper.startFlipping();
return myView;
}
}
This is my XML file.
activity_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.harshit.sangeetasventure.gallery">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ViewFlipper
android:id="#+id/backvf"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="#drawable/slide1" />
<ImageView
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="#drawable/slide2" />
<ImageView
android:id="#+id/img3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="#drawable/slide3" />
<ImageView
android:id="#+id/img4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="#drawable/slide4" />
</ViewFlipper>
</LinearLayout>
And these are the fade_in.xml and fade_out.xml in the #res/amin.
fade_in & fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="2000"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator" />
</set>
I'm new to Android programming. I have the question, that how i can intent from fragment to an activity.
I was trying to code it, but i failed :( ('l_places' is the layout of Fragment Places, and 'p_tr_fr' is a clickable TableRow)
Here's my code of the Fragment:
package com.fragment.toactivity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Places extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.l_places, container, false);//l_places is the layout
return rootView;
}
public void goToAttract(View v)
{
if (v.getId()== R.id.p_tr_fr) {
Intent intent = new Intent(getActivity(), Listact.class);
startActivity(intent);
}
}
public View onCreateView(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
};
}
and here is my 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"
android:background="#ffd4d4d4">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView2" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#ffffffff">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_gravity="bottom"
style="#android:style/Widget.AutoCompleteTextView"
android:layout_margin="0dp"
android:id="#+id/p_tr_fr"
android:onClick="onClick">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/imageView2"
android:src="#drawable/m_pf"
android:scaleType="fitCenter"
android:layout_marginLeft="6dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/s_shop"
android:id="#+id/textView3"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:textSize="18dp"
android:textColor="#ff000000"
android:layout_marginLeft="6dp" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
When I click on the TableRow, the app stops. Please help!
(sorry for my english)
do like this,
Intent myIntent = new Intent(getActivity(), BookmarkActivity.class);
getActivity().startActivity(myIntent);
#NAP code should work fine. If app stops - give us the stack trace?
Possible reason is that your activity is not declared in AndroidManifest.
you should have something like
<activity android:name="yourpackage.Listact" />
between <application> tag