I am creating dictionary and I need to display the data from the database in the ListView. Hope someone can help me. Here is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ls = (ListView) findViewById(R.id.mainlist);
ls.setVisibility(View.VISIBLE);
You will need a custom ArrayAdapter and a method that returns all the values that you need from the database, probably a subclass of the SQLiteOpenHelper class. Read about them in detail and eventually you'll know the answer.
Related
It seems like there are a lot of questions regarding TextView and such but I am completely new to coding. I am working in Java language in Android Studio trying to make an app, I am doing this by following another persons instructions on YT, they do not get this error.
Here is the code:
package com.example.soudsrestautantcuisine;
import...
public class IntroActivity extends AppCompatActivity {
private ConstraintLayout startBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
startBtn=findViewById(R.id.startBtn);
This code is not finished but I get the error at this last line here regardless of whether the whole code is written.
May somebody please help me ?
I want to change some things in a library it is called .hannesdorfmann:swipeback
first I added :
implementation 'com.hannesdorfmann:swipeback:1.0.4'
this library into my android studio
now I have a problem :
enter image description here
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SwipeBack.attach(this, Position.LEFT)
.setContentView(R.layout.activity_mahsol_activity)
.setSwipeBackView(R.layout.swipeback_default);
I want to remove this.
can you help me?
As docuements say did you try these lines of code and change them to false ?
.setDrawOverlay(true)
.setDivider(drawable)
.setDividerEnabled(true)
When I place "setContentView" above the "NumbersClickListners" line the app works as expected.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListners numbersClickListners = new NumbersClickListners();
TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setOnClickListener(numbersClickListners);
}
But as soon as the "setContentView" is placed below the three lines starting with "NumbersClickListners" the app crashes. The code looks like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NumbersClickListners numbersClickListners = new NumbersClickListners();
TextView numbers = (TextView) findViewById(R.id.numbers);
numbers.setOnClickListener(numbersClickListners);
setContentView(R.layout.activity_main);
}
I'm pretty much unsure of the reason for this behavior. Can anybody help me with that please?
Let's look at the life of layout.
First of all, you have to create it by declaring XML file, where you do your design in a user-friendly way and name the elements according to your needs.
Now your layout is just a file, that Android doesn't really care about for performance reasons.
Next thing you wanna do is use your layout. To do that your file needs to be converted to internal structure of objects known as ViewGroup and Views.
This process is called inflating. That's the point where the system can find the views for you by calling findViewById().
So in the second snippet you ask activity to find you a button, which is not inflated. That leads to throwing exception.
Generally speaking, first thing you want to do in the onCreate is to call setContentView().
I am replacing the ActionBar and integrating the Material Design ToolBar within my app. One of my activity's extends ListActivity.
In the onCreate() method, whenever I try to add the ToolBar, the setSupportActionBar() method is producing an error saying it cannot be resolved.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_route_recipients);
// Attaching the toolbar layout to the toolbar object
toolbar = (Toolbar) findViewById(R.id.tool_bar);
// Setting toolbar as the ActionBar with setSupportActionBar() call
setSupportActionBar(toolbar);
// get the default list view associated with this activity
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // we can now check and uncheck multiple friends
// get the array of LatLng points passed in from the map intent
markerPoints = getIntent().getParcelableArrayListExtra("markerPoints");
}
What is the best solution to this problem?
You wrote that your activity extends ListActivity but sadly ListActivity doesn't implement setSupportActionBar. You have to base your activity on AppCompatActivity.
You'll say "but hey, but I really need ListActivity as I've ListView in my Activity" -- consider using RecyclerView as well. I'd say that ListView is a dead end now -- one should port code to RecyclerViews
And as a help, two sample projects showing all new features of Material Design
https://github.com/antoniolg/MaterializeYourApp
https://github.com/chrisbanes/cheesesquare
How can I change the the theme of my Android application from light to dark programmatically?
I've tried something like:
setTheme(R.style.Holo_Theme_Light);
but I found nothing which worked for me.
Should be the first line in onCreate, before calling super.onCreate(savedInstanceState); as it is where view processing takes place and your change should be before that to be included in view creation
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Holo_Theme_Light);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
Also, please refer to docs to know where to call your setTheme
You can take a look here: Android - Change app Theme on onClick
I am sorry it's not a comment but I don't have enough reputation :(
EDIT - You can't use the setTheme before the superoncreate, if you will call it before the superoncreate it will work, like here: Change Activity's theme programmatically
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
See the answer here
It says
As docs say you have to call setTheme before any view output. It seems that super.onCreate() takes part in view processing.
So, to switch between themes dynamically you simply need to call setTheme before super.onCreate like this:
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
So, make sure you have put setTheme(R.style.Holo_Theme_Light); before super.onCreate(savedInstanceState); in your code