I am creating some APP in ADT and I am following one tutorial but I am stuck at performing button to change my text to seomething else. The problem is that, in the tutorial, he uses older version of ADT and mine is newer so it doesn't work anymore. The code what there is is like this:
(package name etc)
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
final TextView textOne = (TextView) findViewById(R.id.textik);
Button pushthis = (Button) findViewById(R.id.gombik);}
}
#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;
}
}
}
This is probably a better example to follow.
http://www.vogella.com/tutorials/AndroidFragments/article.html
https://developer.android.com/training/basics/fragments/creating.html
The main think I see wrong with your code is
final TextView textOne = (TextView) findViewById(R.id.textik);
Button pushthis = (Button) findViewById(R.id.gombik);
They should not be initalized and declared here. If your using fragments, you should move it to the fragments. Below is a simple example.
public static class PlaceholderFragment extends Fragment implements Button.OnClickListener{
TextView textOne;
Button pushthis;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
textOne = (TextView) rootView.findViewById(R.id.textik);
pushthis = (Button) rootView.findViewById(R.id.gombik);
//set handlers and anything else
pushthis.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.gombik:
textOne.setText("HI");
break;
}
}
}
Related
I'm relatively new to both Java and the android platform and I have come across a problem in my code that is just completely beyond me. I am trying to lock onto a users location and move the camera to that location using the google maps api v2 however this is without success even with constant reading of resources and attempts.
The method requestLocationUpdates(String, long, float, LocationListener) in the type >LocationManager is not applicable for the arguments (String, int, int, LocationListener) >MainActivity.java /Loca/src/com/afrostudio/loca line 91 Java Problem
Also a very recent error has randomly occured:
R cannot be resolved to a variable
Any help would be fantastic please :)
package com.afrostudio.loca;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.location.LocationListener;
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.model.LatLng;
import com.google.android.gms.maps.MapFragment;
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
private GoogleMap map;
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
public class googleMap extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Define a listener that responds to location updates
LocationListener locationListner = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
double latti = location.getLatitude();
double longi = location.getLongitude();
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(latti,
longi));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListner);
}
#Override
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
}
public class MyFragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_profile, container, false);
}
}
public class MyFragment3 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_settings, container, false);
}
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = new googleMap();
FragmentManager fragmentManager = getFragmentManager();
switch(position) {
case 1:
fragment = new googleMap();
break;
case 2:
fragment = new MyFragment2();
break;
case 3:
fragment = new MyFragment3();
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
ActionBar ab = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#A4C639"));
ab.setBackgroundDrawable(colorDrawable);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#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 {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return 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;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
The R error thing has to do with your xml files (layouts) check them carefully. Try cleaning the project as well... As for the Location I sugest you to use the LocationClient, it is newer and performs better... https://developer.android.com/training/location/receive-location-updates.html
i have a little problem with eclipse/android.
i am trying to mak my own paint-app in android. my problem is i can only paint with one color(black) , now i want to add 3 more colors(red,green,blue).
im still a newbie to android developing, and i dont know how i can add that option.
anyone has any suggestions?
here is my code so far
-SingleTouchEventView
package nl.hr.teken0;
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;
public class SingleTouchActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SingleTouchEventView(this, null));
}
#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) {
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.main_fragment, container,
false);
return rootView;
}
}
}
SingleTouchActivity
package nl.hr.teken0;
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;
public class SingleTouchActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SingleTouchEventView(this, null));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
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.main_fragment, container,
false);
return rootView;
}
}
}
You can use a color picker. An example: https://code.google.com/p/android-color-picker/
Just import it like other libraries so that the source is in the "src" folder and then add the layout to the preferences file.
Preferences xml file:
<yuku.ambilwarna.widget.AmbilWarnaPreference
android:key="your_preference_key"
android:defaultValue="0xff6699cc"
android:title="Pick a color" />
I'm completely new to Android.
I want just a button that does a task but the sample code on the internet does not work.
Even though eclipse does not give any errors i can't run this app on my device. Here's the code:
package com.example.myfirstapp;
import com.example.myfirstapp.R;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
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.Button;
#SuppressLint("ValidFragment")
public class MainActivity extends ActionBarActivity {
static Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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 implements View.OnClickListener {
public PlaceholderFragment() {
}
public void onClick(View v){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sicioldrart.com"));
startActivity(browserIntent);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
btn=(Button) getView().findViewById(R.id.bottone);
btn.setOnClickListener(this);
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Thanks for the help
You need to have an inflated view before you can look for yout button.
move this:
btn=(Button) getView().findViewById(R.id.bottone);
btn.setOnClickListener(this);
below
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
and change getView() with rootView
btn=(Button) getView().findViewById(R.id.bottone);
btn.setOnClickListener(this);
these two lines will come after
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
and replace getView() with rootView
btn=(Button) rootView.findViewById(R.id.bottone);
btn.setOnClickListener(this);
My goal is to programm an Counter-Application. There is a picture in the middle of the smartphone and under the picture is a countdown starting at 100.
Every time you click on the picture the picture should flash and the counter should decrease till you have clicked 100 times and reached 0.
I have a little bit of code, but every time I run the project on my VM the application crashes.
package com.syntaix.appleclicker;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.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;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
int counter = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView) findViewById(R.id.TextView01);
final ImageView image = (ImageView) findViewById(R.id.appel);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
text.setText("" + counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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;
}
}
}
Here is the LogCat Screenshot:
LogCat
You can find the views in fragment when its created . But your are trying to get the views before attaching the fragment to the activity. You need to move some code to fragment as below...
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
PlaceholderFragment.java
public static class PlaceholderFragment extends Fragment {
int counter = 100;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
final TextView text = (TextView) rootView.findViewById(R.id.TextView01);
final ImageView image = (ImageView) rootView.findViewById(R.id.appel);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
text.setText("" + counter);
}
});
return rootView;
}
}
I am trying to get the selected values of first second and third screen to be used in the final fourth screen. How would I go about accessing the radio groups of the previous fragments?
I have tried to store the selected value in the onPause method of each fragment but it gets called when I slide from the first fragment to the third fragment and not from the first to the second.
I have tried to use a listener for the radiogroup but it never gets called.
I have tried to access the other three fragments from the final Tsiatista fragment but without any success.
package com.tsiatistonmyfriend;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Locale;
import net.justanotherblog.swipeview.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public static String funnySerious;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#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;
}
OnClickListener listener = new OnClickListener()
{
#Override
public void onClick(View v)
{
RadioButton rb = (RadioButton) v;
funnySerious = (String) rb.getText();
}
};
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
Fragment fragment = new FunnySerious();
switch (position)
{
case 0:
return fragment = new FunnySerious();
case 1:
return fragment = new MaleFemale();
case 2:
return fragment = new DirtyClean();
case 3:
return fragment = new Tsiatisto();
default:
break;
}
return fragment;
}
#Override
public int getCount() {
// Show 4 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
}
return null;
}
}
public static class FunnySerious extends Fragment
{
View v;
public FunnySerious() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.funny_serious, container, false);
v = rootView;
return rootView;
}
}
public static class MaleFemale extends Fragment
{
public MaleFemale() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.male_female, container, false);
return rootView;
}
}
public static class DirtyClean extends Fragment
{
public DirtyClean() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.dirty_clean, container, false);
return rootView;
}
}
public static class Tsiatisto extends Fragment
{
public Tsiatisto() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//FunnySerious textFragment = (FunnySerious) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.pager);
View rootView = inflater.inflate(R.layout.tsiatisto, container, false);
DataBaseHelper myDbHelper = new DataBaseHelper(this.getActivity().getApplicationContext());
try
{
myDbHelper.createDataBase();
myDbHelper.openDataBase();
}catch(IOException ioe)
{
throw new Error("Unable to create database");
}
final SQLiteDatabase db = myDbHelper.getDB();
Cursor cursor;
cursor = db.query("Tsiatista ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null);
//cursor = db.rawQuery("SELECT Verse FROM Tsiatista WHERE ID = 1", null);
if(cursor.moveToFirst())
{
String htr;
htr = (String) cursor.getString(cursor.getColumnIndex("Verse"));
EditText et = (EditText) rootView.findViewById(R.id.tsiatistoTxt);
et.setText(htr);
cursor.close();
}
return rootView;
}
}
}
Two ways,
Pass selected values as key value boolean pair in form of bundle, using setArgument method when you switch between fragment. You can then access this passed bundle and use selected values from previous fragment to drive you logic further, you can then club this values with values from current fragment and and navigate it further.
Read about shared preferences, you can always save value persistently and access it from all over your application, so using this you can save value selected by user from first three fragment, retrieve it on fourth and complete your logic.
Hope it help.