Android. Call system dialog - java

Is there a way to open the system dialog settings->location & security->Install from SD card programmatically from my application?

You launch at least the security setting by the following code.
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName(
"com.android.settings",
"com.android.settings.SecuritySettings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}

Related

Issues with Intent

I have been struggling with Intents a little but I keep getting an error in this code.I keep getting a FATAL EXCEPTION..
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.bac1.problemcodereference/com.example.bac1.problemcodereference.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
Code of my project:
public class MainActivity extends AppCompatActivity implements Serializable {
private String cPcode;
Button help , sub;
EditText codetext;
Intent intent = new Intent (this, Main2Activity.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sub = (Button) findViewById(R.id.submit);
help = (Button) findViewById(R.id.help);
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = codetext.getText().toString();
MainActivity onew = new MainActivity();
onew.setcPcode(text);
intent.putExtra("stuff",onew);
startActivity(intent);
}
});}
At this line
Intent intent = new Intent (this, Main2Activity.class);
your activity is not completely constructed so use this call after or inside oncreate function because this mean context which will be null hence the exception
So it will be
public class MainActivity extends AppCompatActivity implements Serializable {
private String cPcode;
Button help , sub;
EditText codetext;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent (this, Main2Activity.class);
sub = (Button) findViewById(R.id.submit);
help = (Button) findViewById(R.id.help);
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = codetext.getText().toString();
MainActivity onew = new MainActivity();
onew.setcPcode(text);
intent.putExtra("stuff",onew);
startActivity(intent);
}
});}
and another problem is you why you are passing a new object of mainActivity so better option is create a separate POJO class and use it to store your data

Passing data from other activity using intents and displaying in listview

i am new to android, i have created two activities where main activity consist one button to go to secondActivity in second activity where the user inputs name and the data is taken back to main activity using intents.The problem i am facing is i want to display the data in listview
MainActivity.java
public class MainActivity extends Activity {
Button addcontact;
ListView listview;
//TextView name;
ArrayAdapter<String> listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listView1);
TextView name = (TextView) findViewById(R.id.txtname);
addcontact = (Button) findViewById(R.id.addbtn);
addcontact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, 1);
savedata();
}
});
}
private void savedata() {
Intent intent = getIntent();
String fName = intent.getStringExtra("fname");
List<String> ListElements = new ArrayList<String>(Arrays.asList(fName));
ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1, ListElements);
listview.setAdapter(adapter);
}
}
SecondActivity.java
public class SecondActivity extends Activity {
EditText edname;
Button addbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
edname =(EditText) findViewById(R.id.edname);
addbtn = (Button) findViewById(R.id.savebtn);
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
intent.putExtra("fname", edname.getText().toString());
startActivity(intent);
}
});
}
}
I would suggest you don't recreate your first Activity. Instead of that approach, use startActivityForResult().
In short - Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity()).
For more chek this link.

Android Studio: app merge release resource FAILD

`public class MainActivity extends Activity {
ImageButton button1;
ImageButton button2;
ImageButton button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton(){
button1 = (ImageButton)findViewById(R.id.Button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
button2 = (ImageButton)findViewById(R.id.Button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ThirdActivity.class);
startActivity(intent);
}
});
button3 = (ImageButton)findViewById(R.id.Button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ForthActivity.class);
startActivity(intent);
}
});
}
}
`I'm stuck with building an apk, using Android Studio. Messages Gradle Build always shows ":app:mergeReleaseResource FAILD". I wonder if there are something wrong with .png file.The picture shows the details in Messages Gradle Build

how to make new intents open after having intent

So far I have a button from my second activity that opens a new activity. As shown below..
public class FifthActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
Now the thing is, I have multiple buttons in my fifthActivity.java that I need to make start a new Activity. Going from this code, From what part of the code do I need to put in my FifthActivity.java to make it so the other buttons open?
I don't really know, if I get your question. But I guess your FifthActivity contains some buttons and you want to start different activities with these buttons. (Am I right?)
Heres an short example:
public class FifthActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity1.class);
FifthActivity.this.startActivity(intent);
}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity2.class);
FifthActivity.this.startActivity(intent);
}
});
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity3.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
This is really basic android programming. If you struggle with this you should probably do some android tutorials, before starting programming your own app.

onClick change imageButton in MainActivity

When i click onClick button change imageButton in MainActivity.class
This is Activity with onClick button:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
This is code in MainActivity with imageButton:
ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton2);
imageButton.setImageResource(R.drawable.psik);
if I understand well, you should pass some indicator to change image resource to the MainActivity and set it in onCreate(). Try to change your code like this:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
myIntent.putExtra("shouldChangeButton", true);
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
and then in MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean defaultValue = false; //or true as you need
if(getIntent().getBooleanExtra("shouldChangeButton", defaultValue)) {
ImageButton ib = (ImageButton) findViewById(R.id.imagebutton);
ib.setImageResource(R.drawable.img);
}
}
or in case you want permanent change, you should keep it in SharedPreferences instead of passing it through intent:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
prefs.edit().putBoolean("shouldChangeButton", true).apply();
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
and
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean defaultValue = false; //or true as you need
SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
boolean shouldChangeButton = prefs.getBoolean("shouldChangeButton", defaultValue);
if(shouldChangeButton) {
ImageButton ib = (ImageButton) findViewById(R.id.imagebutton);
ib.setImageResource(R.drawable.img);
}
}
You need to add a onClickListener to the view which would trigger onClick. And then you need override the onClick where you finish the operations to change imageButton.
Just Look at this:
Button btn=new Button(this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//to change you imageButton here.
imageButton.setBackgroundResource(0);
}
});

Categories

Resources