gravity bottom for viewpager children - android

Trying to push TextViews to the bottom of the ViewPager container. Right now the TextViews only stay on the top, both when I wrap_content and match_parent. Here's the XML:
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_above="#+id/layout_view_pager_navigation">
<TextView
android:id="#+id/string_main_intro_1"
android:text="#string/string_main_intro_1"
android:background="#color/buttonColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/textPrimary"
android:textSize="22sp"
android:textAlignment="center"
android:gravity="bottom"/>
<TextView
android:id="#+id/string_main_intro_2"
android:text="#string/string_main_intro_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/textPrimary"
android:textSize="22sp"
android:textAlignment="center"
android:gravity="bottom"/>
<TextView
android:id="#+id/string_main_intro_3"
android:text="#string/string_main_intro_3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/textPrimary"
android:textSize="18sp"
android:textAlignment="center"
android:gravity="bottom"/>
<TextView
android:id="#+id/string_main_intro_4"
android:text="#string/string_main_intro_4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/textPrimary"
android:textSize="18sp"
android:textAlignment="center"
android:gravity="bottom"/>
</android.support.v4.view.ViewPager>

I would suggest creating a different layout for your TextViews like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/my_tvs"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
>
<TextView
android:id="#+id/string_main_intro_1"
android:text="#string/string_main_intro_1"
android:background="#color/buttonColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/textPrimary"
android:textSize="22sp"
android:textAlignment="center"
/>
<TextView
android:id="#+id/string_main_intro_2"
android:layout_below="#+id/string_main_intro_1"
android:text="#string/string_main_intro_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/textPrimary"
android:textSize="22sp"
android:textAlignment="center"
/>
<TextView
android:id="#+id/string_main_intro_3"
android:layout_below="#+id/string_main_intro_2"
android:text="#string/string_main_intro_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/textPrimary"
android:textSize="18sp"
android:textAlignment="center"
/>
<TextView
android:id="#+id/string_main_intro_4"
android:layout_below="#+id/string_main_intro_3"
android:text="#string/string_main_intro_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/textPrimary"
android:textSize="18sp"
android:textAlignment="center"
/>
</RelativeLayout>
Then your ViewPager layout would look like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_above="#+id/layout_view_pager_navigation">
</RelativeLayout>
Then in your Viewpager.java you can add the views:
ViewPager vpp = (ViewPager) findViewById(R.id.pager);
this.addPages(vpp);
RelativeLayout relative = (RelativeLayout) findViewById(R.id.my_tvs);
relative.setGravity(RelativeLayout.ALIGN_BOTTOM);
//ADD ALL PAGES TO TABLAYOUT
private void addPages(ViewPager pager){
MyFragPagerAdapter adapter=new
MyFragPagerAdapter(getSupportFragmentManager());
adapter.addPage(new MyCustomView()); <----------- This will be the layout with your textViews
You will also need a adapter to add the views--
public class MyFragPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> pages=new ArrayList<>();
public MyFragPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return pages.get(position);
}
#Override
public int getCount() {
return pages.size();
}
//ADD A PAGE
public void addPage (Fragment f){
pages.add(f);
}
//SET TITLE FOR TAB
#Override
public CharSequence getPageTitle(int position) {
return pages.get(position).toString();
}
}

Related

recyclerview items not scrolling?

