Show/Hide Layout onTouch in framelayout - android

I have a full image fragment. There is a RelativeLayout and LinearLayout in framelayout. LinearLayout contain viewpager.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayoutViewpager">
<com.bogdwellers.pinchtozoom.view.ImageViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relativelayoutforbuttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/save_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</RelativeLayout>
I want When user first open it it should only show image(Viewpager layout) and if user click on it that show other option like - share and save(RelativeLayout).
We can find this type of feature in facebook and whatsapp.

You can simply hide or unhide the relativeLayout's instance using setVisibility.(..) method on linearLayout or imageviewpager click.
layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayoutViewpager">
<ImageViewPager
android:id="#+id/viewpager"
android:onClick="onPagerClick"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relative_layout"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/save_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSave"
android:text="Save" />
</RelativeLayout>
</FrameLayout>
Activity:
public class TestActivity extends AppCompatActivity {
private boolean isButtonShown = false;
ImageViewPager viewPager;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
viewPager = findViewById(R.id.viewpager);
layout = findViewById(R.id.relative_layout);
}
public void onPagerClick(View view) {
if (isButtonShown) {
isButtonShown = false;
layout.setVisibility(View.GONE);
} else {
isButtonShown = true;
layout.setVisibility(View.VISIBLE);
}
}
}

Try this type code for visiablity ..
private void share(){
relativeLayout.setVisibility(View.VISIBLE);
linearLayoutViewpager.setVisibility(View.GONE);
}
linearLayoutViewpager=findViewById(R.id.linearLayoutViewpager);
linearLayoutViewpager.setVisibility(View.VISIBLE);
relativeLayout=findViewById(R.id.relativelayoutforbuttons);
relativeLayout.setVisibility(View.GONE);
it only sample code but mange according your needs.
final boolean flag=true;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (flag){
relativeLayout.setVisibility(View.VISIBLE);
flag=false;
}
else{
relativeLayout.setVisibility(View.GONE);
}
}
});

Related

Nested LinearLayout doesn't work in Android

I am trying to make layout like in the picture. Project picture I can only link it because I am new. When I run the code with just one LinearLayout it works fine. But when I make nested linear layout it shows an only white screen. And there are no errors. The code adds a score to teams according to their throw s.Thank you.
Java code:
public class MainActivity extends AppCompatActivity {
int scorea=0;
int scoreb=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void displayforpointb3(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb+=3;
scoreView.setText(""+scoreb);
}
public void displayforpointb2(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb+=3;
scoreView.setText(""+scoreb);
}
public void displayforfreethrowb(View view){
TextView scoreView=(TextView)findViewById(R.id.team_b_score);
scoreb++;
scoreView.setText(""+scoreb);
}
public void displayforpoint3(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea+=3;
scoreView.setText(""+scorea);
}
public void displayforpoint2(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea+=2;
scoreView.setText(""+scorea);
}
public void displayforfreethrow(View view){
TextView scoreView=(TextView)findViewById(R.id.team_a_score);
scorea++;
scoreView.setText(""+scorea);
}
}
XML code:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.android.courtcounter.MainActivity"
android:orientation="horizontal">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Teama"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:text="#string/zero"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:id="#+id/team_a_score" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/point3"
android:onClick="displayforpoint3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/point2"
android:onClick="displayforpoint2"/>
<Button
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:layout_height="wrap_content"
android:text="#string/freethrow"
android:onClick="displayforfreethrow"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Teama"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/zero"
android:id="#+id/team_b_score" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="8dp"
android:text="#string/point3"
android:onClick="displayforpointb3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="#string/point2"
android:layout_weight="1"/>
android:onClick="displayforpointb2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="8dp"
android:text="#string/freethrow"
android:onClick="displayforfreethrowb"/>
</LinearLayout>
</LinearLayout>
First of all you should define your TextView as a private attributes in your class as follows
private TextView scoreViewA;
private TextView scoreViewB;
Then in your onCreate() method, you should find the view of those TextView as follows
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
scoreViewA = findViewById(R.id.team_a_score);
scoreViewB = findViewById(R.id.team_b_score);
}
Then in your onClick methods, you should update those TextView
public void displayforpointb3(View view){
scoreb+=3;
scoreViewB.setText(""+scoreb);
}
public void displayforpointb2(View view){
scoreb+=2; //You had scoreb+=3 I changed it to 2
scoreViewB.setText(""+scoreb);
}
public void displayforfreethrowb(View view){
scoreb++;
scoreViewB.setText(""+scoreb);
}
public void displayforpoint3(View view){
scorea+=3;
scoreViewA.setText(""+scorea);
}
public void displayforpoint2(View view){
scorea+=2;
scoreViewA.setText(""+scorea);
}
public void displayforfreethrow(View view){
scorea++;
scoreViewA.setText(""+scorea);
}
Also I see something wrong in your activity_main.xml I guess is a misstype
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="#string/point2"
android:layout_weight="1"/> <--- Remove this "/>"
android:onClick="displayforpointb2"/>
Happy coding
Problem
The orientation of the root LinearLayout is horizontal. This results in child views appearing to the right of the Toolbar instead of underneath it.
Solution
Make the root LinearLayout vertical and wrap the two inner view groups in a horizontal LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Team A -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Text & Buttons -->
</LinearLayout>
<!-- Team B -->
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Text & Buttons -->
</LinearLayout>
</LinearLayout>
</LinearLayout>
It's not pretty, but it gets the job done. Consider using ConstraintLayout as an alternative to nested view groups if you encounter any performance issues.

