Must scroll up to see upper layout items - android

When activity launches first thing you see is recylerview instead upper items !
The first elements are a cardview including an imageview then a cardview including a radiobutton group. For visiting these items, you must scroll up the screen !
No errors , How can i solve this problem?
NOTE : my problem solves when i don't use of nestedScrollView but in this condition , the final cardview(icluding imageview) does not appear
this is xml code
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp">
<ImageView
android:id="#+id/sarasari_mainact_iv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:adjustViewBounds="true"
android:src="#drawable/ad12_20" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:layout_marginLeft="3dp">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_marginTop="3dp"
android:layout_marginBottom="5dp"
android:id="#+id/reshteha_radio_gruop">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="math" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="science" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="human scinces" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="language" />
<RadioButton
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="religion" />
</RadioGroup>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp" />
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="3dp">
<ImageView
android:id="#+id/sarasari_mainact_iv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:adjustViewBounds="true"
android:src="#drawable/ad12_20" />
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation"/>
this is activity code
public class Main2Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public RadioButton rb1,rb2,rb3,rb4,rb5;
public Typeface vazirTf ;
private RadioGroup reshtehaRg ;
String[] mArrayNames = {"a","b","c","d","e","f","g","h","i"};
int[] mArrayPics = { R.drawable.pic1 , R.drawable.pic2 , R.drawable.pic3 , R.drawable.pic4, R.drawable.pic5 , R.drawable.pic6, R.drawable.pic7,R.drawable.pic8,R.drawable.pic9 };
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
case R.id.navigation_about:
mTextMessage.setText(R.string.title_news);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
itemsSetter();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mRecyclerView= (RecyclerView)findViewById(R.id.my_recycler_view);
mLayoutManager= new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(mArrayNames,mArrayPics,Main2Activity.this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setNestedScrollingEnabled(false);// this item makes the recylcerview scrolling softly inside using nestedScrollView and commenting this code has no action to my problem (showing recylcerview istead uper elements)
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void itemsSetter() {
vazirTf = Typeface.createFromAsset(getAssets(), "font/Vazir.ttf");
rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb2 = (RadioButton) findViewById(R.id.radioButton2);
rb3 = (RadioButton) findViewById(R.id.radioButton3);
rb4 = (RadioButton) findViewById(R.id.radioButton4);
rb5 = (RadioButton) findViewById(R.id.radioButton5);
rb1.setTypeface(vazirTf);
rb2.setTypeface(vazirTf);
rb3.setTypeface(vazirTf);
rb4.setTypeface(vazirTf);
rb5.setTypeface(vazirTf);
reshtehaRg = (RadioGroup)findViewById(R.id.reshteha_radio_gruop);
}
}

You need to add the descendantFocusability in the first layout under the android.support.v4.widget.NestedScrollView like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >

I guess this is the solution for your problem. Check this link:
RecycleView steals focus when inside a NestedScrollView

Related

default view stuck at the top, is their anyway to remove it

