Android

MainActivity.java



public class MainActivity extends ActionBarActivity{

Button btn[] = new Button[3];
ViewPager viewPager = null;
int p=0; //페이지번호
int v=1; //화면 전환 뱡향

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

viewPager = (ViewPager)findViewById(R.id.viewPager);
MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(adapter);
}
}


activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--
<Button
android:id="@+id/btn_a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A"
style="@style/Widget.AppCompat.ActionButton"/>
<Button
android:id="@+id/btn_b"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B"
style="@style/Widget.AppCompat.ActionButton"/>
<Button
android:id="@+id/btn_c"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C"
style="@style/Widget.AppCompat.ActionButton"/>
-->
</LinearLayout>

<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>


</LinearLayout>



fragmentA.java


public class FragmentA extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_a, container, false);
}
}



fragment_a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#5DA4BE">
</LinearLayout>


마찬가지로 fragmentB.java fragmentC.java fragment_b.xml fragment_c.xml 생성


public class MainActivity extends ActionBarActivity implements View.OnClickListener {

Button btn[] = new Button[3];
ViewPager viewPager = null;
Handler handler = null;
int p=0; //페이지번호
int v=1; //화면 전환 뱡향

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//viewPager
viewPager = (ViewPager)findViewById(R.id.viewPager);
MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(adapter);

btn[0] = (Button)findViewById(R.id.btn_a);
btn[1] = (Button)findViewById(R.id.btn_b);
btn[2] = (Button)findViewById(R.id.btn_c);

for(int i=0;i<btn.length; i++){
btn[i].setOnClickListener(this);
}

}

@Override
public void onClick(View v) {

switch(v.getId()){
case R.id.btn_a:
viewPager.setCurrentItem(0);
Toast.makeText(this,"A버튼", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_b:
viewPager.setCurrentItem(1);
Toast.makeText(this,"B버튼", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_c:
viewPager.setCurrentItem(2);
Toast.makeText(this,"C버튼", Toast.LENGTH_SHORT).show();
break;
default:
break;

}

}
}


위와 같이 View.OnClickListener 인터페이스를 상속받아 onClick 메소드를 통해 모든 클릭 이벤트를 일괄 처리

안드로이드 :: API 에러

2017. 6. 26. 16:08



위와같이 Call requires API level 16 (currentmin is 14): android.view.View#setBackground more

어쩌구 농락을 한다.




이럴땐 gradle 을 손보자.




들어가서



defaultConfig {
applicationId "com.fractalwrench.androidbootstrap.sample"

minSdkVersion Integer.parseInt(MIN_SDK_INT)
targetSdkVersion Integer.parseInt(TARGET_SDK_INT)
versionCode = Integer.parseInt(VERSION_CODE)
versionName = VERSION_NAME
}


변경해주자


defaultConfig {
applicationId "com.fractalwrench.androidbootstrap.sample"

minSdkVersion 16
targetSdkVersion Integer.parseInt(TARGET_SDK_INT)
versionCode = Integer.parseInt(VERSION_CODE)
versionName = VERSION_NAME
}



SDK 버전쪽을 변경해주면 끝

안드로이드에서 xml은 레이아웃을 담당한다. 그리고 xml 안에 xml을 넣는 경우가 있다.


예를 들어 버튼이나 텍스트뷰의 모서리 모양을 둥글게 하고 싶을 때, drawable에 둥근 모양을 설정하는 xml파일을 넣는다.


그리고 레이아웃을 구성하는 xml에서 파일을 불러와 둥근 모서리를 구현한다. 


안드로이드 자체 xml 에서는 둥근 모양을 설정할 수 없었기 때문에 이런 비효율적인 방법으로 구현할 수 밖에 없었다.




그리고 오늘 나의 경우는 둥근 모서리의 레이아웃을 구현하되, 특정 레이아웃을 클릭하면 둥근 모서리의 레이아웃의 색상도 변경해야 했다.




firstboard.xml


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="#FFFFFF"/>

<!-- 테두리
<stroke android:width="10dip" android:color="#5DA4BE" />
-->
<corners android:bottomLeftRadius="20dip"
android:bottomRightRadius="20dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>



secondboard.xml


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="#5DA4BE"/>

<!-- 테두리
<stroke android:width="10dip" android:color="#5DA4BE" />
-->
<corners android:bottomLeftRadius="20dip"
android:bottomRightRadius="20dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>




activity_main.xml 중 일부분


<LinearLayout
android:id="@+id/bottomboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8"
android:background="@drawable/firstboard"
android:orientation="vertical">
</LinearLayout>



HomeActivity.java


findViewById(R.id.firstlayout).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

findViewById(R.id.bottomboard).setBackground(
getResources().getDrawable(R.drawable.firstboard)
);
}
});



레이아웃 클릭 시, 색상변경

1. 레이아웃 activity_intro.xml 추가


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="인트로" />

</LinearLayout>



2. 자바 IntroActivity 추가



package com.fractalwrench.androidbootstrap.sample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

/**
* Created by chickenNcola on 2017. 6. 25..
*/

public class IntroActivity extends Activity {
Handler handler;//핸들러 선언

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
handler= new Handler();
handler.postDelayed(mrun, 2000); // 시간 2초 딜레이
}

Runnable mrun = new Runnable(){
@Override
public void run(){
Intent HomeActivity = new Intent(IntroActivity.this, HomeActivity.class); //인텐트 생성(현 액티비티, 새로 실행할 액티비티)
startActivity(HomeActivity);
finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//overridePendingTransition : 서서히 사라지는 효과
}
};
//인트로 중에 뒤로가기를 누를 경우 핸들러를 끊어버려 아무일 없게 만드는 부분
//미 설정시 인트로 중 뒤로가기를 누르면 인트로 후에 홈화면이 나옴.
@Override
public void onBackPressed(){
super.onBackPressed();
handler.removeCallbacks(mrun);
}
}


3.  manifasts 파일에 코드 추가


<!-- 인트로 -->
<activity
android:name=".IntroActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->

<item name="colorPrimaryDark">#A1CDE1</item>
<item name="colorPrimary">#5DA4BE</item>


</style>



Styles.xml 에 위의 코드 추가

16진수 색상값만 원하는 값으로 변경해주면 됨



colorPrimaryDark : 상태바 ( 최상단 시간이랑 LTE, 와이파이 보여주는 바 )

colorPrimary : 타이틀바 ( 앱 이름 나오는 바 )



결과



                       코드 추가 전                                                            코드 추가 후

                                          






오류 내용


Application Installation Failed

Installation failed with message Failed to finalize session : INSTALL_FAILED_INVALID_APK: Split lib_slice_7_apk was defined multiple times. It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, an existing version of the apk if it is present, and then re-installing.


WARNING: Uninstalling will remove the application data!


Do you want to uninstall the existing application?


----------------------------------------------------------------------------------------------------------------------------------


해결 방법


1. 상단에 Build 메뉴 - Clean Project 클릭


2. 상단에 Build 메뉴 - Rebuild Project 클릭


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}


1. 위와 같이 MainActivity 자바 파일에 getWindow().setFlags( ... ); 을 추가하면 상태바를 없앨 수 있다.


<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- No Title Bar-->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

</resources>


2. 위와 같이 values 폴더 내에 있는 styles.xml파일에 No Title Bar 부분의 코드를 추가해주면 타이틀 바를 없앨 수 있다.

+ Recent posts