Warning Unexpected text in layout file and app crashes on click - android

Getting Warning : unexpected text found in layout file : ""
Also on clicking the button, app is crashing.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/container">
<Button android:id="#+id/bntStartService"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/broadcast_intent"
android:onClick="broadcast_intent">
</Button>
</LinearLayout>
Please help.

// try this way hope this will help you...
1. activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="#+id/container">
<Button android:id="#+id/bntStartService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/broadcast_intent"
android:onClick="broadcast_intent">
</Button>
</LinearLayout>
2.MainActivity .java
public class MainActivity extends Activity{
private Button bntStartService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bntStartService = (Button) findViewById(R.id.bntStartService);
bntStartService.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
broadcast_intent(view);
}
});
}
private void broadcast_intent(View view){
Toast.makeText(this,"Button Clicked",Toast.LENGTH_SHORT).show();
}
}

Related

how to show a website within the same activity?

I have created an Application where on Button click the Website should open within the same Activity.
Button 1 -> should load website = >"google"
Button 2 -> should load website = >"google play"
Button 3 -> should load website = >"you tube"
This is How i want it to look: https://i.stack.imgur.com/yzDF9.png
Sample code of activity_main.xml
<?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"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<ScrollView
android:id="#+id/Srcollview1"
android:layout_width="100dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="program 1"
></Button>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="program 2"
></Button>
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="program 3"
></Button>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webviewing"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</WebView>
</LinearLayout>
</LinearLayout> </LinearLayout>
Sample code for MainActivity.java
Button b1,b2,b3;
WebView webView;
LinearLayout l1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.btn1);
b2 = (Button)findViewById(R.id.btn2);
b3 = (Button)findViewById(R.id.btn3);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView = (WebView) findViewById(R.id.webviewing);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView = (WebView) findViewById(R.id.webviewing);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://play.google.com/");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView = (WebView) findViewById(R.id.webviewing);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.youtube.com/");
}
});
}
Your code works fine, except you set the listener for b2 twice. Typo maybe?
If you are facing connection errors, just add the Internet permission to AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET"/>

ViewFlipper/ViewSwitcher 2nd View Empty

I am a newbie to Android programming and now I want to create a ViewFlipper or ViewSwitcher for 2 views which will switch view between 1 and 2 columns. The problem is that the 1st view is ok, but when I use showNext() or showPrevious(), the 2nd view shows as blank, and I have been completely out of mind thinking what is wrong. I have tried ViewFlipper and ViewSwitcher with the same results.
layout_devicelistmainswitcher.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:contentDescription="Grid/List View"
android:id="#+id/viewbutton"
android:background="#drawable/viewicon"
android:layout_gravity="right" />
<ViewFlipper
android:id="#+id/vflipper"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:numColumns="1"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/grid"
android:scrollbars="none" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp">
<GridView
android:numColumns="2"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/grid_l"
android:scrollbars="none" />
</LinearLayout>
</ViewFlipper>
mainactivity.java
public class activity_devicelist extends AppCompatActivity {
GridView grid;
Boolean viewtype = true;
ViewFlipper switcher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_devicelistmainswitcher);
Button button = (Button) findViewById(R.id.viewbutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity_devicelist.this, "Button Clicked", Toast.LENGTH_SHORT).show();
switcher = (ViewFlipper) findViewById(R.id.vflipper);
switcher.showPrevious();
}
});
}
}
Take your findViewById out like below,then It should work
switcher = (ViewFlipper) findViewById(R.id.vflipper);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity_devicelist.this, "Button Clicked", Toast.LENGTH_SHORT).show();
switcher.showPrevious();
}
});

binary xml file line #7 error inflating class Button

