Why my GridView cuts off at The End? - java

In the above picture, the item at the bottom is cut off like a red arrow.
Perhaps the size of the Gridview is the entire screen, and NavigationBottomView seems to cover part of the GridView. I want to specify the height of the GridView from the top of the screen to the top of the BottomNavigationView.
Below is my xml code.
1> Home screen xml code with NavigationBottomView(home.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".home">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="520dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_gravity="left|center_vertical"
android:text="All Of "
android:textColor="#color/colorPrimaryDark"
android:textSize="60sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="196dp"
android:layout_centerInParent="true"
android:layout_gravity="left|center_vertical"
android:text="G"
android:textColor="#8e1414"
android:textSize="60dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="240dp"
android:layout_gravity="left|center_vertical"
android:text="IST"
android:textColor="#color/colorPrimaryDark"
android:textSize="60dp"
android:textStyle="bold"/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
2> Xml code using GridView(site_list.xml)
<?xml version="1.0" encoding="utf-8"?>
<GridView 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#FFFFFF"
tools:context=".home"
android:id="#+id/gridView"
android:columnWidth="96dp"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
3> The xml code that designed the items in the GridView(list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<ImageView
android:layout_width="144dp"
android:layout_height="144dp"
android:id="#+id/image"
tools:src="#mipmap/ic_launcher"
android:layout_gravity="center_vertical"
/>
<TextView
android:layout_width="144dp"
android:layout_height="wrap_content"
android:id="#+id/name"
tools:text="Gistsite"
android:textAppearance="?android:textAppearanceMedium"
android:textStyle="bold"
android:gravity="center_horizontal"
android:textSize="16sp"
/>
</LinearLayout>
4> The class that created the data type named Site(Site.java)
public class Site {
private String msite_name;
private String msite_url;
private int msite_imagesource;
public Site(String name, String url, int imagesource){
msite_name = name;
msite_url = url;
msite_imagesource = imagesource;
}
public String getMsite_name() {
return this.msite_name;
}
public String getMsite_url(){
return this.msite_url;
}
public int getMsite_imagesource(){
return this.msite_imagesource;
}
}
5> SideAdapter code created by customizing an ArrayAdapter(SiteAdapter.java)
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class SiteAdapter extends ArrayAdapter<Site> {
private Site currentSite;
public SiteAdapter(Activity context, ArrayList<Site> sites){
super(context,0,sites);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null){
listItemView =
LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
currentSite = getItem(position);
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
imageView.setImageResource(currentSite.getMsite_imagesource());
TextView name = (TextView) listItemView.findViewById(R.id.name);
name.setText(currentSite.getMsite_name());
return listItemView;
}
}
6> A fragment code indicating the capture screen attached above(OrganizationFragment.java)
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;
public class OrganizationFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
View organizationView = inflater.inflate(R.layout.site_list, null);
final ArrayList<Site> organizations = new ArrayList<Site>();
organizations.add(new Site("Gist 총학생회", "https://www.facebook.com/gistunion/",R.drawable.gistunion));
organizations.add(new Site("Gist 동아리연합회", "https://www.facebook.com/gistclubunite/",R.drawable.clubnight));
organizations.add(new Site("Gist 하우스", "https://www.facebook.com/GISTcollegeHOUSE/",R.drawable.gisthouse));
organizations.add(new Site("Gist 문화행사위원회", "https://www.facebook.com/Moonhangwe/",R.drawable.moonhangwe));
organizations.add(new Site("Gist 신문", "https://www.facebook.com/pg/GistSinmoon/posts/",R.drawable.gistnews));
organizations.add(new Site("Gist 홍보대사", "http://blog.naver.com/PostList.nhn?blogId=gist1993&from=postList&categoryNo=28",R.drawable.gionnare));
SiteAdapter organizationAdapter = new SiteAdapter(getActivity(), organizations);
GridView gridViewO =
(GridView)organizationView.findViewById(R.id.gridView);
gridViewO.setAdapter(organizationAdapter);
gridViewO.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Site site = organizations.get(position);
openWebPage(site.getMsite_url());
}
});
return organizationView;
}
public void openWebPage(String url){
Uri webpage = Uri.parse(url);
Intent siteMove = new Intent(Intent.ACTION_VIEW, webpage);
if(siteMove.resolveActivity(getActivity().getPackageManager()) != null)
startActivity(siteMove);
}
}
7> Java code representing the main home screen(home.java)
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;
public class home extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_officials:
selectedFragment = new OfficialFragment();
break;
case R.id.navigation_organizations:
selectedFragment = new OrganizationFragment();
break;
case R.id.navigation_circles:
selectedFragment = new CircleFragment();
break;
case R.id.navigation_projects:
selectedFragment = new ProjectFragment();
break;
}
return loadFragment(selectedFragment);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
loadFragment(new HomeFragment());
BottomNavigationView navigation = (BottomNavigationView)
findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener
(mOnNavigationItemSelectedListener);
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment).commit();
return true;
}
return false;
}
}
I would be grateful if you could read the questions I have made and give them some advice.

