Display webview in CustomDilog in android - android

This Class
package com.example.foootnotes;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
public class compare extends Activity {
WebView web1;
WebView web2;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.compare);
// String str1="<html>This first Html data</html>";
String str2 = "<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework"
+ "help help with homework homework assignments elementary school high school middle school"
+ "// --><font color='#60c000' size='4'><strong>Please!</strong></font>"
+ "<img src='http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif' />";
Intent i = getIntent();
String str1 = i.getStringExtra("htmlrespones");
web1 = (WebView) findViewById(R.id.webfirst);
web2 = (WebView) findViewById(R.id.websecond);
web1.loadDataWithBaseURL("", str1, "text/html", "UTF-8", "");
web2.loadDataWithBaseURL("", str2, "text/html", "UTF-8", "");
btn = (Button) findViewById(R.id.compare);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
WebView web5 = (WebView) findViewById(R.id.webView1);
WebView web6 = (WebView) findViewById(R.id.webView2);
web5.loadDataWithBaseURL("", "This is first ", "text/html",
"UTF-8", "");
final Dialog dialog = new Dialog(compare.this);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Differences");
Button dialogButton = (Button) dialog
.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
my custom .xml file
<?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" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/image"
android:textColor="#FFF" />
/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:text=" Ok " />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/dialogButtonOK"
android:layout_marginTop="41dp"
android:text="This is for first Html"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentRight="true"
android:text="This is for scond Html" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1" />
<WebView
android:id="#+id/webView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/webView1" />
</RelativeLayout>
I am able to Display Textview But i want display data in web view in custom Dialog when i display it is in Textview it working Fine But i have display large data in Webview i cant display data in Textview so please tell how i display data in webview within Coustome dilog ..Please Help!

Try this
String str2 = "<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework"
+ "help help with homework homework assignments elementary school high school middle school"
+ "// --><font color='#60c000' size='4'><strong>Please!</strong></font>"
+ "<img src='http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif' />";
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(compare.this);
LayoutInflater layoutInflater = (LayoutInflater)compare.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.Your_layout, null);
dialog.setContentView(layout);
dialog.setTitle("Differences");
Button dialogButton = (Button) layout
.findViewById(R.id.dialogButtonOK);
WebView web5 = (WebView) layout.findViewById(R.id.webView1);
WebView web6 = (WebView) layout.findViewById(R.id.webView2);
web5.getSettings().setJavaScriptEnabled(true);
web5.loadDataWithBaseURL(null, str2, "text/html",
"UTF-8", null);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
Your Xml is
<?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" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1" />
<WebView
android:id="#+id/webView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/webView1" />
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:text=" Ok " />
</RelativeLayout>

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"/>

Change the text message, to be a clikable message

I'm trying to change the text message of the AlertDialog message. To be a clikable message.
With this code the text is blue, but not clikable
I want the phone, email and site from text to be clikable
final SpannableString All =
new SpannableString(textmess + "\n\nTel :\n " + phone + "\n\nEmail : \n " + email + "\n\nAddress: \n " + address + "\n\nWeb site : \n " + site );
Linkify.addLinks(All, Linkify.ALL );
AlertDialog.Builder builder = new AlertDialog.Builder( new
ContextThemeWrapper(context, R.style.AlertDialogCustom) );
builder.setTitle(title).setMessage(All);
builder.create();
//not working
//TextView textView = (TextView) findViewById(android.R.id.message);
builder.show();
GeoPoint1.close();
db1.close();
Create a custom layout for the dialog and also use setMovementMethod to get it done
Ex:
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate(R.layout.dialog_custom, null);
TextView dTitle = (TextView) view.findViewById(R.id.textDialogTitle);
TextView dMessage = (TextView) view.findViewById(R.id.textDialogMessage);
dMessage.setMovementMethod(LinkMovementMethod.getInstance());
dTitle.setText("Title");
dMessage.setText(ALL);
dialog.setView(view);
dialog.show();
This example will help you :
res/layout/dialog.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000">
<TextView
android:id="#+id/tv_pc_forDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tv_pc_dialogTitle"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:ems="10"
android:layout_centerHorizontal="true"
android:text="THIS IS A CLICKABLE MESSAGE"
android:singleLine="true"
android:textColor="#FFFFFF" >
</TextView>
<TextView
android:id="#+id/tv_pc_dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:text="SEARCH"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/bt_pc_goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_pc_forDialog"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="GO"
android:textColor="#FFFFFF"
android:textSize="25sp" /> </RelativeLayout>
res/layout/activity_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: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=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="177dp"
android:text="Open Dialog" />
</RelativeLayout>
MainActivity.java :
package com.bhavit.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button openDialog = (Button) findViewById(R.id.button1);
openDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog dialog = new Dialog(MainActivity.this); // here write the name of your activity in place of "YourActivity"
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
TextView tv = (TextView)dialog.findViewById(R.id.tv_pc_dialogTitle);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/GOTHIC.TTF"); // here GOTHIC.TTF is the font-file I have pasted in the asset/fonts folder in my project
tv.setTypeface(tf); // Set the typeface like this
Button bt = (Button) dialog.findViewById(R.id.bt_pc_goButton);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Write here, what do you want to do on button click
}
});
TextView tview = (TextView) dialog.findViewById(R.id.tv_pc_forDialog);
tview.setOnClickListener(new View.OnClickListener() { // set onClickListener to the textview
#Override
public void onClick(View v) {
// Write here, what do you want to do on Message click
Toast.makeText(getApplicationContext(), "You clicked on message", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
});
}
}
What I have done here is that I made a customised dialog, and set an onClickListener on the TextView shown as a message. Like this, you can achieve your goal.

