Display programming language code exactly as it is in textview - java

Is there any way to display the code by the help of using textview. Here is my problem, when i write the code into android:text=" " of the textview it shows an error.I am just trying to show the code using textview.
Please help me
Error 1(Line 14):String literal is not properly closed by a double-quote
Error 2(line 20):Multiple markers at this line
- Illegal modifier for the local class MainActivity; only abstract or final is
permitted
- The nested type MainActivity cannot hide an enclosing type
Here is my xmlcode:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</FrameLayout>
Here is my Java Code:
package com.example.test;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.widget.TextView;
import com.example.han.R;
public class MainActivity extends ActionBarActivity {
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textView1);
tv.setText("package com.example.test;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import com.example.han.R;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}");
}
}

Related

DDMS Won't Show Debug Output

I am currently taking an Android course and I just learned about DDMS and debugging with Logcat.
However, the problem I have is that there are no outputs being shown on DDMS whenever I run the program on the emulator (using Eclipse IDE)
Here is my code
package com.fv.android.practice.project;
import java.io.FileOutputStream;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public static final String DEBUGTAG = "FV";
public static final String TEXTFILE = "notesquirrel.txt";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addSaveButtonListener();
}
private void addSaveButtonListener() {
Button saveBtn = (Button) findViewById(R.id.save);
saveBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.text);
String text = editText.getText().toString();
Log.d(DEBUGTAG, "Save button clicked: " + text);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And XML
<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:orientation="vertical" >
<EditText
android:id="#+id/text"
android:maxLength="400"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Enter Your Text Here" />
<Button
android:id="#+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
When I type something into the "note" app, I should have some sort of output saying that I typed something, but I have zero output from the DDMS. Nothing at all. What can I do to fix this? I already went to the manifest and tried to set Debuggable to true but Eclipse tells me not to hardcode the debugging process, or something like that.
Also, I checked to see if the emulator was selected for debugging in the DDMS, and it is.
Fixed the issue. Now using Android Studio.

Android PictureView keeps crashing

I've recently added a pictureView to my app, and for some reason, whatever I do, it crashes.
It crashes when I add picture using both xml and adding it programmatically.
Source is below:
XML
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
.Java file:
package tk.yteditors.london2013;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class PictActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pict);
Drawable drawable = getResources().getDrawable(R.drawable.map);
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageview.setLayoutParams(vp);
imageview.setImageDrawable(drawable);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Edit: also the case on BlueStacks app player, so on all devices i suppose.

Android App crashing at beginning