I am making android cv app and I have implemented RecyclerView but items not scrolling even though, I have used RecyclerViewLayout Manager.
below my SkillsAdapter.class
public class SkillsAdapter extends RecyclerView.Adapter {
public List skillList;
public Context context;
public SkillsAdapter(List<Skill> skillList, Context context) {
this.skillList = skillList;
this.context = context;
}
#NonNull
#Override
public SkillsAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context)
.inflate(R.layout.skills_item, parent, false); // change
return new SkillsAdapter.ViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull SkillsAdapter.ViewHolder holder, int position) {
Skill skill = skillList.get(position);
holder.programming.setText(skill.getProgramming());
holder.framework.setText(skill.getFrameworkLibraries());
holder.architecture.setText(skill.getAndroidArchitectureComponents());
holder.software.setText(skill.getSoftwareMethodologies());
holder.ide.setText(skill.getIDES());
}
#Override
public int getItemCount() {
return skillList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView programming, framework, architecture, software, ide;
public ViewHolder(View view) {
super(view);
programming= (TextView) view.findViewById(R.id.programming);
framework = (TextView) view.findViewById(R.id.framework);
architecture = (TextView) view.findViewById(R.id.architecture);
software = (TextView) view.findViewById(R.id.software);
ide = (TextView)view.findViewById(R.id.ide);
}
}
}
below my SkillsItem.java class
public class SkillItem extends AppCompatActivity {
private SkillsAdapter skillsAdapter;
public List<Skill> skillList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.skills);
KitabInterface kitabInterface = ApiClient.getApiService();
Call<KitabSawti> call = kitabInterface.getSkills();
call.enqueue(new Callback<KitabSawti>() {
#Override
public void onResponse(Call<KitabSawti> call, Response<KitabSawti> response) {
skillList= response.body().getSkills();
RecyclerView recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
skillsAdapter = new SkillsAdapter( skillList, SkillItem.this); // changes
recyclerView.setAdapter(skillsAdapter);
}
#Override
public void onFailure(Call<KitabSawti> call, Throwable t) {
}
});
}
}
below my skills_items.xml where I have implemented items
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBlust"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlust"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<ImageView
android:id="#+id/educationImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:src="#drawable/it_skills"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/education_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:text="#string/text_skills"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlust"
android:orientation="horizontal"
android:padding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/subjectImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/programming_skills"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/programming"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:text="#string/programming_skills"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
<View style="#style/dividerHorizontal" />
</RelativeLayout>
</LinearLayout>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlust"
android:orientation="horizontal"
android:padding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/framework_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/it_frameworks"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/framework"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:text="#string/text_framework"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
<View style="#style/dividerHorizontal" />
</RelativeLayout>
</LinearLayout>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlust"
android:orientation="horizontal"
android:padding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/android_component"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/android_components"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/architecture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:text="#string/architecture"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
<View style="#style/dividerHorizontal" />
</RelativeLayout>
</LinearLayout>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlust"
android:orientation="horizontal"
android:padding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/software_method"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/software_methodologies"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/software"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:text="#string/software"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
<View style="#style/dividerHorizontal" />
</RelativeLayout>
</LinearLayout>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlust"
android:orientation="horizontal"
android:padding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/software_ide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/software_ide"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/ide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:text="#string/ides"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
<View style="#style/dividerHorizontal" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
below my skills.xml where I have hosted RecyclerView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
below screenshot
current ui
The problem is, that the root layout of your item (the root LinearLayout at skills_items.xml) has height of match_parent. This messes up the way RecyclerView does the scrolling event calculations.
Also, it is generally advised against using wrap_content for height or width of the root layout for an activity and for wrap_content for height (if scroll is vertical) or width (if scroll is horizontal) of RecyclerView or ListView.

Fragment (Viewpager) inside NestedScrollView not loading

I have Fragment with layout of CoordinatorLayout Inside with NestedScrollView and inside nestedscrollview i have ViewPager and TabLayout with fragments. Tabs is visible but fragment related to tabs is not loading.
Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:background="?android:attr/colorBackground"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!--app:contentScrim="?attr/colorPrimary" -->
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ViewPagerImages"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7"
android:scrollIndicators="top|right">
</android.support.v4.view.ViewPager>
<ProgressBar
android:layout_width="120dp"
android:layout_height="120dp"
android:id="#+id/Progress_Bar_Image"
android:layout_gravity="center_horizontal|center_vertical"
android:visibility="visible"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="right|top"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:background="#drawable/round_default"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="/5"/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_gravity="bottom"
android:padding="15dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/unsa"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/user_S"
android:layout_width="25dp"
android:layout_height="25dp"
android:padding="10dp" />
<TextView
android:id="#+id/ViewPager_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
app:layout_behavior="com.boysjoys.com.pro_working1.CustomClass.UserProfile_Behaviour"
android:textSize="35sp" />
<TextView
android:id="#+id/ViewPager_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Age"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="New Delhi"
android:layout_below="#id/unsa"
android:layout_marginLeft="37dp"
android:id="#+id/ViewPager_City"
/>
<Button
android:id="#+id/Attached"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#drawable/round_button"
android:layout_alignParentBottom="true"
android:elevation="14dp"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="WRITE ME"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</RelativeLayout>
<View
android:id="#+id/fabBGLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/custom_transparent_color1"
android:visibility="gone"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabOne"
android:layout_gravity="bottom|end"
android:padding="12dp"
android:visibility="gone"
android:layout_marginBottom="90dp"
android:layout_marginRight="20dp"
android:layout_width="45dp"
android:layout_height="45dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabTwo"
android:padding="12dp"
app:fabSize="mini"
android:visibility="gone"
android:layout_gravity="bottom|end"
android:layout_marginBottom="90dp"
android:layout_marginRight="20dp"
android:layout_width="45dp"
android:layout_height="45dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabThree"
android:layout_gravity="bottom|end"
android:padding="12dp"
app:fabSize="mini"
android:visibility="gone"
android:layout_marginBottom="90dp"
android:layout_marginRight="20dp"
android:layout_width="45dp"
android:layout_height="45dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabMain"
android:padding="12dp"
android:layout_gravity="bottom|end"
android:layout_marginBottom="90dp"
android:layout_marginRight="20dp"
android:src="#drawable/com_facebook_tooltip_black_xout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
// NESTED SCROLL VIEW WHERE THE PROBLEM ARISE
// TABLAYOUT IS VISIBLE BUT FRAGMENT IS NOT.
<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/userProfile_NestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="13dp"
android:background="#android:color/white"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollViewChild"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="15dp">
<!--To show tab on top of view pager-->
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabTextColor="#color/place_autocomplete_prediction_primary_text_highlight"
app:tabSelectedTextColor="#color/colorPrimary"
app:tabIndicatorColor="#color/colorPrimary"
android:id="#+id/userProfile_Viewpager_Tab">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/userProfile_Viewpager_Tab"
android:id="#+id/userProfile_Viewpager_ViewPager">
</android.support.v4.view.ViewPager>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
ViewPager Adapter
public class UserProfile_TabAdapter extends FragmentPagerAdapter {
String TAG = "###TabAdapter###";
Context context;
public UserProfile_TabAdapter(FragmentManager fm,Context context) {
super(fm);
this.context=context;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
Log.d(TAG," Positions "+position);
switch (position) {
case 0:
fragment = Fragment.instantiate(context,Info_fragment.class.getName());
break;
case 1:
Log.d(TAG, "User Photos Running");
fragment = Fragment.instantiate(context,Photo_fragment.class.getName());
break;
case 2:
Log.d(TAG, "User Connections Running");
fragment = Fragment.instantiate(context,Connections_fragment.class.getName());
break;
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "About";
case 1:
return "Photo";
case 2:
return "Connections";
}
return null;
}
}
Fragment Where i try to load fragment into viewpager
//NESTED SCROLL VIEW TAB LAYOUT AND VIEW PAGER
userInfo_ViewPager=(ViewPager) view.findViewById(R.id.userProfile_Viewpager_ViewPager);
UserProfile_TabAdapter userProfile_tabAdapter=new UserProfile_TabAdapter(getChildFragmentManager(),getActivity());
userInfo_ViewPager.setAdapter(userProfile_tabAdapter);
tabLayout=(TabLayout) view.findViewById(R.id.userProfile_Viewpager_Tab);
tabLayout.setupWithViewPager(userInfo_ViewPager);
Fragment to load into viewpager
public class Info_fragment extends Fragment {
public static final String TAG="### INFO ####";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.userprofile_photos,container,false);
//Above Layout only have colorful background to match parent.
return view;
}
}
Try this
NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.userProfile_NestedScrollView);
scrollView.setFillViewport (true);
Add This Line in your in NestedScrollView
android:fillViewport="true"
// your appBar layout height is match_parent change it with wrap_content
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
The problem is your view pager height. Just make some height then your page will be visible.
Example:
<android.support.v4.view.ViewPager
android:id="#+id/userProfile_Viewpager_ViewPager"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_below="#id/userProfile_Viewpager_Tab">
</android.support.v4.view.ViewPager>
You have to find the solution why the view pager not taken the height
or
NestedScrollView scrollView = (NestedScrollView) findViewById (R.id.userProfile_NestedScrollView);
scrollView.setFillViewport (true);
I have same issue and the best solution is to set hight for your viewpager
Example:
android:layout_height="500dp"