Its happening because you have set the FrameLayout height 520dp in xml
set the frameLayout height match and also use android:layout_above="#+id/navigation"
try this code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/navigation"
android:orientation="horizontal">
<OtherViews.../>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>

Related

Cannot see widgets in tab layout in android studio

I am facing the problem of cannot see widgets in tab layouts. I have created textview and button in Overview Fragment but I cannot see them when run code. Is it right to initialize textviews and buttons in oncreateview() of fragment which we don't want to transfer to another tab. I have searched a lot about my problem but cannot get any hint. Code is given below:
package com.example.progluattempt;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
public class GlucosePlotterTips extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glucose_plotter_tips);
TabLayout Tablayout1 = findViewById(R.id.Tablayout1);
TabItem OverviewTab = findViewById(R.id.Overviewtab);
TabItem HistoryTab = findViewById(R.id.Historytab);
TabItem TipsTab = findViewById(R.id.Tipstab);
ViewPager Viewpager1 = findViewById(R.id.viewpagery1);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Tablayout1.getTabCount());
Viewpager1.setAdapter(pagerAdapter);
Tablayout1.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
Viewpager1.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
//PgeAdapter
package com.example.progluattempt;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter {
private int numoftabs;
public PagerAdapter(FragmentManager fm, int numoftabs){
super(fm);
this.numoftabs = numoftabs;
}
#NonNull
#Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new OverviewFragment();
case 1:
return new HistoryFragment();
case 2:
return new TipsFragment();
default:
return null;
}
}
#Override
public int getCount() {
return numoftabs;
}
}
//Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GlucosePlotterTips">
<com.google.android.material.tabs.TabLayout
android:id="#+id/Tablayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.004">
<com.google.android.material.tabs.TabItem
android:id="#+id/Overviewtab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overview" />
<com.google.android.material.tabs.TabItem
android:id="#+id/Historytab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="History" />
<com.google.android.material.tabs.TabItem
android:id="#+id/Tipstab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tips" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpagery1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
//fragmentOverview
<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"
tools:context=".OverviewFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="183dp"
android:orientation="vertical">
<TextView
android:id="#+id/textViewov2"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="#string/overview1"
android:textColor="#color/black"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textViewov3"
android:layout_width="match_parent"
android:layout_height="46dp"
android:text="TextView"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
<Button
android:id="#+id/buttonov2"
android:layout_width="277dp"
android:layout_height="93dp"
android:layout_gravity="center"
android:text="#string/planner"
android:textColor="#color/black"
android:textSize="24sp"
app:backgroundTint="#color/Yellow" />
//OverviewFragment
package com.example.progluattempt;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class OverviewFragment extends Fragment {
TextView Results, Condition;
Button Mybuttonov2;
public OverviewFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_overview, container, false);
Results = (TextView) view.findViewById(R.id.textViewov2);
Intent i =getActivity().getIntent();
String LatestReading=i.getStringExtra("TimeofReading");
Results.setText("Last Checked: " +LatestReading);
//type
Condition = (TextView) view.findViewById(R.id.textViewov3);
String TypeofDiab = i.getStringExtra("Concentration");
Condition.setText("" +TypeofDiab);
//Button
Mybuttonov2 = (Button) view.findViewById(R.id.buttonov2);
//to display conditon of user
if(Condition.getText() != null){
String num;
int numi=0;
num = Condition.getText().toString();
try{
numi = Integer.parseInt(num);
}catch(NumberFormatException ex){
}
if(numi <= 70){
System.out.println("Hypoglycemia");}
else if(numi >=70 & numi <= 140){
System.out.println("Fine");}
else if(numi>140 & numi <200){
System.out.println("Prediabetes");}
else {
System.out.println("Hyperglycemia");
}
}
//to add click event for button
Mybuttonov2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i3 = new Intent(getActivity(), SensorScreen.class );
startActivity(i3);
}
});
return view;
}
}
Logcat
When run emulator

