I'm recently started to program and faced with this problem. My button is not clicking in emulator, however, I wrote onClickListener in java. It still doesn't work.
Here is my xml code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/SD"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:background="#color/blue_gray"
android:visibility="visible"
android:onClick="onClick"/>
<Button
android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/SD2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:background="#color/follow"
android:visibility="gone"
android:onClick="onClick"/>
</FrameLayout>
Here is my java code:
enter code here
package com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.example.app.R;
/**
* Created by ww on 12.02.14.
*/
public class fragment_main extends Activity {
Button i1;
Button i2;
protected void onCreate (Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
setContentView(R.layout.fragment_main);
i1= (Button) findViewById(R.id.btn1);
i2=(Button) findViewById(R.id.btn2);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn1:
i1.setVisibility(View.GONE);
i2.setVisibility(View.VISIBLE);
break;
case R.id.btn2:
i1.setVisibility(View.VISIBLE);
i2.setVisibility(View.GONE);
break;
}
}
}
Here's the working code,
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sd"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:onClick="onClick"/>
<Button
android:id="#+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sd2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="140dp"
android:visibility="gone"
android:onClick="onClick"/>
</RelativeLayout>
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button b1;
Button b2;
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);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn1:
b2.setVisibility(View.VISIBLE);
b1.setVisibility(View.INVISIBLE);
break;
case R.id.btn2:
b2.setVisibility(View.INVISIBLE);
b1.setVisibility(View.VISIBLE);
break;
}
}
}
Output (Compiled & ran in Emulator):
When the app starts Button2 will be invisible since it was set as invisible in xml layout.
Once the user click button1 above, button2 will be visible & button1 will be invisible.
you have to register a listener to your buttons, e.g.:
i1 = (Button) findViewById(R.id.btn1);
i2 = (Button) findViewById(R.id.btn2);
//inside onCreate do this:
i1.setOnClickListener(myhandler1);
i2.setOnClickListener(myhandler2);
Then you have to create those listeners.
// somewhere outside onCreate do this:
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
}
};
Related
I wrote below codes for Android in Eclipse:
package mohammad.negahdari.mystartup4;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MyStartup4Activity extends Activity {
public int counter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);
final Button btn1 = (Button) findViewById(R.id.txtCaption);
txtCaption.setText("Hooora");
txtCaption.setTextColor(Color.parseColor("#0000ff"));
txtCaption.setBackgroundColor(Color.parseColor("#ffffff"));
for (int i = 0; i <= 4; i++)
Toast.makeText(MyStartup4Activity.this, "Mohammad " + i, Toast.LENGTH_SHORT).show();
txtCaption.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("LOG", "Clicked");
Toast.makeText(MyStartup4Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
counter++;
txtCaption.setText("Number is : " + counter);
}
});
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("LOG", "Clicked");
Toast.makeText(MyStartup4Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
txtCaption.setVisibility(View.GONE);
}
});
}
}
and receive this error:
The application has stopped unexpectedly. please try again
But when I delete these lines:
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("LOG", "Clicked");
Toast.makeText(MyStartup4Activity.this, "Clicked", Toast.LENGTH_SHORT).show();
txtCaption.setVisibility(View.GONE);
}
});
I have no error.
My 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" >
<TextView
android:id="#+id/txtCaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff00ff"
android:gravity="center"
android:text="..."
android:textColor="#00ff00"
android:textSize="40dip" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
The application has stopped unexpectedly. please try again But when I
delete these lines: have no error.
Obviously,you are referring to the wrong id
final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);
final Button btn1 = (Button) findViewById(R.id.txtCaption);
Check your button id in xml. You should assign different id to your button.
In your case, you should use
final Button btn1 = (Button) findViewById(R.id.btn1);
final TextView txtCaption = (TextView) findViewById(R.id.txtCaption);
final Button btn1 = (Button) findViewById(R.id.txtCaption);
Both txtCaption and btn1 point to the same id (R.id.txtCaption)
My xml codes :
<?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" >
<TextView
android:id="#+id/txtCaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff00ff"
android:gravity="center"
android:text="..."
android:textColor="#00ff00"
android:textSize="40dip" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
hello I'm new to android and I have a problem with multiple button first I implemented multiple button in my MainActivity and it is work with me now I create another activity and implement the same what i do in the mainActivity in the new activity I have a multiple button in this activity
the new Activity (Activity6.class):
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Activity6 extends AppCompatActivity implements
View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adminbackdoor);
Button ee = (Button) findViewById(R.id.button12);
Button ff = (Button) findViewById(R.id.button9);
Button gg = (Button) findViewById(R.id.button10);
Button hh = (Button) findViewById(R.id.button11);
ee.setOnClickListener(this);
ff.setOnClickListener(this);
gg.setOnClickListener(this);
hh.setOnClickListener(this);
}
#Override
public void onClick (View vv){
switch (vv.getId()) {
case R.id.button12:
Intent e = new Intent(Activity6.this, Admin1.class);
startActivity(e);
break;
case R.id.button9:
Intent f = new Intent(Activity6.this, User1.class);
startActivity(f);
break;
case R.id.button10:
Intent g = new Intent(Activity6.this, Teacher1.class);
startActivity(g);
break;
case R.id.button11:
Intent h = new Intent(Activity6.this, Class1.class);
startActivity(h);
break;
default:
break;
}
}
Admin1.class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Admin1 extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin);
}
}
User1.class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class User1 extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adminuser);
}
}
activity Teacher1.class and Class1.class have the same with Admin1.class and User1.class but with different layout.
Note: I add all the activities in manifest like this:
<activity android:name=".Activity6"></activity>
<activity android:name=".Admin1"></activity>
<activity android:name=".Class1"></activity>
<activity android:name=".Teacher1"></activity>
<activity android:name=".User1"></activity>
adminbackdoor.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="العمليات"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:fontFamily="serif"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginTop="60dp"
android:layout_marginRight="140dp" />
<Button
android:id="#+id/button12"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="admin"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="#+id/button9"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="user"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="#+id/button10"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="teacher"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="#+id/button11"
android:layout_width="231dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:background="#drawable/round"
android:text="class"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
and onClick does not work with me I do not know what is the problem ??
if I run the application and click on the button they did not work ( go to another layout )
First of all you should extend Activity and not AppCompatActivity.
And one thing I can recommend is to use the click listener like this in this situation:
Button ee = (Button) findViewById(R.id.button12);
ee.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent e = new Intent(Activity6.this, Admin1.class);
startActivity(e);
}
});
How to I hide the next button and will only show when the user choose a radio group button
I have this XML code
<?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">
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/assembly"
android:checked="false"
/>
<RadioButton
android:id="#+id/csharp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/java"
android:checked="false" />
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:onClick="OnClick"
/>
</RadioGroup>
</LinearLayout>
and this class code
package com.example.quiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
/**
* Created by leste on 3/5/2016.
*/
public class CS_Category extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cs_category);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = (RadioButton) findViewById(selectedId);
if (radioSexButton.getId() == R.id.assembly) {
Intent i = new Intent(CS_Category.this, CS_Assembly.class);
startActivity(i);
} else {
if (radioSexButton.getId() == R.id.csharp) {
Intent i = new Intent(CS_Category.this, CS_Csharp.class);
startActivity(i);
} else {
if (radioSexButton.getId() == R.id.java) {
Intent i = new Intent(CS_Category.this, CS_Java.class);
startActivity(i);
}
}
Toast.makeText(CS_Category.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
What should I do to hide the next button and show only if there is a selected radio button
In your button xml you should use:
android:visibility="gone"
In java you should use:
btnDisplay.setVisibility(View.VISIBLE);
First of all I would take the button out of the radio group and just have it be below it. Then in my java code I would have this function:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.assembly:
if (checked)
make_button_visible();
break;
case R.id.csharp:
if (checked)
make_button_visible();
break;
}
}
Make sure that each radio button has onClick="onRadioButtonClicked" set.
Then have a function called make_button_visible() that has these lines:
Button mButton=(Button)findViewById(R.id.btnDisplay);
mButton.setVisibility(View.VISIBLE);//This will make it visible
add below code in your onCreate method
addListenerOnButton();
btnDisplay.setVisibility(View.INVISIBLE);
radioSexGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (btnDisplay.getVisibility() == View.INVISIBLE)
btnDisplay.setVisibility(View.VISIBLE);
}
});
I had three button on my main form. I added a third. When I try to assign the button using findViewById a null is returned.
This is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1" android:clickable="false">
<TextView android:layout_width="wrap_content"
style="#style/TitleText"
android:text="#string/app_name"
android:background="#drawable/black_gradient">
</TextView>
<Button android:id="#+id/btnvalidatemark"
android:background="#drawable/grad_btn_green"
style="#style/ButtonText" android:text="#string/validate_mark_button">
</Button>
<Button android:id="#+id/btncheckregistry"
android:background="#drawable/grad_btn_yellow"
style="#style/ButtonText" android:text="#string/check_registry_button">
</Button>
<Button android:id="#+id/btnregcreds"
android:background="#drawable/grad_btn_red"
style="#style/ButtonText" android:text="#string/registry_config_button">
</Button>
</TableLayout>
This is my Activity code:
package uid.android.uidchecker;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.util.Log;
public class UIDCheckerActivity
extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Button validatebutton = (Button) findViewById(R.id.btnvalidatemark);
Button checkbutton = (Button) findViewById(R.id.btncheckregistry);
Button setcredbutton = (Button) findViewById(R.id.btnregcreds);
validatebutton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(UIDCheckerActivity.this, SelectFileActivity.class);
i.putExtra("func", "validatemark");
startActivity(i);
}
});
checkbutton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//Intent i = new Intent(UIDCheckerActivity.this, validatemark.class);
Intent i = new Intent(UIDCheckerActivity.this, SelectFileActivity.class);
i.putExtra("func", "checkregisrty");
startActivity(i);
}
});
setcredbutton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(UIDCheckerActivity.this, RegistryCredsActivity.class);
startActivity(i);
}
});
}
}
I'm working in an application that has in the same view a set of buttons and a web view(GONE), when the app starts it shows only the buttons from where at a click it shows the web view and loads a url (the layout where the buttons are is set to GONE) that contains a flash media player and a single button that when is click it makes the web view invisible again and shows the first layout that contains the buttons to be able to choose a different website, the problem is that when the web view goes invisible the flash media players stays stuck on the screen and blocks the buttons, how can I fix this? It would be ok if the media player stays behind the buttons but I cannot find an answer, any help will be highly appreciated, Thank you.
Update with Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="match_parent" android:orientation="vertical">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Web 1" android:id="#+id/buttonWeb1"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/buttonWeb2" android:text="Web 2"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Web 3" android:id="#+id/buttonWeb3"></Button>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="#+id/linearLayout2" android:layout_width="match_parent" android:orientation="vertical">
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/buttonBack" android:text="Back to Buttons"></Button>
<WebView android:layout_height="match_parent" android:layout_width="match_parent" android:id="#+id/webView"></WebView>
</LinearLayout>
And the main Activity
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
public class FlashejemploActivity extends Activity {
WebView wv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
final LinearLayout buttons = (LinearLayout) findViewById(R.id.linearLayout1);
final LinearLayout webV = (LinearLayout) findViewById(R.id.linearLayout2);
final Button web1 = (Button) findViewById(R.id.buttonWeb1);
final Button web2 = (Button) findViewById(R.id.buttonWeb2);
final Button web3 = (Button) findViewById(R.id.buttonWeb3);
final Button back = (Button) findViewById(R.id.buttonBack);
wv = (WebView) findViewById(R.id.webView );
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.setWebViewClient(new WebViewClient());
wv.setInitialScale(1);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setUseWideViewPort(true);
final Activity PActivity = this;
wv.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
PActivity.setTitle("Cargando....");
PActivity.setProgress(progress * 100);
if(progress == 100)
PActivity.setTitle(R.string.app_name);
}
});
webV.setVisibility(View.GONE);
web1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttons.setVisibility(View.GONE);
webV.setVisibility(View.VISIBLE);
wv.loadUrl("http://los40.com.mx/Player.htm");
}});
web2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttons.setVisibility(View.GONE);
webV.setVisibility(View.VISIBLE);
wv.loadUrl("http://www.beat1009.com.mx/n2/paginas/radio.php");
}});
web3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttons.setVisibility(View.GONE);
webV.setVisibility(View.VISIBLE);
wv.loadUrl("http://besame.com.mx/Player.htm");
}});
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttons.setVisibility(View.VISIBLE);
webV.setVisibility(View.GONE);
}});
}
}
I know its been a while but I have just solved a similar problem by getting the webview to empty
wv.loadUrl("about:blank");