I'm learning Android development and I'm wondering why is my app crashing
Main code:
package com.tester.myapp;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class Main extends Activity {
Button carrega;
TextView texto;
#Override
protected void onCreate(Bundle savedInstanceState) {
carrega = (Button)findViewById(R.id.carregar);
texto = (TextView)findViewById(R.id.textView3);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
carrega.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
texto.setText("newtext");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And my XML is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:background="#A5BB76"
android:text="#string/message" />
<Button
android:id="#+id/carregar"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="#string/OK"
/>
</LinearLayout>
And this app works OK just by the XML, but if I define those variables (Button and TextView) it crashes at start. Why is this happening? How can I solve this? If I take off my Java new code (and maintain the default) the app works, but it doesn't whenever I declare one of those variables.
I appreciate your time. Thank you very much. Any help would be great.
You should Initialise the View after the setContentView(R.layout.activity_main);
Change It
#Override
protected void onCreate(Bundle savedInstanceState) {
carrega = (Button)findViewById(R.id.carregar);
texto = (TextView)findViewById(R.id.textView3);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
To
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
carrega = (Button)findViewById(R.id.carregar);
texto = (TextView)findViewById(R.id.textView3);

Methods in extended class are not working

I created a menu, where you can swipe up and down. I created this menu as another activity. Now, I need this menu to be added to on other activities.
Following is the code of my menu(SlidingDrawer)
Java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class DropDownMenu extends Activity {
private TextView addInquiry, addEvent, additionalInfo, addToContacts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drop_down_menu);
//Intializing instance variables
addInquiry = (TextView)findViewById(R.id.menu_add_inquiry);
addEvent = (TextView)findViewById(R.id.menu_add_event);
additionalInfo = (TextView)findViewById(R.id.menu_additional_info);
addToContacts = (TextView)findViewById(R.id.menu_add_to_contacts);
//Register the Listeners
addInquiry.setOnClickListener(new AddInquiryEvent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.drop_down_menu, menu);
return true;
}
//Test Button
private class AddInquiryEvent implements OnClickListener
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent intent = new Intent(DropDownMenu.this,NewLead.class);
startActivity(intent);
}
}
}
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" >
<SlidingDrawer
android:id="#+id/SlidingDrawer"
android:layout_width="wrap_content"
android:layout_height="250dip"
android:layout_alignParentBottom="true"
android:content="#+id/contentLayout"
android:handle="#+id/slideButton"
android:orientation="vertical" >
<Button
android:id="#+id/slideButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/bottom_bar" >
</Button>
<LinearLayout
android:id="#+id/contentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:background="#cbcbcc" >
<TextView
android:id="#+id/menu_add_inquiry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/add_inquiry"
android:textColor="#ffffff"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
Following is the Code of the other activity
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sales_inqury_main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
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=".SalesInqury" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="#string/sales_inqury"
android:textSize="40sp" />
<include layout = "#layout/activity_drop_down_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SalesInqury extends DropDownMenu {
private ImageView addNewSalesInqury;
private RelativeLayout salesInquryMainLayout;
private TextView testEditSales;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sales_inqury);
//Intializing instance variables
addNewSalesInqury = (ImageView)findViewById(R.id.add_new_sales_inqury_btn);
salesInquryMainLayout = (RelativeLayout)findViewById(R.id.sales_inqury_main_layout);
testEditSales = (TextView)findViewById(R.id.testWord);
}
}
But, there is an issue. Even thougn I can open the menu in this other activity, I can't click on it's Buttons (TextViews) and navigate to other activities. Why is this?
I think it's because after you've set a contentView in your DropDownMenu activity and bound the listener to the button, in SalesInquiry you set a different contentView. Although this includes the menu-layout, the Views are created anew, so there's no Listener bound to the button anymore.
A solution would either be to specify the menu callbacks in the layout xml via android:onClick="..." or by having a separate method in DropDownMenu that adds the Listener and that you call from your SalesInquiry class after setting the content view.
Example 1, specifying callback in XML
Add android:onClick="onMenuItemClicked" to menu item TextView
<TextView
android:id="#+id/menu_add_inquiry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/add_inquiry"
android:textColor="#ffffff"
android:clickable="true"
android:onClick="onMenuItemClicked"
android:textAppearance="?android:attr/textAppearanceMedium"/>
Add this method to DropDownMenu class and remove the AddInquiryEvent class and the registering of the listeners.
public void onMenuItemClicked(View view) {
switch (view.getId()) {
case R.id.menu_add_inquiry:
Intent intent = new Intent(DropDownMenu.this,NewLead.class);
startActivity(intent);
break;
case R.id.menu_other:
// handle other menu item
break;
// ... and so on ...
default:;
}
}
Example 2, bind listeners in a separate method
In DropDownMenu move the registering of the Listeners to a separate method:
protected void registerListeners() {
//Register the Listeners
findViewById(R.id.menu_add_inquiry).setOnClickListener(new AddInquiryEvent());
}
Note, that you have to find the view again, as the one kept in the local variable is the wrong one, after SalesInquiry set it's content view.
Call the method in SalesInquiry after setting the content view.
setContentView(R.layout.activity_sales_inqury);
registerListeners();
I'm not sure. but can you please try with extending the DropDownMenu-Activity instead of Activity.
public class NewActivity extends DropDownMenu {
// override which you want to override
}
And make sure DropDownMenu in such a way that you can extends what you want to implements. Hope you have got my point.
Consistency Issue (Recommendation)
There is a HUGE misnomer in your class name, you should NOT call an Activity DropDownMenu.
This is confusing, you should instead call it DropDownActivity or something like that. Also do you really need to use an Activity? You do realize a View can still listen to the click of a button right? Not only an Activity can listen to button clicks. If you encapsulate all of this in a View and have a dedicated listener for that view, then you wouldn't have any need for another Activity.
Possible Solution
It's likely related to the activity/component you have registered to listen to the buttons is not correct. Validate your code so that you can at least get the correct response of the onClick.
The problem is that you are attaching the listener to addInquiry in the DropDownMenu:
//Register the Listeners
addInquiry.setOnClickListener(new AddInquiryEvent());
This listener is not attached to the TextView in the extended class, since the setContentView is called and layout is refreshed.
Possible solution, if you are not using the DropDownMenu activity on its own then simply (remove setContentView):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //in DropDownMenu
// setContentView(R.layout.activity_drop_down_menu);
...
...
Not the best solution but would work.
Update: Better Solution
(1) Let the subclass decide which layout to show. This layout must contain the menu (which is true in your case).
public class DropDownMenu extends Activity {
private TextView addInquiry, addEvent, additionalInfo, addToContacts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_setContentView();
//Intializing instance variables
addInquiry = (TextView)findViewById(R.id.menu_add_inquiry);
addEvent = (TextView)findViewById(R.id.menu_add_event);
additionalInfo = (TextView)findViewById(R.id.menu_additional_info);
addToContacts = (TextView)findViewById(R.id.menu_add_to_contacts);
//Register the Listeners
addInquiry.setOnClickListener(new AddInquiryEvent());
}
protected void _setContentView() {
setContentView(R.layout.activity_drop_down_menu);
}
...
...
}
#override the _setContentView method so that we do not set the view twice.
public class SalesInqury extends DropDownMenu {
...
...
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//Intializing instance variables
addNewSalesInqury = (ImageView)findViewById(R.id.add_new_sales_inqury_btn);
salesInquryMainLayout = (RelativeLayout)findViewById(R.id.sales_inqury_main_layout);
testEditSales = (TextView)findViewById(R.id.testWord);
}
#Override
protected void _setContentView() {
// need to make sure this layout contains the menu (otherwise NullPointerException may arise)
setContentView(R.layout.activity_sales_inqury);
}
}

Android App Iframe a site

I was trying to make android app for my radio station. I want when user will open the app the site will load automatically. I was following this tutorial
but when I tried to run the app it's showing this error "Unfortunately,Tarab Radio stop work "
Here is my activity_main.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">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
& MainActivity.java
package com.jibon.tarabradio;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends Activity {
#SuppressLint("SetJavaScriptEnabled") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview;
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://hoicoimasti.com/radio/");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Any Idea where I did mistake. Thanks in advance.
put setContentView(R.layout.activity_main); in your onCreate method.

Categories