I'm trying to create a button to reload a Webview but ( sorry I'm new in android developement ) I didn't find a way to do it ( even in stackoverflow )
Here is the Mainactivity.java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;
import static com.d4rkunicorn.partyhard.R.id.webView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("url");
}
}
The webview.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:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/webview.xml">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="REFRESH"
android:id="#+id/button" />
</RelativeLayout>
Thanks for helping
In Activity class :
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;
import static com.d4rkunicorn.partyhard.R.id.webView;
public class MainActivity extends ActionBarActivity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("url");
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webview.reload();
}
});
}
}
In Layout 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:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/webview.xml">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="REFRESH"
android:id="#+id/button" />
</RelativeLayout>
There many possible way to reload your webview:
you can use webview.loadUrl( "javascript:window.location.reload( true )" ); inside any method/action event
webview.loadUrl(webview.toString); //getting the webview URL and use to reload ur web page
webview.loadUrl(url); // you can use your desire URL to reload your web page
webview.loadUrl( "onclick=\"window.history.back()"\");
You can reload your web view or you can use below code :
yourActivity.this.webView.loadUrl(url);
on your button click.
Related
I have created two textViews that the first one is the main title and the second one is the subtext of the title. So I want to make the subtexts visibility to depend on clicking the main title.
It looks so easy as saying butI tried a lot of methods and MainActivity.java crashes all the time.
On the second step I want a different main title under the first one and the subtext above can toggle between them.
Maybe there is an easier widget instead of textView but I couldn't find any.
Please help.
Thanks.
Ps: The strings work well, there is no problem with them.
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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="100dp"
android:clickable="true"
android:focusable="true"
android:text="#string/text1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.062" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text11"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textSize="15sp"
android:layout_alignTop="#id/textView"
android:layout_marginTop="35dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintVertical_bias="0.083"
android:visibility="invisible"
tools:visibility="visible" />
</RelativeLayout>
MainActivity.java
package com.example.Notes;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MainActivity);
}
}
Edit: adding to java this code is the solution:
TextView title = findViewById(R.id.textView);
TextView subText = findViewById(R.id.textView2);
title.setOnClickListener(v -> {
if (subText.getVisibility() == View.VISIBLE)
subText.setVisibility(View.INVISIBLE);
else
subText.setVisibility(View.VISIBLE);
});
To change the visibility of the subText by clicking the title textview one could do:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MainActivity);
TextView title = findViewById(R.id.textView);
TextView subText = findViewById(R.id.textView2);
title.setOnClickListener(v -> {
subText.setVisibility(View.VISIBLE); //OR View.GONE OR View.INVISIBLE
});
}
}
Thanks, I just modified this part and it became what I exatly want:
//subText.setVisibility(View.VISIBLE); //OR View.GONE OR View.INVISIBLE
if (subText.getVisibility() == View.VISIBLE)
subText.setVisibility(View.INVISIBLE);
else
subText.setVisibility(View.VISIBLE);
Main Activity XML File
<?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"
tools:context="com.harsh1507.pc.t_1372.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal"
android:id="#+id/l1">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.harsh1507.pc.t_1372.Frag_a"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/tv"/>
</LinearLayout>
Main Activity JAVA File
package com.harsh1507.pc.t_1372;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements Frag_a.comm
{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.tv);
}
#Override
public void com(double s) {
tv.setText("Result is : "+Double.toString(s));
}
}
Fragment XML File
<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:orientation="horizontal"
tools:context="com.harsh1507.pc.t_1372.Frag_a">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:inputType="number"
android:id="#+id/ed1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/ed2"
android:inputType="number"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="cal"
android:id="#+id/btn"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rg"
android:orientation="vertical">
<RadioButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="ADD"
android:checked="true"
android:id="#+id/rb1"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="SUBSTRACT"
android:id="#+id/rb2"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="MULTIPLY"
android:id="#+id/rb3"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="DIVIDE"
android:id="#+id/rb4"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
Fragment JAVA File ,main problem may be present in this part but i am not able to debug that ,please go through the code and let me know where i missed something.
package com.harsh1507.pc.t_1372;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
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.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class Frag_a extends Fragment
{
View v;
comm c;
EditText ed1,ed2;
Button btn;
RadioGroup rg;
RadioButton rb1,rb2,rb3,rb4;
double d1,d2,comp;
#Override
public void onAttach(Context context)
{
super.onAttach(context);
c=(comm) context;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
v= inflater.inflate(R.layout.fragment_frag_a, container, false);
return v;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
ed1=v.findViewById(R.id.ed1);
ed2=v.findViewById(R.id.ed2);
btn=v.findViewById(R.id.btn);
rb1=v.findViewById(R.id.rb1);
rb2=v.findViewById(R.id.rb2);
rb3=v.findViewById(R.id.rb3);
rb4=v.findViewById(R.id.rb4);
rg= v.findViewById(R.id.rg);
d1=Double.parseDouble(ed1.getText().toString());
d2=Double.parseDouble(ed2.getText().toString());
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
if (rb1.isChecked()) {
comp = d1 + d2;
c.com(comp);
}
if (rb2.isChecked()) {
comp = d1 - d2;
c.com(comp);
}
if (rb3.isChecked()) {
comp = d1 * d2;
c.com(comp);
}
if (rb4.isChecked()) {
comp = d1 / d2;
c.com(comp);
}
}
});
}
public interface comm{
void com(double s);
}
}
Make sure that ur main activity extends FragmentActivity instead of AppCompat.
The corresponding import would be :
import android.support.v4.app.FragmentActivity;
Any activities using fragments must be implemented as a subclass of FragmentActivity instead of the traditional Activity class
And this is how the Java code looks:
package com.example.admin.testingwebview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void go(View view){
EditText editText = (EditText) findViewById(R.id.editText);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl(editText.getText().toString());
}
}
The layout XML File:
<?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"
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.admin.testingwebview.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go"
android:id="#+id/button"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:onClick="go" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_centerHorizontal="true"
android:layout_below="#+id/button" />
</RelativeLayout>
I tried it on an emulator as well as real device.
The EditText and Button part are A-OK, But the Web View shows nothing . . . .
What am I doing wrong?
You need something like this
EditText data = (EditText) findViewbyId(R.id.edittext);
String result = data.getText().toString(); //Take URL from edittext
if (!result.startsWith("http://") && !result.startsWith("https://"))
result = "http://" + result;
If you don't set the http after the link string nothing is shown in the webview, same for intent that start the BrowserWeb
I have 2 activities:
the first - is just an opening page and it has a button that goes to the second activity
the second activity - will open up the contact list and will allow the user to pick contacts and retrieve their numbers - they will be saved for later use.
My problem is I can not figure out how to get it to work in the second activity. All the examples only have the contact list in the Main/first activity and I am really new to this and don't understand how to get it to move to the second.
my code I have right now
Main.java
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) this.findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View v){
Intent i = new Intent(MainActivity.this, Contacts.class);
startActivity(i);
}
});
}
}
Main XML
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:id="#+id/button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="93dp"/>
</RelativeLayout>
Contacts java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Contacts extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
}
}
Contacts XML
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context="akh.seniorproj.Contacts">
</RelativeLayout>
Check this solution: How to call Android contacts list?
Its pretty much what you need to do but instead name you should search the number with the cursor.
Please have a look at the following code
game_activity.xml
<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=".Game" >
//Please note the GUI Has been removed
<include layout="#layout/common_status_bar"/>
</RelativeLayout>
Game.java
package game.games;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class Game extends Activity {
private ImageView goToLanguageSelection;
private ImageView goToInternet;
private Button giveUp;
private LayoutInflater layoutInflater;
private View commonView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layoutInflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
commonView = layoutInflater.inflate(R.layout.common_status_bar, null);
//Intializing instance variables
goToLanguageSelection = (ImageView)commonView.findViewById(R.id.backToLanguageSelectionButton);
goToInternet = (ImageView)commonView.findViewById(R.id.internetButton);
giveUp = (Button)commonView.findViewById(R.id.giveUpButton);
flag = getIntent().getBooleanExtra("LANGUAGE", true);
//Add listeners
goToLanguageSelection.setOnClickListener(goToLanguageSelectionClicked);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
//Will get activated when the ThunderBolt image is clicked
private OnClickListener goToLanguageSelectionClicked = new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(),LanguageSelector.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
};
}
common_status_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#373734"
android:orientation="horizontal" >
<ImageView
android:id="#+id/backToLanguageSelectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:paddingTop="5dp"
android:src="#drawable/thunderbolt" />
<ImageView
android:id="#+id/internetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:src="#drawable/globe_small_2" />
<ImageView
android:id="#+id/justForFun"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp" />
<Button
android:id="#+id/giveUpButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Button" />
</LinearLayout>
Here, the common_status_bar.xml is a common layout. I have added this layout to game_activity.xml by <include layout="#layout/common_status_bar"/>.
It displays everything fine, no issue. But the case is I can't make any of it's elements to work. I have attached a OnClickListener to the first element of it, the goToLanguageSelection but it is not responding to the Click. I tested this by adding the click event to all other buttons, images as well (one at a time) and I got the same result. It is not responding to user click, or whatever.
Why this button and images in separate layout do not respond to the user events?
The commonView should not be created again.
If you want keep a reference of the common_status_bar.xml, you can give name it by an id, like the code below:
<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="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<include
android:id="#+id/common_status_bar"
layout="#layout/common_status_bar" />
</RelativeLayout>
Then, change this code:
commonView = layoutInflater.inflate(R.layout.common_status_bar, null);
to:
commonView = findViewById(R.id.common_status_bar);