FindViewById in android studio - android-studio

hi im beginner an trying to define a button on android studio. i get an as i write "FindV" and android studio have no suggestion for me. if complete code myself, get error. here is the code:
package com.example.neomn.myapplication1;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) FindV // no suggestions
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Can you try to type as findViewById?
It should start from small letter f.

Without completing findViewById you will get error if you run.
Check if Power Save Mode on File menu is disabled,that may cause disabling auto complete features

Related

Opening a new fragment from within a ViewPager in Android Studio

I have a question about opening a new fragment from within a ViewPager.
I setup FragmentA, FragmentB and FragmentC to be fragments of a viewpager and then proceeded to setup Fragment1 and Fragment2 as fragments of another viewpager which resides in FragmentB.
So Fragment1 and Fragment2 are in a viewpager which is essentially nested in another viewpager. I'm playing around with UI styles etc and this is is similar to that found in the Flickr app.
The problem I'm having is that once I get into the fragment1 or fragment2 (the nested fragment) I want to be able to click on something and enter a full screen fragment bypassing the toolbar I set up in FragmentB.
Below is the the code I have for FragmentB with the toolbar, tablayout and viewpager.
package com.app.fragmentdemo;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.tabs.TabLayout;
public class FragmentB extends Fragment {
public FragmentB() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_b,container,false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.viewPagerFragmentB);
ViewPagerAdapter adapter = new ViewPagerAdapter(((FragmentActivity) getContext()).getSupportFragmentManager());
adapter.addFragment(new Fragment1(),"Fragment 1");
adapter.addFragment(new Fragment2(),"Fragment 2");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
// Inflate the layout for this fragment
return rootView;
}
}
Here is the code I've put together for fragment2 which contains a button I would like to take to take me to another fragment.
package com.app.fragmentdemo;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class Fragment2 extends Fragment {
public Fragment2() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_2,container,false);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new FragmentFull();
FragmentManager fm = ((FragmentActivity) getContext()).getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentFullContainer,fragment);
ft.commit();
Toast.makeText(getActivity(), "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
// Inflate the layout for this fragment
return rootView;
}
}
Upon clicking the button the app crashes. I get the error:
No view found for id 0x7f080084 (com.app.fragmentdemo:id/fragmentFullContainer) for fragment FragmentFull
If I change the id of the fragment container to the id I assigned to the Fragment2 layout file, it works fine. However, this is still contained in the nested viewpager and doesn't allow me to enter a new layout.
I really appreciate your help.
Thanks.
In the following line
ft.add(R.id.fragmentFullContainer,fragment);
fragmentFullContainer should be a view in your R.layout.fragment_2
You are getting the crash because the OS cannot find fragmentFullContainer in R.layout.fragment_2
UPDATE
replace getSupportFragmentManager with getChildFragmentManager
If you want full screen , you have to manually hide Activity View elements.
Most probably, you have toolbar from activity, you can remove it like that.
((AppCompatActivity)getActivity()).getSupportActionBar().hide();

Android Studio Opening an activity

I am trying to open an activity with in an activity. I succeeded to do to from MainActivity but for some reason, this wont allow me to do so again.
I am very new to this, please help me.
package com.example.edonfreiner.siddur;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Davening extends AppCompatActivity {
Button shacharisButton, minchaButton, maarivButton;
public void openShacharis() {
shacharisButton = (Button) findViewById(R.id.shacharis);
shacharisButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent open = new Intent(Davening.this, Shacharis.class);
startActivity(open);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_davening);
openShacharis();
}
}
This is the code to my class, there are no errors, meaning all buttons and references exist in the XML file.
Thank you in advance.
The logic you built is mistaken, cause you are calling a function in your onCreate that will not activate the onClick inside it. And when you click your object, it does not activate the onClick because this is encapsulated inside openShacharis. So the solution is to take off onClick from openShacharis, this way:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_davening);
shacharisButton = (Button) findViewById(R.id.shacharis);
shacharisButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openShacharis();
}
});
}
public void openShacharis() {
Intent open = new Intent(Davening.this, Shacharis.class);
startActivity(open);
}
All activities need to be registered on the Android Manifest.
As you stated on your comment, if you don't have it registered, it won't open.
This documentation will probably help you. In particular the part where it Adds the required <activity> element in AndroidManifest.xml.

Android Studio - "Cannot Resolve onCreate as a method", same for setContentView, getMenuInflater and onOptionsItemSelected

I am making a splash screen for my app and I am following this tutorial: Android Programming Tutorial - 4 - Adding a Splash Screen. But for some reason, when I create the splash screen activity, it throws errors!
package com.haziqhussain.hazgames;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SplashScreen {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splash_screen, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Everything seems normal however some code is not having it.
You need to extend the ActionBarActivity class. The ActionBarActivity contains the methods which you are trying to override.
Need not necessarily be ActionBarActivity. Whenever your class is using the life cycle states of an Activity(OnCreate(), OnStart(), OnPause, etc.)you have to extend Activity because all of the activity classes are subclasses of android.app.Activity.

Syntax error while changing Font

I'm trying to change the Font of a textView (in my case custom_font)
but if gives the 2 error on this line:
txt.setTypeface(font);
At the (dot) it says:
Syntax error on token(s), misplaced construct(s)
At (Font) it says:
Syntax error on token "font", VariableDeclaratorId expected after this token
I have no idea how to fix it, i would be nice if anyone would help me :)
Thanks in advance.
MainActivity:
package com.test.testforStack;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/hallo.ttf");
txt.setTypeface(font);
#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);
}
Try to move these three lines into your onCreate method:
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/hallo.ttf");
txt.setTypeface(font);

cannot find symbol variable Android Studio 0.8.1

I am new to android studio and I am trying to run this code.
My code:
package app.sunshine.android.example.com.sunshine;
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.ViewGroup;
import android.os.Build;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
if (savedInstanceState == null) {
getFragmentManager().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.my, 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_my, container, false);
// Create some dummy data for the ListView. Here's a sample weekly
// Represented as "day, whether, high/low"
String[] forecastArray= {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 70/40",
"Weds - Cloudy - 72/63",
"Thurs - Asteroids - 75/65",
"Fri - Heavy Rain - 65/56",
"Sat - HELP TRAPPED IN WEATHERSTATION - 60/51",
"Sun - Sunny - 80/68"
};
List<String> weekForecast = new ArrayList<String>(
Arrays.asList(forecastArray));
// Now that we have some dummy forecast data, create an ArrayAdapter.
// The ArrayAdapter will take data from a source ( Like our dummy forecast
// use to populate the ListView it's attached to
mforecastAdapter =
new ArrayAdapter<String>(
//The current context( this fragment's parent activity
getActivity(),
// ID of list item layout
R.layout.list_item_forecast,
// ID of the textview to populate
R.id.list_item_forecast_textview,
// Forecast data
weekForecast);
// Get a reference to the LIstView and attach this adpater to it
ListView listView = (ListView) rootView.findViewById(
R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
return rootView;
}
}
}
but I get the following error : cannot find symbol variable
I tried cleaning the project gradle and restarted android application .
but I am still encountering the issue
any help would be greatly appreciated :)
I am following this tutorial https://www.udacity.com/course/viewer#!/c-ud853/l-1395568821/e-1395668601/m-1395668603
You need to declare mForecastAdapter.
public static class PlaceholderFragment extends Fragment {
ArrayAdapter<String> mForecastAdapter;
public PlaceholderFragment() {
}
Since you have not done that yet it does not recognize the variable mForecastAdapter. Hope this helps.

Resources