How to avoid the softkeyboard overlapping the EditText in fragment?

I try both methods addling line android:windowSoftInputMode="adjustPan" in manifest or adding getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);on Fragment onCreateView but still facing the same issue. That when i type in the my custom EditText the screen does not auto resize.
here is my fragment:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:fillViewport="true"
tools:context=".Fragments.HelpersFrag.UseAvailablePackage.AddListBasicFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical">
<include layout="#layout/firstpage_indicator_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="24dp"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="#string/pencil_icon"
android:src="#drawable/pencil_icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="#style/TextViewTitles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/basic" />
<include
android:id="#+id/edt_jobTitle"
layout="#layout/custom_edittext" />
<include
android:id="#+id/cbAceptCreditCard"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbCoupons"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbSos"
layout="#layout/checkbox_layout" />
<include
android:id="#+id/cbNonSmoker"
layout="#layout/checkbox_layout" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
My Custom EditText:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:are="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="15dp"
android:background="#drawable/boundaries">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottombar"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:padding="5dp">
<com.chinalwb.are.AREditText
android:id="#+id/arEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:hint="#string/job_description" />
</ScrollView>
<LinearLayout
android:id="#+id/bottombar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:padding="5dp"
android:orientation="horizontal"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:weightSum="1000"
tools:ignore="RtlSymmetry">
<com.chinalwb.are.styles.toolbar.ARE_ToolbarDefault
android:id="#+id/areToolbar"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1000"
android:gravity="center_vertical"/>
<ImageButton
android:id="#+id/btnUnlink"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_unlink"
android:scaleType="fitCenter"
android:src="#drawable/unlink_icon" />
<ImageButton
android:id="#+id/btnUndo"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_undo"
android:scaleType="fitCenter"
android:src="#drawable/undo_icon" />
<ImageButton
android:id="#+id/btnRedo"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="11dp"
android:layout_marginLeft="11dp"
android:background="#color/transparent_color"
android:contentDescription="#string/text_redo"
android:scaleType="fitCenter"
android:src="#drawable/redo_icon" />
</LinearLayout>
</RelativeLayout>
checkbox
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
android:fontFamily="#font/raleway_bold"
android:text="#string/financial_planning"
android:textColor="#color/texthint_color"
android:textSize="13sp"
android:padding="10dp"
android:theme="#style/CheckBox" />
AddListFragment.java
package com.blendev.niffty.Fragments.HelpersFrag.UseAvailablePackage;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.blendev.niffty.Constants.Constants;
import com.blendev.niffty.Fragments.BaseFragment;
import com.blendev.niffty.R;
import com.chinalwb.are.AREditText;
import com.chinalwb.are.styles.toolbar.IARE_Toolbar;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Bold;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Italic;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Link;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_ListBullet;
import com.chinalwb.are.styles.toolitems.ARE_ToolItem_ListNumber;
import com.chinalwb.are.styles.toolitems.IARE_ToolItem;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import ren.qinc.edit.PerformEdit;
public class AddListBasicFragment extends BaseFragment {
Button btnNext;
CheckBox cbCreditCard, cbCoupons, cbSos, cbNonSmoker;
View view;
private IARE_Toolbar mToolbar;
private AREditText mEditText;
PerformEdit mPerformEdit;
EditText edtTitle;
ImageButton btnUndo, btnRedo, btnUnlink;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
String selectedText;
private String mParam1;
private String mParam2;
List arrayList;
public AddListBasicFragment() {
}
public static AddListBasicFragment newInstance(String param1, String param2) {
AddListBasicFragment fragment = new AddListBasicFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_add_list_basic, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
initView();
initToolbar();
return view;
}
void initView() {
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
toolbar.setTitle("");
arrayList = new ArrayList<String>();
ImageButton btnClose = (ImageButton) view.findViewById(R.id.btnClose);
edtTitle = view.findViewById(R.id.edt_jobTitle);
btnNext = view.findViewById(R.id.btnNext_id);
cbCreditCard = view.findViewById(R.id.cbAceptCreditCard);
cbCoupons = view.findViewById(R.id.cbCoupons);
cbSos = view.findViewById(R.id.cbSos);
cbNonSmoker = view.findViewById(R.id.cbNonSmoker);
btnUndo = view.findViewById(R.id.btnUndo);
btnRedo = view.findViewById(R.id.btnRedo);
btnUnlink = view.findViewById(R.id.btnUnlink);
cbCreditCard.setText(R.string.cbAcceptCreditCard);
cbCoupons.setText(R.string.cbCoupons);
cbSos.setText(R.string.cbSosHelper);
cbNonSmoker.setText(R.string.cbNonSmoker);
edtTitle.setHint(R.string.job_title);
btnClose.setOnClickListener(this);
btnNext.setOnClickListener(this);
btnUndo.setOnClickListener(this);
btnRedo.setOnClickListener(this);
btnUnlink.setOnClickListener(this);
}
private void initToolbar() {
mToolbar = view.findViewById(R.id.areToolbar);
IARE_ToolItem bold = new ARE_ToolItem_Bold();
IARE_ToolItem italic = new ARE_ToolItem_Italic();
IARE_ToolItem listNumber = new ARE_ToolItem_ListNumber();
IARE_ToolItem listBullet = new ARE_ToolItem_ListBullet();
IARE_ToolItem link = new ARE_ToolItem_Link();
mToolbar.addToolbarItem(bold);
mToolbar.addToolbarItem(italic);
mToolbar.addToolbarItem(listNumber);
mToolbar.addToolbarItem(listBullet);
mToolbar.addToolbarItem(link);
mEditText = view.findViewById(R.id.arEditText);
mEditText.setTextSize(13);
mEditText.setToolbar(mToolbar);
mPerformEdit = new PerformEdit(mEditText);
bold.getStyle().getImageView().setImageResource(R.drawable.bold_icon);
bold.getStyle().getImageView().setScaleType(ImageView.ScaleType.CENTER_INSIDE);
italic.getStyle().getImageView().setImageResource(R.drawable.italic_icon);
italic.getStyle().getImageView().setScaleType(ImageView.ScaleType.CENTER_INSIDE);
try {
listNumber.getStyle().getImageView().setImageResource(R.drawable.numbering_icon);
listBullet.getStyle().getImageView().setImageResource(R.drawable.bullet_icon);
link.getStyle().getImageView().setImageResource(R.drawable.link_icon);
} catch (Exception e) {
Log.e("ERROR: ", e.toString());
}
}
#SuppressLint("NonConstantResourceId")
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNext_id:
String jobTitle = edtTitle.getText().toString();
String jobDescription = mEditText.getText().toString();
validateInputs(jobTitle, jobDescription);
break;
case R.id.btnClose:
getActivity().onBackPressed();
break;
case R.id.btnUndo:
mPerformEdit.undo();
break;
case R.id.btnRedo:
mPerformEdit.redo();
break;
case R.id.btnUnlink:
break;
}
}
void validateInputs(String jobTitle, String jobDescription) {
if (jobTitle.isEmpty() || jobDescription.isEmpty()) {
Log.d("TAG", "String field empty");
Snackbar snackBar = Snackbar.make(getActivity().findViewById(android.R.id.content),
"Please fill the requirements....", Snackbar.LENGTH_LONG);
snackBar.show();
} else {
Bundle bundle = new Bundle();
bundle.putString(Constants.JOB_TITLE, jobTitle);
bundle.putString(Constants.JOB_DESCRIPTION, jobDescription);
arrayList.clear();
if (cbCreditCard.isChecked()) {
arrayList.add(cbCreditCard.getText());
}
if (cbCoupons.isChecked()) {
arrayList.add(cbCoupons.getText());
}
if (cbSos.isChecked()) {
arrayList.add(cbSos.getText());
}
if (cbNonSmoker.isChecked()) {
arrayList.add(cbNonSmoker.getText());
}
Log.d("TAG", "List size: " + arrayList.size());
Fragment imgFileFragment = new ImagesFilesFragment();
imgFileFragment.setArguments(bundle);
this.changeFragment(imgFileFragment, Constants.ADD_LIST_BASIC_FRAGMENT);
}
}
}
Manifest
<activity
android:name=".Activities.MainScreenActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan"/>

