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>
Related
Im wondering why my bottom navigation bar is not showing up. Can anyone help me out please. Im still learning Android so i apologise in advance if i seem to be asking the wrong questions!
This is my home.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:title="Home"
android:icon="#drawable/ic_action_home"
/>
<item
android:id="#+id/Login"
android:title="Login"
android:icon="#drawable/ic_action_login"
/>
<item
android:id="#+id/About"
android:title="About"
android:icon="#drawable/ic_action_about"
/>
</menu>
This is my fragment_home.xml file
<?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"
android:orientation="vertical"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="#+id/listViewDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
This is HomeFragment.java file
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
String[] menuItems = {"Do Something!", "Do something else!", "Do yet another thing!"};
ListView listView = (ListView) view.findViewById(R.id.listViewDetails);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
menuItems
);
listView.setAdapter(listViewAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Toast.makeText(getActivity(),"Hi! You CLikced the first item", Toast.LENGTH_LONG).show();
}else if (position == 1) {
Toast.makeText(getActivity(),"Hi! You CLikced the Second item", Toast.LENGTH_LONG).show();
}else if (position == 2) {
Toast.makeText(getActivity(),"Hi! You CLikced the Third item", Toast.LENGTH_LONG).show();
}
}
});
// Inflate the layout for this fragment
return view;
}
}
This is my AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sg.edu.rp.c346.a3pdwork">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This is LoginFragment.java
package sg.edu.rp.c346.a3pdwork;
import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
*/
public class LoginFragment extends Fragment {
Button b1, b2;
EditText ed1, ed2;
TextView tx1;
int counter = 3;
public LoginFragment() {
// 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_login, container, false);
b1 = findViewByID(R.id.button);
ed1 = findViewByID(R.id.editText);
ed2 = findViewByID(R.id.editText2);
b2 = (Button)findViewById(R.id.button2);
tx1 = (TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Wrong Credentials", Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
This is my fragment_about.xml file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".AboutFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
This is my fragment_login.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="match_parent"
android:layout_height="match_parent"
tools:context=".LoginFragment">
<!-- TODO: Update blank fragment layout -->
<TextView android:text = "Tutor Seeker"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:id = "#+id/textview"
android:textSize = "35dp"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true" />
<EditText
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "#+id/editText"
android:hint = "Enter Name"
android:focusable = "true"
android:textColorHighlight = "#ff7eff15"
android:textColorHint = "#ffff25e6"
android:layout_marginTop = "46dp"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_alignParentRight = "true"
android:layout_alignParentEnd = "true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:textColorHint="#ffff299f"
android:hint="Enter Password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:"
android:id="#+id/textView2"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textview"
android:textSize="25dp"
android:layout_toRightOf="#+id/textview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/textview"
android:layout_toStartOf="#+id/textview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/textview"
android:layout_toEndOf="#+id/textview" />
</RelativeLayout>
This is my AboutFragment.java file
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class AboutFragment extends Fragment {
public AboutFragment() {
// 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_about, container, false);
}
}
This is MainActivity.java file
package sg.edu.rp.c346.a3pdwork;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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);
}
}
This is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
app:menu="#menu/home"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Try to use RelativeLayout instead of LinearLayout like below:
<RelativeLayout 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">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
... />
<com.google.android.material.bottomnavigation.BottomNavigationView
...
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
The fragment in your activity_main has android:layout_height="match_parent" and LinearLayout has vertical orientation. This means that your fragment takes all the place on the screen and your bottom navigation menu placed below your fragment. Also, you shouldn't set navigation menu height as match_parent. I suggest you to try to use ConstraintLayout as layout root.
NOTE! I use AndroidX, but it also works for AppCompat.
<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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_menu_height"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/navigation" />
<fragment
android:id="#+id/fragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
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 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:
I am trying to load a frame on the Onclicklistener of a Button in the main activity. In my view I have written all the code correctly of main activity and fragment that requires to attach a fragment to activity. But something seems off. Appreciate any thoughts or suggestions.
I have used this link as a reference.
Below are the code details.
MainActivity.java
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.app.ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Button btnLoad = (Button) findViewById(R.id.Button01);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
AboutFragment mAboutFragment = new AboutFragment();
fragmentTransaction.add(R.id.fragment_container, mAboutFragment,"About");
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
}
}
activity_main.xml
<?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">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_about"
android:minHeight="92dp"
android:layout_marginTop="40dp"
android:layout_marginLeft="40dp"
android:textSize="22sp"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_report"
android:id="#+id/Button02"
android:minHeight="92dp"
android:textSize="22sp"
android:layout_marginTop="40dp"
android:layout_marginLeft="80dp"
android:layout_toRightOf="#+id/Button01"></Button>
</RelativeLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
AboutFragement.jave
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AboutFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.about_fragment_main, container, false);
return view;
}
}
about_fragement_main.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<TextView android:id="#+id/about_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:paddingTop="30px"
android:text="header text"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="text here" />
<TextView android:id="#+id/about_textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="zariya mission"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingTop="10px"
android:text="text here" />
<TextView android:id="#+id/about_textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="zariya vision"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingTop="10px"
android:text="text here" />
<TextView android:id="#+id/about_textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="zariya values"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingTop="10px"
android:text="sometezt." />
</LinearLayout>
The error is in the main XML file.
You root is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
But then the first child have the height fill_parent this makes the first child take all the space of the LinearLayout so when you try to put your fragment in the container, it wont appear in the screen.
Instead try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
Try to get used to use match_parent instead of fill_parent :)
I've found out the ImageViewTouch since I wanted to create a zooming image preview on my app though I need to display that image on one of my DialogFragment. But it works just fine on a normal FragmentActivity which is the MainActivity but not in my DialogImage and I'm not sure why.
I imported the library using the gradle and here's my code so far:
The MainActivity
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
ImageViewTouch mImage;
#Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView( R.layout.fragment_main );
mImage = (ImageViewTouch) findViewById( R.id.image );
ImageDownloader2 imgDownload = new ImageDownloader2();
imgDownload.download("MY_URL", mImage);
Button click = (Button)findViewById(R.id.button);
click.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
showPromoImage();
}
});
}
private void showPromoImage(){
DialogImage promo_details = new DialogImage();
promo_details.show(getFragmentManager(), "promo_details");
}
}
The DialogImage
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
/**
* Created by Bahay on 2/19/14.
*/
public class DialogImage extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_image, container);
getDialog().setTitle("Test");
getDialog().setCanceledOnTouchOutside(true);
ImageViewTouch promo = (ImageViewTouch) view.findViewById(R.id.image);
ImageDownloader2 imgDownload = new ImageDownloader2();
imgDownload.download("MY_URL", promo);
return view;
}
}
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<it.sephiroth.android.library.imagezoom.ImageViewTouch
android:id="#+id/image"
android:layout_width="158dp"
android:layout_height="322dp"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_weight="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:gravity="center"
android:weightSum="2" >
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="20dp"
android:layout_height="wrap_content"
android:text="Load Image" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="20dp"
android:layout_height="wrap_content"
android:text="Change Display" />
</LinearLayout>
</LinearLayout>
dialog_image.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">
<it.sephiroth.android.library.imagezoom.ImageViewTouch
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
</LinearLayout>
The ImageDownloader2 is just a library which downloads the image from the URL then convert it into bitmap. I just made some customizations for this to work with ImageViewTouch.
As for the ImageViewTouch you can see it here:
https://github.com/sephiroth74/ImageViewZoom
For the ImageDownloader2 class you can see it here:
http://theandroidcoder.com/utilities/android-image-download-and-caching/
I just replaced the ImageView with ImageViewTouch for the download method.
UPDATE:
Tried using a normal old-school dialog wherein I use Activity and Intent where I just themed the DialogImage as android:theme="#android:style/Theme.DeviceDefault.Dialog". Output is same where image is not loaded.
Hope someone can help. Thanks!