android - Circle shape in XML drawable file






activity_main.xml



<?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:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity"
android:background="#f0f7e7"
>
<!--
To get a circular shape, we need to apply the same value for width and height
-->
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Circular Shape"
android:layout_centerInParent="true"
android:background="@drawable/circle_shape"
android:textColor="#fff"
/>
</RelativeLayout>







res/drawable/circle_shape.xml



<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<!--
android:shape
Keyword. Defines the type of shape. Valid values are:

rectangle : A rectangle that fills the containing View. This is the default shape.
oval : An oval shape that fits the dimensions of the containing View.
line : A horizontal line that spans the width of the containing View. This
shape requires the <stroke> element to define the width of the line.
ring : A ring shape.
-->
<shape android:shape="oval">
<solid android:color="#e42828"/>
<stroke android:color="#3b91d7" android:width="5dp"/>
<!-- Set the same value for both width and height to get a circular shape -->
<size android:width="250dp" android:height="250dp"/>
</shape>
</item>
</selector>

















android - Detect when WebView finish loading a URL









MainActivity.java



package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {
private MainActivity mContext;
private WebView mWebView;
private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the context
mContext = this;

// Get the widgets reference from XML layout
mWebView = findViewById(R.id.webView);
mTextView = findViewById(R.id.textView);
Button button = findViewById(R.id.button);


// Set a click listener for button widget
button.setOnClickListener(view -> {
// Request to render the web page
renderWebPage("https://www.android--code.com/?m=1");
});
}



// Custom method to render a web page
protected void renderWebPage(String urlToRender){
// Set a WebViewClient for WebView
mWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon){
// Page loading started
// Do something
}


@Override
public void onPageFinished(WebView view, String url){
// Page loading finished
showToast("Page fully loaded");
}
});


// Set a WebChromeClient for WebView
// Another way to determine when page loading finish
mWebView.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(
WebView view, int newProgress){

// Show the loading progress on text view
mTextView.setText("Page loading : "
+ newProgress + "%");

if(newProgress == 100){
// Page loading finish
mTextView.setText(
"Page fully loaded.");
}
}
});


// Enable JavaScript
mWebView.getSettings().setJavaScriptEnabled(true);

// Load the url in the WebView
mWebView.loadUrl(urlToRender);
}



// Method to show toast message
private void showToast(String message){
Toast.makeText(mContext,message,Toast.LENGTH_SHORT).show();
}
}





activity_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"
android:background="#DCDCDC">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Load URL"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="sans-serif"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="@+id/button"
tools:text="TextView" />

<WebView
android:id="@+id/webView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>







android - How to restart an Activity








MainActivity.java



package com.cfsuman.androidtutorials;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the widgets reference from XML layout
Button buttonRestart = findViewById(R.id.buttonRestart);
Button buttonRecreate = findViewById(R.id.buttonRecreate);
TextView textView = findViewById(R.id.textView);


// Set the TextView text with a random generated number
textView.setText("Random number : "
+ new Random().nextInt(100));


// Set a click listener for restart Button widget
buttonRestart.setOnClickListener(view -> {
Intent intent = getIntent();
// Finish the activity
finish();

// Restart the activity
startActivity(intent);
});


// Set a click listener for recreate Button widget
buttonRecreate.setOnClickListener(view -> {
// Recreate the activity
recreate();
});
}
}






activity_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"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#DCDCDC"
android:padding="24dp">

<Button
android:id="@+id/buttonRestart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restart Activity"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/buttonRecreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Recreate Activity"
app:layout_constraintBottom_toBottomOf="@+id/buttonRestart"
app:layout_constraintStart_toEndOf="@+id/buttonRestart" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textStyle="italic"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonRestart"
tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>