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.
Related
am trying to implement a button to the left in the navigation bar in my android app. I want this button to open the navigation drawer. Right now the only thing I can to is to swipe from left top right to open it.
I have tried to google, and read the documentation. Could someone please help?
My code MainActivity.java is added below. Thanks!
package safefence.tc.safefence;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity implements LocationListener {
ArrayList<NavigationDrawerItem> listItems;
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItems = new ArrayList<NavigationDrawerItem>();
listItems.add(new NavigationDrawerItem("Home", R.drawable.ic_drawer));
listItems.add(new NavigationDrawerItem("About", R.drawable.ic_drawer));
listItems.add(new NavigationDrawerItem("Follow", R.drawable.ic_drawer));
listItems.add(new NavigationDrawerItem("Log out", R.drawable.ic_drawer));
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.red_string,R.string.abc_action_mode_done) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
}
};
drawerLayout.setDrawerListener(drawerToggle);
list = (ListView) findViewById(R.id.drawer);
list.setAdapter(new NavDrawerAdapter(this, listItems));
list.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = drawerLayout.isDrawerOpen(list);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
#Override
public void onProviderEnabled(String provider) { }
#Override
public void onProviderDisabled(String provider) { }
}
XML file
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent" >
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment">
</fragment>
</FrameLayout>
<ListView
android:id="#+id/drawer"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"
android:layout_width="240dp"
android:layout_height="match_parent">
</ListView>
</android.support.v4.widget.DrawerLayout>
To add icon
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.red_string,R.string.abc_action_mode_done)
ic_drawer will be your icon.
If you working on sliding menu See this link. This is a good tutorial.
I am trying to do something very basic and I cannot figure out why it will not find the ImageView from the Java code. When I type R.id.scanButton I get an error saying that resource does not exist.. but as you can see in my XML it has been created. What am I missing here?
MainActivity.java
package com.erinkabbash.coolmeter;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView button = (ImageView) findViewById(R.id.scanButton); // ImageView not Found
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
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"
tools:context="com.erinkabbash.coolmeter.MainActivity$PlaceholderFragment" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<TextView
android:id="#+id/resultView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/scanButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/scanButton" />
</LinearLayout>
</RelativeLayout>
import R.java
com.erinkabbash.coolmeter.R; //Your.base.pkg.com.R /* You base package .R*/
And clean your project. it might be work,
If you working with your base package domain ,then there is no need of importing R.java. just Clean your project or workspace. . It will work.
Only use lowercase and underscore for res/, drawables/ and others res fies
There is nothing wrong in your code, You just need to Clean and Build your project to register your id that was added.
resultView and scanButton should be lower-cased to resultview and scanbutton. The R.java file is very picky about upper-case letters and special characters anywhere in your res folder.
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.
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;
}
}");
}
}
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.