How to create main activity with list of ImageButton

I'm building an Android app.
In my first activity I want to create a list of button like this:
So I have build this code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#e3e3e3"
android:gravity="center_horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="15dp"
>
<ImageButton
android:id="#+id/ButtonArticoli"
android:layout_width="150px"
android:layout_height="150px"
android:onClick="visualizzaArticoli"
android:scaleType="fitCenter"
android:src="#drawable/articoli"
/>
<TextView
android:id="#+id/textViewArticoli"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/articoli"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200px"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="15dp"
>
<ImageButton
android:id="#+id/ImageButtonCreateOrder"
android:layout_width="150px"
android:layout_height="150px"
android:onClick="creaOrdine"
android:src="#drawable/order"/>
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/ordini"
android:gravity="center" />
</LinearLayout>
<!-- Prima Riga -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200px"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="15dp"
>
<ImageButton
android:id="#+id/ImageButtonClienti"
android:layout_width="150px"
android:layout_height="150px"
android:onClick="viewClienti"
android:scaleType="fitCenter"
android:src="#drawable/customer"/>
<TextView
android:id="#+id/textView23"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/cliente"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200px"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="15dp"
>
<ImageButton
android:id="#+id/ImageButtonAlignDatabase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="allineaDatase"
android:scaleType="fitCenter"
android:src="#drawable/sincronizza"/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/allinea_database"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200px"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="15dp"
>
<ImageButton
android:id="#+id/ImageButtonSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setting"
android:scaleType="fitCenter"
android:src="#drawable/setting"/>
<TextView
android:id="#+id/TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/impostazioni"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
But the result of my app when I try yo start my application is this:
Can we help me?
Use this code if you have a fixed size of elements.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#F1C983">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
card_view:cardBackgroundColor="#D9EE9A">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:background="#null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
card_view:cardBackgroundColor="#D9EE9A">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:background="#null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
card_view:cardBackgroundColor="#D9EE9A">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:background="#null"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Use compile 'com.android.support:cardview-v7:25.0.0' to get the CardView.
this is the sample output.
use ListView or RecyclerView with your custom list item which will contain image buttons and custom adapter
Instead of px use dp it´s much better!
If you want a fixed list, your XML is correct, but if you want a dynamic list, you should use ListView or RecycleView , as the others users said.
The weight in your LinearLayout is wrong, weight should be used when you desires your screen to fit in every screen, no matter the size ( and you will need to "distribute" your weigth throught the children´s).
To make your screen similar to the first image, you need to set a background in your second LinearLayout
Okey this a recyclerview for what you want,
public class MainPageAdapter extends RecyclerView.Adapter<MainPageAdapter.ViewHolder> {
private #DrawableRes int[] iconDrawables;
private String[] texts;
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.img_lockIcon.setImageResource(iconDrawables[position]);
holder.txt_whatever.setText(texts[position]);
}
#Override
public int getItemCount() {
return iconDrawables != null ? iconDrawables.length : 0;
}
public void setData(#DrawableRes int[] iconDrawables, String[] texts){
this.iconDrawables = iconDrawables;
this.texts = texts;
this.notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView img_icon;
TextView txt_whatever;
ImageView img_lockIcon;
public ViewHolder(View itemView) {
super(itemView);
img_icon = itemView.findViewById(R.id.item_img_icon);
txt_whatever = itemView.findViewById(R.id.item_txt_whatever);
img_lockIcon = itemView.findViewById(R.id.item_img_lockIcon);
}
}
}
this is R.layout.item,
<?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="wrap_content"
android:layout_margin="8dp"
android:padding="8dp"
android:background="#color/colorAccent"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/item_img_icon"
android:layout_width="36dp"
android:layout_height="36dp"
tools:src="#mipmap/ic_launcher"/>
<TextView
android:id="#+id/item_txt_whatever"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textColor="#android:color/white"
tools:text="YouTube"/>
<ImageView
android:id="#+id/item_img_lockIcon"
android:layout_width="40dp"
android:layout_height="40dp"
tools:src="#mipmap/ic_launcher"/>
</LinearLayout>
and below is MainActivity layout,
<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="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp"
app:cardElevation="0dp"
app:cardBackgroundColor="#color/colorPrimary"
android:layout_margin="8dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/item"/>
</android.support.v7.widget.CardView>
</FrameLayout>
and below is MainActivity,
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
MainPageAdapter adapter = new MainPageAdapter();
recyclerView.setAdapter(adapter);
//Set your data
//adapter.setData();
}
}
You can change color as you want and also notify that ı used two different arrays for adapter but you can use pojo classes also. Hope this helps you.