Dialog is not displaying textview & imageview

I am new to android world and am working on dialog and in that I am trying to create a text view, button and imageview, but it is neither showing me error nor textview or imageview. It is showing a button. Here is the image:
.
But i am want to display my dialogue as follows
My xml file is as follows ....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity">
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/submit_dialog"
android:layout_alignParentRight="true"
android:layout_marginRight="37dp"
android:contentDescription="#string/add_Friend"
android:src="#drawable/add" />
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="64dp"
android:layout_toLeftOf="#+id/add"
android:text="#string/submit" />
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/add"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/add"
android:ems="10" />
</RelativeLayout>
I am calling it in java file as follows
dialog = new Dialog(context);
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.activity_add_friends_dialog, null, false);
dialog.setContentView(view);
dialog.setCancelable(true);
//dialog.setContentView(R.layout.activity_add_friends_dialog);
//dialog.setTitle("Add Friends");
add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);
submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
try {
friendsCount++;
Log.d("addFriendsActivity", "ashish comes here ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
submit_dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
startActivity(i);
}
});
dialog.show();
After some google I understand that the issues might be in r.java or you have imported android .r but i have not done any thing in this ..
Thanks in advance.... :)
<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:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
There is no error in your java code. Its perfect you have just change your xml. Use this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity" >
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:ems="10" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/writename"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/writename"
android:contentDescription="Add Friend"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/writename"
android:layout_below="#+id/add"
android:layout_marginRight="46dp"
android:text="Submit" />
</RelativeLayout>
For your xml I think it should look like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/add_btn" />
<ImageButton
android:id="#+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/add_btn"
android:background="#null" />
<Button
android:id="#+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/my_edit_text"
android:text="Submit" />
</RelativeLayout>
Be aware that there can be some typo mistakes, because I didn't compile that code, and you should change your real variables. And create your AlertDialog like this:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MyActivity.this);
View mView = mInflater.inflater(R.layout.my_layout, null, false);
EditText mEdit = (EditText) mView.findViewById(R.id.my_edit_text);
ImageButton mImageBtn = (ImageButton) mView.findViewById(R.id.add_btn);
Button mSubmit = (Button) mView.findViewById(R.id.submit_btn);
mBuilder.setView(mView);
AlertDialog mAlert = mBuilder.create();
mAlert.show();
Using AlertDialog.Builder instead of Dialog is lot more easier for your situation (at least in my opinion). And you can add your Submit Button to your AlertDialog as default positive button.
Hope this helps you.
Look at your code i think
add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);
as
add= (ImageView) view.findViewById(R.id.add);
friends1 = (EditText) view.findViewById(R.id.writename);
That should work.
Try this..
final Dialog dialog = new Dialog(AddFriendsActivity.this);
dialog.setContentView(R.layout.activity_add_friends_dialog);
dialog.setCanceledOnTouchOutside(true);
add= (ImageView) dialog.findViewById(R.id.add);
EditText friends1 = (EditText) dialog.findViewById(R.id.writename);
submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
add.setOnClickListener(new OnClickListener() {
//#Override
#SuppressWarnings({ "unchecked", "rawtypes" })
#SuppressLint("NewApi")
public void onClick(View v) {
friendsCount++;
//TextView textview = new TextView(context);
//textview.setId(friendsCount);
//final RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//params.addRule(RelativeLayout.BELOW, friendsCount--);
//textview.setLayoutParams(params);
//textview.setVisibility(View.VISIBLE);
//relativelayout.addView(textview ,params);
Log.d("addFriendsActivity", "ashish comes here ");
}
});
submit_dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
startActivity(i);
}
});
dialog.show();
Your view is covered.
May be you can try to set your parent layout to Linearlayout, not RelativeLayout.