How do i reset a Card Stack or get the Previous Card?

So, I have followed this tutorial (https://www.youtube.com/watch?v=cS5l_hPwC10) on how to do a card stack and it works fine even with 2 cardstacks at the same time. now I want to add a button so i can reset both stacks and/or a button to get to the previous card
Here is all the code I have related to the card stack. Tell me if you need more since this is the first time I am trying to programm something
hauptanwendung_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentHauptanwendung">
<com.wenchao.cardstack.CardStack
android:id="#+id/card_stack"
android:layout_width="280dp"
android:layout_height="170dp"
android:clipChildren="false"
android:clipToPadding="false" />
<com.wenchao.cardstack.CardStack
android:id="#+id/card_stack2"
android:layout_width="280dp"
android:layout_height="170dp"
android:clipChildren="false"
android:clipToPadding="false" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/stern"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
app:srcCompat="#drawable/ic_stern02" />
<Space
android:layout_width="50sp"
android:layout_height="match_parent"
android:layout_marginRight="15sp"
android:layout_weight="1" />
<ImageButton
android:id="#+id/stift"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
app:srcCompat="#drawable/ic_bearbeiten01" />
</LinearLayout>
</LinearLayout>
card_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center" />
</LinearLayout>
card_layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_content2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center" />
</LinearLayout>
FragmentHauptanwendung.java
package com.example.ideenfinder3;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.wenchao.cardstack.CardStack;
import java.util.Objects;
public class FragmentHauptanwendung extends Fragment implements CardStack.CardEventListener {
View view;
private CardsDataAdapter card_adapter;
private CardsDataAdapter2 card_adapter2;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hauptanwendung_fragment, container, false);
initImages();
initImages2();
CardStack card_stack = (CardStack) view.findViewById(R.id.card_stack);
card_stack.setContentResource(R.layout.card_layout);
card_stack.setStackMargin(20);
card_stack.setAdapter(card_adapter);
card_stack.setListener(this);
CardStack card_stack2 = (CardStack) view.findViewById(R.id.card_stack2);
card_stack2.setContentResource(R.layout.card_layout2);
card_stack2.setStackMargin(20);
card_stack2.setAdapter(card_adapter2);
card_stack2.setListener(this);
return view;
}
private void initImages2() {
card_adapter2 = new CardsDataAdapter2(Objects.requireNonNull(getActivity()).getApplicationContext(),0);
card_adapter2.add(R.drawable.logo);
card_adapter2.add(R.drawable.schriftzug);
}
private void initImages() {
card_adapter = new CardsDataAdapter(Objects.requireNonNull(getActivity()).getApplicationContext(),0);
card_adapter.add(R.drawable.bild01);
card_adapter.add(R.drawable.bild02);
card_adapter.add(R.drawable.bild03);
card_adapter.add(R.drawable.bild04);
card_adapter.add(R.drawable.bild05);
}
#Override
public boolean swipeEnd(int i, float v) {
return v > 300;
}
#Override
public boolean swipeStart(int i, float v) {
return false;
}
#Override
public boolean swipeContinue(int i, float v, float v1) {
return false;
}
#Override
public void discarded(int i, int i1) {
}
#Override
public void topCardTapped() {
}
}
CardsDataAdapter
package com.example.ideenfinder3;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class CardsDataAdapter extends ArrayAdapter<Integer> {
public CardsDataAdapter(Context context, int resource) {
super(context, resource);
}
#Override
public View getView(int position, final View ImgcontentView, ViewGroup parent){
ImageView ImgV = (ImageView) (ImgcontentView.findViewById(R.id.image_content));
ImgV.setImageResource(getItem(position));
return ImgcontentView;
}
}
CardsDataAdapter2
package com.example.ideenfinder3;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class CardsDataAdapter2 extends ArrayAdapter<Integer> {
public CardsDataAdapter2(#NonNull Context context, int resource) {
super(context, resource);
}
#Override
public View getView(int position, final View ImgcontentView2, ViewGroup parent){
ImageView ImgV2 = (ImageView) (ImgcontentView2.findViewById(R.id.image_content2));
ImgV2.setImageResource(getItem(position));
return ImgcontentView2;
}
}
Just keep handles to CardStack instead:
private CardStack cardStack01;
private CardStack cardStack02;
And let the Fragment implement getters and setters for these.

