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
Related
So I was programming in some In app Billing code for my Android app using Android Studio. I was following an Android tutorial called 'Preparing your In-app Billing Application' at the Android Developer website (not allowed to give a link because I'm a new user). All was going well, until I get to the part where I have to paste their code into my code. Check out their code put into mine (I left the base64EncodedPublicKey empty so nobody would steal it):
Code:
public class MainActivity extends ActionBarActivity {
//Add other java files to the main class
private JokeBook mJokeBook = new JokeBook();
private ColorWheel mColorWheel = new ColorWheel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declare View variables
final TextView jokeLabel = (TextView) findViewById(R.id.jokeTextView);
final Button showJokeButton = (Button) findViewById(R.id.showJokeButton);
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
final Button moreJokes = (Button) findViewById(R.id.moreJokes);
//On Click
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String joke = mJokeBook.getJoke();
//Update label with fact
jokeLabel.setText(joke);
int color = mColorWheel.getColor();
relativeLayout.setBackgroundColor(color);
showJokeButton.setTextColor(color);
moreJokes.setTextColor(color);
}
};
showJokeButton.setOnClickListener(listener);
goToTwitter();
goToFacebook();
goToExxellerate();
IabHelper mHelper;
@Override
public void [U]onCreate(Bundle savedInstanceState)[/U] {
// ...
String base64EncodedPublicKey = "";
// compute your public key and store it in base64EncodedPublicKey
[B]mHelper = new IabHelper(this, base64EncodedPublicKey);[/B]
}
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
// Oh noes, there was a problem.
[I]Log.d(TAG, "Problem setting up In-app Billing: " + result);[/I]
}
// Hooray, IAB is fully set up!
}
});
@Override
public void [U]onDestroy()[/U] {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
}
The code that they gave me had many errors when pasted into Android Studio, which were:
TAG has private access in 'android.support.v4.app.FragmentActivity' (Italicized text)
Annotations are not allowed here (@Override)
; expected (Underlined text)
IabHelper (android.view.View.onClickListener, String) in IabHelper cannot be applied to (com.exxellerate.joketeller.MainActivity, String) (Bolded Text)
Any help with fixing there errors? I'm a beginner at code and very confused that the code they gave me was wrong...
Hi .
I'm newbie so I need your help in some issue
I'm coding an app that has 2 activity .
The problem is I want Some codes to be execute right after user goes to 2end activity .
So I write the codes in Oncreate function . but in test when user goes to 2end activity he will see just empty design of activity . like I didn't write any code for that :/
Please help me what should I do .
p.n : I want 2end activity to show the inbox content to user and I'm sure that my codes is correct .
sry for my bad english .
You need to display all your codes.
hiphop12ism said:
You need to display all your codes.
Click to expand...
Click to collapse
Activity A
Code:
public class A extends Activity implements View.OnClickListener {
Button inbox,draft,sent,call;
TextView lblmsg,lblNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
inbox = (Button) findViewById(R.id.btn1);
sent = (Button) findViewById(R.id.btn2);
draft = (Button) findViewById(R.id.btn3);
call = (Button) findViewById(R.id.btn4);
inbox.setOnClickListener(this);
draft.setOnClickListener(this);
call.setOnClickListener(this);
sent.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == inbox) {
Intent i;
i = new Intent(this, B.class);
startActivity(i);
}
}
Activity B
Code:
public class B extends AppCompatActivity {
ListView lv1;
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
lv1=(ListView)findViewById(R.id.lv);
Uri InboxUri= Uri.parse("Content://sms/inbox");
String [] C= new String[] { "_id", "address", "body" };
ContentResolver cr=getContentResolver();
Cursor D = cr.query(InboxUri, C, null, null, null);
adapter = new SimpleCursorAdapter(
this,
R.layout.row,
D,
new String[]{"body","address"},
new int[]{R.id.lblmsg,R.id.lblNumber});
lv1.setAdapter(adapter);
}
and I forgot to say Android studio detecting no error
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.
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);
}
}
I am designing an app where the user clicks a button and the interstitial ads get displayed. On closing the ad, the counter gets added to one, it gets displayed. Basically, it is to notify the users how many ads they have watched. But the counter doesn't get added and the ads aren't getting updated. Here is the code:-
Code:
Stack Overflow
Questions Jobs Tags Users Badges Ask
up vote
0
down vote
favorite
Admob Interstitial ads shows up only once in the App
android admob
I am designing an app where the user clicks a button and the interstitial ads get displayed. On closing the ad, the counter gets added to one, it gets displayed. Basically, it is to notify the users how many ads they have watched. But the counter doesn't get added and the ads aren't getting updated. Here is the code:-
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
private Button watchButton;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TextView tv_data=(TextView)findViewById(R.id.disCount);
MobileAds.initialize(this,
"ca-app-pub-3940256099942544~3347511713");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
count++;
TextView tv_data=findViewById(R.id.disCount);
setContentView(R.layout.activity_main);
tv_data.setText(Integer.toString(count));
// Load the next interstitial.
}
});
watchButton = findViewById(R.id.button_send);
watchButton.setOnClickListener(new View.OnClickListener() {
// Listen for when user presses button
public void onClick(View v) {
// If a interstitial is ready, show it
if(mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
else
{
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
// Otherwise end this activity (go back to first activity)
}
});
}
}
You need to load new ad each time user clicks on the button.
lioce said:
You need to load new ad each time user clicks on the button.
Click to expand...
Click to collapse
Thanks a lot, It worked, and there was a problem with Test ad id.When I updated to real id. it works.