I want current layout to be changed so that I can scroll entire layout.

<LinearLayout
android:id="#+id/layout_question_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
>
<Button
android:id="#+id/btn_question_backButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/backicon_round"/>
<TextView
android:id="#+id/text_question_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#color/color_green"
android:text="Error"
android:textSize="19dp"
/>
</RelativeLayout>
<ImageView
android:id="#+id/image_question_detail_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:id="#+id/text_question_detail_userComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:layout_marginBottom="25dp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3">
<ListView
android:id="#+id/list_question_detail_expComments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"></ListView>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Now, I set layout for title on the top. Then, a Imageview, a textview and a listview follow below this layout, and only 1 textview is inside listview.
listview can vary in size.
The problem is, if the size of the listview is very big, I can only scroll the screen assigned to listview.
But, I want scroll the entire screen.
To solve this problem, I added scrollview outside of the first linearlayout.
However, It didn't work.(the imageview disappeared)
What can I do?
you can make your listview addheaderview,and put your imageview and textview into headerview
yourlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout_question_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp">
<Button
android:id="#+id/btn_question_backButton"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="#+id/text_question_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Error"
android:textColor="#000000"
android:textSize="19dp"
/>
</RelativeLayout>
<ListView
android:id="#+id/list_question_detail_expComments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"></ListView>
lv_header.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="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/image_question_detail_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/text_question_detail_userComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:textSize="20dp" />
Activity.class
public class MainActivity extends AppCompatActivity {
private ListView list_question_detail_expComments;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_question_detail_expComments = (ListView) findViewById(R.id.list_question_detail_expComments);
View lvHeaderView = View.inflate(this,R.layout.lv_header,null);
list_question_detail_expComments.addHeaderView(lvHeaderView);
list_question_detail_expComments.setAdapter(new LvAdapter());
}
private class LvAdapter extends BaseAdapter{
#Override
public int getCount() {
return 20;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
TextView tv = new TextView(MainActivity.this);
tv.setText("test"+i);
return tv;
}
}
}

Fragment inside ViewPager doesn't scroll