make same fab Transformation exapnd behavior like shown at material.io

I wanna have same fab transformation behavior as shown in
Material Website in Full Screen (or morph) section.
I have tried default FabTransformationScrimBehavior and FabTransformationSheetBehavior. but scrim behavior doesn't do anything and sheet behavior don't animate just like I wanted and also it reduce the elevation of the fab to value 0f. and I haven't found any way to unexpand fab. and I wanna know how to use fab transformation behavior. Actually I didn't find any doc on how to use it. So Please help
Code
This is the code that I used:
this is the code of first activity:-
final FloatingActionButton actionButton = findViewById(R.id.add_fab);
actionButton.setOnClickListener(view -> {
actionButton.setExpanded(!actionButton.isExpanded());
});
And this is the xml of first activity:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<include layout="#layout/include_generic_toolbar" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="76dp"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
tools:listitem="#layout/media_list_item_card" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:includeFontPadding="false"
android:text="Add Media"
android:textAllCaps="false"
android:visibility="visible"
app:srcCompat="#drawable/ic_add_24"/>
<com.google.android.material.circularreveal.CircularRevealFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
app:layout_behavior="com.google.android.material.transformation.FabTransformationSheetBehavior"
>
<fragment
android:id="#+id/fab_transformation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.zimong.ssms.fragments.AddMediaFragment"
/>
</com.google.android.material.circularreveal.CircularRevealFrameLayout>
And the code of AddMediaFragment(second screen):-
package com.zimong.ssms.fragments;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.DialogFragment;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.zimong.ssms.R;
import com.zimong.ssms.common.model.User;
import com.zimong.ssms.extended.CallbackHandlerImpl;
import com.zimong.ssms.model.Source;
import com.zimong.ssms.model.ZResponse;
import com.zimong.ssms.service.AppService;
import com.zimong.ssms.service.ServiceLoader;
import com.zimong.ssms.util.Constants;
import com.zimong.ssms.util.Util;
import java.util.Calendar;
import java.util.Date;
import retrofit2.Call;
import retrofit2.Response;
public class AddMediaFragment extends DialogFragment {
private ArrayAdapter<Source> adapter;
private TextView source;
private int checkedItem = 0;
private TextView mediaDate;
private Date currentSelectedDate;
public static AddMediaFragment newInstance() {
AddMediaFragment fragment = new AddMediaFragment();
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
return fragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.AppTheme_Light);
setHasOptionsMenu(true);
}
private void getSources() {
final User user = Util.getUser(getActivity());
AppService service = ServiceLoader.createService(AppService.class);
Call<ZResponse> call = service.mediaGallerySources(Constants.DEFAULT_PLATFORM, user.getToken());
call.enqueue(new CallbackHandlerImpl<Source[]>(getActivity(), true, true, Source[].class) {
#Override
protected void failure(Throwable t) {
}
#Override
protected void failure(Response<ZResponse> response) {
}
#Override
protected void success(Source[] response) {
if (response.length > 0) {
source.setText(response[0].getName());
}
adapter = new ArrayAdapter<Source>(getContext(), R.layout.dialog_singlechoice_item, response) {
#Override
public long getItemId(int position) {
return getItem(position).getPk();
}
};
}
});
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_media, container, false);
TextView headline = view.findViewById(R.id.media_headline);
Toolbar toolbar = view.findViewById(R.id.toolbar);
((AppCompatActivity) getContext()).setSupportActionBar(toolbar);
source = view.findViewById(R.id.source);
mediaDate = view.findViewById(R.id.media_date);
Calendar fromDateInstance = Calendar.getInstance();
currentSelectedDate = fromDateInstance.getTime();
mediaDate.setText(Util.formatDate(fromDateInstance.getTime(), "dd MMM yyyy"));
final DatePickerDialog fromDatePicker = new DatePickerDialog(getContext(), (datePicker, year, month, date) -> {
fromDateInstance.set(Calendar.YEAR, year);
fromDateInstance.set(Calendar.MONTH, month);
fromDateInstance.set(Calendar.DATE, date);
currentSelectedDate = fromDateInstance.getTime();
mediaDate.setText(Util.formatDate(currentSelectedDate, "dd MMM yyyy"));
}, fromDateInstance.get(Calendar.YEAR), fromDateInstance.get(Calendar.MONTH), fromDateInstance.get(Calendar.DATE));
mediaDate.setOnClickListener(v -> fromDatePicker.show());
source.setOnClickListener(v -> alertDialog());
getSources();
return view;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getActivity().onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
private void alertDialog() {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext());
builder.setTitle("Source");
builder.setSingleChoiceItems(adapter, checkedItem, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
source.setText(adapter.getItem(which).getName());
checkedItem = which;
dialog.dismiss();
}
});
builder.show();
}
}
And the xml of Add Media is:-
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.circularreveal.coordinatorlayout.CircularRevealCoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myCoordinatorLayout"
android:layout_width="match_parent"
android:background="#color/white"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior"
android:theme="#style/AppTheme.AppBarOverlay">
<include layout="#layout/include_toolbar" />
</com.google.android.material.appbar.AppBarLayout>
<include
layout="#layout/content_add_media"
android:layout_width="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
android:layout_height="match_parent"/>
</com.google.android.material.circularreveal.coordinatorlayout.CircularRevealCoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.circularreveal.CircularRevealLinearLayout
android:id="#+id/dial"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_gravity="bottom|center_horizontal"
android:visibility="invisible"
android:layout_marginBottom="16dp"
app:layout_anchor="#id/fab"
app:layout_anchorGravity="top|center_horizontal"
android:background="#color/colorPrimary"
app:layout_behavior="com.google.android.material.transformation.FabTransformationSheetBehavior">
<ImageButton
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back"/>
</com.google.android.material.circularreveal.CircularRevealLinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:backgroundTint="#color/colorPrimary"
app:srcCompat="#android:drawable/ic_dialog_email"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
For the FabTransformationSheetBehavior to work, you need to change the isExpanded value.
on change of this value, expand and collapse animation will be called and visibility of the linearlayout and fab button will be handled automatically. Don't change visibility manually as this would block transformation animation.
Use back button to transform back to FAB.
val fab: FloatingActionButton = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener { fab.isExpanded = !fab.isExpanded } // this sets isExpanded as true
val back = findViewById<ImageButton>(R.id.back)
back.setOnClickListener{ fab.isExpanded = !fab.isExpanded } // this sets isExpanded as false

