[Q] Android Studio - Action Bar Error - Android Studio

Hello,
I'm very new to Android development and to this site, so I'm sorry if I'm posting a common issue or in the wrong place.
I am using a tutorial to just develop a simple app to get my feet wet.
I'm at the point where I create an Action Bar, and this is what I have in MainActivity.java :
@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu.
// Adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Access the Share Item defined in menu XML
MenuItem shareItem = menu.findItem(R.id.menu_item_share);
// Access the object responsible for
// putting together the sharing submenu
if (shareItem != null) {
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider();
}
// Create an Intent to share your content
setShareIntent();
return true;
}
For the bold and highlighted part of the code, Android Studio is saying that cannot be applied. This is not allowing me to run the app.
Thanks for any help/advice!

Related

adding xml with java code

im not sure how to do this. but im doing a simple crazy 8 countdown score tracker.
So far i got main activity with 4 buttons 1, 2, 3, 4 for players. when you click on them what i want to do is popul,ate input text fields based on how manyt players are joining the game.
so for example if i select 3 i want the next activity to display 3 text fields so i can put their names in.
Im pretty new here so im not sure how to achieve this. God i wish this was more like php. i would do it with a breeze.
eg php code.
while($players){
echo "<input type=text name=plyr".$players."name>Enter ".$players." player name</input>";
}
Search Google for add views programmatically.
Trimis de pe al meu Sony Z2 D6503
Try this code:
Code:
TextView textView = new TextView(context);
textView.setText("your text");
textView.setId(TextView.generateViewId()); //generate id for this View if you want to access it in future
idList.add(textView.getId()); //idList is a ArrayList contains all TextView id which are created dynamically
parentView.addView(textView);
In Android, you can pass parameters/data between activities by using Intent.
Let's say selectOne is your button's onclick function
Code:
// MainActivity
public final static String EXTRA_NUMBER_PLAYER = "com.example.myfirstapp.NUMBER_PLAYER";
public void selectOne(View view) {
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra(EXTRA_NUMBER_PLAYER, 1);
startActivity(intent);
}
In your NextActivity, you can retrieve the number
Code:
// NextActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
// Other codes
Intent intent = getIntent();
int numberPlayer = intent.getIntExtra(MainActivity.EXTRA_NUMBER_PLAYER);
}
For more detail, you can refer to https://developer.android.com/training/basics/firstapp/starting-activity.html.
I'm a PHP developer also. Feel free to drop me any question.

OnClickListener in a Fragment

Hi,
I'm new to Android development and I'm developing my first Android app about music that contains two fragments: Home Fragment and Genres Fragment. This app is a school project and it's kinda urgent.
In Genres Fragment, I have four ImageButtons and I want to add some action to them, like when clicking a button, it goes to another fragment
So, in the Java file of that fragment, I already have the code for OnClickListener but I don't know what to put in the case condition of each button.
P.S: I can't post images because is says: "All new users prevented from posting outside links in their message". So, instead of an image, I will post the code.
Code:
public class GenresFragment extends Fragment implements View.OnClickListener{
public GenresFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_genres, container, false);
ImageButton rapBtn = (ImageButton)v.findViewById(R.id.RapButton);
ImageButton popBtn = (ImageButton)v.findViewById(R.id.PopButton);
ImageButton edmBtn = (ImageButton)v.findViewById(R.id.EDMButton);
ImageButton rockBtn = (ImageButton)v.findViewById(R.id.RockButton);
rapBtn.setOnClickListener(this);
popBtn.setOnClickListener(this);
edmBtn.setOnClickListener(this);
rockBtn.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.RapButton:
break;
case R.id.PopButton:
break;
case R.id.EDMButton:
break;
case R.id.RockButton:
break;
}
}
}
Can you help me with this?
Thank you
Replace the fragment on button click
Hello,
you just need to replace the fragment on button click.
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Thanks!

New to Forum, New to Android