I have a problem while developing an android app, I have created a default view in DrawerLayout nesting recyclerview but the view stuck after when the the app is getting json from the server while the view with data are showing below it.
Below are the code I have included for mainactivity.xml content.xml, adapter.java class and mainactivity.java class
Mainactivity.xml
<?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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/customeView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>``
content.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="7dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:background="?android:attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:transitionName="img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
tools:ignore="UnusedAttribute" />
<ImageView
android:id="#+id/shadow_bottom"
android:src="#drawable/bottom_shadow"
android:layout_alignBottom="#id/image"
android:layout_width="match_parent"
android:layout_height="80dp" />
<FrameLayout
android:id="#+id/layoutDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image"
android:background="#drawable/round_white"
android:padding="5dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="-50dp">
<ImageView
android:src="#drawable/ic_date"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
<TextView
android:textColor="#606060"
android:id="#+id/publishedAt"
android:layout_marginLeft="27dp"
android:layout_marginRight="10dp"
android:text="01 January 1990"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textStyle="bold"
android:textColor="#color/colorTextTitle"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="Title"
android:textSize="17sp"
android:layout_marginTop="10dp"
android:layout_below="#id/image"
android:id="#+id/title" />
<TextView
android:id="#+id/txtContents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:text="Desc"
/>
<TextView
android:id="#+id/source"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="#id/txtContents"
android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="10dp"
android:fontFamily="sans-serif-light"
android:textStyle="bold"
android:textColor="#color/colorTextTitle"
android:maxLines="1"
android:drawablePadding="10dp"
android:singleLine="true"
android:ellipsize="end"
android:text="Source" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>``
Adapter.java
``public class Adapter extends
RecyclerView.Adapter<Adapter.AdapterViewHolder> {
private List<Articles> dataList;
private Context context;
private String storage_url="http://192.168.56.1/muslimnews/storage/";
public Adapter(Context context,List<Articles> dataList){
this.context = context;
this.dataList = dataList;
}
class AdapterViewHolder extends RecyclerView.ViewHolder {
public final View mView;
TextView txtTitle;
TextView txtContents;
TextView source;
TextView date;
private ImageView coverImage;
AdapterViewHolder(View itemView) {
super(itemView);
mView = itemView;
txtTitle = mView.findViewById(R.id.title);
txtContents=mView.findViewById(R.id.txtContents);
coverImage = mView.findViewById(R.id.image);
source=mView.findViewById(R.id.source);
date=mView.findViewById(R.id.publishedAt);
}
}
#NonNull
#Override
public AdapterViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View view=layoutInflater.inflate(R.layout.content_main,parent,false);
return new AdapterViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull AdapterViewHolder holder, int position) {
holder.txtTitle.setText(dataList.get(position).getTitle());
holder.txtContents.setText(Html.fromHtml(dataList.get(position).getShort_content()));
holder.source.setText(dataList.get(position).getSource());
try {
Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dataList.get(position).getCreated_at());
String publishedDate=new SimpleDateFormat("dd MMMM yyyy").format(date);
holder.date.setText(publishedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(new OkHttp3Downloader(context));
builder.build().load(storage_url+dataList.get(position).getImgUrl())
.placeholder((R.drawable.ic_launcher_background))
.error(R.drawable.ic_launcher_background)
.into(holder.coverImage);
}
#Override
public int getItemCount() {
return dataList==null ? 0: dataList.size();
}
}``
mainactivity.java
`public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
SwipeRefreshLayout.OnRefreshListener {
private Adapter adapter;
private RecyclerView recyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetDataService service= RetrofitClientInstance.getRetrofitInstance()
.create(GetDataService.class);
Call<List<Articles>> call=service.getAllArticles();
call.enqueue(new Callback<List<Articles>>() {
#Override
public void onResponse(Call<List<Articles>> call, Response<List<Articles>> response) {
recyclerView=findViewById(R.id.customeView);
adapter=new Adapter(MainActivity.this,response.body());
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(adapter);
//generateDataList(response.body());
swipeRefreshLayout.setRefreshing(false);
}
#Override
public void onFailure(Call<List<Articles>> call, Throwable t) {
Toast.makeText(MainActivity.this,"Something went wrong",Toast.LENGTH_LONG).show();
}
});
swipeRefresh();
createMenu();
}
private void swipeRefresh(){
swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
}
private void createMenu(){
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void fillMain(){
GetDataService service= RetrofitClientInstance.getRetrofitInstance()
.create(GetDataService.class);
Call<List<Articles>> call=service.getAllArticles();
call.enqueue(new Callback<List<Articles>>() {
#Override
public void onResponse(Call<List<Articles>> call, Response<List<Articles>> response) {
swipeRefreshLayout.setRefreshing(false);
generateDataList(response.body());
}
#Override
public void onFailure(Call<List<Articles>> call, Throwable t) {
swipeRefreshLayout.setRefreshing(true);
Toast.makeText(MainActivity.this,"Something went wrong",Toast.LENGTH_LONG).show();
}
});
}
private void generateDataList(List<Articles> articlesList){
recyclerView=findViewById(R.id.customeView);
adapter=new Adapter(this,articlesList);
adapter.notifyDataSetChanged();
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
// recyclerView.setItemAnimator(new DefaultItemAnimator());
// recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(adapter);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onRefresh() {
fillMain();
}
}`
set your manifest theme to Appcomat.theme.nocationbar and put custom toolbar or actionbar in activity.
<application
android:name="androidx.multidex.MultiDexApplication"
android:allowBackup="true"
android:appComponentFactory="whateverString"
android:debuggable="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:testOnly="false"
android:theme="#style/AppTheme" //here you can edit it. completly.
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning,HardcodedDebugMode"
tools:replace="android:appComponentFactory"
tools:targetApi="m">