Showing Toast in fragments

I want to show toast on button click, and that button is in the fragment. I have tried methods to get context for the toast, but it is not showing on button click.
This is my code
public class Bottom_Sheet_Fragment extends Fragment {
Button addComment;
public Bottom_Sheet_Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_bottom__sheet, container, false);
addComment=(Button) container.findViewById(R.id.addCommentBtn);
addComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Added", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
This is fragment layout
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_sheet1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
android:fillViewport="true"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/commentList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/etComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:hint="Write comment" />
<Button
android:id="#+id/addCommentBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Add" />
</RelativeLayout>
</LinearLayout>
this layout file is included in another activity, with in include statement.dont know where is the problem. and this fragment works like a bottomsheet.
Try this: addComment = view.findViewById(R.id.addCommentBtn);
Because you must get button from layout was inflated.
You need to use view.findViewById.
Try This
addComment=(Button) view.findViewById(R.id.addCommentBtn);
addComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Added", Toast.LENGTH_SHORT).show();
}
});

Android toolbar is covering a fragment

I have 2 layouts in an app that are being covered by the toolbar. In each layout I've tried using android:layout_below="#id/app_bar_layout or android:layout_below="#id/toolbar, with no luck. How can I fix this?
The recycler view is called by a touch on an menu item and, inflated by rv adpater and view holder classes. So far, I have no other problems with the app except the toolbar covering the views.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.aaron.walkingtourtest09feb.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/app_bar_layout"
android:theme="#android:style/Theme.Material">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:background="#607D8B"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
</android.support.design.widget.CoordinatorLayout>
content_main.xml:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
tools:context="com.example.test09feb.MainActivity"
tools:showIn="#layout/app_bar_main"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/listFragment"
android:tag="unique_tag"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.test09feb.MainActivity$ExampleFragment" >
</fragment>
Recyclerview_activity.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cardview_light_background"
android:animateLayoutChanges="true"
android:padding="6dp">
<android.support.v7.widget.RecyclerView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rv">
</android.support.v7.widget.RecyclerView>
cards.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cv"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/card_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#EEEEEE"
android:orientation="vertical"
android:padding="0dp">
<ImageView
android:id="#+id/subject_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
<TextView
android:id="#+id/subject_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/subject_photo"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:textColor="#android:color/black"
android:textSize="16sp"/>
<TextView
android:id="#+id/subject_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/subject_name"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#777777"
/>
<Button
android:id="#+id/card_button_left"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/subject_text"
android:layout_toLeftOf="#+id/card_button_right"
android:text="#string/watch"
/>
<Button
android:id="#id/card_button_right"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/subject_text"
android:text="#string/read"/>
<TextView
android:id="#+id/expanded_subject_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card_button_right"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#777777"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
RVAdapter:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.SubjectViewHolder> {
List<Subject> subjects;
static SubjectViewHolder svh;
View v = null;
Context cxt;
private static MyListener listener;
public interface MyListener {
void onClick(View itemView, int viewPosition);
}
RVAdapter(List<Subject> subjects, Context cxt) {
this.subjects = subjects;
this.cxt = cxt;
}
public void setOnClickListener(MyListener listener) {
this.listener = listener;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public SubjectViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards, viewGroup, false);
svh = new SubjectViewHolder(v);
return svh;
}
String[] localLinks = {
"http://192.168.11.111:8000/pic1.png",
"http://192.168.11.111:8000/pic2.jpg",
"http://192.168.11.111:8000/pic3.png",
"http://192.168.11.111:8000/pic4.jpg",
"http://192.168.11.111:8000/pic5.png",
"http://192.168.11.111:8000/pic6.jpg",
"http://192.168.11.111:8000/pic7.png",
"http://192.168.11.111:8000/pic8.jpg",
"http://192.168.11.111:8000/pic9.png",
"http://192.168.11.111:8000/pic10.jpg",
"http://192.168.11.111:8000/pic11.png",
"http://192.168.11.111:8000/pic12.png",
"http://192.168.11.111:8000/pic13.png",
"http://192.168.11.111:8000/pic14.png",
};
#Override
public void onBindViewHolder(SubjectViewHolder subjectViewHolder, int i) {
subjectViewHolder.subjectName.setText(subjects.get(i).subjectName);
subjectViewHolder.subjectText.setText(subjects.get(i).subjectText);
Picasso.with(cxt).load(localLinks[i]).into(subjectViewHolder.subjectPhoto);
subjectViewHolder.expandedSubjectText.setText(subjects.get(i).expandedSubjectText);
subjectViewHolder.expandedSubjectText.setVisibility(View.GONE);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemCount() {
return subjects.size();
}
public static class SubjectViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView subjectName;
TextView subjectText;
TextView expandedSubjectText;
ImageView subjectPhoto;
Button leftButton;
Button rightButton;
SubjectViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
subjectName = (TextView) itemView.findViewById(R.id.subject_name);
subjectText = (TextView) itemView.findViewById(R.id.subject_text);
expandedSubjectText = (TextView) itemView.findViewById(R.id.expanded_subject_text);
subjectPhoto = (ImageView) itemView.findViewById(R.id.subject_photo);
leftButton = (Button) itemView.findViewById(R.id.card_button_left);
rightButton = (Button) itemView.findViewById(R.id.card_button_right);
rightButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (expandedSubjectText.getVisibility() == View.GONE) {
expandedSubjectText.setVisibility(View.VISIBLE);
} else {
expandedSubjectText.setVisibility(View.GONE);
}
// Triggers click upwards to the adapter on click
if (listener != null)
v.findViewById(R.id.card_container);
listener.onClick(rightButton, svh.getAdapterPosition());
}
});
leftButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Triggers click upwards to the adapter on click
if (listener != null)
listener.onClick(leftButton, svh.getAdapterPosition());
}
});
}
}}
result of adding appbar_scrolling_view_behavior as suggested. A blank bar covers the textview that has just animated itself visible.
You have to add following attribute to the parent view in content_main.xml
app:layout_behavior="#string/appbar_scrolling_view_behavior"
Like this:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test09feb.MainActivity"
tools:showIn="#layout/app_bar_main"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</FrameLayout>
Your Toolbars height is defined by: android:layout_height="?attr/actionBarSize"
so I added a top margin with those values to the list view:
android:layout_marginTop="?attr/actionBarSize"
Worked for me in the app I'm working on.

