Fragment layout not displaying anything? - android

IN this layout i have made tab layout with fragment activity and wanted to display few things,however nothing is being shown in fragment activity.
This is the base xml where the fragment is being made change.xml
<android.support.v4.widget.DrawerLayout 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/activity_change"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseParallaxMultiplier="0.5"
tools:context="com.example.alpit.formula2.Change">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/change_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/change"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.NoActionBar.PopupOverlay"></android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/change_tab"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/change_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/change_appbar"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_change"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_menu"
app:theme="#style/NavigationTheme">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
This is the java code of the tab layout
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Distance extends Fragment {
protected View onCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.activity_distance, container, false);
}
}
This is the xml code of distance
<?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:id="#+id/activity_distance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.alpit.formula2.Distance"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg"
android:textSize="100sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Weight in kg" />
</LinearLayout>
This is my java code of adding tabs in the layout
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
class Change extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private ActionBarDrawerToggle toggle3;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change);
Toolbar toolbar = (Toolbar) findViewById(R.id.change);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.activity_change);
toggle3 = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.Close);
drawerLayout.addDrawerListener(toggle3);
toggle3.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_change);
navigationView.setItemIconTintList(null);
navigationView.setNavigationItemSelectedListener(this);
setTitle("Conversion");
viewPager = (ViewPager) findViewById(R.id.change_viewpager);
setUpViewpager(viewPager);
TabLayout tabs = (TabLayout) findViewById(R.id.change_tab);
tabs.setupWithViewPager(viewPager);
}
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (toggle3.onOptionsItemSelected(menuItem)) {
return true;
}
return onOptionsItemSelected(menuItem);
}
private void setUpViewpager(ViewPager upViewpager) {
Adapter_tab adapter = new Adapter_tab(getSupportFragmentManager());
adapter.addFragment(new Distance(), "Distance");
adapter.addFragment(new Mass(), "Mass");
upViewpager.setAdapter(adapter);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
static class Adapter_tab extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter_tab(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentTitleList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

just add onCreateView in Distance

Related

Fragment not displaying the whole recyclerview. Its displaying only one cardview

The fragment only displays one cardview when I am trying to retrieve it from the news API.
Here is the code.
Main Activity
package com.manish.newapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create an instance of the tab layout from the view.
TabLayout tabLayout = findViewById(R.id.tab_layout);
// Set the text for each tab.
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label1));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label2));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label3));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label4));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label5));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label6));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label7));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label8));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label9));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_label10));
// Set the tabs to scroll through the entire layout.
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
// Use PagerAdapter to manage page views in fragments.
// Each page is represented by its own fragment.
final ViewPager viewPager = findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
// Setting a listener for clicks.
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new
TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
TabNews Fragment
package com.manish.newapp.Fragments;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.manish.newapp.Adapter;
import com.manish.newapp.ApiClient;
import com.manish.newapp.Models.Articles;
import com.manish.newapp.Models.Headlines;
import com.manish.newapp.Models.Source;
import com.manish.newapp.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class TabNews1 extends Fragment {
View v;
RecyclerView recyclerView;
final String API_KEY = "5a1ad719eee94e7ba60d19b51f4ff159";
String sources = "bbc-news";
Adapter adapter;
List<Articles> articles = new ArrayList<>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// String country = getCountry();
// retrieveJson(country,API_KEY);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v= inflater.inflate(R.layout.fragment_tab_news1, container, false);
recyclerView = (RecyclerView)v.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
String country = getCountry();
retrieveJson(country,API_KEY);
// retrieveJson(sources,API_KEY);
// Inflate the layout for this fragment
return v;
}
public void retrieveJson(String country, String apiKey){
Call<Headlines> call = ApiClient.getInstance().getApi().getHeadlines(country,apiKey);
call.enqueue(new Callback<Headlines>() {
#Override
public void onResponse(Call<Headlines> call, Response<Headlines> response) {
if (response.isSuccessful() && response.body().getArticles() != null){
articles.clear();
articles = response.body().getArticles();
adapter = new Adapter(getContext(),articles);
recyclerView.setAdapter(adapter);
}
}
#Override
public void onFailure(Call<Headlines> call, Throwable t) {
Toast.makeText(getContext(), t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public String getCountry(){
Locale locale = Locale.getDefault();
String country = locale.getCountry();
return country.toLowerCase();
}
}
adapter Class
package com.manish.newapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.manish.newapp.Models.Articles;
import com.squareup.picasso.Picasso;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
Context context;
List<Articles> articles;
public Adapter(Context context, List<Articles> articles) {
this.context = context;
this.articles = articles;
}
#NonNull
#Override
public Adapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items_recycler,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull Adapter.ViewHolder holder, int position) {
Articles art = articles.get(position);
holder.txtTitle.setText(art.getTitle());
holder.txtDescription.setText(art.getDescription());
holder.txtDate.setText(art.getPublishedAt());
String imageUrl = art.getUrlToImage();
Picasso.with(context).load(imageUrl).into(holder.imageView);
}
#Override
public int getItemCount() {
return articles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtTitle, txtDescription, txtDate, txtTime;
ImageView imageView;
CardView cardView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
txtTitle = (TextView)itemView.findViewById(R.id.title_text);
txtDescription = (TextView)itemView.findViewById(R.id.desc);
txtTime = (TextView)itemView.findViewById(R.id.time);
txtDate = (TextView)itemView.findViewById(R.id.date);
imageView = (ImageView)itemView.findViewById(R.id.image_news);
cardView = (CardView)itemView.findViewById(R.id.cardView);
}
}
}
Pager Adapter class
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import com.manish.newapp.Fragments.TabNews1;
import com.manish.newapp.Fragments.TabNews10;
import com.manish.newapp.Fragments.TabNews2;
import com.manish.newapp.Fragments.TabNews3;
import com.manish.newapp.Fragments.TabNews4;
import com.manish.newapp.Fragments.TabNews5;
import com.manish.newapp.Fragments.TabNews6;
import com.manish.newapp.Fragments.TabNews7;
import com.manish.newapp.Fragments.TabNews8;
import com.manish.newapp.Fragments.TabNews9;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(#NonNull FragmentManager fm,int NumOfTabs) {
super(fm, NumOfTabs);
this.mNumOfTabs = NumOfTabs;
}
#NonNull
/**
* Return the Fragment associated with a specified position.
*
* #param position
*/
#Override
public Fragment getItem(int position) {
switch (position){
case 0 : return new TabNews1();
case 1 : return new TabNews2();
case 2 : return new TabNews3();
case 3 : return new TabNews4();
case 4 : return new TabNews5();
case 5 : return new TabNews6();
case 6 : return new TabNews7();
case 7 : return new TabNews8();
case 8 : return new TabNews9();
case 9 : return new TabNews10();
default: return null;
}
}
/**
* Return the number of views available.
*/
#Override
public int getCount() {
return mNumOfTabs;
}
}
Here are my layout files.
item_recycler.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardView"
android:layout_margin="16dp"
android:padding="10dp"
app:cardElevation="4dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/image_news"
android:src="#drawable/image"
android:scaleType="centerCrop"/>
<!-- <FrameLayout-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:id="#+id/frame_layout"-->
<!-- android:layout_below="#+id/image_news"-->
<!-- android:padding="5dp">-->
<!-- <RelativeLayout-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content">-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TITLE"
android:textSize="20dp"
android:layout_below="#+id/image_news"
android:textStyle="bold"
android:id="#+id/title_text"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/desc"
android:text="DESCRIPTION"
android:layout_marginTop="10dp"
android:textSize="14dp"
android:layout_below="#+id/title_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/time"
android:layout_below="#+id/desc"
android:text="TIME"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/date"
android:layout_below="#+id/time"
android:text="DATE"
android:layout_marginTop="5dp"/>
<!-- </RelativeLayout>-->
<!-- </FrameLayout>-->
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
fragment_tab_news1.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=".Fragments.TabNews1"
android:background="#color/colorBackground">
<!-- TODO: Update blank fragment layout -->
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- <TextView-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="BBC NEWS"-->
<!-- android:textStyle="bold"-->
<!-- android:textSize="17sp"-->
<!-- android:layout_marginLeft="16dp"-->
<!-- android:layout_marginRight="16dp"-->
<!-- android:layout_marginTop="10dp"-->
<!-- android:fontFamily="sans-serif-light"/>-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
Mainactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="#+id/relative_main"
android:padding="16dp"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/tab_layout"/>
</RelativeLayout>
Please help me with this, I am unable to find whats wrong here.

