I have problem with button in activity_main.xml,
I need to launch by click on "imageButton8" a webView (website URL) in application.
MainActivity.Java
package com.XX.app;
import com.XX.app.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
WebView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Empêcher le téléphone de passer en mode veille
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//When User click on the button imageButton8 the Activity2 launch
Button imageButton8 = (Button) findViewById(R.id.imageButton8);
setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Activity2.class);
startActivityForResult(intent, 0);
}
});
}
private void setOnClickListener(OnClickListener onClickListener) {
// TODO Auto-generated method stub
}
//loads RETURN URL on lastpage
#Override
public void onBackPressed() {
if(this.view.canGoBack())
{
this.view.goBack();
} else {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("إغلاق تطبيق جمعية البر و التعاون")
.setMessage("هل أنت متأكد أنك تريد إغلاق تطبيق جمعية البر و التعاون ؟")
.setPositiveButton("نعم", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("لا", null)
.show();
}
}
}
activity_main.xml for imageButton8 is
<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:background="#drawable/background_1">
<ImageButton
android:id="#+id/imageButton8"
android:onClick="click"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginLeft="140dp"
android:layout_marginTop="380dp"
android:background="#0000"
android:scaleType="fitXY"
android:src="#drawable/btn_www" />
</RelativeLayout>
Activity2.java
package com.XX.app;
import com.XX.app.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Application;
import android.os.Bundle;
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
}
}
activity_webview.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:background="#drawable/background_1">
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
In the same project,
2. Others Issues: I have 08 buttons. How do I execute each button in its template xml?
For example: Button 1 => about_obama.xml. When user click (in this file) it will find the description of obama.
Button 2 => ....xml...etc!
Button 3......
You need to set the onclickListener for the ImageButton using the ImageButton's setOnClickListener() method. So, in your MainActivity, inside your onCreate() method, do the following:
Button imageButton8 = (Button) findViewById(R.id.imageButton8);
// Set the OnClickListener on the button itself
imageButton8.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activity2.class); // Use the MainActivity context
startActivityForResult(intent, 0);
}
});
// You should delete the private setOnClickListener() method as it does not
// have any purpose
To specify the onClick() in the xml when the Button is clicked, set the:
android:onClick:"some_method"
property for your button. Then declare and define this method in your Activity.
Related
My initial plan was to have a UI preference so the user can pick a bg color from the main activity whenever he/she wants - I got that to work BUT it isn't showing up the right color it's specified to. i.e. When Red button is pressed in Main Activity, it shows up Blue in the next activity instead.
Here is a snippet of the code with only two buttons using multiple intents for demo purposes...
My Main layout:
<Button
android:id="#+id/buttonRed"
android:onClick="passBG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RED"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/buttonBlue"
android:onClick="passBG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLUE"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button" />
Main Activity is working:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
View view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view=this.getWindow().getDecorView();
view.setBackgroundResource(R.color.gray);
public void passBG(View v) {
Intent intent = new Intent(this, AudioActivity.class);
intent.putExtra("Red", R.color.red);
intent.putExtra("Blue", R.color.blue);
startActivity(intent);
}
}
But something's not right with the second Activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
public class AudioActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
int redBG = getIntent().getIntExtra("Red", -1);
int blueBG = getIntent().getIntExtra("Blue", -1);
RelativeLayout rootView = (RelativeLayout) findViewById(R.id.activity_audio);
rootView.setBackgroundResource(redBG);
rootView.setBackgroundResource(blueBG);
}
}
I figured out that whichever color in "..."BG is implemented first in
rootView.setBackgroundResource("..."BG)
will determine the one that will only pop out. It's something to do with the sequence I figured - AT FIRST ATTEMPT, I tried using the intents on separate methods i.e.
public void goRED(View v)
{
view.setBackgroundResource(R.color.red);
Intent intent = new Intent(this, AudioActivity.class);
intent.putExtra("Red", R.color.red);
startActivity(intent);
}
public void goBLUE(View v)
{
view.setBackgroundResource(R.color.blue);
Intent intent = new Intent(this, AudioActivity.class);
intent.putExtra("Blue", R.color.blue);
startActivity(intent);
}
BUT THAT WILL ONLY END UP IN ERROR so I called the buttons on the same method to prevent that problem. So now I'm stuck with a new problem - how do I correctly implement multiple intents to show up the right color BG on the next activity? should I use some if-else statement into it? Any help would be appreciated. Thanks!
Well try this:
Main Activity is working:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
View view;
private Button redBtn;
private Button blueBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view=this.getWindow().getDecorView();
view.setBackgroundResource(R.color.gray); //This can be achieved by using the `android:background` attribute to your main element on your activity layout
redBtn = (Button) findViewById(R.id.buttonRed); //also, attributes ids on xml layouts shouldnt use uppercase letters, separete words using '_'
blueBtn = (Button) findViewById(R.id.buttonBlue);
redBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onButtonClick(R.color.red);
}
});
blueBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onButtonClick(R.color.blue);
}
});
}
private void onButtonClick(int color){
Intent intent = new Intent(this, AudioActivity.class);
intent.putIntExtra("background",color);
startActivity(intent);
}
}
then, on your second activity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
public class AudioActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
int color = getIntent().getIntExtra("background", -1);
if(color != -1){
RelativeLayout rootView = (RelativeLayout) findViewById(R.id.activity_audio);
rootView.setBackgroundResource(color);
}
}
}
I integrated Tap Research sdk on my Android app, but application getting force stop.
Any idea why this might be happening?
This is my code tapresearchactivity.java
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.tapr.sdk.TapResearch;
import com.tapr.sdk.TapResearchOnRewardListener;
class MyApp extends Application {
final private String YOUR_API_TOKEN = "fa7dbeeacebe7ae3e0b24e8dccaa6acb";
public class onCreate{} {
super.onCreate();
TapResearch.configure(YOUR_API_TOKEN, this);
}
}
public class TapresearchActivity extends Activity {
private Button show_button;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_tapresearch);
/** Set up button to show an ad when clicked */
show_button = (Button) findViewById(R.id.showbutton);
if (TapResearch.getInstance().isSurveyAvailable()) {
show_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TapResearch.getInstance().showSurvey();
}
});
}
}
}
And this is my xml layout file here activity_tapreserch.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"
tools:context=".TapresearchActivity">
<Button
android:layout_width="298dp"
android:layout_height="68dp"
android:enabled="false"
android:id="#+id/showbutton"
android:background="#drawable/button_blue"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
This is Activity_main.xml for an android app
<?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" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sanzuup150"
android:layout_gravity="center_horizontal" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sanzuuq150"
android:layout_gravity="center_horizontal" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sanzuur150"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I'm very new to android developing so I dont know how to create addListenerOnButton for imageButton2 and imageButton3 by different url. Please help me......
Ths is MainActivity.java
package com.android.screation;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sanzuu.blogspot.com"));
startActivity(browserIntent);
}
});
}
}
A listener should be put on each imageButton.
Each imageButton is an item in the xml then should be (let us say) linked to in the java using the id. Therefore the ids in the same xml should be unique. Always link the element to its reference in xml before using or else the code will null pointer exception.
package com.android.screation;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageButton imageButton;
ImageButton imageButton2;
ImageButton imageButton3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.imageButton1); // refer to the id in the xml
imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
imageButton3 = (ImageButton) findViewById(R.id.imageButton3);
//add the listener to the specific button
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "This is button 1",
Toast.LENGTH_LONG).show();
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sanzuu.blogspot.com"));
startActivity(browserIntent);
}
});
imageButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//what imageButton2 is supposed to do
Toast.makeText(getApplicationContext(), "This is button 2",Toast.LENGTH_LONG).show();
}
});
imageButton3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// what imageButton3 is supposed to do here
Toast.makeText(getApplicationContext(), "This is button 3",Toast.LENGTH_LONG).show();
}
});
}
}
If you need any more help please feel free to ask.
I have two activities and the first is generating properly however the second is not. only the first element in the xml file will show up. here is my xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/alternateActivity"
android:shadowColor="#color/shadowColor"></TextView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/oldActivityButton"
android:text="#string/oldActivityButton"></Button>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/changeText"
android:text="#string/changeTextButton"></Button>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/originalText"
android:id="#+id/textToChange"></TextView>
</LinearLayout>
and here is my activity that corresponds:
package fsg.dev.activitytest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class newActivity extends Activity {
private Button oldActivityButton;
private Button changeText;
private TextView textToChange;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alternate);
oldActivityButton = (Button) findViewById(R.id.oldActivityButton);
changeText = (Button) findViewById(R.id.changeText);
textToChange = (TextView) findViewById(R.id.textToChange);
oldActivityButton.setOnClickListener(new OnClickListener(){
public void onClick(View view){
changeActivity();
}
});
changeText.setOnClickListener(new OnClickListener(){
public void onClick(View view){
changeText();
}
});
}
private void changeActivity(){
Intent i = new Intent(this, activityTest.class);
startActivity(i);
}
private void changeText(){
if(textToChange.equals(R.string.originalText)){
textToChange.setText(R.string.newText);
} else {
textToChange.setText(R.string.originalText);
}
}
}
has anyone else seen this problem? or know of a way to fix it?
Try to add android:orientation="vertical" to your LinearLayout
replace
Intent i = new Intent(this, activityTest.class);
by
Intent i = new Intent().setClass(this, activityTest.class);
I want integrate one combo filled with currency types. please tell me any way to create select box in android. i have created spinner but i have similar different views to show on page. and it is not looking attractive. please share any code for select.
use this dynamic_spinner_main.java
package example.sampleLocalization;
import java.util.ArrayList;
//import com.matthias.dynamicSpinnerT.R;
//import com.matthias.dynamicSpinnerT.R.id;
//import com.matthias.dynamicSpinnerT.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class dynamic_spinner_main extends Activity {
private Spinner m_myDynamicSpinner;
private EditText m_addItemText;
private ArrayAdapter<CharSequence> m_adapterForSpinner;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_spinner);
///////////////////////////////////////////////////////////////
//grab our UI elements so we can manipulate them (in the case of the Spinner)
// or add listeners to them (in the case of the buttons)
m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);
m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
Button addButton = (Button)findViewById(R.id.AddBtn);
Button clearButton = (Button)findViewById(R.id.ClearBtn);
////////////////////////////////////////////////////////////////
//create an arrayAdapter an assign it to the spinner
m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
m_adapterForSpinner.add("gr");
////////////////////////////////////////////////////////////////
//add listener for addButton
addButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
addNewSpinnerItem();
}
});
////////////////////////////////////////////////////////////////
//add listener for addButton
clearButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
clearSpinnerItems();
}
});
}
private void addNewSpinnerItem() {
CharSequence textHolder = "" + m_addItemText.getText();
m_adapterForSpinner.add(textHolder);
}
private void clearSpinnerItems() {
m_adapterForSpinner.clear();
m_adapterForSpinner.add("dummy item");
}
}
main_spinner.xml
<?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">
<EditText android:layout_height="wrap_content"
android:layout_margin="4px"
android:id="#+id/newSpinnerItemText"
android:layout_width="fill_parent"></EditText>
<Button android:layout_height="wrap_content"
android:id="#+id/AddBtn"
android:layout_margin="4px"
android:layout_width="fill_parent"
android:text="Add To Spinner"></Button>
<Button android:layout_height="wrap_content"
android:id="#+id/ClearBtn"
android:layout_margin="4px"
android:layout_width="fill_parent"
android:text="Clear Spinner Items"></Button>
<Spinner android:layout_height="wrap_content"
android:id="#+id/dynamicSpinner"
android:layout_margin="4px"
android:layout_width="fill_parent"></Spinner>
</LinearLayout>