SwipeRefreshLayout Tab layout. Webview can't scroll up

Okay so I have a Tab view going inside of which I have a webview, a listview and a few other pages. I want to be able to do a SwipeRefreshLayout to refresh each item. I have this working on each page. But, when I scroll down in a webview I can't scroll back up. It triggers the refresh and rebuilds it. I'd like to be able to scroll up in my webview.
Layout stuff
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="20px"
android:layout_weight="1"
android:nestedScrollingEnabled="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Splash$PlaceholderFragment">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:overScrollMode="ifContentScrolls"
android:nestedScrollingEnabled="false"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"
android:visibility="visible" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
The SwipeRefreshLayout is being used inside the fragment for my tabbed layout.
swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
swipeLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
#Override public void onRefresh() {
if(getArguments().getInt(ARG_SECTION_NUMBER) == 4 ) {
stringArrayList.clear();
new updatetheQueue().execute(ctx);
}
else {
swipeLayout.setRefreshing(false);
}
}});
You can use an OnScrollChangedListener to disable SwipeRefreshLayout when the WebView is not scrolled to the top.
// Only enable swipe to refresh if the `WebView` is scrolled to the top.
webView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if (webView.getScrollY() == 0) {
swipeRefreshLayout.setEnabled(true);
} else {
swipeRefreshLayout.setEnabled(false);
}
}
});
Getting this working was very challenging. However there is a fairly simple modification that can be made to the layout XML to work around this. It means I can't update the webviews on swipe but thats okay.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Splash$PlaceholderFragment">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:overScrollMode="ifContentScrolls" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Splash$PlaceholderFragment">
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:visibility="visible" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
you can make a class extend SwipeRefreshLayout and Override canChildScrollUp to sit scro
#Override
public boolean canChildScrollUp() {
// TODO Auto-generated method stub
return true; // if you want it to scroll up
// check if child Get To Top
//return false // if you want it to execute swipe down to refresh listener
}
If you are using the ViewPager and the tabs are Fragments, you can fix your WebViewFragment with this snippet:
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
#Override
public void onStart() {
super.onStart();
swipeLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if (mWebView.getScrollY() == 0)
swipeLayout.setEnabled(true);
else
swipeLayout.setEnabled(false);
}
});
}
#Override
public void onStop() {
swipeLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
super.onStop();
}
More info in Android - SwipeRefreshLayout with empty textview.
this is how i solve the problem, hope it will help :
mWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if(mWebView.getScrollY() == 0){
mSwipeRefreshLayout.setEnabled(true);
}else{
mSwipeRefreshLayout.setEnabled(false);
}
}
});
In kotlin:
webView.viewTreeObserver.addOnScrollChangedListener {
binding.swipeRefreshLayout.isEnabled = webView.scrollY == 0
}

How to hide and show views based on button clicks?

I have an Activity which uses the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="#+id/btnCapture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start capture" />
<Button
android:id="#+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Play video" />
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
How can I hide surfaceView and show videoView when btnPlay is clicked? I would also like to do something similar where videoView is hidden and surfaceView is shown when clicking btnCapture.
This is how I'm hiding and showing views:
surfaceView.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
The easiest way is to put both surfaceView and videoView in different Linearlayout and then play with linearlayout setVisibility() behind the btnCapture click.
maybe like this:
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (surfaceView_linearlayout.getVisibility() == 0) {
videoView_linearlayout.setVisibility(View.VISIBLE);
surfaceView_linearlayout.setVisibility(View.GONE);
}
}
});
btnCapture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (videoView_linearlayout.getVisibility() == 0) {
surfaceView_linearlayout.setVisibility(View.VISIBLE);
videoView_linearlayout.setVisibility(View.GONE);
}
}
});

Categories

Resources