How to replace and open a fragment from another fragment in android by the click of a button?

I am unable to remove a present fragment in my second fragment while trying to replace it from the first fragment along with trying to open the second fragment from the first fragment on a click of a button.
Here is my code
FirstFragment.java
package com.example.testanderase;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;
import java.util.Objects;
public class FirstFragment extends Fragment implements View.OnClickListener{
TextInputEditText inputEditText;
Button enterButton;
String getMessage;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.first_fragment,container,false);
inputEditText = view.findViewById(R.id.enter_edit_txt_input);
enterButton = view.findViewById(R.id.first_fragment_btn);
enterButton.setOnClickListener(this);
return view;
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onClick(View v) {
getMessage = inputEditText.getText().toString();
SecondFragment secondFragment = new SecondFragment();
Bundle b = new Bundle();
b.putString("data",getMessage);
secondFragment.setArguments(b);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.second_fragment_layout,secondFragment);
//transaction.add(R.id.second_fragment_layout,secondFragment);
transaction.commit();
***Below here I am trying to change the code
***In order to open the second fragment from the first fragment
/assert getFragmentManager() != null;
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.second_fragment_layout,secondFragment).commit();/
//fm.beginTransaction().replace(SecondFragment.newInstance());
}
}
first_fragment.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"
android:gravity="center">
<LinearLayout
android:id="#+id/first_fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/enter_edit_txt_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/input"
android:textAlignment="center"
android:gravity="center_horizontal" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/first_fragment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/enter"
android:background="#color/black"
android:textColor="#color/white"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
</LinearLayout>
</LinearLayout>
SecondFragment.java
package com.example.testanderase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SecondFragment extends Fragment implements View.OnClickListener{
TextView textView;
Button enterButton;
String firstFragmentValue;
Toast t;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.second_fragment,container,false);
textView = view.findViewById(R.id.second_fragment_txt_view);
enterButton = view.findViewById(R.id.second_fragment_btn);
enterButton.setOnClickListener(this);
//assert getArguments() != null;
//firstFragmentValue = getArguments().getString("data");
//textView.setText(firstFragmentValue);
Bundle bundle = getArguments();
if(bundle != null)
{
String name = bundle.getString("data");
textView.setText(name);
Toast.makeText(getContext(),"passed data "+name,Toast.LENGTH_SHORT).show();
}
//textView.setText(firstFragmentValue);
return view;
}
#Override
public void onClick(View v) {
//
ToastButton();
/*assert getFragmentManager() != null;
FirstFragment firstFragment = new FirstFragment();
FragmentManager fragmentTransaction = getFragmentManager();
fragmentTransaction.beginTransaction().replace(R.id.first_fragment_layout, firstFragment).commit();*/
}
public void ToastButton()
{
if(t!=null)
{
t.cancel();
}
else
{
Toast.makeText(getContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
}
}
second_fragment.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"
android:gravity="center">
<TextView
android:id="#+id/second_fragment_txt_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:text="#string/change_text"
android:textAlignment="center"
android:gravity="center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
<Button
android:id="#+id/second_fragment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/back_button_text"
android:background="#color/black"
android:textColor="#color/white"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
</LinearLayout>
SectionPageAdapter.java
package com.example.testanderase;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class SectionPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentListTitle = new ArrayList<>();
public void addFragment(Fragment fragment, String title)
{
fragmentList.add(fragment);
fragmentListTitle.add(title);
}
public SectionPageAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return fragmentListTitle.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
}
MainActivity.java
package com.example.testanderase;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity{
private SectionPageAdapter mSectionPageAdapter;
private ViewPager mainViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());
mainViewPager = findViewById(R.id.container);
setMainViewPager(mainViewPager);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mainViewPager);
}
public void setMainViewPager(ViewPager viewPager)
{
SectionPageAdapter adapter = new SectionPageAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstFragment(),"FIRST");
adapter.addFragment(new SecondFragment(),"SECOND");
viewPager.setAdapter(adapter);
}
}
activity_main.xml
<?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"
android:gravity="center"
tools:context=".MainActivity"
android:id="#+id/main_layout"
android:layout_margin="20dp">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
where is R.id.second_fragment_layout
anyway change your second_fragment.xml to this
<?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"
android:gravity="center">
<LinearLayout
android:id="#+id/second_fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/second_fragment_txt_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:text="hello"
android:textAlignment="center"
android:gravity="center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
<Button
android:id="#+id/second_fragment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="back"
android:background="#000"
android:textColor="#fff"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
</LinearLayout>
</LinearLayout>
and to open the second fragment from the first fragment on a click of a button
first make mainViewPager static
static ViewPager mainViewPager
put this on first fragment onclick
int page =1;
mainViewPager.setCurrentItem(page);
to back to first fragment
put this on second fragment onclick
int page =0;
mainViewPager.setCurrentItem(page);
ts working for me.!