Activities togglebutton when clicking a part of an image

I'm currently trying to make a small application in java, and I have encountered a small problem.
I need to activate a togglebutton when I touch a particular area of the image that I have provided.
I enclose the code of my pages.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnPag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lights"
tools:ignore="HardcodedText" >
</Button>
<Button
android:id="#+id/btnPag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Automation"
tools:ignore="HardcodedText" >
</Button>
<Button
android:id="#+id/btnPag3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Garden"
tools:ignore="HardcodedText" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</LinearLayout>
page1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 1"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 2"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 3"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 4"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/casa"
tools:ignore="ContentDescription" />
</LinearLayout>
page2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gate"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activates"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
page3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Irrigation"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MercuryActivity.java
package it.anddev.bradipao.mercury;
// derived from http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/
import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MercuryActivity extends FragmentActivity implements Page1Fragment.OnPageListener {
// list contains fragments to instantiate in the viewpager
List<Fragment> fragments = new Vector<Fragment>();
// page adapter between fragment list and view pager
private PagerAdapter mPagerAdapter;
// view pager
private ViewPager mPager;
// activity data
public String p2text,p3text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// creating fragments and adding to list
fragments.add(Fragment.instantiate(this,Page1Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Page2Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Page3Fragment.class.getName()));
// creating adapter and linking to view pager
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(),fragments);
mPager = (ViewPager) super.findViewById(R.id.pager);
mPager.setAdapter(this.mPagerAdapter);
// upper bar button listener, allows direct page access
Button button = (Button)findViewById(R.id.btnPag1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0); // go to first page
}
});
button = (Button)findViewById(R.id.btnPag2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(1); // go to second page
}
});
button = (Button)findViewById(R.id.btnPag3);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(2); // go to third page
}
});
}
// page 1 fragment listener implementation
#Override
public void onPage1(String s) {
// set activity data with received string
p2text = new String(s+" 2");
p3text = new String(s+" 3");
// page 2 fragment update
Page2Fragment f2 = (Page2Fragment) fragments.get(1);
f2.ptext = p2text;
// if page 2 view is already created, update
View v2 = f2.getView();
if (v2!=null) {
}
// page 3 fragment update
Page3Fragment f3 = (Page3Fragment) fragments.get(2);
f3.ptext = p3text;
// if page 3 view is already created, update
View v3 = f3.getView();
if (v3!=null) {
}
}
}
Page1Fragment.java
package it.anddev.bradipao.mercury;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class Page1Fragment extends Fragment {
public String ptext="..PAGE 1..";
// activity listener interface
private OnPageListener pageListener;
public interface OnPageListener {
public void onPage1(String s);
}
// onAttach : set activity listener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// if implemented by activity, set listener
if(activity instanceof OnPageListener) {
pageListener = (OnPageListener) activity;
}
// else create local listener (code never executed in this example)
else pageListener = new OnPageListener() {
#Override
public void onPage1(String s) {
Log.d("PAG1","Button event from page 1 : "+s);
}
};
}
// onCreateView :
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page1,container,false);
// update text
return view;
}
// set text helper function
}
Page2Fragment.java
package it.anddev.bradipao.mercury;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class Page2Fragment extends Fragment {
public String ptext="..PAGE 2..";
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page2,container,false);
// update text
return view;
}
// set text helper function
}
Page3Fragment.java
package it.anddev.bradipao.mercury;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class Page3Fragment extends Fragment {
Button btnWrite;
public String ptext="..PAGE 3..";
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page3,container,false);
// update text
return view;
}
}
PageAdapter.java
package it.anddev.bradipao.mercury;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter {
// fragments to instantiate in the viewpager
private List<Fragment> fragments;
// constructor
public PagerAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
// return access to fragment from position, required override
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
// number of fragments in list, required override
#Override
public int getCount() {
return this.fragments.size();
}
}

Categories

Resources