trying to dynamically insert photos into horizontalscrollview (android) - java

I am trying to extract photos from my drawable folder into an ImageView array and add the views from the array as childs to the linearlayout which is part of the horizontalscrollview.
However when I launch the app, it stops unexpectedly. Would anyone know if there is any problem with my code. Thanks a mil for your help.
package com.example.myfirstapp;
import java.lang.reflect.Field;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class NewGallery extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_gallery);
LinearLayout gallery = (LinearLayout) findViewById(R.id.mygallery);
Field[] field = R.drawable.class.getFields();
final int arraylength = field.length;
ImageView[] images = new ImageView[arraylength];
for (int i=0; i<=arraylength; i=i+1)
{
images[i] = new ImageView(getApplicationContext());
images[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("icon"+i, "drawable", getPackageName())));
gallery.addView(images[i]);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_new_gallery, menu);
return true;
}

If you are using Eclipse, refer to logcat error messages. I think you got a ClassCastException due to incorrect casting :
LinearLayout gallery = (LinearLayout) findViewById(R.id.mygallery).
Are you sure you are casting the correct view ?

Related

How to not navigate back to certain fragments using a bottom navigation bar in Android

I have a bottom navigation bar with 4 buttons. One of them is a back-button that just navigates back to the previously displayed fragment (I use a one-activity multiple-fragments approach). Basically everything works fine. Now I wanted to know whether it is possible not to navigate back to a certain fragment A? So basically whenever this Fragement A was the last fragment that was being displayed and the user clicks on the back-button, it should not navigate back but rather stay on the current fragment (and maybe display a small toast message).
So here you can see my main activity where the navigation is implemented:
package com.example.td.bapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import com.example.td.bapp.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
public static DataBaseHelper myDB;
public static boolean veryFirstCreation = true;
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
final NavController navController = Navigation.findNavController(this,
R.id.navHostfragment);
NavigationUI.setupWithNavController(binding.bottomNavigation, navController);
myDB=new DataBaseHelper(this);
if(veryFirstCreation) {
navController.navigate(R.id.FR_LanguageSelection);
veryFirstCreation = false;
}
// Define the bahaviour when clicking on items of the bottomNavigationBar; Overwrites the JetpackNavigation behaviour (automatic navigation using the id specified in the BottomNavigation XML menu file)
binding.bottomNavigation.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId()== R.id.BottomNavigation_Back) {
//Here I navigate back to the previous fragment
navController.navigateUp();
}
if (item.getItemId()== R.id.BottomNavigation_Language) {
navController.navigate(R.id.FR_LanguageSelection);
}
if (item.getItemId()== R.id.BottomNavigation_MyItems) {
navController.navigate(R.id.FR_MyItems);
}
if (item.getItemId()== R.id.BottomNavigation_Menu) {
navController.navigate(R.id.FR_Menu);
}
return true;
}
});
};
}
I'd appreciate every comment and would be quite thankful for your help.
You could use the SupportFragmentManager, with this you could know if your back stack contains at least one fragment. Use this in your back function (or in your onBackPressed if necessary)
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() ==1) {
Log.d("ppl","This is the last fragment. Show your message, poppup or whatever you want here")
} else if (fm.getBackStackEntryCount() >1) {
Log.d("ppl","the backstack still have more to show")
fm.popBackStackImmediate();
}

"Expected resource of type layout/menu" in Android Studio

package com.example.nimehr.streaming;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.videostream.VideoViewActivity;
public class MainActivity extends Activity {
Button button;
public MainActivity() {
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(2130903040);
this.button = (Button)this.findViewById(R.id.myButton);
this.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, VideoViewActivity.class);
MainActivity.this.startActivity(intent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
this.getMenuInflater().inflate(2131165184, menu);
return true;
}
}
I'm getting the error "Expected resource of type layout/menu" at 2130903040 in this.setContentView(2130903040); and 2131165184in this.getMenuInflater().inflate(2131165184, menu); respectively. I fixed it here this.button = (Button)this.findViewById(2131230720); by changing it to R.id.MyButton
DO not do it this way:
this.setContentView(2130903040);
R class exists for a reason to avoid explicitly using ids like this, and to replace it with a more readable cleaner code:
this.setContentView(R.layout.layout_name_here);
From the docs:
When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory.

Android SDK - Changing Properties in Array List

Can anyone please help me out. I'm having problems changing my array list properties. I am more familiar with changing properties via the XML file. Here is my code:
package com.example.examproject;
import java.util.Random;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.Intent;
public class Menu_Lists extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myAttractions = new String[2];
myAttractions[0] = "Grocery List";
myAttractions[1] = "Go Back";
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, myAttractions));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
switch(position) {
case 0: {
startActivity(new Intent(Menu_Lists.this, MainActivity.class));
break;
}
default: {
startActivity(new Intent(Menu_Lists.this, MainActivity.class));
}
}
}
#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;
}
}
What I would like to do is to change the background color. That's my main goal. I would also like each option to change color whenever the user hovers over a specific option.
Please go easy with the terminologies as well. I am still a beginner with Android SDK.
Thanks in advance.

Runtime error : NullPointerException in android although it is working on java normally

package com.example.climaconproject;
import java.net.InetSocketAddress;
import java.util.Collection;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import com.nxp.jip.JIPImpl;
import com.nxp.jip.PacketHandlerIPv6;
import com.nxp.jip.exception.JipException;
import com.nxp.jip.service.JenNetIPNetwork;
import com.nxp.jip.service.Node;
import com.nxp.jip.service.Service;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView display= (TextView) findViewById(R.id.textView2);
PacketHandlerIPv6 my_packet = new PacketHandlerIPv6();
JIPImpl my_jip = new JIPImpl(my_packet);
Service service =new Service(my_jip);
}
#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;
}
}
and the runtime error is
Fatal exception: receive
java.lang.NullExceptionPointer
at com.nxp.jip.PacketHandlerIPv6$ReceiveWorker.run(PAcketHandlerIPv6.java:223)
When i run the same lines of the code on a java program it works normally but in android the NullPointerException runtime error appears
You need at least the internet permission. Edit your AndroidManifest.xml and add
<uses-permission android:name="android.permission.INTERNET" />
inside the manifest element i.e. outside application.

How to place image at Layout by java? Android

I have an image called c1.png in drawable-mdpi folder. I want to put it to layout by Java with this:
package ...;
import android.os.Bundle;
//import android.provider.MediaStore.Images;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)findViewById(getCurrentFocus().getId() );
ImageView img = new ImageView(this);
ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
img.setLayoutParams(lp);
img.setImageDrawable(getResources().getDrawable(R.drawable.c1));
layout.addView(img);
setContentView(layout);//Doesn't matter, if it's commented or not. The error still exists.
}
#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;
}
}
I got: "Unfortunately, %application_name has stopped" when launched it. I saw many examples, using R.id.* in id params, but it stopped too (like R.id.linearLayout1), or R had not such constansts (like R.id.c1).
How can I do this?
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutID ); // //layoutID is id of the linearLayout that defined in your main.xml file
ImageView img = new ImageView(this);
ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
img.setLayoutParams(lp);
img.setBackgroundResource(R.drawable.c1);
layout.addView(img);
}
#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;
}
}
Hope this helps.
You need not do a setContentView(layout); Also, replace :
findViewById(getCurrentFocus().getId() )
by
findViewById(R.id.layoutId); //layoutId is the id of the linearLayout defined in your layout.xml file. And R would refer to your local resources folder path and not the android Resources path.
You xml file could look something like this (this would be the main.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout
android:id="#+id/layoutId"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Use the code :
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.c1);

Categories