actionbar is not showing hide feature when i use grid layout with appbar layout

i am using AppBarLayout. it has a ViewPager in which i have added four fragments two fragments have recyclerView in them. and one has GridView. when i am on the fragment with RecyclerView the ActionBar hides when i scroll list up, but in case of gridview fragment this doesn't happen. ActionBar remains still at its position.
please help me! thanks in advance.
MainActivity
package com.bajpays.financemanager;
import android.support.design.internal.NavigationMenu;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TabLayout tabLayout;
DrawerLayout drawerLayout;
NavigationView navigationView;
ViewPager viewPager;
Toolbar toolbar;
CircleImageView img_user1,img_user2,img_user3,img_user4,img_user5,img_activeuser;
NavigationView navview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar=getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.grouppic);
actionBar.setDisplayShowHomeEnabled(true);
viewPager=(ViewPager)findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
if (viewPager!=null){
setUpViewPager(viewPager);
}
tabLayout=(TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
navigationView=(NavigationView) findViewById(R.id.navigatioview);
if (navigationView != null) {
setupDrawerContent(navigationView);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer);
}
navview=(NavigationView)findViewById(R.id.navigatioview);
View headerview=navview.getHeaderView(0);
RelativeLayout header=(RelativeLayout) headerview.findViewById(R.id.header);
img_activeuser=(CircleImageView)headerview.findViewById(R.id.img_icon_activeuser);
img_user1=(CircleImageView)headerview.findViewById(R.id.img_icon_user1);
img_user2=(CircleImageView)headerview.findViewById(R.id.img_icon_user2);
img_user3=(CircleImageView)headerview.findViewById(R.id.img_icon_user3);
img_user4=(CircleImageView)headerview.findViewById(R.id.img_icon_user4);
img_user5=(CircleImageView)headerview.findViewById(R.id.img_icon_user5);
img_user1.setTag(R.drawable.deep);
img_user2.setTag(R.drawable.grouppic);
img_user3.setTag(R.drawable.indelhi);
img_user4.setTag(R.drawable.kanishk);
img_user5.setTag(R.drawable.tshirt);
img_user1.setOnClickListener(this);
img_user2.setOnClickListener(this);
img_user3.setOnClickListener(this);
img_user4.setOnClickListener(this);
img_user5.setOnClickListener(this);
}
static class MyAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments=new ArrayList<>();
ArrayList<String> titles=new ArrayList<>();
public MyAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment,String title){
fragments.add(fragment);
titles.add(title);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
public void setUpViewPager(ViewPager viewPager){
MyAdapter adapter=new MyAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment_Home(),"Home");
adapter.addFragment(new Fragment_Catagory(),"Category");
adapter.addFragment(new Fragment_Top5(),"Top 5 Expences");
adapter.addFragment(new Fragment_Statistics(),"Statistics");
viewPager.setAdapter(adapter);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
}
}
Fragment with grid layout
package com.bajpays.financemanager;
import android.content.Context;
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;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Created by Bajpay's on 7/22/2016.
*/
public class Fragment_Home extends Fragment {
View view;
ArrayList<Model_Grid> grids;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.layout_home, container, false);
grids=makegrids();
GridAdapter adapter=new GridAdapter(this.getActivity(),grids);
GridView gridView=(GridView) view.findViewById(R.id.gridview);
gridView.setAdapter(adapter);
Toast.makeText(this.getActivity(), "oncreate of home rns", 200).show();
return view;
}
public ArrayList<Model_Grid> makegrids()
{
ArrayList<Model_Grid> grids=new ArrayList<>();
grids.add(new Model_Grid(R.drawable.cheese_1, "food"));
grids.add(new Model_Grid(R.drawable.cheese_2,"cloth"));
grids.add(new Model_Grid(R.drawable.cheese_3,"outing"));
grids.add(new Model_Grid(R.drawable.cheese_4,"bank"));
grids.add(new Model_Grid(R.drawable.cheese_5,"travel"));
grids.add(new Model_Grid(R.drawable.cheese_1,"rent"));
grids.add(new Model_Grid(R.drawable.cheese_2,"health"));
grids.add(new Model_Grid(R.drawable.cheese_3,"makeup"));
grids.add(new Model_Grid(R.drawable.cheese_4,"home"));
grids.add(new Model_Grid(R.drawable.cheese_5,"daily"));
grids.add(new Model_Grid(R.drawable.cheese_1,"study"));
grids.add(new Model_Grid(R.drawable.cheese_5,"electronics"));
grids.add(new Model_Grid(R.drawable.cheese_4,"mobile"));
grids.add(new Model_Grid(R.drawable.ic_forum,"add new"));
return grids;
}
}
class GridAdapter extends BaseAdapter{
Context context;
LayoutInflater inflater;
View view;
ImageView imageView;
TextView gridname;
ArrayList<Model_Grid> grids=new ArrayList<>();
GridAdapter(Context context,ArrayList<Model_Grid> gridlist){
this.context=context;
grids=gridlist;
inflater=LayoutInflater.from(context);
}
#Override
public int getCount() {
return grids.size();
}
#Override
public Object getItem(int position) {
return grids.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view=inflater.inflate(R.layout.dapter_for_home,null);
imageView=(ImageView)view.findViewById(R.id.grid_img);
gridname=(TextView)view.findViewById(R.id.grid_name);
imageView.setImageResource(grids.get(position).getImageid());
gridname.setText(grids.get(position).getGridName());
return view;
}
}
Layout of MainActivity
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinatorlayout"
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:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#659eaf"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="enterAlways|scroll|snap" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff75cc"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigatioview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/header"
app:menu="#menu/menu"/>
</android.support.v4.widget.DrawerLayout>
layout of fragment
<?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">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:columnWidth="120dp"
android:stretchMode="spacingWidthUniform"
android:paddingBottom="50dp">
</GridView>
</LinearLayout>
it works only on Lollipop add this code-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
gridview.setNestedScrollingEnabled(true);
}
otherwise try to put your grid layout inside NestedScrollview. Hope this will help.

