Happy day to all of you.
I have been attempting to make a stopwatch application as my first one, while using the Chronometer function. The problem is, when I click on the Start button (while the app is running on tablet), it crashes. The situation is similar to the others.
I am not sure how I should fix this, and I greatly appreciate your help.
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<ImageView
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#color/colorPrimaryDark" />
<LinearLayout
android:id="#+id/navcon"
android:layout_width="match_parent"
android:layout_height="92dp"
android:orientation="vertical">
<ImageView
android:id="#+id/navbar"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/colorPrimary" />
</LinearLayout>
<LinearLayout
android:id="#+id/navtextcon"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="vertical">
<TextView
android:id="#+id/navtext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="rocket.studios"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
<LinearLayout
android:id="#+id/chronocon"
android:layout_width="match_parent"
android:layout_height="95dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="78dp"
android:orientation="vertical">
<Chronometer
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="60sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/startbcon"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="155dp"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="#+id/startb"
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#color/colorPrimary"
android:text="#string/start"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/stopbcon"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="#+id/stopb"
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#color/colorPrimary"
android:text="#string/stop"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/resetbcon"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="158dp"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="#+id/resetb"
android:layout_width="match_parent"
android:layout_height="68dp"
android:background="#color/colorPrimary"
android:text="#string/reset"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
MainActivity.java :
package testing.ezekieralt.com.stopwatcher;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.os.SystemClock;
//chronometer is...guess it
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Chronometer chronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.startb).setOnClickListener(this);
findViewById(R.id.resetb).setOnClickListener(this);
findViewById(R.id.stopb).setOnClickListener(this);
chronometer = findViewById(R.id.chronometer);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.stopb:
chronometer.stop();
break;
case R.id.startb:
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
break;
case R.id.resetb:
chronometer.setBase(SystemClock.elapsedRealtime());
break;
}
}
}
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="testing.ezekieralt.com.stopwatcher">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You didn't added id to your Chronometer. Add id to your Chronometer
<Chronometer
android:id="#+id/chronometer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="60sp"
android:textStyle="bold" />
Related
I am trying to implement a true caller like alert dialog in flutter. Since I am very new to flutter+dart, I was looking for any out of the box implementation. So far I have found system_alert_window but this creates the alert dialogue in JAVA and uses platform channels to communicate with it. My question is that if it's possible to do this is pure flutter+dart.
I do not know if this kind of dialogues has any special names, also in truecaller the dialogue is shown regardless the app is running or not. Can anyone provide some pointers?
Thanks
No you can't do with flutter
You can only go with Java.
Flollow the steps
your project > android > app > src > main > AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
add this permission on your Androidmanifest.xml file
after adding the permission add the below receiver class
<receiver android:name=".CallReceiver"
android:enabled="true"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
the receiver class
ServiceReceiver.Java
public class ServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
System.out.println("incomingNumber : "+incomingNumber);
WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
View view = LayoutInflater.from(context).inflate(R.layout.popup_window, null); // Code for inflating xml layout
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
windowManager.addView(view, params);
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
}
add the below xml file on the folder
your project > android > app > src > main > res > layout > popup_window.xml
popup_window.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:background="#000000"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/popup_color"
android:orientation="vertical"
tools:ignore="UselessParent">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/paymentlogo"
android:layout_width="65dp"
android:layout_height="65dp"
android:scaleType="fitCenter"
android:src="#drawable/rsoft_logo"
app:srcCompat="#drawable/rsoft_logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal">
<TextView
android:id="#+id/lbl_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:layout_weight="0.3"
android:singleLine="true"
android:text="Anand"
android:textColor="#000000"
android:textSize="20sp" />
<ImageView
android:id="#+id/iv_close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.7"
android:src="#drawable/ic_close_black" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/txt_record_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="gone"
tools:ignore="HardcodedText" />
<View
android:id="#+id/btw_view"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_gravity="center"
android:background="#bab9b9" />
<LinearLayout
android:id="#+id/details_adp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/tv_icon"
android:layout_width="17dp"
android:layout_marginTop="4dp"
android:layout_height="17dp"
android:layout_weight="1"
android:textColor="#000000"
android:src="#android:drawable/sym_call_incoming"
android:textSize="10sp" />
<TextView
android:id="#+id/tv_mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_weight="1"
android:text="7667673691"
android:padding="0dp"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>
<TextView
android:id="#+id/tv_mobile_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:text="Incomingg"
android:textColor="#000000"
android:textSize="12sp" />
<!-- <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="4dp"
android:padding="3dp">
<ImageView
android:id="#+id/iv_im"
android:layout_width="14dp"
android:layout_height="14dp"
android:src="#drawable/sim" />
<TextView
android:id="#+id/tv_sim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="Incomingg"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>-->
</LinearLayout>
<TextView
android:id="#+id/tv_lead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="Save As Lead"
android:background="#drawable/rounded_btn"
android:padding="8dp"
android:layout_marginTop="6dp"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
You can use flutter_overlay_window. Its JAVA only but works fine. The example app has a truecaller like overlay demo. The other answer is correct, no it can't be done in pure flutter.
This question already has answers here:
findViewById returns null
(4 answers)
Can not find a View with findViewById()
(4 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
My apps unfortunately stopped after right I put the OnClickListener
My problem shows :
at com.example.whatisthat.Dashboard.onCreate(Dashboard.java:23)
Error :
LoginBtn.setOnClickListener(this);
Dashboard.java
package com.example.whatisthat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Dashboard extends AppCompatActivity implements View.OnClickListener
{
private CardView LoginBtn, CaptureBtn, AboutBtn, FeedbackBtn, IgBtn, HelpBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//DEFINE CARDS
//ADD CLICK LISTENER TO THE CARDS
LoginBtn = findViewById(R.id.Login);
LoginBtn.setOnClickListener(this);
CaptureBtn = findViewById(R.id.Capture);
CaptureBtn.setOnClickListener(this);
AboutBtn = findViewById(R.id.About);
AboutBtn.setOnClickListener(this);
FeedbackBtn = findViewById(R.id.Feedback);
FeedbackBtn.setOnClickListener(this);
IgBtn = findViewById(R.id.Ig);
IgBtn.setOnClickListener(this);
HelpBtn = findViewById(R.id.Help);
HelpBtn.setOnClickListener(this);
}
#Override
public void onClick(View view)
{
Intent i;
switch (view.getId())
{
case R.id.Login : i = new Intent(this,Login1.class);
startActivity(i);
break;
case R.id.Capture : i = new Intent(this, Capture.class);
startActivity(i);
break;
case R.id.About : i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.Feedback : i = new Intent(this, Feedback.class);
startActivity(i);
break;
case R.id.Ig : i = new Intent(this, Instagram.class);
startActivity(i);
break;
case R.id.Help : i = new Intent(this, Help.class);
startActivity(i);
break;
default:break;
}
}
}
activity_dashboard.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Dashboard">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="230dp"
android:orientation="vertical"
android:gravity="center"
android:background="#6A287E">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/iconfyp"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DASHBOARD"
android:textColor="#color/colorAccent"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="215dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<androidx.cardview.widget.CardView
android:id="#+id/Login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView1"
android:layout_height="50dp"
android:src="#drawable/people"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGIN"
android:textSize="16sp"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonLogin"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/Capture"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView2"
android:layout_height="50dp"
android:src="#drawable/cameraa"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAPTURE"
android:textSize="16sp"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonCapture"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<androidx.cardview.widget.CardView
android:id="#+id/About"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView3"
android:layout_height="50dp"
android:src="#drawable/aboutt"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABOUT"
android:textSize="16sp"
android:layout_below="#+id/imageView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonAbout"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/Feedback"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView4"
android:layout_height="50dp"
android:src="#drawable/feedback"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FEEDBACK "
android:textSize="16sp"
android:layout_below="#+id/imageView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonFeedback"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<androidx.cardview.widget.CardView
android:id="#+id/Ig"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView5"
android:layout_height="50dp"
android:src="#drawable/instagram"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INSTAGRAM"
android:textSize="16sp"
android:layout_below="#+id/imageView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonInstagram"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/Help"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView6"
android:layout_height="50dp"
android:src="#drawable/help"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELP"
android:textSize="16sp"
android:layout_below="#+id/imageView6"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonHelp"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".Help"
android:label="#string/Help"/>
<activity android:name=".Instagram"
android:label="#string/Instagram"/>
<activity android:name=".Feedback"
android:label="#string/Feedback"/>
<activity android:name=".About"
android:label="#string/About"/>
<activity android:name=".Capture"
android:label="#string/Capture"/>
<activity android:name=".Login1"
android:label="#string/Login1"/>
<activity android:name=".Dashboard"
android:label="#string/Dashboard"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
In your onCreate() method there isn't
setContentView(R.layout.activity_dashboard);
before the methods findViewById() which return null.
Use:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard); //<-- add the method
LoginBtn = findViewById(R.id.Login);
i am working on a filter app and have created a splash screen and one other activity named 'ControlActivity' but as soon as the splash screen disappears the app closes without loading the 'ControlActivity'.
Also it does not gives any error closing the app.
The code for Main Activity is:-
package com.example.harsh.filters;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this, ControlActivity.class);
startActivity(intent);
}
},1000);
}
}
The XML code for the ControlActivity:-
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="#android:color/white"
tools:context=".ControlActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:padding="12dp"
android:theme="?attr/actionBarTheme" />
<ImageView
android:id="#+id/centerImageView"
android:layout_width="match_parent"
android:layout_height="315dp"
android:layout_alignParentTop="true"
android:layout_marginTop="55dp"
app:srcCompat="#mipmap/img1"
tools:layout_below="#+id/toolbar"
tools:padding="16dp" />
<LinearLayout
android:id="#+id/controlLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/centerImageView"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp"
tools:layout_below="#+id/centerImageView"
tools:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/uncheck" />
<SeekBar
android:id="#+id/seekBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/check" />
</LinearLayout>
<LinearLayout
android:id="#+id/thumbnailLinearLayout"
android:layout_width="match_parent"
android:layout_height="147dp"
android:orientation="horizontal"
android:padding="8dp"
tools:layout_below="#+id/controlLinearLayout">
<ImageView
android:id="#+id/imageView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect1" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect2" />
<ImageView
android:id="#+id/imageView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect3" />
<ImageView
android:id="#+id/imageView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#mipmap/effect4" />
</LinearLayout>
</RelativeLayout>
**The ControlActivity:-**
package com.example.harsh.filters;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class ControlActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
}
}
Manifest:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.harsh.filters">
<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=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ControlActivity"
android:label="#string/title_activity_control"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
Please help me out and thanks.
The Intent is working for some activity's, for some specific activity intent is crashing I have changed and tried it in different ways but for some activity intent is not working, app is getting built but after running on the phone it does not show any error during the debugging time ? I tried it in different way but it still crashes can anyone help me solve it?
MainActivity java
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button conee = (Button)findViewById(R.id.b1);
conee.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
});
Button cupp = (Button)findViewById(R.id.b2);
cupp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent ko = new Intent(MainActivity.this,Main3Activity.class);
startActivity(ko);
}
});
}
}
Main3Activity java
package cheercreams.design;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Main3Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
}
}
MainActivity 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cheercreams.design.MainActivity">
<Button
android:id="#+id/b1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="45dp"
android:layout_x="25dp"
android:layout_y="17dp"
android:text="Cone"
android:textSize="14dp" />
<Button
android:id="#+id/b2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="#+id/b1"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:text="cup"
android:textSize="14dp" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/b3"
android:layout_toRightOf="#+id/b2"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:text="Butterfly box"
android:textSize="14dp"/>
<Button
android:id="#+id/b4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="45dp"
android:layout_below="#+id/b1"
android:text="family pack"
android:textSize="14dp" />
<Button
android:id="#+id/b5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="#+id/b4"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b2"
android:text="kulfi"
android:textSize="14dp" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/b6"
android:layout_toRightOf="#+id/b5"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b3"
android:text="stick"
android:textSize="14dp"/>
<Button
android:id="#+id/b7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="45dp"
android:layout_below="#+id/b4"
android:text="sundae"
android:textSize="14dp" />
<Button
android:id="#+id/b8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="#+id/b4"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b5"
android:text="special pack"
android:textSize="14dp" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/b9"
android:layout_toRightOf="#+id/b5"
android:layout_marginTop="45dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/b6"
android:text="cassette"
android:textSize="14dp"/>
</RelativeLayout>
Main3Activity 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cheercreams.design.Main3Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/heading"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:id="#+id/t1"
android:layout_marginTop="20dp"/>
<ImageView
android:layout_width="240dp"
android:layout_height="200dp"
android:id="#+id/i1"
android:layout_below="#+id/t1"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:background="#drawable/bs"
android:layout_toRightOf="#+id/i2"/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="45dp"
android:numStars="5"
android:id="#+id/r1"
android:rating="4.5"
android:contextClickable="true"
android:layout_marginLeft="60dp"
android:layout_below="#+id/i1"
/>
<TextView
android:layout_width="60dp"
android:layout_height="30dp"
android:text="Type :"
android:textSize="20dp"
android:id="#+id/t4"
android:layout_below="#+id/r1"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="60dp"
android:layout_height="30dp"
android:text="Price:"
android:textSize="20dp"
android:id="#+id/t5"
android:layout_below="#+id/t4"
android:layout_marginLeft="60dp"
android:layout_marginTop="25dp"
/>
<TextView
android:layout_width="40dp"
android:layout_height="30dp"
android:id="#+id/t6"
android:text="#string/Rs"
android:textSize="20dp"
android:layout_below="#+id/b1"
android:layout_toRightOf="#+id/t5"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:id="#+id/b1"
android:text="500ml"
android:layout_below="#+id/r1"
android:layout_toRightOf="#+id/t4"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="80dp"
android:layout_height="40dp"
android:id="#+id/b2"
android:text="1000ml"
android:layout_below="#+id/r1"
android:layout_toRightOf="#+id/b1"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"/>
<ImageButton
android:layout_width="80dp"
android:layout_height="70dp"
android:id="#+id/i2"
android:layout_below="#+id/t1"
android:background="#drawable/bs"
android:layout_marginTop="60dp"
android:layout_marginLeft="2dp"
/>
<ImageButton
android:layout_width="80dp"
android:layout_height="70dp"
android:id="#+id/i3"
android:layout_marginTop="2dp"
android:layout_below="#+id/i2"
android:layout_marginLeft="2dp"
android:background="#drawable/bs"
/>
<Button
android:layout_width="220dp"
android:layout_height="50dp"
android:id="#+id/b3"
android:layout_below="#+id/t5"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
android:text="Add to Cart"
android:background="#color/colorOrange"
/>
</RelativeLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cheercreams.design">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity" />
<activity android:name=".Main3Activity" />
<activity android:name=".Main4Activity" />
<activity android:name=".Main5Activity"></activity>
</application>
</manifest>
You defined Main2Activity in manifest file but you don't initiate Main2Activity class in package.
So, You able to go only from MainActivity to Main3Activity but not to Main2Activity.
I very a newbie at android programming. I'm here because i want your help. Im designing an activity for my Thesis.
Heres the Image
I want to have an activity that has 2 Frames/Layout. The upper frame have buttons, the lower frame will display something when i click the button on the upper frame.
What would i use to make this happen?
Thank you in advance.
You can split the layout in half using LinearLayout and layout_weight param, like this:
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
layout_weight parameter functions as a priority in deciding who gets the space, given the other desciption would cause any of FrameLayouts to fill entire parent. You can find more in documentation.
Here is the complete solution:
step1:
create main_layout.xml file into layout folder:
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView" />
</LinearLayout>
step2: create MainActivity:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private View mBtn1;
private View mBtn2;
private TextView mTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
mBtn1 = findViewById(R.id.button1);
mBtn2 = findViewById(R.id.button2);
mTxt = (TextView)findViewById(R.id.textView);
mBtn1.setOnClickListener(mBtnClickListener);
mBtn2.setOnClickListener(mBtnClickListener);
}
View.OnClickListener mBtnClickListener = new View.OnClickListener() {
/**
* This is all sync because we want to have only one event at a time
* #param v
*/
#Override
public synchronized void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
mTxt.setText("button1");
break;
case R.id.button2:
mTxt.setText("button2");
break;
}
}
} ;
}
step3: add the activty into your AndroidManifest.xml:
<activity
android:name=".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>