Navigation Drawer too slow while opening and closing

Opening and closing of the drawer is very slow and laggy. No related errors are shown in monitor. If any one is familiar with this issue ? What might be possible errors ?
Thanks in advance
This is what i did
Recycler view adapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
String[] titles;
TypedArray icons;
Context context;
// The default constructor to receive titles,icons and context from MainActivity.
public RecyclerViewAdapter(String[] titles, TypedArray icons, Context context) {
this.titles = titles;
this.icons = icons;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (viewType == 1) {
View itemLayout = layoutInflater.inflate(R.layout.drawer_item_layout, null);
return new ViewHolder(itemLayout, viewType, context);
} else if (viewType == 0) {
View itemHeader = layoutInflater.inflate(R.layout.header_layout, null);
return new ViewHolder(itemHeader, viewType, context);
}
return null;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (position != 0) {
holder.navTitle.setText(titles[position - 1]);
holder.navIcon.setImageResource(icons.getResourceId(position - 1, -1));
}
}
#Override
public int getItemCount() {
return titles.length + 1;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView navTitle;
ImageView navIcon;
Context context;
public ViewHolder(View drawerItem, int itemType, Context context) {
super(drawerItem);
this.context = context;
drawerItem.setOnClickListener(this);
if (itemType == 1) {
navTitle = (TextView) itemView.findViewById(R.id.tv_NavTitle);
navIcon = (ImageView) itemView.findViewById(R.id.iv_NavIcon);
}
}
/**
* This defines onClick for every item with respect to its position.
*/
#Override
public void onClick(View v) {
switch (getAdapterPosition()){
case 1:
Intent home= new Intent(context,HomeActivity.class);
home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(home);
break;
case 2:
// Intent profile= new Intent(context,HomeActivity.class);
// profile.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
// context.startActivity(profile);
break;
case 3:
// Intent accnt= new Intent(context,HomeActivity.class);
// accnt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
// context.startActivity(accnt);
break;
case 4:
Intent cart= new Intent(context,MyCartLoginActivity.class);
cart.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(cart);
break;
case 5:
// Intent phis= new Intent(context,HomeActivity.class);
// phis.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
// context.startActivity(phis);
// break;
case 6:
Intent logout= new Intent(context,LoginActivity.class);
logout.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(logout);
break;
}
}
}
public int getItemViewType(int position) {
if (position == 0) return 0;
else return 1;
}
}
Main Activity
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
RecyclerView recyclerView;
String navTitles[];
TypedArray navIcons;
RecyclerView.Adapter recyclerViewAdapter;
ActionBarDrawerToggle drawerToggle;
FrameLayout frameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);frameLayout = (FrameLayout)findViewById(R.id.containerView);
setupToolbar();
//Initialize Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerMainActivity);
// Setup Titles and Icons of Navigation Drawer
navTitles = getResources().getStringArray(R.array.navDrawerItems);
navIcons = getResources().obtainTypedArray(R.array.navDrawerIcons);
recyclerViewAdapter = new RecyclerViewAdapter(navTitles,navIcons,this);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//recyclerView.setLayoutManager(new LinearLayoutManager(this));
// Finally setup ActionBarDrawerToggle
setupDrawerToggle();
}
void setupToolbar(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// toolbar.setNavigationIcon(R.drawable.ic_action_menu);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
void setupDrawerToggle(){
drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
//This is necessary to change the icon of the Drawer Toggle upon state change.
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_action_menu);
drawerToggle.syncState();
}
}
Header Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:layout_marginTop="25dp">
<RelativeLayout
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/rlh"
android:layout_marginLeft="15dp"
android:background="#drawable/circ"
android:gravity="center">
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:src="#drawable/logo5" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/rlh"
android:layout_toEndOf="#+id/rlh"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nithin Kumar"
android:textStyle="bold"
android:textColor="#FFF"
android:id="#+id/uname"
android:textSize="17dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/uname"
android:layout_marginTop="2dp"
android:textStyle="bold"
android:text="+919876543210"
android:textColor="#FFF"
android:textSize="13dp" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
Drawer Item Layout
<?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"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_marginTop="140dp"
android:layout_height="60dp">
<RelativeLayout
android:id="#+id/drawerItem"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:layout_marginLeft="45dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:tint="#251e1e"
android:layout_centerVertical="true"
android:id="#+id/iv_NavIcon"/>
<de.morrox.fontinator.FontTextView
android:layout_toRightOf="#+id/iv_NavIcon"
app:font="Lato-Regular.ttf"
android:layout_gravity="center"
android:text="Claimed Offers"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:id="#+id/tv_NavTitle"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#251e1e"
android:textSize="16sp"
android:gravity="center"/>
</RelativeLayout>
</RelativeLayout>
Main activity layout
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerMainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<FrameLayout
android:layout_below="#+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/containerView">
</FrameLayout>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="240dp"
android:layout_height="fill_parent"
android:id="#+id/recyclerView"
android:scrollbars="vertical"
android:background="#FFF"
android:layout_gravity="left"
/>
</android.support.v4.widget.DrawerLayout>
The problem was solved by clearing cache and increasing heap size of the application. Also decreased the size of the logo in the navigation drawer

