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);
Related
My project has three tabs.
I want to open a new Activity from one tab
My Java current tab Activity
package com.example.muhsin.tabstes;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by muhsin on 22/07/17.
*/
public class Tab2Feeds extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab2feed, container, false);
return rootView;
}
}
layout:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="open new"
android:id="#+id/button1"
android:layout_below="#+id/section_label"
android:layout_alignParentEnd="true"
android:layout_marginEnd="204dp"
android:layout_marginTop="112dp" />
Then I create a new empty Activity called profile Activity
package com.example.muhsin.tabstes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class ProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
}
}
How to navigate from Tab2Feeds to ProfileActivity?
To start activity from Fragment:
put your code in onCreateView method or onViewCreated method:
Button button = (Button) rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getActivity() != null) { // check if activity not null
Intent intent = new Intent(getActivity(), ProfileActivity.class);
getActivity().startActivity(intent);
}
}
});
In your Tab2Feeds Fragment, get a reference to the button by calling finViewById.
From there set a clicklistener with setOnClickListener.
Finally use an Intent to open the new activity.
You will need a Context which is gotten by calling getActivity()
Button button = (Button) rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ProfileActivity.class);
getActivity().startActivity(intent);
}
});
A the top of the file dont forget your imports
import android.widget.Button;
import android.content.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
with the new ADT from android, the new way of designing layout is using fragment layer.
My problem today however isn't really related to fragment layer. It is in regards to
error opening trace file no such file or directory
Here's my MainActivity code:
package com.skws.calcutil;
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.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 display = (TextView) findViewById(R.id.display);
Button btn0 = (Button) findViewById(R.id.btn0);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
Button btn4 = (Button) findViewById(R.id.btn4);
Button btn5 = (Button) findViewById(R.id.btn5);
Button btn6 = (Button) findViewById(R.id.btn6);
Button btn7 = (Button) findViewById(R.id.btn7);
Button btn8 = (Button) findViewById(R.id.btn8);
Button btn9 = (Button) findViewById(R.id.btn9);
Button btnDecimal = (Button) findViewById(R.id.btnDecimal);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
Button btnMinus = (Button) findViewById(R.id.btnMinus);
Button btnMultiply = (Button) findViewById(R.id.btnMultiply);
Button btnDivide = (Button) findViewById(R.id.btnDivide);
Button btnEqual = (Button) findViewById(R.id.btnEqual);
btn0.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
display.setText("0");
}
});
}
#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;
}
}
}
The problem is if I removed this section of the code:
btn0.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
display.setText("0");
}
});
This will only load my Layout, which works perfectly. If I add the above code into MainActivity, I'll get that Error.
Is there a new way google wanted us to design with Button Clicks?
I've tried re-installing everything and that didn't work.
I've also tried to sync my versions and that didn't work.
I've tried to remove the version and add in the SD card permission trick and that didn't work.
I've also tried to add an virtual sd space and that didn't work.
I've tried my code on my mobile phone and I get the same results.
Any help will be grateful.
Thank you
Ok, I've found the solution.
After comparing with my old codes, it seems that the problem is with the extension of ActionBarActivity.
Once I've moved all the layout into activity_main.xml, and in the Main_Activity.java changing the ActionBarActivity into Activity, it is working again.
Guess that just means that I need to be updated on how to code with Fragments.
package com.example.sliderapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add functionality for android button
MyClickListener listener = new MyClickListener();
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(listener);
// add functionality for apple button
MyClickListener2 listener2 = new MyClickListener2();
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(listener2);
// Add functionality for don't care button
MyClickListener3 listener3 = new MyClickListener3();
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(listener3);
//Add functionality for reset button
MyClickListener4 listener4 = new MyClickListener4();
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(listener4);
}
#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;
}
/**
* click listener method referring to android button
*
* #author Ross
*
*/
private class MyClickListener implements View.OnClickListener {
public void onClick(View view) {
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(R.drawable.happypng);
}
}
/**
* click listener method referring to apple button
*
* #author Ross
*
*/
private class MyClickListener2 implements View.OnClickListener {
public void onClick(View view) {
ImageView imageView = (ImageView) findViewById(R.id.imageView2);
imageView.setImageResource(R.drawable.happypng);
}
}
/**
* Click listener referring to don't care button
* #author Ross
*
*/
private class MyClickListener3 implements View.OnClickListener {
public void onClick(View view) {
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(R.drawable.whynotpng);
ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView2.setImageResource(R.drawable.whynotpng);
}
}
/**
* Click listener referring to Reset button
* #author Ross
*
*/
private class MyClickListener4 implements View.OnClickListener {
public void onClick(View view) {
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(R.drawable.ic_launcher);
ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView2.setImageResource(R.drawable.apple_gray_logo);
}
}
}
In the code above the last method (myClicklistener4) I am trying to set up the imageResources as stated however it is setting them equal to .whynotpng like in the 3rd class. I am unsure why this is happening.
I cannot tell you where exacltly your error occurs as I can't see anything particular wrong in your code. This is your code shortened. Does it work now? And do you have saved all your drawables in the right folder and with the right names?
package com.example.sliderapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener {
private ImageView ivFirst, ivSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// register your button listeners
((Button) findViewById(R.id.button1)).setOnClickListener(this);
((Button) findViewById(R.id.button2)).setOnClickListener(this);
((Button) findViewById(R.id.button3)).setOnClickListener(this);
((Button) findViewById(R.id.button4)).setOnClickListener(this);
// you can also define the onClick method via xml by calling android:onClick="onClick"
// save your ImageViews so that you don't have to find them for every buttonclick
ivFirst = (ImageView) findViewById(R.id.imageView1);
ivSecond = (ImageView) findViewById(R.id.imageView2);
}
#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 void onClick(View view) {
// depending on which button is clicked do some action
switch(view.getId()) {
case R.id.button1:
ivFirst.setImageResource(R.drawable.happypng);
break;
case R.id.button2:
ivFirst.setImageResource(R.drawable.happypng);
break;
case R.id.button3:
ivFirst.setImageResource(R.drawable.whynotpng);
ivSecond.setImageResource(R.drawable.whynotpng);
break;
case R.id.button4:
ivFirst.setImageResource(R.drawable.ic_launcher);
ivSecond.setImageResource(R.drawable.apple_gray_logo);
break;
}
}
}
One of the approach to short down your code, is already explained by "Endzeit" .
Another approach which you can follow to short down your code is explained below:
First:
Add android:onClick="method name" in all 4 Buttons in your xml file.
Example:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClick"
android:text="button1" />
Similarly do it for button2, button3 and button 4
Now, add this code in your Java class
public class MainActivity extends Activity {
private ImageView imageView1, imageView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
}
#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 void myClick(View view) {
// depending on which button is clicked do some action
switch(view.getId()) {
case R.id.button1:
imageView1.setImageResource(R.drawable.happypng);
break;
case R.id.button2:
imageView2.setImageResource(R.drawable.happypng);
break;
case R.id.button3:
imageView1.setImageResource(R.drawable.whynotpng);
imageView2.setImageResource(R.drawable.whynotpng);
break;
case R.id.button4:
imageView1.setImageResource(R.drawable.ic_launcher);
imageView2.setImageResource(R.drawable.apple_gray_logo);
break;
}
}
}
Note: You dont have to find the ids of buttons in this case.
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