how can i switch other fragment layout programmatically in Android?

I' ve got two layouts (layout_first.xml and layout_second.xml).
I want to show layout_second, after click a Button (Show Second) in layout_first. And get an EditText value in the layout_first to set EditText value in the layout_second programmaticaly. How can i do this? Thanks for your answers.
my codes:
activity_main.xml
<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">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="#dimen/custom_tab_layout_height"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
layout_first.xml
<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="horizontal"
android:focusable="true"
android:background="#bc8383"
android:id="#+id/content_frame">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Second"
android:id="#+id/btnShowSecond"
android:layout_weight="0.18" />
<EditText
android:id="#+id/et1"
android:text="et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:elegantTextHeight="false"
android:maxLength="20"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#85b27e"
android:textSize="40dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_marginTop="10dp"
android:layout_weight="1" />
</LinearLayout>
layout_second.xml
<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"
android:focusable="true"
android:background="#bc8383">
<EditText
android:id="#+id/et2"
android:text="et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:elegantTextHeight="false"
android:maxLength="20"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#85b27e"
android:textSize="40dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn2"
android:id="#+id/btn2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
custom_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tab"
android:textColor="#color/colorAccent"
android:textSize="14dp"
android:gravity="center"
android:fontFamily="#string/font_fontFamily_medium"/>
MainActivity.java
package xmaxsoft.delfragment;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("One");
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("Two");
tabLayout.getTabAt(1).setCustomView(tabTwo);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new First(), "One");
adapter.addFrag(new Second(), "Two");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Second.java
package xmaxsoft.delfragment;
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.Button;
import android.widget.EditText;
public class Second extends Fragment {
public Second() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.layout_second, container, false);
String data = getArguments().getString("value");
et2.setText(data);
return view;
}
}
First.java
package xmaxsoft.delfragment;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class First extends Fragment {
public First() {
}
Button btnShowSecond;
EditText et1;
public void addFragment(Fragment fragment, boolean addToBackStack, String tag) {
FragmentManager manager = getFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (addToBackStack) {
ft.addToBackStack(tag);
}
ft.replace(R.id.aid1, fragment, tag);
ft.commitAllowingStateLoss();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.layout_first, container, false);
btnShowSecond = (Button) view.findViewById(R.id.btnShowSecond);
et1 = (EditText) view.findViewById(R.id.et1);
btnShowSecond.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//what kind of code must be here ???
//what kind of code must be here ???
//what kind of code must be here ???
Second myfragment = new Second();
Bundle bundle = new Bundle();
bundle.putString("value",et1.getText().toString());
myfragment.setArguments(bundle);
Activity.addFragment(myfragment, false,"Second");//error ??? cannot resolve method ???
}
});
return view;
}
}
How can i display or switch layout_second programmatically in layout_first? Thank you for your helps.
You can do this by using Intent class.
1. on First.java put this code in clicklistener
public void mybutton_click()
{
Second myfragment = new Second();
Bundle bundle = new Bundle();
bundle.putString("value",et1.getText());
myfragment.setArguments(bundle);
activity.addFragment(myfragment, false,
"Second");
}
To call other Fragment
public void addFragment(Fragment fragment, boolean addToBackStack,
String tag) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
if (addToBackStack) {
ft.addToBackStack(tag);
}
ft.replace(R.id.content_frame, fragment, tag);
ft.commitAllowingStateLoss();
}
In Second.java put this on Oncreate():
String data = getArguments().getString("value");
et2.setText(data);