I write a program with FrameLayout, when i clicked on the first button, the program show introduce.xml but when i clicked on the second button, the program Force Class. i think because i use Button in product.xml file because when i changed button to imageView, the program runs good. Why? How can solve this?
Runtime Error:
binary xml file line #7 error inflating class Button in android
MainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/white">
<TextView
android:id="#+id/logoText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/dark_blue"
android:text="#string/logotxt"/>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:layout_gravity="bottom">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_weight="1"/>
<Button android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/b1"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="#string/btn1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/b2"
android:layout_toRightOf="#id/btn1"
android:layout_alignBottom="#id/btn1"
android:text="#string/btn2"
/>
<Button android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/b3"
android:layout_toRightOf="#id/btn2"
android:layout_alignBottom="#id/btn1"
android:text="#string/btn3"
/>
<Button android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/b4"
android:layout_toRightOf="#id/btn3"
android:layout_alignBottom="#id/btn1"
android:text="#string/btn4"
/>
</RelativeLayout>
</LinearLayout>
product.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Buton
android:id="#+id/android_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/red"
android:text="#string/android"/>
</LinearLayout>
MainActivity.java:
public class MainActivity extends FragmentActivity {
public String fonts="BZar.ttf";
TextView logoText;
Button btnIntroduce;
Button btnProduct;
Button btnContact;
Button btnMore;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setFont();
btnIntroduce.setOnClickListener(onClickListener);
btnProduct.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener=new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
switch(v.getId()){
case R.id.btn4:
Log.e("button", "4click");
//ft.add(R.id.frameLayout, new Introduce());
ft.setCustomAnimations(R.anim.push_right_in, R.anim.push_left_out);
ft.replace(R.id.frameLayout, new Introduce());
ft.commit();
break;
case R.id.btn3:
Log.e("button", "3Click");
ft.setCustomAnimations(R.anim.push_right_in, R.anim.push_left_out);
ft.replace(R.id.frameLayout, new Product());
ft.commit();
break;
}
}
};
Product.java:
public class Product extends Fragment{
public String fonts="BZar.ttf";
Typeface face;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view=inflater.inflate(R.layout.product, container, false);
face=Typeface.createFromAsset(getActivity().getAssets(), "font/"+fonts+"");
/*Button btnAndroid=(Button)view.findViewById(R.id.android_btn);
btnAndroid.setTypeface(face);
String android=(String)btnAndroid.getText().toString();
btnAndroid.setText(PersianReshape.reshape(android));*/
return view;
}
}
Its a typo. product.xml only has 1 T in Button.

How to setting listener for button inside the Crouton Toast and clear that Crouton Toast using that button?

I have an app, which shows toast below the actionbar. I'm using Crouton library for displaying toast. Here I'm placing custom layout for toast, that layout contains one textview and one button (for close option). So Toast appears with button. If I click that it should close the toast but nothing happens.. Below Code for this example. Any Help Appreciated
MainActivity.java
public class MainActivity extends SherlockActivity implements OnClickListener {
private View customToastView;
private TextView customToastMessageView;
private Button customToastCloseButton;
private Button doneButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
doneButton = (Button) findViewById(R.id.done_button);
customToastView = getLayoutInflater().inflate(R.layout.toast_custom_layout, null);
customToastMessageView = (TextView) customToastView.findViewById(R.id.messages);
customToastCloseButton = (Button) customToastView.findViewById(R.id.close_button);
customToastCloseButton.setOnClickListener(this);
doneButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.done_button:
Crouton.cancelAllCroutons();
customToastMessageView.setText("Message");
Crouton.show(this, customToastView);
break;
case R.id.close_button:
Crouton.cancelAllCroutons();
break;
}
}
}
toast_custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/messages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="23dp"
android:text="Button" />
</RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/done_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="146dp"
android:text="Button" />
</RelativeLayout>

Android Button does not startActivity or react to onClick

I'm trying to use a button to start a new activity like this:
public class MainActivity extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.button1)).setOnClickListener(this);
((Button) findViewById(R.id.button2)).setOnClickListener(this);
}
public void onClick(View view) {
Button clickedBtn = (Button) view;
switch (clickedBtn.getId()) {
case R.id.button1: startActivity(new Intent(this, NewActivity.class));
break;
case R.id.button2: startActivity(new Intent(this, AnotherActivity.class));
break;
}
}
}
But nothing seems to happen when I click on either button. I guess it's because the onClick method does not know which button is actually clicked...but I'm not sure how I can fix this...please advice, thanks!
Edit: added XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first button" />
<Button android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second button" />
</LinearLayout>
</ScrollView>
</LinearLayout>
I put the code in the wrong file; everything is working now. Thanks!

Categories

Resources