button events in dynamic ListAcitvity android

Hi I have generated a dynamic list. I have SeekBar and a ToggleButton for each of the rows in List. I have been trying to generate events for the toggle and seek from the last 1 week but in vain. Please help me with it.
Here is the XML code with the List(smartunits.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/unitsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/grid_gradient"
android:orientation="vertical" >
<ListView
android:id="#android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Now the code for each row(listunits.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" >
<TextView
android:id="#+id/unitName"
android:layout_width="40px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="20px"
android:textColor="#ffffff" />
<TextView
android:id="#+id/unitCodeId"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#ffffff"
android:visibility="invisible" />
<SeekBar
android:id="#+id/unitsseek"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<ToggleButton
android:id="#+id/toggle"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:background="#drawable/off_grey"
android:textOff=""
android:textOn="" />
</LinearLayout>
Here is the code for ListActivity
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SimpleCursorAdapter;
import android.widget.ToggleButton;
public class UnitsListActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// put the bottom code here
setContentView(R.layout.smartunits);
Bundle extras = getIntent().getExtras();
final AutomationDBAccessor db = new AutomationDBAccessor(this);
Cursor cUnits = db.getUnits(extras.getInt("roomFloorId"), extras.getInt("roomId"));
String[] from = new String[] { AutomationDBAccessor.colUnitName, AutomationDBAccessor.colUnitCodeID };
int[] to = new int[] { R.id.unitName, R.id.unitCodeId };
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.listunits, cUnits, from, to);
this.setListAdapter(sca);
View viewUnits = LayoutInflater.from(getBaseContext()).inflate(R.layout.listunits, null);
final ToggleButton toggle = (ToggleButton) viewUnits.findViewById(R.id.toggle);
toggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) { // TODO Auto-generated method stub
if (toggle.isChecked()) {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.on));
} else {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.off_grey));
}
}
});
}
}
Now I am unable to handle the ToggleButton events.
I never try this, But you can do something like,
In your listunits.xml
<ToggleButton
android:id="#+id/toggle"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:textOff=""
android:textOn=""
android:onClick="toggleButtonClick" />
And in your UnitsListActivity
write this method..
public void toggleButtonClick(View view)
{
ToggleButton toggle = (ToggleButton) view;
if (toggle.isChecked()) {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.on));
} else {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.off_grey));
}
}
Run this code and let me know what happen..

"Source not found" error in Eclipse

Developing my first app for android. Works OK on emulator, but on phone I get a "Source not found error" ViewRoot.handleMessage(Message)line:1757. I get the error when I press button number 4 on the application and try to display my media images on phone.
Code:
package com.example.famiily_connect_v1;
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.EditText;
import android.widget.TextView;
public class family_connect extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button incrementButton, decrementButton, makecallButton,
media_cameraButton;
TextView numberdisplayTextView;
EditText inputEditText;
int A = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
incrementButton = (Button) findViewById(R.id.Button01);
decrementButton = (Button) findViewById(R.id.Button02);
makecallButton = (Button) findViewById(R.id.Button03);
media_cameraButton = (Button) findViewById(R.id.Button04);
numberdisplayTextView = (TextView) findViewById(R.id.TextView01);
inputEditText = (EditText) findViewById(R.id.EditText01);
incrementButton.setOnClickListener(this);
decrementButton.setOnClickListener(this);
makecallButton.setOnClickListener(this);
media_cameraButton.setOnClickListener(this);
numberdisplayTextView.setOnClickListener(this);
inputEditText.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.Button01:
A++;
numberdisplayTextView.setText(String.valueOf(A));
break;
case R.id.Button02:
A--;
numberdisplayTextView.setText(String.valueOf(A));
break;
case R.id.Button03:
break;
case R.id.Button04:
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivity(myIntent);
break;
default:
break;
}
}
}
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" android:background="#00AAAA">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello" />
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<Button android:id="#+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="increment"></Button>
<Button android:id="#+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="decrement"></Button>
<Button android:id="#+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="make call"></Button>
<Button android:id="#+id/Button04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="media_camera"></Button>
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="24sp"
android:text="0" android:textStyle="bold" android:textColor="#000000"></TextView>
<EditText android:id="#+id/EditText01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="enter new button text"></EditText>
<RadioButton android:id="#+id/RadioButton01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="camera"></RadioButton>
</LinearLayout>
Enable in your Eclipse the LogCat
Window > Show View > Other > Android >
LogCat
then you can find what is the real description of your Exception.

Categories

Resources