Afternoon.
Basically im using action bar sherlock as my navigation through fragments, one of my fragments layouts consists of four images with buttons underneath, the problem im having is i want to be able to switch to a different activity or XML layout. on the button click. I've tred many different ways to achieve this with no result
Ive got as far as the button click is registered by the device after the app is loaded up but im stuck on were to go for my switch and case code to load up a new layout.
package com.westcheshirecollege;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import com.actionbarsherlock.app.SherlockFragment;
public class Fragment_1 extends SherlockFragment implements OnClickListener{
Button button;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.home, container, false);
Button b = (Button) v.findViewById(R.id.button1);
b.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// CODE NEEDED HERER TO LOAD ACTIVITY / LAYOUT
break;
}
}
}
Any Help would be much appreciated thanks.
Just Use This..
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
setContentView(R.id.YOUR_LAYOUT_NAME)
break;
}
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(), NewActivityname);
getActivity().startActivity(intent);
}
use this to work on button
Related
I have this fragment activity called BmiFragment from Navigation Drawer using Sliding Menu, from which i want to go to a new activity i.e. BmiCalculator.class to perform some task but i am not able to do so. I am trying to do this by implementing onclicklistener to the fragment activity. In the XML layout there is simply four buttons by clicking on them i want to open a new activity to perform certain task. Please provide me some help. Click here to view for errors
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class BmiFragment extends Fragment implements OnClickListener {
public BmiFragment() {
}
Button btn, btn1, btn2, btn3;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bmi, container,
false);
btn = (Button) rootView.findViewById(R.id.button1);
btn.setOnClickListener(this);
btn1 = (Button) rootView.findViewById(R.id.button2);
btn1.setOnClickListener(this);
btn2 = (Button) rootView.findViewById(R.id.button3);
btn2.setOnClickListener(this);
btn3 = (Button) rootView.findViewById(R.id.button4);
btn3.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Intent intent = new Intent(this, BmiCalculator.class);//***ERROR: The constructor Intent(BmiFragment, Class<BmiCalculator>) is undefined***//
startActivity(intent);[enter image description here][1]
break;
case R.id.button2:
break;
case R.id.button3:
break;
case R.id.button4:
break;
default:
break;
}
}
}
Intent intent = new Intent(this, BmiCalculator.class);//ERROR: The constructor Intent(BmiFragment, Class) is undefined//
startActivity(intent);[enter image description here]
instead of this use
Intent intent = new Intent(getActivity(), BmiCalculator.class);
startActivity(intent);
I'm brand new to android and trying to create an activity that has a fragment inside it. The idea is the fragment has 3 ImageButtons. The ImageButton you click will pass it's image to the activity and then show it in another fragment created after the onClick (full screen ImageView) within the main activity. The problem is I can't figure out how to pass the information. I've read over multiple questions already asked similar to this one, and went over android developers website but the more I read the more confusing it becomes. I'm also running into the problem of findViewById saying it cannot resolve the method. I have no idea why it is telling me this and have tried many things to fix it, nothing is working. Here is my code and any suggestions would be appreciated.
Main Activity:
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements MainActivityFragment.CanPassData{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.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);
}
public void passData(int value){
MainActivityFragment fragment = new MainActivityFragment();
fragment.setData(5);
getSupportFragmentManager().beginTransaction().add(R.id.fragment, fragment).commit();
//***Taken From Android Developer, I believe this is how you create a new fragment when you pass the data to the main activity****
if (fragment != null) {
fragment.getData();
} else {
MainActivityFragment newFragment = new MainActivityFragment();
Bundle args = new Bundle();
//******No idea what this is calling, pictureChosen and picture are default values taken from the website*******
args.putInt(MainActivityFragment.pictureChosen, picture);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
//*****************************************************************************************
}
public void processData(){
MainActivityFragment fragment = (MainActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
int value = fragment.getData();
}
}
Fragment:
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivityFragment extends Fragment implements View.OnClickListener {
private int data;
private CanPassData parent;
public interface CanPassData{
public void passData(int value);
}
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//***findViewById is saying it cannot resolve below
setContentView(R.layout.fragment_main);
ImageButton button1 = (ImageButton) findViewById(R.id.imageButton);
ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
ImageButton button3 = (ImageButton) findViewById(R.id.imageButton3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
//******************************************************************************************
return inflater.inflate(R.layout.fragment_main, container, false);
}
public void onAttach(MainActivity activity){
parent = (CanPassData) activity;
}
public void setData(int value){
data = value;
}
//***onClick also has all findViewByID as cannot resolve method, and i'm not exactly sure what to do within each case, the stuff I have in there was also taken from android developer.
#Override
public void onClick(View v){
switch (v.getId()) {
case R.id.imageButton:
ImageView box = (ImageView)findViewByID(R.id.imageButton);
Drawable name = box.getBackground().getCurrent();
Intent intent = new Intent (this, newFragment.class);
intent.putExtra("com.example.coding.assignment2", name);
startActivity(intent);
break;
case R.id.imageButton2:
box = (ImageView)findViewByID(R.id.imageButton2);
name = box.getBackground().getCurrent();
intent = new Intent (this, newFragment.class);
intent.putExtra("com.example.coding.assignment2", name);
startActivity(intent);
break;
case R.id.imageButton3:
box = (ImageView)findViewByID(R.id.imageButton3);
name = box.getBackground().getCurrent();
intent = new Intent (this, newFragment.class);
intent.putExtra("com.example.coding.assignment2", name);
startActivity(intent);
break;
}
}
public void pushData(){
parent.passData(data);
}
public int getData(){
return data;
}
}
A lot of the methods such as getData and pushData I'm not exactly sure how to use to get the info to the main activity, it was information given to me during class. I am wondering if there is a way to just pass the drawable itself if it is clicked, then assign that drawable to a ImageView in the new fragment that displays it covering the entire fragment. I've been told I need to make a 2nd fragment xml, but the java file itself should be created via the main activity. Is this correct?
You cannot resolve the findViewById because you have to inflate a View in order to use Fragments findViewById, like this:
View v = inflater.inflate(R.layout.fragment_main, parent, false);
ImageButton button1 = (ImageButton) v.findViewById(R.id.imageButton);
ImageButton button2 = (ImageButton) v.findViewById(R.id.imageButton2);
ImageButton button3 = (ImageButton) v.findViewById(R.id.imageButton3);
//call your button.setOnClickListener
return v;
There is no need to call setContentView()
In the onClick(View v) you also need to call v.findViewById()
Also you decleare your variables in a case, you should declare them before any of the case if you want to use them that way. It should look something like this:
public void onClick(View v){
ImageView box=null;
Drawable name = null;
Intent intent = null;
switch(v.getId()){
case R.id.imageButton:
box = (ImageView) v.findViewByID(R.id.imageButton);
name = box.getBackground().getCurrent();
intent = new Intent (this, newFragment.class);
intent.putExtra("com.example.coding.assignment2", name);
startActivity(intent);
break;
And so on
I have a DialogFragment that is styled to full screen using setStyle(STYLE_NORMAL, R.style.Theme_App).
The DialogFragment shows fine but the up action (the homeAsUp action on the ActionBar) does not work. I tried implementing onOptionsItemSelected in the DialogFragment but it is never called.
Is there a way to get the up action callback in the DialogFragment so I can dismiss it ? For reference, I'm using ActionBarCompat.
This wasn't possible but there is a workaround for this using aToolbar. Now you can include Toolbar as part of your DialogFragment layout xml and can set its design/icon according to your needs. You will also need to implement setNavigationOnClickListener if you want the back button to behave like it does normally. See the sample class below.
package com.package.name;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MyDialogFragment extends DialogFragment {
private View parentView;
private Toolbar toolbar;
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.Theme_AppCompat_NoActionBar);
return super.onCreateDialog(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//The layout xml file contains the toolbar
parentView = inflater.inflate(R.layout.dialogfragment_createpost, container, false);
initView();
initData();
return parentView;
}
private void initView() {
toolbar = (Toolbar) parentView.findViewById(R.id.toolbar);
}
private void initData() {
toolbar.setTitle("Post");
//Set naviagtion icon to back button drawable
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// handle back button naviagtion
dismiss();
}
});
}
}
There is no way to attach an ActionBar to the DialogFragment even though you can set the theme of the DialogFragment it will not register as a ActionBar to it, Dialog.getActionBar() will always return null.
Instead of getting the ActionBar you can always attach a Layout that will look like an ActionBar and set the functionality on it using menu.
The other way is to create an activity with actionBar as a Dialog you can refer to this post
In order for the DialogFragment to receive calls to onOptionsItemSelected(MenuItem item) you need the set setHasOptionsMenu(true); in the onCreate() method of the Fragment.
Another potential solution is to handle the up action in the activities onOptionsItemSelected(MenuItem item) callback. Something like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Respond to the action bar's Up/Home button
onBackPressed();
return true;
}
}
Just delegate the up action from the component, that receives it (i.e. the parent Activity or Fragment), to your DialigFragment. When up occurs, check if your DialogFragment is shown and if so, call the apropriate method.
Check out here for detailed description.
I solved these problem by adding the below coding.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
In MainActivity:
Previously,
public class MainActivity extends BaseActivity
Then I change into
public class MainActivity extends FragmentActivity
Most probably you need to extend an MainActivity to FragmentActivity.
The following code worked for me:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Hope this helps :)
define toolbar in layout and call it on your dialog fragment
Toolbar toolbar=dialog.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
toolbar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
My intent is to get text from an EditText and view it in a Toast message. The code is this:
package com.example.primaapplicazione;
import android.app.Activity;
import android.app.ActionBar;
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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void fromEditToToast()
{
EditText e = (EditText)findViewById(R.id.editText1);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
}
#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;
}
}
}
If I call the method "fromEditToToast()" in onCreate() without the "setOnClickListener()", it compiles properly and the app opens. If I call the same method with that function on the emulator app doesn't open, showing this message "Unfortunately, PrimaApplicazione has stopped.
What should I have to do?
I have seen this is a very common error, The PlaceholderFragment uses the fragment_main.xml that must contain the elements editText1 and button1, probably you have that elements defined in your activity_main.xml
It may be that views are not being instantiated while the activity has started. Device works faster than emulator so it might work in a device while not in emulator.
Implement your onClickListener directly in fragment and show the toast from there, it will work (assuming EditText and Button lies in fragment).
Try following code in your fragment and call the following function in your fragment's onViewCreated function to set the button's listener : -
private void set buttonOnClickListener()
{
EditText e = (EditText)getView().findViewById(R.id.editText1);
Button b = (Button)getView().findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//your code goes here;
}
});
}
I am creating an app in android.
In this app i want to show google Map after click on button.
I have declared button and i declared listener.
But my problem is i want to call another activity class in the action listener and show google map.
But it is not working, I changed emulator from google to Android it work but google API is not called and shows me error.
for this i used this code But my Map is not show and my Application is Terminated .
//----------------Main Activity ---------------//
import java.security.PublicKey;
import com.google.android.maps.MapView;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
EditText TextFeild;
Button button1;
Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addOnListenerAction();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void addOnListenerAction()
{
TextFeild=(EditText) findViewById(R.id.editText1);
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(context,MapViwer.class);
}
});
}
#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);
}
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;
}
}
}
//_-------------------MapViwer Class------------//
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class MapViwer extends MapActivity
{
MapView map;
public void onCreate(Bundle setInstanceState)
{
super.onCreate(setInstanceState);
setContentView(R.layout.activity_main);
map=(MapView) findViewById(R.id.mapView);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
So you're saying, you have a class named (MapViwer) that defines a layout and the class (MapViwer) contains information for the map to work. To deploy a class from your MainActivity.class, use the following method.
in MainActivity, under the public class add this
private Button button1;
then add this function -
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), MapViwer.class);
startActivityForResult(i, 100);
}
});
Use it like this..It worked for me..
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,MapViewer.class);
startActivity(intent);
}
});
and in the Mapviewer class just call the mapviewer.xml
Now, the mapviewer.xml should look like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="34dp" />
</LinearLayout>
Try it out..
Replace context with Mainactivity.this in the code
Intent intent=new Intent(context,MapViwer.class);
You just only defined intent but didn't call the Mapviewr activity
Correct code:
public void addOnListenerAction()
{
TextFeild=(EditText) findViewById(R.id.editText1);
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Mainactivity.this,MapViwer.class);
startActivity(intent);
}
});
}