I have an Activity that have an appBarLayout with a TabLayout that should cover the 2/5 of the screen, so it's a big AppBarLayout. I created it, made the tabLayout and configured the viewPager and it is working, but there are three with issues that I can't figure out how to resolve:
Without this app:layout_behavior="#string/appbar_scrolling_view_behavior" the viewpager shows beside the appBarLayout
Whenever I enter in any of the editTexts the AppBarLayout shows fullscreen.
The second fragment has big height and should scroll down, but the it doesn't.
Relevant code:
Activity with the appBarLayout and the viewPager
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rootLayout"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
>
<TextView
android:id="#+id/title"
android:textSize="17sp"
android:text="#string/app_name"
android:textColor="#color/green_900"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="#drawable/logo_home"
android:fitsSystemWindows="true"
/>
<TextView
android:id="#+id/txt_version"
android:text="#string/app_version"
android:textColor="#color/white"
android:textSize="10sp"
android:layout_below="#+id/img_logo"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabMode="fixed"
app:layout_collapseMode="pin"
app:tabIndicatorHeight="3dp"
app:tabIndicatorColor="#color/light_green_a700"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/white_background" />
</android.support.design.widget.CoordinatorLayout>
The second page (Tab) that should scroll:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#color/white_background"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="23dp"
android:background="#color/white_background">
<Button
android:id="#+id/btn_facebook_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/facebook_register"
android:drawableLeft="#drawable/fb_logo"
android:textSize="15sp"
android:padding="5dp"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:textColor="#color/white"
android:background="#drawable/button_facebook_shape"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_marginTop="20dp"
android:id="#+id/white_line"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="0.5dp"/>
<View
android:background="#color/login_hint"
android:layout_width="match_parent"
android:layout_below="#+id/white_line"
android:layout_height="0.5dp"/>
<TextView
android:layout_marginTop="20dp"
android:textColor="#color/login_hint"
android:background="#color/white_background"
android:layout_width="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="o con tu mail..."/>
</RelativeLayout>
<EditText
android:id="#+id/edt_register_name"
android:layout_width="match_parent"
android:background="#drawable/edit_text_shape"
android:padding="20dp"
android:textSize="15sp"
android:maxLines="1"
android:inputType="textCapWords"
android:imeOptions="actionNext"
android:textColorHint="#color/login_hint"
android:hint="#string/txt_register_name"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edt_register_lastname"
android:layout_marginTop="15dp"
android:padding="20dp"
android:textSize="15sp"
android:maxLines="1"
android:inputType="textCapWords"
android:imeOptions="actionNext"
android:background="#drawable/edit_text_shape"
android:layout_width="match_parent"
android:textColorHint="#color/login_hint"
android:hint="#string/txt_register_last_name"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edt_register_email"
android:layout_marginTop="15dp"
android:padding="20dp"
android:textSize="15sp"
android:maxLines="1"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:background="#drawable/edit_text_shape"
android:layout_width="match_parent"
android:textColorHint="#color/login_hint"
android:hint="#string/txt_register_email"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edt_register_pass"
android:layout_marginTop="15dp"
android:padding="20dp"
android:textSize="15sp"
android:maxLines="1"
android:imeOptions="actionNext"
android:inputType="textPassword"
android:background="#drawable/edit_text_shape"
android:layout_width="match_parent"
android:textColorHint="#color/login_hint"
android:hint="#string/txt_register_pass"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edt_register_pss_conf"
android:layout_marginTop="15dp"
android:padding="20dp"
android:textSize="15sp"
android:maxLines="1"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:background="#drawable/edit_text_shape"
android:layout_width="match_parent"
android:textColorHint="#color/login_hint"
android:hint="#string/txt_register_pass_confirm"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="15dp"
android:id="#+id/btn_register_done"
android:text="#string/btn_register_done"
android:drawableLeft="#drawable/kick"
android:paddingLeft="30dp"
android:textColor="#color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_done_shape"/>
</LinearLayout>
</ScrollView>
The java code for the Activity and the viewPager
public class LoginRegisterActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new myFragmentPagerAdapter(getSupportFragmentManager(), LoginRegisterActivity.this));
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
public class myFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;
private String tabTitles[] = new String[]{"Ingresar", "Registrarse"};
private Context context;
public myFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return LoginFragment.newInstance(position);
case 1:
return RegisterFragment.newInstance(position);
default:
return null;
}
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}

Categories

Resources