viewpager scrollview not working android

I'm integrating ViewPager and ScrollView on android but it doesn't seem to work. The ViewPager scroll works just fine but the ScrollView does not work at all on all the solutions I've tried. Below is my MainActivity class and xml layout.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
hostDescription = (TextView) findViewById(R.id.host_description);
hostDescription.setTypeface(quickSandRegular);
hostDescription.setTextColor(Color.BLACK);
scrollView = (ScrollView) findViewById(R.id.scroll);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header=navigationView.getHeaderView(0);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter();
viewPager.setAdapter(adapter);
// set navigation font
Menu m = navigationView.getMenu();
for (int i=0;i<m.size();i++) {
MenuItem mi = m.getItem(i);
// apply font to subment
SubMenu subMenu = mi.getSubMenu();
if (subMenu!=null && subMenu.size() >0 ) {
for (int j=0; j <subMenu.size();j++) {
MenuItem subMenuItem = subMenu.getItem(j);
applyFontToMenuItem(subMenuItem);
}
}
applyFontToMenuItem(mi);
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
}
xml
<?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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- here insert your scrollview and all views you need for this page this is actually your page -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayoutMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/blue"
android:orientation="vertical" >
<TextView
android:id="#+id/welcome_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginBottom="30dp"
android:textColor="#android:color/holo_green_light"
android:layout_marginLeft="17dp"
android:background="#color/blue"
android:textSize="26dp"/>
<TextView
android:id="#+id/intro_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:layout_marginBottom="10dp"
android:text="Save on your next adventure."
android:textColor="#android:color/holo_green_light"
android:layout_marginLeft="17dp"
android:background="#color/blue"
android:textSize="28dp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical" >
<TextView
android:id="#+id/host_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Become a Host"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:textSize="21dp"/>
<!--<ImageView
android:id="#+id/image1"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:scaleType="fitXY"
android:src="#drawable/sv" />-->
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
/>
<TextView
android:id="#+id/host_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Earn money sharing your extra space"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:textSize="17dp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical" >
<TextView
android:id="#+id/host_telxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Become a Host"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:textSize="21dp"/>
<!--<ImageView
android:id="#+id/image1"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:scaleType="fitXY"
android:src="#drawable/sv" />-->
<TextView
android:id="#+id/host_escription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Earn money sharing your extra space"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:textSize="17dp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#cccccc" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
ImageAdapter
class ImageAdapter extends PagerAdapter {
#Override
public int getCount() {
return IMAGES.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(IMAGES[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
You need to implement ViewPager.OnPageChangeListener and write your logic in onPageScrollStateChanged
#Override
public void onPageScrollStateChanged(int state) {
ScrollView.requestDisallowInterceptTouchEvent(!(!lastPage && state == ViewPager.SCROLL_STATE_IDLE));
}
You need to write logic to find if you are on last page or not which is simple.
Solved the problem by customizing layout_height of the ViewPager. Below is the code:
<android.support.v4.view.ViewPager
android:id="#+id/view_pager2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"/>
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter();
viewPager.setAdapter(adapter);
viewPager.setClipToPadding(false);
viewPager.setPadding(70, 0, 70, 0);
class ImageAdapter extends PagerAdapter {
#Override
public int getCount() {
return IMAGES.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(IMAGES[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
setPrimaryItem((View) container, position, object);
// lastPage = IMAGES[position]==object;
}
}

Viewpager With tabLayout and PagerTabStrip

I Experimented a bit in viewPager.
One viewpager with different FragmentAdapter.
For now the code works perfectly but I don't know if its the right way to do that.
For my Activity is this.
public class Navigation extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener, View.OnClickListener {
private DrawerLayout drawer;
private ViewPager pager;
public static TabHost tabHost;
public static TabWidget tabWidget;
public static FloatingActionMenu fab;
private boolean doubleBackToExitPressedOnce = false;
private String currentTab;
private Button mButton;
private LinearLayout mLinearLayout;
private PagerSlidingTabStrip mTabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getAttributes().windowAnimations = 0;
setContentView(R.layout.activity_navigation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
fab = (FloatingActionMenu) findViewById(R.id.fabMenu);
fab.setVisibility(View.GONE);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
tabHost.setOnTabChangedListener(this);
tabWidget = (TabWidget) findViewById(android.R.id.tabs);
setNewTab(tabHost, "tab1", R.string.textTabTitle1, R.drawable.icon_search, R.id.tab1);
setNewTab(tabHost, "tab2", R.string.textTabTitle1, R.drawable.icon_comment, R.id.tab2);
setNewTab(tabHost, "tab3", R.string.textTabTitle1, R.drawable.icon_car, R.id.tab3);
mTabLayout = (PagerSlidingTabStrip) findViewById(R.id.tab);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new FragmentAdapterSearch(getSupportFragmentManager()));
mTabLayout.setViewPager(pager);
pager.addOnPageChangeListener(this);
mButton = (Button) findViewById(R.id.btnSearch);
mLinearLayout = (LinearLayout) findViewById(R.id.btnSelectLocation);
mButton.setOnClickListener(this);
mLinearLayout.setOnClickListener(this);
TabIcon.setColor(0, mTabLayout, iconSearchOpac, iconSearch);
}
private void setNewTab(TabHost tabHost, String tag, int title, int icon, int contentID) {
TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
tabSpec.setIndicator(getTabIndicator(tabHost.getContext(), title, icon));
tabSpec.setContent(contentID);
tabHost.addTab(tabSpec);
}
private View getTabIndicator(Context context, int title, int icon) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
final ImageView iv = (ImageView) view.findViewById(R.id.imageView);
iv.setImageResource(icon);
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText(title);
return view;
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (!drawer.isDrawerOpen(GravityCompat.START)) {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Tap Again to Exit Toweelo", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.navigation, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (drawer.isDrawerOpen(Gravity.LEFT)) {
drawer.closeDrawer(Gravity.LEFT);
} else {
drawer.openDrawer(Gravity.LEFT);
}
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onTabChanged(String tabId) {
currentTab = tabId;
if (currentTab.equalsIgnoreCase("tab1")) {
if (pager != null) {
pager.setAdapter(new FragmentAdapterSearch(getSupportFragmentManager()));
mButton.setVisibility(View.VISIBLE);
TabIcon.setColor(0, mTabLayout, iconSearchOpac, iconSearch);
}
}
if (currentTab.equalsIgnoreCase("tab2")) {
if (pager != null) {
pager.setAdapter(new FragmentAdapterComments(getSupportFragmentManager()));
mButton.setVisibility(View.INVISIBLE);
TabIcon.setColor(0, mTabLayout, iconCommentOpac, iconComment);
}
}
if (currentTab.equalsIgnoreCase("tab3")) {
if (pager != null) {
pager.setAdapter(new FragmentAdapterCars(getSupportFragmentManager()));
mButton.setVisibility(View.VISIBLE);
TabIcon.setColor(0, mTabLayout, iconCarOpac, iconCar);
}
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (currentTab.equalsIgnoreCase("tab1")) {
TabIcon.setColor(position, mTabLayout, iconSearchOpac, iconSearch);
}
if (currentTab.equalsIgnoreCase("tab2")) {
TabIcon.setColor(position, mTabLayout, iconCommentOpac, iconComment);
}
if (currentTab.equalsIgnoreCase("tab3")) {
TabIcon.setColor(position, mTabLayout, iconCarOpac, iconCar);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearch:
startActivity(new Intent(Navigation.this, SelectCategory.class));
break;
case R.id.btnSelectLocation:
startActivity(new Intent(Navigation.this, SelectLocation.class));
}
}
}
And for my XML is this.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="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:background="#color/colorWhite"
android:fitsSystemWindows="true"
tools:context="com.toweelo.Navigation">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<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"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:weightSum="10">
<LinearLayout android:id="#+id/btnSelectLocation"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5.5"
android:background="#drawable/ripple_effect"
android:clickable="true"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingLeft="8dp">
<com.toweelo.custom.CustomTextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Quezon City"
android:textColor="#color/colorWhite"
android:textSize="#dimen/title"/>
<ImageView
android:layout_width="24dp"
android:layout_height="match_parent"
android:src="#drawable/ic_down_arrow"/>
</LinearLayout>
<RelativeLayout android:layout_width="0dp"
android:layout_height="36dp"
android:layout_marginLeft="10dp"
android:layout_weight="4.5">
<Button android:id="#+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/custom_search_button"
android:drawableRight="#drawable/ic_search"
android:text="Search"
android:textColor="#color/colorWhite"
android:textSize="14dp"/>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tab"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/colorPrimary"
app:pstsDividerColor="#color/colorPrimary"
app:pstsIndicatorColor="#color/colorWhite"
app:pstsIndicatorHeight="3dp"
app:pstsShouldExpand="true"
app:pstsTextAllCaps="false"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/fabMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
fab:menu_animationDelayPerItem="50"
fab:menu_backgroundColor="#B3FFFFFF"
fab:menu_buttonSpacing="0dp"
fab:menu_colorNormal="#DA4336"
fab:menu_colorPressed="#E75043"
fab:menu_colorRipple="#99FFFFFF"
fab:menu_fab_size="normal"
fab:menu_icon="#drawable/fab_add"
fab:menu_labels_colorNormal="#color/colorWhite"
fab:menu_labels_cornerRadius="3dp"
fab:menu_labels_ellipsize="none"
fab:menu_labels_margin="0dp"
fab:menu_labels_maxLines="-1"
fab:menu_labels_padding="8dp"
fab:menu_labels_paddingBottom="4dp"
fab:menu_labels_paddingLeft="8dp"
fab:menu_labels_paddingRight="8dp"
fab:menu_labels_paddingTop="4dp"
fab:menu_labels_position="left"
fab:menu_labels_showShadow="true"
fab:menu_labels_singleLine="false"
fab:menu_labels_textColor="#color/colorBlack"
fab:menu_labels_textSize="14sp"
fab:menu_openDirection="up"
fab:menu_shadowColor="#66000000"
fab:menu_shadowRadius="4dp"
fab:menu_shadowXOffset="1dp"
fab:menu_shadowYOffset="3dp"
fab:menu_showShadow="true"
>
<com.github.clans.fab.FloatingActionButton
android:id="#+id/menuIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_premium"
fab:fab_colorNormal="#color/colorPremium"
fab:fab_colorPressed="#color/colorPremiumDark"
fab:fab_colorRipple="#color/colorPremiumAccent"
fab:fab_label="Premium"
fab:fab_size="mini"
/>
<com.github.clans.fab.FloatingActionButton
android:id="#+id/menuOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_saves"
fab:fab_colorNormal="#color/colorSaves"
fab:fab_colorPressed="#color/colorSavesDark"
fab:fab_colorRipple="#color/colorSavesAccent"
fab:fab_label="Saves"
fab:fab_size="mini"/>
<com.github.clans.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_edit"
fab:fab_colorNormal="#color/colorPrimary"
fab:fab_colorPressed="#color/colorPrimaryDark"
fab:fab_colorRipple="#color/colorAccent"
fab:fab_label="Write"
fab:fab_size="mini"/>
</com.github.clans.fab.FloatingActionMenu>
<TabHost android:id="#+id/tabHost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="52dp"
android:layout_weight="0"
android:showDividers="none"/>
</LinearLayout>
</TabHost>
</android.support.design.widget.CoordinatorLayout>
So I'am wondering if my code will do some errors in the future use?
I mean, will it have out of memory problem since i am using only one viewpager for different adapters.
And for the tab with pagerTabStrip Am i doing it right implemeting it both?
Thanks.
So you are setting a new FragmentPagerAdapter on each TabHost, thus creating 3 new fragments at runtime when the user clicks another tab. This might cause confusion to the user and if the framgments load heavy resources, greater memory consumption and further user annoyance.

Why does my ListView not show any content?

I'm trying to create a NavigationDrawer layout in Android, the main panel is working properly, and the drawer is a ListView. I've tried replacing the ListView with a TextView, which works fine. So, here's my code to add String content to the ListView.
Here is the relevant code.
private ArrayList<String> drawerContentStrings = new ArrayList<>();
private DrawerLayout homeLayout;
private ListView navigationDrawer;
private GridLayout homeContent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
//homeContent = (GridLayout) findViewById(R.id.home_content);
//drawerContentStrings = getResources().getStringArray(R.array.navigation_content_strings);
//homeLayout = (DrawerLayout) findViewById(R.id.home_layout);
navigationDrawer = (ListView) findViewById(R.id.navigation_drawer);
System.out.println(drawerContentStrings.get(0));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.nav_panel_item, drawerContentStrings);
navigationDrawer.setAdapter(arrayAdapter);
}
Here is the code for nav_panel_item (in the layout folder)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="100dp"
android:layout_height="100dp"
android:textColor="#000"
/>
And here is the code for the actual DrawerLayout: (idk if this is even relevant)
<GridLayout
android:id="#+id/home_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:orientation="horizontal"
android:rowCount="2">
<Button
android:id="#+id/button1"
android:layout_gravity="left|top"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_gravity="left|top"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_gravity="left|top"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_gravity="left|top"
android:text="Button" />
</GridLayout>
<ListView android:id="#+id/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#aff"/>
So, how do I make this work? Because I have no idea. Thanks!
In strings.xml, create an array list:
<string-array name="drawer_options">
<item>Test</item>
<item>Test</item>
<item>Test</item>
</string-array>
Create a new layout file, called activity_drawer.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="#+id/nav_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Create a new Activity, called BaseActivity; it will help you to create a NavigationDrawer.
public class BaseActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String[] options;
private TextView titleActionBar;
protected FrameLayout content;
protected void onCreateDrawer(final int layoutResID) {
setContentView(R.layout.activity_drawer);
content = (FrameLayout) findViewById(R.id.content_frame);
getLayoutInflater().inflate(layoutResID, content, true);
setupActionBar();
options = getResources()
.getStringArray(R.array.drawer_options);
drawerLayout =
(DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.nav_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, options));
drawerList.setOnItemClickListener(this);
setupDrawer();
}
private void setupActionBar() {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
titleActionBar = (TextView) findViewById(R.id.title_action_bar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
titleActionBar.setText("AppName");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
titleActionBar.setText("Options");
invalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(mDrawerToggle);
}
//get the position of the clicked list item
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 2:
//For example, I redirect users to LoginActivity when they click on position 2 //(in this case, last item)
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
drawerLayout.closeDrawer(drawerList);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
In MainActivity:
public class HomeActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreateDrawer(R.layout.home_activity);
}
}
home_activity.xml is a "normal" layout file. Example:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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:id="#+id/home_activity"
android:weightSum="1">
<TextView
android:gravity="center"
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:textSize="20dp"
android:textStyle="bold"/>
</LinearLayout>
</ScrollView>
I think it is because your set GridLayout height to match_parent which takes all the screen.
Change your GridLayout height to wrap_content and check.
<GridLayout
android:id="#+id/home_content"
android:layout_width="match_parent"
android:layout_height="match_parent" // <---- change here to wrap_content and see.
android:columnCount="2"
android:orientation="horizontal"
android:rowCount="2">

Categories

Resources