Hey ladies and gents,
I'm new to your forums, seems it will be a helpful resource as I undertake this new project. I'm not new to programming, but i'm rusty. About 20 years ago I started with Qbasic, in high school i moved on to Visual Studio and C++. But I have been out of it for awhile now.
I started learning a little python to help my dad with his own program. But Decided I wanted to use Android Studio for my own. I have already started looking into tutorials, but have yet to see some information I am looking for. ( Or just don't recognize due to inexperience )
Traditionally, whats best to use for a multiscreen App? I am currently running windows in Fragments. I have a sidescreen that pops out with a menu button (works), windows slide out with options ( works ), when you select the option the window slides away (works) and it brings up the fragment so you can fill in forms (works.)
Inside my Fragments java file I have this
public class ac extends Fragment {
private EditText od_input;
private EditText sp_input;
private EditText hp_input;
private EditText sl_input;
private EditText hl_input;
private EditText return_input;
private EditText vent_input;
private TextView diag_output;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public ac() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ac.
*/
// TODO: Rename and change types and number of parameters
public static ac newInstance(String param1, String param2) {
ac fragment = new ac();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
EditText odtext;
EditText idtext;
EditText sptext;
EditText hptext;
EditText sltext;
EditText hltext;
EditText returntext;
EditText venttext;
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ac, container, false);
// NOTE : We are calling the onFragmentInteraction() declared in the MainActivity
// ie we are sending "Fragment 1" as title parameter when fragment1 is activated
if (mListener != null) {
mListener.onFragmentInteraction("Air Conditioning");
}
// Here we will can create click listners etc for all the gui elements on the fragment.
// For eg: Button btn1= (Button) view.findViewById(R.id.frag1_btn1);
// btn1.setOnclickListener(...
//odtext = view.findViewById(R.id.odtext);
//idtext = (EditText) findViewById(R.id.idtext);
//sptext = view.findViewById(R.id.sptext);
//hptext = view.findViewById(R.id.hptext);
//sltext = view. findViewById(R.id.sltext);
//hltext = view.findViewById(R.id.hltext);
//returntext = view.findViewById(R.id.returntext);
//venttext = view.findViewById(R.id.venttext);
//TextView diagtext = view.findViewById(R.id.diagtext);
//diagtext.setText((CharSequence) odtext);
return view;
}
@override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// NOTE : We changed the Uri to String.
void onFragmentInteraction(String title);
}
}
Firstly, yes there is a lot of useless crapification going on. Was running different experiments and have not fully cleaned up yet.
But, as people type in the field I want to capture the inputs. Does this require a Listener? Can you capture as they type or do I need a button (Really, Really don't want a button)?
Also, I am unfamiliar with the layout of the java.
Oncreate is when the program boots up?
onCreateview is when the Fragment is booted up?
onattach is when the main activity is associated?
ondetach is when its associated from activity?
I don't fully understand yet where the best place is to add stuff, would it be after onattach?
Thanks for pointing me in the right directions guys.
Chris W.

Using Intent(send mail) in Navigation drawer

I want to send mail from Navigation Drawer using the intent. First, my MainActivity.
Code:
else if(id==R.id.nav_mail) {
fragment = new MailFragment();
}
and MailFragment.
Code:
public class MailFragment extends Fragment {
public MailFragment() {
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("plain/text");
String[] address = {"********@gmail.com"};
email.putExtra(Intent.EXTRA_EMAIL, address);
email.putExtra(Intent.EXTRA_SUBJECT, "Subject___****");
email.putExtra(Intent.EXTRA_TEXT, "Text___****.\n\n");
startActivity(email);
}
// @Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Bundle savedInstanceState) {
// TextView textView = new TextView(getActivity());
// textView.setText(R.string.hello_blank_fragment);
// return textView;
// }
}
Run to create crash. The reason why I used to use fragment is because I made the simple screen change function fragment.
If you need more code, comment plz.
I didn't get what you are trying to do. If you are trying to invoke the "Select your mail app" screen and then send a message thru the Intent all of this when the user clicks on a row on the drawer then you should just copy the code to
Code:
else if(id==R.id.nav_mail) {
// here
}
without switching any fragment.
By the way, as far as I know, the fragment's public constructor must be empty.
You cannot do it from Fragment's constructor. Move your code to onActivityCreated() method.
qlife1146 said:
I want to send mail from Navigation Drawer using the intent. First, my MainActivity.
Code:
else if(id==R.id.nav_mail) {
fragment = new MailFragment();
}
and MailFragment.
Code:
public class MailFragment extends Fragment {
public MailFragment() {
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("plain/text");
String[] address = {"********@gmail.com"};
email.putExtra(Intent.EXTRA_EMAIL, address);
email.putExtra(Intent.EXTRA_SUBJECT, "Subject___****");
email.putExtra(Intent.EXTRA_TEXT, "Text___****.\n\n");
startActivity(email);
}
// @Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Bundle savedInstanceState) {
// TextView textView = new TextView(getActivity());
// textView.setText(R.string.hello_blank_fragment);
// return textView;
// }
}
Run to create crash. The reason why I used to use fragment is because I made the simple screen change function fragment.
If you need more code, comment plz.
Click to expand...
Click to collapse
Creating a new Intent in the constructor is a really bad idea. An example from the android's developer guide
Code:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}

TooBar problem using Outlined text fields

Hi everyone,
I'm trying to use a ToolBar in my android studio project. In build gradle(module app) I implements :
'com.google.android.material:material:1.1.0-alpha07' //I use this because i want '<com.google.android.material.textfield.TextInputLayout>' on my xml/layout files,
'com.android.support.constraint:constraint-layout:1.1.3',
'com.android.support.constraint:constraint-layout:1.1.3',
testImplementation 'junit:junit:4.12'.
In MainActivity.Java:
public class MainActivity extends Activity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
}
}
If I use 'toolbar.inflateMenu(R.menu.menu_bar);' or 'setSupportActionBar(toolbar)' when I ran my app it crashs immediately.
I tried also 'setActionBar();' the brackets should be filled this way 'Toolbar toolbar' as 'toolbar' I put 'toolbar' ;
but I don't know what to put as 'Toolbar'.
I've looked for solution but always in the build gradle(module app) was implemented 'com.android.support:appcompat-v7:28.0.0'.
And if I implement both of them('com.android.support:appcompat-v7:28.0.0' and 'com.google.android.material:material:1.1.0-alpha07') my app crashs immediately.
Can anyone help me to set my toolbar as an ActinBar?
If you need more specification on my project feel free to ask.
Tanks,
1lbert0

Categories

Resources