Android Navigation Drawer with Image Error

I have written a simple application for navigation Drawer based on this tutorial: https://www.youtube.com/watch?v=tZ2DNC3FIic
but there is a problem when I run the application.
The Error is i am having a blank or Empty Drawer List when i launch the application, and instead of that i Got the list in the application launch.
like this image:
This is the main_activity layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
tools:context="com.subhi.tabhost.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/main_content"
android:layout_height="match_parent"></RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/drawer_pane"
android:layout_gravity="start"
android:background="#FF4081"
android:layout_height="match_parent"></RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/profile_box"
android:padding="8dp"
android:gravity="center_vertical"
android:layout_height="100dp">
<ImageView
android:id="#+id/icon"
android:layout_margin="5dp"
android:layout_width="50dp"
android:background="#drawable/ic_launcher"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_toRightOf="#+id/icon"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#000000"
android:text="Subhi"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#000000"
android:text="Subhi"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:id="#+id/nav_list"
android:layout_below="#id/profile_box"
android:choiceMode="singleChoice"
android:background="#ffffff"
android:layout_height="match_parent"></ListView>
</android.support.v4.widget.DrawerLayout>
and this is the MainActivity class:
package com.subhi.tabhost;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
RelativeLayout drawerPane;
ListView lvNav;
List<NavItem> listNavItems;
List<Fragment> listFragments;
ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
drawerPane= (RelativeLayout) findViewById(R.id.drawer_pane);
lvNav =(ListView)findViewById(R.id.nav_list);
listNavItems=new ArrayList<NavItem>();
listNavItems.add(new NavItem("sHome", "HomePage", R.drawable.ic_launcher));
listNavItems.add(new NavItem("Setting", "HomePage", R.drawable.ic_launcher));
listNavItems.add(new NavItem("Setting", "HomePage", R.drawable.ic_launcher));
NavListAdapter navListAdapter=new NavListAdapter(getApplicationContext(),R.id.nav_list,listNavItems);
lvNav.setAdapter(navListAdapter);
listFragments=new ArrayList<Fragment>();
listFragments.add(new MyHome());
listFragments.add(new MySettings());
listFragments.add(new MyAbout());
FragmentManager fragmentManager=getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.main_content,listFragments.get(0)).commit();
setTitle(listNavItems.get(0).getTitle());
lvNav.setItemChecked(0, true);
drawerLayout.closeDrawer(drawerPane);
lvNav.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.main_content, listFragments.get(position)).commit();
setTitle(listNavItems.get(position).getTitle());
lvNav.setItemChecked(position, true);
drawerLayout.closeDrawer(drawerPane);
}
});
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_opened,R.string.drawer_closed){
#Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
super.onDrawerOpened(drawerView);
}
#Override
public void onDrawerClosed(View drawerView) {
invalidateOptionsMenu();
super.onDrawerClosed(drawerView);
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
}
and this is the Navlist Adapter Class:
package com.subhi.tabhost;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by subhi on 2/9/2016.
*/
public class NavListAdapter extends ArrayAdapter<NavItem> {
Context context;
int reslayout;
List<NavItem> listNavItem;
public NavListAdapter(Context context, int reslayout, List<NavItem> listNavItem) {
super(context, reslayout, listNavItem);
this.context=context;
this.reslayout=reslayout;
this.listNavItem=listNavItem;
}
#SuppressLint("ViewHolder") #Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=View.inflate(context, reslayout, null);
TextView tvtitle= (TextView) v.findViewById(R.id.titlemain);
TextView tvsubtitle=(TextView)v.findViewById(R.id.subtitle);
ImageView navicon= (ImageView) v.findViewById(R.id.nav_icon);
NavItem navItem=listNavItem.get(position);
tvtitle.setText(navItem.getTitle());
tvsubtitle.setText(navItem.getSubtitle());
navicon.setImageResource(navItem.getResicon());
return v;
}
}
and this is the NavItem Class:
package com.subhi.tabhost;
/**
* Created by subhi on 2/9/2016.
*/
public class NavItem {
private String title;
private String subtitle;
private int resicon;
public NavItem(String title,String subtitle, int resicon ) {
this.subtitle = subtitle;
this.resicon = resicon;
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public int getResicon() {
return resicon;
}
public void setResicon(int resicon) {
this.resicon = resicon;
}
}
and this is one of the Fragments that i used when the item is clicked:
package com.subhi.tabhost;
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;
/**
* Created by subhi on 2/9/2016.
*/
public class MyHome extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_home,container,false);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
and this is the item_nav_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_launcher"
android:id="#+id/nav_icon" />
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_margin="10dp"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:id="#+id/titlemain"
android:textStyle="bold"
android:textSize="18sp"
android:text="Titile"
android:textColor="#000"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:text="title2"
android:textSize="18sp"
android:id="#+id/subtitle"
android:textColor="#000"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
The error is from this line
new NavListAdapter(getApplicationContext(),R.id.nav_list,listNavItems)
The parameter that you have for R.id.nav_list should be a R.layout.nav_item where nav_item.xml is the layout for a row in the NavListAdapter.
Here
NavListAdapter navListAdapter=new NavListAdapter(getApplicationContext(),R.id.nav_list,listNavItems);
you pass R.id.nav_list which then you want to inflate with
View v=View.inflate(context, reslayout, null);
reslayout cannot be R.id, it has to be layout file (R.layout).
EDIT
Now your main_layout is wrong. Should be like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
tools:context="com.subhi.tabhost.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/main_content"
android:layout_height="match_parent"></RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/drawer_pane"
android:layout_gravity="start"
android:background="#FF4081"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/profile_box"
android:padding="8dp"
android:gravity="center_vertical"
android:layout_height="100dp">
<ImageView
android:id="#+id/icon"
android:layout_margin="5dp"
android:layout_width="50dp"
android:background="#drawable/ic_launcher"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_toRightOf="#+id/icon"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#000000"
android:text="Subhi"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#000000"
android:text="Subhi"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:id="#+id/nav_list"
android:layout_below="#id/profile_box"
android:choiceMode="singleChoice"
android:background="#ffffff"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>

Categories

Resources