I am trying to switch between two WebViews in my layout with Android Java. The two WebViews load different urls when the oncreate method is called. The only problem am having is that the first webview shows up with the site content when oncreate is called but when I click the button that is supposed to show the next webview, it removes the current webview from view and then shows white screen. Help me resolve that, below is my layout file and the android java code behind file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ViewSwitcher
android:layout_height="wrap_content"
android:id="#+id/viewSwitcher1"
android:layout_width="wrap_content">
<LinearLayout
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.webkit.WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.webkit.WebView
android:id="#+id/webView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</ViewSwitcher>
<Button
android:id="#+id/button1"
android:text="Show Next"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
and Android Java code for the behavior
public class MainActivity : AppCompatActivity
{
ViewSwitcher viewSwitcher;
private Button next;
protected WebView mywebview,webview2;
protected int mycount=0;
protected View view1, view2;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
viewSwitcher = FindViewById<ViewSwitcher>(Resource.Id.viewSwitcher1);
next = FindViewById<Button>(Resource.Id.button1);
mywebview = FindViewById<WebView>(Resource.Id.webView1);
webview2 = FindViewById<WebView>(Resource.Id.webView2);
view1 = FindViewById<View>(Resource.Id.view1);
view2 = FindViewById<View>(Resource.Id.view2);
WebViewClient client = mywebview.WebViewClient;
mywebview.LoadUrl("https://www.upwork.com");
webview2.LoadUrl("https:www.sololearn.com");
//set the in and out animation for the viewswitcher
viewSwitcher.SetOutAnimation(this, Resource.Animation.abc_slide_out_top);
viewSwitcher.SetInAnimation(this, Resource.Animation.design_bottom_sheet_slide_in);
next.Click += Next_Click;
//hide the action bar
}
private void Next_Click(object sender, System.EventArgs e)
{
if (viewSwitcher.CurrentView != view1)
{
viewSwitcher.ShowPrevious();
}else if(viewSwitcher.CurrentView != view2)
{
viewSwitcher.ShowNext();
}
}
}
Related
I want to have a layout constantly on top of everything and visible all the time, however, once I swipe to the next element in the ViewPager, the FrameLayout is getting behind the ViewPager.
<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"
android:gravity="center_vertical"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/videosViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
</androidx.viewpager2.widget.ViewPager2>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/preview_display_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:visibility="visible"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="0dp">
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
I tried to place a button instead of and on top of the FrameLayout and, guess what, the button remains on top of everything without going behind the ViewPager after each sroll.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/videosViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
</androidx.viewpager2.widget.ViewPager2>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="197dp"
android:layout_height="286dp">
</Button>
<FrameLayout
android:id="#+id/preview_display_layout"
android:layout_width="197dp"
android:layout_height="286dp"
android:visibility="visible"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="0dp">
</FrameLayout>
</RelativeLayout>
</FrameLayout>
Here is my code in MainActivity
private ViewPager2 videosViewPager;
private SurfaceView previewDisplayView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewLayoutResId());
previewDisplayView = new SurfaceView(this);
setupPreviewDisplayView();
videosViewPager = findViewById(R.id.videosViewPager);
List<VideoItem> videoItems = new ArrayList<>();
VideoItem life = new VideoItem();
String path = "android.resource://"+getPackageName()+"/"+ R.raw.video;
life.videoURL = Uri.parse(path);
videoItems.add(life);
VideoItem rest = new VideoItem();
String path2 = "android.resource://"+getPackageName()+"/"+ R.raw.video1;
rest.videoURL = Uri.parse(path);
videoItems.add(rest);
videosViewPager.setAdapter(new VideoAdapter(videoItems));
}
private void setupPreviewDisplayView() {
previewDisplayView.setVisibility(View.GONE);
ViewGroup viewGroup = findViewById(R.id.preview_display_layout);
viewGroup.addView(previewDisplayView);
previewDisplayView
.getHolder()
.addCallback(
new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
processor.getVideoSurfaceOutput().setSurface(holder.getSurface());
Log.d("Surface","Surface Created");
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
onPreviewDisplaySurfaceChanged(holder, format, width, height);
Log.d("Surface","Surface Changed");
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
processor.getVideoSurfaceOutput().setSurface(null);
Log.d("Surface","Surface destroy");
}
});
}
I guess my issue is in adding SurfaceView(previewDisplayView)in ViewGroup.
What can I do to make the previewDisplayView get always on top of everything? By the way, I tried bringToFront(), but after a swipe the previewDisplayLayout is automatically going behind the ViewPager.
Thank you in advance
Have you tried android:elevation property ? I think it is what you're looking for,
Hope will help
I like to integrate comment section in Bottom-sheet. In Comment section i have recyclerView that show all comment and a EditText that situated bottom of the view. when bottom-sheet is open in Collapsed State the Edit text not showing but when State is EXPANDED the the Edit text layout is show. so i need help to show the edit text option in every state.
This is Collapsed state Image
This is Expanded State Image
I want to show Edit text in Collapsed mode just like Expended mood.
XML Layout
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragment.CommentFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:scrollingCache="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<View
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize">
</View>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<View
android:background="#B1B1B1"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Code
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
final View view = View.inflate(getContext(), R.layout.fragment_comment, null);
unbinder = ButterKnife.bind(this, view);
dialog.setContentView(view);
mBehavior = BottomSheetBehavior.from((View) view.getParent());
mBehavior.setPeekHeight(BottomSheetBehavior.PEEK_HEIGHT_AUTO);
mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
mBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
if (BottomSheetBehavior.STATE_EXPANDED == newState) {
Log.d(TAG, "onStateChanged: STATE_EXPANDED");
}
if (BottomSheetBehavior.STATE_COLLAPSED == newState) {
Log.d(TAG, "onStateChanged: STATE_COLLAPSED");
}
if (BottomSheetBehavior.STATE_HIDDEN == newState) {
Log.d(TAG, "onStateChanged: STATE_HIDDEN");
dismiss();
}
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
});
commentsAdapterNew = new CommentsAdapterNew(getActivity(), commentsClassList);
RecyclerView.LayoutManager mManager = new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, true);
rvMain.setLayoutManager(mManager);
rvMain.setAdapter(commentsAdapterNew);
ViewCompat.setNestedScrollingEnabled(rvMain, false);
getAllComment("876");
return dialog;
}
I am having a layout with a single editText in it,below the editText I added a add button. Now my question is when I click the add button I need to get another editText below the editText and has to repeat the same.Please anyone help, Thank you.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll_edit_texts_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD" />
</LinearLayout>
and put following code in your Activity
final LinearLayout llEditTextsContainer = (LinearLayout) view.findViewById(R.id.ll_edit_texts_container);
Button buttonAdd = (Button) view.findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = new EditText(getActivity());
//editText.setId(); you should set id smartly if you wanted to use data from this edittext
llEditTextsContainer.addView(editText);
}
});
First of all create a container view in your main layout which is going to hold the new Edittexts.
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.vuclip.dynamictextedit.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/addTextView"
android:text="Add EditText"
android:onClick="onClick"
/>
<TableLayout
android:id="#+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
Here, on pressing the button, new Edittexts will be created and added into the TableLayout.
Now create a new Layout which will hold your edittext(I called this file new_layout.xml).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/newEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Now add this layout into your main activity.
public class MainActivity extends AppCompatActivity {
TableLayout container;
static int rowIndex = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (TableLayout) findViewById(R.id.containerLayout);
}
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRowView = inflater.inflate(R.layout.new_layout,null);
container.addView(newRowView,rowIndex);
rowIndex++;
}}
I am trying to make a custom media player where when ever the music start a loader will start and when the music is paused or ends the the loader goes invisible .This is what i am trying to get when the button is clicked ..How to set the desired output.I am using the following gradel
compile 'com.leo.simplearcloader:simplearcloader:1.0.+'
compile 'de.hdodenhof:circleimageview:2.0.0'
my xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
tools:context="com.union.playerfront1.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/load1">
<com.leo.simplearcloader.SimpleArcLoader
android:visibility="visible"
android:id="#+id/loader"
android:layout_centerInParent="true"
android:layout_width="200dp"
android:layout_height="200dp"
custom:arc_style="simple_arc"
custom:arc_speed="medium"
custom:arc_margin="3dp"
>
</com.leo.simplearcloader.SimpleArcLoader>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_marginBottom="44dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
my java code
public class MainActivity extends AppCompatActivity {
boolean isPressed = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mediaPlayer =MediaPlayer.create(this,R.raw.song);
final RelativeLayout real=(RelativeLayout)findViewById(R.id.load1);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isPressed){
button.setBackgroundResource(R.drawable.profile);
real.setVisibility(View.VISIBLE);
mediaPlayer.start();
}
}
});
}
}
How I can change the visibility of the loader on the base that a song is playing or it is stopped I am able to set the button visibility properly.How i can set the visibility on the basis of song pause,song start,song stop?
I am trying to hide certain elements in a layout when Android goes out of focus, such that when viewed in the "View recent applications" display, the layout elements will not be visible. Upon selecting the application and going into focus, these elements will be visible again.
My implementation below attempts to show the "content" layout when the application is in focus, and show the "overlay" layout when out of focus, via onWindowFocusChanged().
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the content page"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the overlay"/>
</RelativeLayout>
</FrameLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
FrameLayout layoutContainer;
RelativeLayout layoutContent;
RelativeLayout layoutOverlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutContainer = (FrameLayout) findViewById(R.id.container);
layoutContent = (RelativeLayout) findViewById(R.id.content);
layoutOverlay = (RelativeLayout) findViewById(R.id.overlay);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
layoutContent.setVisibility(View.VISIBLE);
layoutOverlay.setVisibility(View.GONE);
} else {
layoutOverlay.setVisibility(View.VISIBLE);
layoutContent.setVisibility(View.GONE);
}
}
}
enter code here
However, my implementation does not work. Anyone with any suggestion as to solve this? Thanks!
If you want to hide views in your layout in recent apps try like this
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE);