Button wont start another activitie - android

my button with the id radio, to start second activity, isnt working and my app keeps crashing. The second button however starts another activity wich is empty for the moment. can somebody please tell me what did i miss, so my app stops crashing when i press the button.
my first activity xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mitja.radiohead.PrvaStran">
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayou xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mitja.radiohead.PrvaStran">
<LinearLayout
android:layout_width="368dp"
android:layout_height="0dp"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="49dp"
android:weightSum="1">
<ImageView
android:id="#+id/slika"
android:layout_width="match_parent"
android:layout_height="218dp"
android:layout_weight="0.16"
android:src="#drawable/radio"
android:layout_marginLeft="1dp"
android:layout_marginRight="2dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="16dp" />
<Button
android:id="#+id/radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="0.50"
android:text="radio"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="323dp" />
<Button
android:id="#+id/predvajalnik"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="predvajalnik"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="371dp" />
</LinearLayout>
java code of the first activity
public class PrvaStran extends AppCompatActivity {
private static Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prva_stran);
onClickButton();
onClickButton2();
}
public void onClickButton()
{
btn1 = (Button) findViewById(R.id.radio);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openMainAct = new Intent(PrvaStran.this, Radio.class);
startActivity(openMainAct);
}
});
}
public void onClickButton2()
{
btn1 = (Button) findViewById(R.id.predvajalnik);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.mitja.radiohead.Predvajalnik");
startActivity(intent);
}
});
}
}
and the android manifestxml code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mitja.radiohead">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".PrvaStran">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Radio"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.example.mitja.radiohead.Radio" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Predvajalnik">
<intent-filter>
<action android:name="com.example.mitja.radiohead.Predvajalnik" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
what am i missing here? the second activity is made from a tutorial i saw on youtube to create a listview with images and texts. I got the code from this site now im trying to use it in this app for practice. i havent had any experiance with android studio before so im asking for a little guide.

In your code you are referencing same button to two different ids
private Button btn1;
btn1 = (Button) findViewById(R.id.radio);
btn1 = (Button) findViewById(R.id.predvajalnik);
That is why app is getting crashed.
declare two different buttons and then refer them to respected ids.
private Button btn1;
private Button btn2;
btn1 = (Button) findViewById(R.id.radio);
btn2 = (Button) findViewById(R.id.predvajalnik);

You don't need to write the buttons as fields, but you do need to actually set both buttons listeners
findViewById(R.id.radio).setOnClickListener(
...
);
findViewById(R.id.predvajalnik).setOnClickListener(
...
);

Related

Show dynamic edittext above the keyboard in android

I have an android screen and I need to create an edit text when some button is clicked, the edit text is not shown in the screen by default, I want to show it directly above the keyboard like the following screen shot. Is there are any way to do that?
enter image description here
I think this will help you
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/btnOpen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open" />
</RelativeLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
AppCompatButton btnOpen;
RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOpen = findViewById(R.id.btnOpen);
relativeLayout = findViewById(R.id.relativeLayout);
btnOpen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addEditText();
}
});
}
private void addEditText() {
// add edittext
EditText et = new EditText(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
et.setLayoutParams(lp);
et.setHint("Text");
relativeLayout.addView(et);
}
}

Starting activity from fragment button click (intent) throws blank screen

I'm trying to start an activity from a button which is placed in a fragment, but when I run the app (in emulator and in a real device) and I press that button, the activity doesn't start and a blank screen whithout navigation bar appears. I've read a lot about this, and the most common error people had is that they didn't declare the Activity or they weren't inflating the correct layout. Any idea would be appreciated, because I don't now what to do right now.
Here is the code of the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.isa.example" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/Theme.CustomMaterial" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NewActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/Theme.CustomMaterial" >
<intent-filter>
<action android:name="com.isa.example.NewActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the code for the fragment layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start new activity"/>
</RelativeLayout>
Code for the fragment:
public class Fragment1 extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), NewActivity.class);
startActivity(i);
}
});
return view;
}
}
Code for the new activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="This is new activity"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
And code for the new activity:
public class NewActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.newactivity_layout);
}
}
What is your minimum sdk version? Your version of onCreate is introduced in 21: stackoverflow.com/a/29871359/1541763 Remove the PersistableBundle persistentState from your parameters

soft keyboard doesn't close and freeze when close

SCENARIO
My App is open 3rd party application from Activity C (A->B->C). After, 3rd party application finished it return to my application on Activity C. Everything work fine until only one EditText in Activity C was typing with soft keyboard and I want to close it.
Soft Keyboard freeze and not close, also my app didn't freeze.
My test device is base on Android KitKat.
I change to other keyboard both default and custom.. no hope
I push home button and return to my app again soft keyboard behavior fine.
Here is my Sample XML (which reproduce this bug)
ACTIVITY A
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go to Activity B"
android:id="#+id/btnNext"/>
</LinearLayout>
ACTIVITY B
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go To Activity C"
android:id="#+id/btnNext"/>
</LinearLayout>
ACTIVITY C
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/padding_medium"
android:background="#DEE1E3"
android:id="#+id/llReceiverContainer">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/padding_default"
android:hint="Receiver Name"
android:id="#+id/edtReceiver"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="#dimen/button_height_big"
android:layout_marginBottom="#dimen/padding_default"
android:layout_marginLeft="#dimen/padding_default"
android:layout_marginRight="#dimen/padding_default"
android:text="Ex. Call 3rd Party App"
android:textColor="#fff"
android:textStyle="bold"
android:textSize="#dimen/font_large"
android:id="#+id/btnTestMpos"/>
</LinearLayout>
</ScrollView>
Here is my Sample CLASS (A->B->C just call startActivity)
CLASS ActivityC
public class ActivityC extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
Button btnTestMpos = (Button) findViewById(R.id.btnTestMpos);
EditText edtUser = (EditText) findViewById(R.id.edtReceiver);
btnTestMpos.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
payWithMPOS();
}
});
}
private void payWithMPOS() {
//call thire pparty application
}
// return from 3rd party application
protected void onNewIntent(Intent intent) {
setIntent(intent);
getResultFromMPOS();
}
public void getResultFromMPOS() {
// extract result from 3rd party applicaiton
}
}
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ui.keyboard"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14"/>
<uses-permission android:name="android.permission.GET_TASKS" >
</uses-permission>
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name=".ActivityA"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ActivityB"
android:label="#string/app_name">
</activity>
<activity android:name=".ActivityC"
android:launchMode="singleTask"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.example.mpos.integration" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
try thisin your Activity C Java class
ScrollView yourScrollViewName= (Button) findViewById(R.id.yourScrollViewId);
yourScrollViewName.setOnTouchListener(this);
#Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
return false;
}

"the application has stopped unexpectedly android please try again"

I have wrote this simple code but i don know why my emulator always shows me this error:"the application has stopped unexpectedly android please try again". I tried to get rid of it but was not done successfully help me please thanks.
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
sub = (Button) findViewById(R.id.bSub);
display = (Button) findViewById(R.id.tvDisplay);
add = (Button) findViewById(R.id.bAdd);
// *****onClick method****//
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter = counter + 1;
display.setText("Your Total is " + counter);
}
});
}
}
//my xml//
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="#+id/tvDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/total"
android:textSize="45sp" />
<Button
android:id="#+id/bAdd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/add" />
<Button
android:id="#+id/bSub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/sub" />
</LinearLayout>
my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thenewbostonnn"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.thenewbostonnn.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Use
display = (TextView) findViewById(R.id.tvDisplay);
Instead of
display = (Button) findViewById(R.id.tvDisplay);
Currently you are casting an TextView to Button
sub = (Button) findViewById(R.id.bSub);
display = (TextView ) findViewById(R.id.tvDisplay); // change button to TextView
add = (Button) findViewById(R.id.bAdd);

android button click not working in program

Hello everybody i have a problem that when i click a button it remains as it is instead of starting new activity. I searched he problem in this site and found some solutions but none of them worked for me so i am writing my problem here.
the xml layout is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/profile_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/profile" />
<Button
android:id="#+id/create_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/create_profile" />
<Button
android:id="#+id/edit_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/create_profile"
android:text="#string/edit_profile" />
<Button
android:id="#+id/delete_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/edit_profile"
android:text="#string/delete_profile" />
<Button
android:id="#+id/activate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/delete_profile"
android:text="#string/activate" />
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/create_profile"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#id/profile_title" />
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/create_profile"
android:layout_below="#id/profile_title"
android:gravity="center_vertical|center_horizontal"
android:text="#string/no_profiles" />
</RelativeLayout>
and the activity is
package com.android.SmartSwitch;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Profile_Manager extends Activity {
private Button createButton;
private Button editButton;
private Button deleteButton;
private Button activateButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
setUpViews();
}
private void setUpViews() {
createButton = (Button)findViewById(R.id.create_profile);
editButton = (Button)findViewById(R.id.edit_profile);
deleteButton = (Button)findViewById(R.id.delete_profile);
activateButton = (Button)findViewById(R.id.activate);
createButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(Profile_Manager.this, Add_Profile.class);
startActivity(intent);
}
});
editButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
activateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
}
}
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.SmartSwitch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".SmartSwitchActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Location_Manager" android:label="#string/app_name"/>
<activity android:name=".Profile_Manager" android:label="#string/app_name"/>
<activity android:name=".Schedule_Manager" android:label="#string/app_name"/>
<activity android:name=".Location_In_Map" android:label="#string/app_name"/>
<activity android:name=".Add_Profile" android:label="#string/app_name"/>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
When i click createButton, it doesn't respond
As your Main Activity i.e., Profile_Manager consists of the following Code:
<activity
android:label="#string/app_name"
android:name=".SmartSwitchActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You have to note if the Profile Manager class is the class which will be launched i.e., if Profile_Manager Class is the First Screen then, you should create the above code in AndroidManifest.xml with the only change as android:name=".Profile_Manager" instead of "SmartSwitchActivity" with the code additionally added for Add_Profile class as follows
<activity
android:label="#string/app_name"
android:name=".Add_Profile" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I suppose by doing this changes,things will work fine.
If you are not able to understand the above working then,Here is the Link with Perfect Working Example and with available Source Code-Correct Working of Button Click

Categories

Resources