I've created a simple 'app' to put values from text to an array and it doesn't seem to work.. I don't know why..
public class Register extends AppCompatActivity {
String[] data = new String[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
gotoReg();
}
public void gotoReg() {
Button register = (Button) findViewById(R.id.Submit);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data[0] = "ss";
data[1] = "dd";
}
}
);
}
}
From this code I get the error :
java.lang.NullPointerException: Attempt to write to null array
I did almost the same code in another app and it worked.. seems really wierd to me...
Nevermind, thanks everyone, just re-opened Android studio and everything worked fine.
Wasted on it several hours ughh
Just put your code to get button reference after setContent view then,you can assign the values.
And you have checked the id of button?
Try to add
data = new String[2];
to your gotoReg() method, before setting the onClick listener.
Related
I'm new to JAVA and Android. Previously, I was working on Javascript & Jquery.
As in HTML using Javascript (or Jquery) we can add custom data attribute to any element. for example-
$("#element").data("customdata", "customvalue");
And later I can get the value by doing-
var customvalue = $("#element").data("customdata");
Is there any method available in Android to achieve this type of thing?
Like, if I need to set multiple type of strings to a single TextView and later get them as needed.
Thank you all in advance.
You can get your textview from XML and set text on it . Something like this
TextView textView = findViewById(R.id.textview_id);
textView.setText("Your awesome text");
// Later fetch the text details back
String textDetails = textView.getText().toString();
This is a basic code sample of how to add and retrieve text from the text-view. Hope this help your case
You can use the View's setTag() and getTag() for that purpose.
However, most of the time there are cleaner ways like using ViewModel architecture etc. but that is a different story.
I saw your link. I think you can use AlertDialog.
this is my code.I think you would need it.
public class MainActivity extends AppCompatActivity {
EditText e1;
TextView t4;
Buttob btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t4=(TextView)findViewById(R.id.t4);
t4.setOnClickListener(t4click);
e1=(EditText)findViewById(R.id.e1);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(btnlogin);
}
private Button.OnClickListener btnlogin=new Button.OnClickListener(){
//click button
public void onClick(View v){
String account=e1.getText().toString();//get edittext value
String tmp="";
tmp=t4.getText().toString()+account; //get textview value and add
edittext string
t4.setText(tmp);//insert new value
}
}
};
private TextView.OnClickListener t4click=new TextView.OnClickListener(){
#Override
//if click textview it,would show dialog
//if your dialog value is from edittext ,you can reuse it.
public void onClick(View view) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("product")
.setMessage("item:pen"+"\nid=A001"+"\nprice:10 USD")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();
}
};
}
I'm using a listview to display grocery items with checkmark using android.R.layout.simple_list_item_checked. The problem is when i check an item and go to the previous page and comeback to the list my item is not checked anymore..i would like to use something else than sharedpreference is that possible and how can i do that?
I try to use SparseBooleanArray but it didnt work i probably use it wrong.
Since i use the previous phone button and after i click on the button that call my list activity i should use something in my onCreate method but i'm not sure what and how?
I also try to use oninstanceResore method like some people suggest me but i dont thin it what i'm looking for let say im on the activity 2 which is my item list with some item checked i decide to go back do something else using the phone previous button and then come back to the activity 2 using the button in my apps ...i want my item to still be checked. Can someone help me on how to do this i will be very grateful…
public class FruitList_Activity extends AppCompatActivity {
private ListView fruitsList;
private ArrayAdapter<String> adapterFruit;
private Button btn_Delete;
private Button btn_SelectAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_fruit_list_);
fruitsList = findViewById(R.id.list_Fruits);
fruitsList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
btn_Delete = findViewById (R.id.btn_delete);
CreateActivity.itemsFruit = FileHelper.readData(this);
adapterFruit = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, CreateActivity.itemsFruit);
fruitsList.setAdapter(adapterFruit);
/*Remove items*/
btn_Delete.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
SparseBooleanArray checkedItemPositions = fruitsList.getCheckedItemPositions();
int itemCount = fruitsList.getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
fruitsList.setItemChecked(i,true);
adapterFruit.remove(CreateActivity.itemsFruit.get(i));
FileHelper.writeData(CreateActivity.itemsFruit, FruitList_Activity.this );
}
}
adapterFruit.notifyDataSetChanged();
}
});
}
}
Can you guide me on how i can do that i'm just starting with java so please explain me so i can understand. thanks!
You need to create a Bundle with the information of the list, and before calling the second activity, you'll set the data inside that bundle, and when you came back to the main activity just check the bundle and if is different at null is because you need to load the data.
I Hope this cal help you :D
this is saved Instance()
also, this cal help you when you have onConfigurationChanged()
Save your adapterFruit using onSaveInstanceState
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("adapter",(Serializable) adapterFruit);
}
The onRestoreInstance executes when you go back to the activity
#Override
protected void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(saveInstanceState != null){
adapterFruit = (ArrayAdapter<Adapter> )savedInstanceState.getSerializable("adapter");
}
}
Hope it helps.
you can do like this
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(holder.checkBox.isChecked()==true) {
holder.checkBox.setChecked(true);
model_image.get(position).setSelected(true);
}
else
{
holder.checkBox.setChecked(false);
model_image.get(position).setSelected(false);
}
}
});
Hello I'm starting in Android Studio and I wanted to know how to pass from one activity to another. I've tried different methods which I've seen in youtube tutorials but all of them show me the same error:
Cannot resolve symbol 'activity_menu'.
Does anyone know how to solve it?
Here is my code:
public class Menu extends AppCompatActivity {
Button boton_start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
boton_start=(Button) findViewById(R.id.boton_menu);
boton_start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(activity_menu.this,activity_dishes.class);
startActivity(in);
}
});
}
}
The same happens with the other activity, but I suppose that the solution is the same for both.
You've mentioned the layout file names instead of name of the activities
Your code:
Intent in = new Intent(activity_menu.this,activity_dishes.class);
It should be:
Intent in = new Intent(Menu.this,Dishes.class);
Mention the name of the activity, not layout files.
Try getting activity_menu like this: R.layout.activity_menu
I am learning how to use strings and onlclick in java. I have written a programme below which shuffle three names and then outputs them into three buttons.
When I click on Paul, I want the message to be displayed in message box. Since Paul will be in a button each time. I am puzzled on how to attach my message to Paul.
Paul moves around due to the use of array. I understand this is a tough question, but I also know, there are some very clever ppl out there who love a challenge.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void generate(View view) {
String [] names = new String[3];
names[0] = "Bob";
names[1] = "Paul";
names[2] = "Mike";
Button btn_a = (Button) findViewById(R.id.a);
Button btn_b = (Button) findViewById(R.id.b);
Button btn_c = (Button) findViewById(R.id.c);
TextView message = (TextView)findViewById(R.id.message);
Arrays.asList(names);
Collections.shuffle(Arrays.asList(names));
btn_a.setText(names[0]);
btn_b.setText(names[1]);
btn_c.setText(names[2]);
}
public void a1(View view) {
}
public void b1(View view) {
}
public void c1(View view) {
}
}
This is a trick practical implementation in Java where a single listener is used for multiple buttons, rather than one listener for each button, so that each button's content determines what happens, not each button's listener. Helps for dynamic button grids (i.e. an 8x8 chessboard) to not define 64 listeners and code them all.
I don't have an Android IDE on hand, so this is pseudo-code, but you should be able to get the gist from this.
//Create a Universal Listener for all our buttons
OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button b = (Button)v;
String text = b.getText().toString(); //get the button's name
if(text.equals("Paul")) {
//do anything for Paul ONLY in here
}
}
});
btn_a.setOnClickListener(listener); //give all the buttons the same listener, but only Paul's listener will do anything when you click on it
btn_b.setOnClickListener(listener);
btn_c.setOnClickListener(listener);
Using info from: http://developer.android.com/reference/android/widget/Button.html and https://stackoverflow.com/a/5620816/2958086
I'm running through a for-loop and am generating TextViews that should be clickable because I want to start then an intent and pass the url as parameter as well as the source.
So, I've tried this
articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
articleURL[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//System.out.println(articleURL[v.getId()].getText().toString());
System.out.println(v.getId());
}
});
The problem i encounter is that the v.getId() is always 0. And when i use the commented code
System.out.println(articleURL[v.getId()].getText().toString());
I get an exception that says
java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
I just need the content of the TextView i clicked on. How exactly do i get it? articleURL[i] doesn't work because he doesn't know i then. How can v.getId() always be -1? No matter which one I click?
This here is the complete for-loop
TextView articleURL = new TextView[hashMapSize];
for (int i = 0; i < hashMapSize; i++) {
articleURL[i] = new TextView(getActivity());
articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
articleURL[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(articleURL[v.getId()].getText().toString());
//System.out.println(v.getId());
}
});
}
You actually get the View in the parameter. Just cast it to TextView and call getText()
public void onClick(View v) {
Log.d("text",((TextView) v).getText().toString());
}
Also don't use System.out.println. This is Android, not desktop Java, and coding android is a huge difference to normal Java. You should get a book on Android and read it, otherwise your apps will start crashing pretty soon and you won't have any chance to fix them.
You may try the following:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(((TextView)v).getText());
}
};
// ... some loop
articleURL[i].setOnClickListener(listener);
If you also want to get index of item, try this:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(v.getTag());
}
};
// ... some loop
articleURL[i].setTag(i);
articleURL[i].setOnClickListener(listener);
Try this..
use this as globel
TextView articleURL[];
and then initial the articleURL like below
articleURL = new TextView[hashMapSize];
and then if your extends fragement means use below
articleURL[i] = new TextView(getActivity());
extends activity means
articleURL[i] = new TextView(this);
and
System.out.println(((TextView)v).getText().toString());