I made list view with below code and I made array list in values. but I can't see my list when I run my program. What can I do?
public class ListView extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);;
ArrayAdapter<string>adapter;
adapter=new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,com.example.listview.R.array.countries);
setListAdapter(adapter);
}
This line is missing: setContentView(R.layout.simple_list_item_1); , no? Try this.
Related
I am confused here... let's say that you have a new class and would like to add a clickable image button to that class, can that be done? I read somewhere that it needs to be in the onCreate method, which is only found in the activity_main xml file though......
I am also having another error, where I can't find the solutions. I am having a zero constructor error in my second class when trying to use the intent method to take the user from the first class to the second class, would appreciate some assistance here as it is taking me too long to find the answers...
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PaintView pv = new PaintView(this);
setContentView(R.layout.activity_main);
Button mButton = (Button) findViewById(R.id.practice);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, PaintView.class));
}
});
My PaintView class is too long, just wondering if you could suggest which part of the code I should correct?
This is the error message:
Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
I put the setOnClickListener under my MainActivity.java and also tried it under the PaintView.java class but none is working...
I want ass my application screen comes on my bottons to animate from button to place i know how to do it in an activity but in a fragment it having issues.
There is an error in the this that says this cannot be applied to fragment and when i put get context it does not animate. Please tell me what wrong.
This is my main fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_main, container, false);
samabtn=(Button) view.findViewById(R.id.samabtn);
samabtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity mainActivity=(MainActivity)getActivity();
mainActivity.loadParrablesFragment();
}
});
Lovebtn=(Button) view.findViewById(R.id.Lovebtn);
Lovebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity mainActivity=(MainActivity)getActivity();
mainActivity.loadChapter2Fragment();
}
});
Adventurebtn=(Button) view.findViewById(R.id.Adventurebtn);
Adventurebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity mainActivity=(MainActivity)getActivity();
mainActivity.loadAdventureFragmnet();
}
});
menbtn=(Button)view.findViewById(R.id.menbtn);
frombottom= AnimationUtils.loadAnimation(this,R.anim.frombottom);
menbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity mainActivity=(MainActivity)getActivity();
mainActivity.loadMysteryFragment();
}
});
i have tried get context now the error it gone but the animation i want does not work
I am assuming that the problem you're talking about happens on this line:
frombottom= AnimationUtils.loadAnimation(this,R.anim.frombottom);
Assuming I'm right about that, the problem is that the first argument to loadAnimation() must be a Context. Activities are Contexts, which is why this works when you do it inside an Activity. However, Fragments are not Contexts, so you can't pass this as the first argument.
Within a Fragment, the easiest way to get a Context object is to call getContext(), though there are parts of the lifecycle where this will return null. Inside onCreateView(), though, this is safe.
So write this instead:
frombottom= AnimationUtils.loadAnimation(getContext(), R.anim.frombottom);
I'm new to Android Studio (and also pretty new to javascrips), so this might be a stupid question.
I do not want to maintain the same code more than once if possible, which is why I am trying to create a navigation-menu, to be used in all my activities.
So far I have created a "BaseActivity" that contains all the functions for my menu. This one works fine.
Now my problem:
In my other activities I can not get it to show up (not without issues anyway). It seems to me like it overwrites the layout.
My base ("BaseActivity"):
public class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.string.Open, R.string.Close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView NavigationView=(NavigationView)findViewById(R.id.navigation_view);
NavigationView.setNavigationItemSelectedListener(this);
}
#Override
public void setContentView(int layoutResID)
{
super.setContentView(R.layout.activity_base);
ViewGroup content = (ViewGroup) findViewById(R.id.navigation_view);
getLayoutInflater().inflate(layoutResID, content, true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if(id==R.id.nav_Home) {
startActivity(new Intent(this, MainActivity.class));
}
else
if(id==R.id.nav_Activity1) {
startActivity(new Intent(this, Activity1.class));
}
return super.onOptionsItemSelected(item);
}
}
If I change the app to start through this activity the menu works, but the layout seems to be overwritten (it's empty).
When I try to run it normally (MainActivity), I get some issues.
MainActivity:
public class MainActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }}
The menu-icon is there, but I can not click on it.
If I remove the code in my MainActivity it works as before (with the layout problem).
I have tried something like this:
public class MainActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewGroup content = (ViewGroup) findViewById(R.id.navigation_view);
getLayoutInflater().inflate(R.layout.activity_main, content, true);
}}
Not working well. The layout from MainActivity seems to be implemented in the menu. It looks very strange.
Any ideas how to solve my problem?
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.
Hello I created my app using fragments and I need to implement a viewpager in one of them (In that viewpager user can swipe between different information (about using the application that I am currently developing. I assume there will be 3 to 5 views with text and details)). Bellow is the code to current fragment. Can you guys give me an idea what would be the best way to implement Viewpager in fragment with let's say 3 views and every view will have like 3 or 4 things on it(Imageview,Textview etc). I have already implemented Viewpager in other activity but this is my first time I'm implementing it inside a fragment. What's the logic about it. I tried using google but it sound to complicated.
public class AboutSectionFragment extends Fragment {
// The onCreateView method is called when Fragment should create its View object hierarchy,
// either dynamically or via XML layout inflation.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.about_section, parent, false);
// Defines the xml file for the fragment
return rootView ;
}
// This event is triggered soon after onCreateView().
// Any view setup should occur here. E.g., view lookups and attaching view listeners.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
}
I have used ViewPager inside a fragment and its working fine for me.
Fragment class
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_content, container, false);
fragments = getFragmentsList();
pagerAdapter = new HomeViewPagerAdapter(getChildFragmentManager(),fragments);
ViewPager = (ViewPager) view.findViewById(R.id.Home_View_Pager);
ViewPager.setAdapter(pagerAdapter);
private ArrayList<Fragment> getFragmentsList(){
fragments = new ArrayList<>();
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
fragments.add(HomeViewPagerFragment.getInstance());
return fragments;
}
ViewPager Fragment Class
public class HomeViewPagerFragment extends Fragment {
public static HomeViewPagerFragment getInstance(){
HomeViewPagerFragment fragment = new HomeViewPagerFragment();
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_pager_fragment_layout,container,false);
return view;
}
}