i have this simple code in my java class but it not run. where is the error?
i want open in a webview different sites from a fragment with some buttons...Thanks in advance for help me
public class SitoFragment extends Fragment {
private WebView URL13, URL14;
private Button button12,button13;
button12 = (Button) view.findViewById(R.id.button12);
button12.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
URL13 = (WebView) URL13.findViewById(R.id.webviewsito);
URL13.setWebViewClient(new WebViewClient());
URL13.setScrollbarFadingEnabled(true);
URL13.setHorizontalScrollBarEnabled(false);
URL13.getSettings().setJavaScriptEnabled(true);
URL13.loadUrl("http://www.xxxxx.com");
}
});
button13 = (Button) view.findViewById(R.id.button13);
button13.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
URL14 = (WebView) URL14.findViewById(R.id.webviewsito);
URL14.setWebViewClient(new WebViewClient());
URL14.setScrollbarFadingEnabled(true);
URL14.setHorizontalScrollBarEnabled(false);
URL14.getSettings().setJavaScriptEnabled(true);
URL14.loadUrl("http://www.xxxx.com");
}
});
return view;
}
}
Related
Context: My Style Activity corresponds to a layout with 4 imageButtons and a regular button. I want the user to only be able to select one imageButton at a time. Upon the click of the regular button, I want to send the data regarding which imageButton is selected to my ReviewActivity while simultaneously opening my ReflectionActivity.
I have 2 questions. First, how do I dry up my code surrounding OnClick's and disabled imageButtons? Second, how do I set a variable based on which imageButton was selected and send to another activity with an intent? I am fairly sure I've done this the long/hard way. All suggestions greatly appreciated!
public class StyleActivity extends AppCompatActivity {
Button btn_open_reflection;
ImageButton style1;
ImageButton style2;
ImageButton style3;
ImageButton style4;
public static final String style_selection = "com.example.application.hearttoart.style_selection";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_style );
// set up image buttons for the onClick function
style1 = (ImageButton)findViewById(R.id.style1);
style2 = (ImageButton)findViewById(R.id.style2);
style3 = (ImageButton)findViewById(R.id.style3);
style4 = (ImageButton)findViewById(R.id.style4);
// TODO: DRY up when possible, lots of repeated code here
style1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style2.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
String style_selection = "#string/style1";
}
});
style2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style1.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
String style_selection = "#string/style2";
}
});
style3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style1.setEnabled(false);
style2.setEnabled(false);
style4.setEnabled(false);
String style_selection = "#string/style3";
}
});
style4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
style1.setEnabled(false);
style2.setEnabled(false);
style3.setEnabled(false);
String style_selection = "#string/style4";
}
});
btn_open_reflection =(Button) findViewById(R.id.btn_open_style);
btn_open_reflection.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View view){
// Open Style Activity - Navigate to Activity from the Click
openReflection();
sendStyle();
}
});
}
public void sendStyle() {
Intent styleIntent = new Intent(StyleActivity.this, ReviewActivity.class );
styleIntent.putExtra("style", style_selection);
}
public void openReflection() {
Intent intent = new Intent( this, ReflectionActivity.class );
startActivity( intent );
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_open_reflection;
ImageButton style1;
ImageButton style2;
ImageButton style3;
ImageButton style4;
String style_selection = "com.example.application.hearttoart.style_selection";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set up image buttons for the onClick function
style1 = (ImageButton)findViewById(R.id.style1);
style2 = (ImageButton)findViewById(R.id.style2);
style3 = (ImageButton)findViewById(R.id.style3);
style4 = (ImageButton)findViewById(R.id.style4);
btn_open_reflection =(Button) findViewById(R.id.btn_open_style);
btn_open_reflection.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View view){
// Open Style Activity - Navigate to Activity from the Click
openReflection();
}
});
}
public void openReflection() {
Intent intent = new Intent( MainActivity.this, OtherActivity.class );
intent.putExtra("style", style_selection);
startActivity(intent);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.style1:
//disable other buttons
style2.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
style_selection = "#string/style1";
break;
case R.id.style2:
style1.setEnabled(false);
style3.setEnabled(false);
style4.setEnabled(false);
style_selection = "#string/style2";
break;
case R.id.style3:
style1.setEnabled(false);
style2.setEnabled(false);
style4.setEnabled(false);
style_selection = "#string/style4";
break;
case R.id.style4:
style1.setEnabled(false);
style2.setEnabled(false);
style3.setEnabled(false);
style_selection = "#string/style4";
break;
}
}
}
This link might help.
I am new to development field. I am developing android application where in my activity_main layout I have many different items. On clicking one of the button the top layout is replaced with a new layout.
Now instead of defining the new layouts buttons, textview etc in my main class , I want to have a separate class which can initialize my buttons etc, and also in that class I can declare onClickListners.
In my main Activity I want:-
public void onCreate(){
button bb = (Button)findViewById(R.id.mybutton);
View CurrentContentView= getLayoutInflater().inflate(R.layout.activity_main, null, false);
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView);
}
});
}
In my MyNewViewInit() class :-
public class MyNewViewInit{
ImageButton backbutton;
ChipsMultiAutoCompleteTextview search;
ImageButton searchclear;
ImageButton modeTime;
ImageButton modeTag;
TextView modeTimeTxt;
TextView modeTagTxt;
ScrollView mainScroll;
ScrollView selectedScroll;
public MyNewViewInit(View v){
backbutton = (ImageButton)v.findViewById(R.id.backbutton);
search = (ChipsMultiAutoCompleteTextview)v.findViewById(R.id.search);
searchclear = (ImageButton)v.findViewById(R.id.searchclear);
modeTime = (ImageButton)v.findViewById(R.id.modeTime);
modeTag = (ImageButton)v.findViewById(R.id.modeTag);
modeTimeTxt = (TextView)v.findViewById(R.id.modeTimeTxt);
modeTagTxt = (TextView)v.findViewById(R.id.modeTagTxt);
mainScroll = (ScrollView)v.findViewById(R.id.HAT1);
selectedScroll = (ScrollView)v.findViewById(R.id.HAT2);
tag = new OtherHelper().arrayread();
mainHashTag.setVisibility(View.GONE);
selectedHashTag.setVisibility(View.GONE);
clickListners();
}
public void clickListners(){
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
searchclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTimeTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTagTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
So when I try using this code the onclicklistners are not working.
What is the best way to do this.
Thanks
Write an Interface in your class,
class MyNewViewInit{
public interface ClickListenerInterface{
void onCLickSomething(Something something);
}
ClickListenerInterface mClickListener;
...
public MyNewViewInit(View v,ClickListenerInterface clickListener){
mClickListener = clickListener;
...
}
public void clickListners(){
...
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mClickListener!=null){
mClickListener.onCLickSomething(v);
}else{
//throw an error that activity should implement ClickListenerInterface
}
}
});
}
}
In your Activity,
class MyActivity extends Activity implements ClickListenerInterface{
public void onCreate(){
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView,this);
//if using Android Studio, then use the context menu to implement the interface
}
});
}
void onCLickSomething(Something something){
//do something with something
}
}
You can try do a abstract class extends Activity, like :
public abstract class MyInitActivity extends Activity
{
EditText editText;
TextView textView;
#Override
public void setContentView(int layoutResID)
{
super.setContentView(layoutResID);
editText = (EditText)findViewById(R.id.editText);
textView = (TextView)findViewById(R.id.textView);
}
}
and in your main activity you can extend your abstract class, like :
public class MainActivity extends MyInitActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*********** Listener ************/
public void onButtonClick(View v)
{
textView.setText(editText.getText().toString());
}
}
you can also implement onClick() in the abstract class
i don't know if its a good practice , i prefer do the methods with view arguments and set this methods in the layout.xml file .
I had use this in one of code
STEP 1
Declare button in layout(XML file)
STEP 2
Declare all ur button like this--
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
Button btn3 = (Button)findViewById(R.id.btn3);
............so on
STEP 3
implement View.OnClickListener
Now after step 2 do this
btn1.setOnClickListener(this);
.
.
.
same way for all
STEP 4:
Use switch case inside Onclick
public void OnClick(View v)
{
case:R.id.btn1{
//ur command similarly use multiple case stmt for many button
}
}
I am having a layout file and 5 buttons -- 1 for next and 2 for previous etc. Application runs fine, when I click first button, it loads the image correctly, then as I click another button, it gives the error -Caused by java lang Out Of Memory Error
public class MainActivity extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btn7;
ImageButton btn8;
ImageView image1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnButton2();
addListenerOnButton3();
addListenerOnButton4();
addListenerOnButton5();
addListenerOnButton6();
}
public void addListenerOnButton() {
btn1= (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
}
}
);
}
public void addListenerOnButton2() {
btn2= (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k2);
}
}
);
}
public void addListenerOnButton3() {
final Context context =this;
btn3= (ImageButton) findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k3);
}
}
);
}
public void addListenerOnButton4() {
final Context context =this;
btn4= (ImageButton) findViewById(R.id.btn4);
btn4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k4);
}
}
);
}
public void addListenerOnButton5() {
final Context context =this;
btn5= (ImageButton) findViewById(R.id.btn5);
btn5.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k5);
}
}
);
}
public void addListenerOnButton6() {
final Context context =this;
btn6= (ImageButton) findViewById(R.id.btn6);
btn6.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k6);
}
}
);
}
You are changing the whole layout instead of changing the image. When you do that, garbage collector is not called and you should be calling it on your own. Since it's not called, you are going to get that error pretty soon.
Do it like this instead
int[] imageArray = {R.drawable.image1, R.drawable.image2, ... };
and when you click a button, just do
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
someImageView.setImageResource(imgageArray[0])
}
}
Also make sure you don't use too big images. The take up a lot of memory.
i have 6 layouts and each layout having two button back and next..without button back and next application runs properly but when i put code in back and next button having name button 7 is used for back and button 8 is used for next layout it gives me a error null exception.
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btn7;
ImageButton btn8;
ImageView view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton7();
//addListenerOnButton8();
addListenerOnButton();
addListenerOnButton2();
addListenerOnButton3();
addListenerOnButton4();
addListenerOnButton5();
addListenerOnButton6();
public void addListenerOnButton7() {
btn7= (ImageButton) findViewById(R.id.btn7);
btn7.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton() {
btn1= (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton2() {
btn2= (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k2);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton3() {
btn3= (ImageButton) findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k3);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton4() {
btn4= (ImageButton) findViewById(R.id.btn4);
btn4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k4);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton5() {
btn5= (ImageButton) findViewById(R.id.btn5);
btn5.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k5);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton6() {
btn6= (ImageButton) findViewById(R.id.btn6);
btn6.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k6);
Runtime.getRuntime().gc();
}
}
);
}
You already use it in there
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Looking at the code, it looks like it comes from inside an activity so you need to call setContentView(R.layout.activity_main); in the onCreate() of your activity before btn7= (ImageButton) findViewById(R.id.btn7); and that should fix the NullPointer.
Hi,
I have an issue as the above image shown, on the left there is a button inside a webview. When I click on button a popupwindow in android should be appeared. In that popup window i need a webview.
now its all working fine without the webview in popup window.
this popup window is invoked by javascript interface
public class AppJavaScriptProxy {
private Activity activity = null;
public AppJavaScriptProxy() {
}
#JavascriptInterface
public String showMessage(String footNoteNo) {
Integer footNoteNoInt = Integer.parseInt(footNoteNo);
footnote = myDbHelper.getFootnote(chapterNumber, footNoteNoInt);
try {
LayoutInflater inflater = (LayoutInflater) HomeActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.footnote_popup,
(ViewGroup) findViewById(R.id.popup_footnote));
popup = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
txt_footnote=(TextView) layout.findViewById(R.id.text_footnote);
txt_footnote.setTypeface(malayalamfont);
popup_close = (ImageButton) layout.findViewById(R.id.btn_close_popup);
txt_footnote.setText(footnote);
final WebView footnoteWView = (WebView) layout.findViewById(R.id.footnotePopupWebview);
footnoteWView.getSettings().setJavaScriptEnabled(true);
footnoteWView.loadUrl("file:///android_asset/www/footNotePopup.html");
footnoteWView.clearCache(true);
footnoteWView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){ footnoteWView.loadUrl("javascript:getFootnote('" + footnote + "')");
}
});
popup_close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
catch (Exception e) {
e.printStackTrace();
};
}
}
please help
Try this.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title here");
WebView wv = new WebView(this);
wv.loadUrl("http:\\www.yourweb.com");
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert.show();
public class List extends ActionBarActivity implements OnClickListener {
LinearLayout layoutOfPopup;
PopupWindow popupMessage;
Button popupButton;
WebView popupText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
init();
popupInit();
public void init() {
popupButton = (Button) findViewById(R.id.textview1);
popupText = new WebView(this);
popupText.setBackgroundColor(Color.TRANSPARENT);
String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";
String summary = "Some text go here</body></html>";
popupText.loadData(String.format(text, summary), "text/html", "utf-8");
popupText.setVerticalScrollBarEnabled(true);
popupText.setHorizontalScrollBarEnabled(true);
insidePopupButton = new Button(this);
insidePopupButton.setText("(X)Close");
insidePopupButton.setBackgroundResource(R.drawable.myborder);
layoutOfPopup = new LinearLayout(this);
LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layoutOfPopup.addView(insidePopupButton, lpView);
layoutOfPopup.addView(popupText);
layoutOfPopup.setOrientation(1);
layoutOfPopup.setBackgroundColor(Color.WHITE);
}
public void popupInit() {
popupButton.setOnClickListener(this);
insidePopupButton.setOnClickListener(this);
popupMessage = new PopupWindow(layoutOfPopup,
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
popupMessage.setContentView(layoutOfPopup);
}
#Override
public void onClick(View v) {
//Code goes here and to open the show as drop down
and dismiss
}