I can not understand why my code does not work for 30 minutes.
MainActivity.java:
package app.test;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
It is main.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:gravity="center"
android:orientation="vertical" >
<TextView
android:text="#string/text"
android:autoLink="email|web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/bottom" />
</LinearLayout>
And here is the main code bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size
android:height="1dp"
android:width="70dp" />
<solid android:color="#android:color/black" />
</shape>
Also I attach a screenshot on which everything is clearly visible: https://imgur.com/a/LRh0N
Your shape is black as your background layout, you will never see it.
It's a good approach you change your backgrounds elements color (even just to see the elements size) to align your layout with your expectations
Related
I'm currently trying to figure out how to create a ListView together with a bottom navigation bar, however, though after running the emulator it did not show anything. Why is this so?
This is my activity_main.xml file
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_nav"
app:menu="#menu/home"
/>
</LinearLayout>
This is my nav_graph.xml file
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_graph.xml"
app:startDestination="#id/home">
<fragment
android:id="#+id/home"
android:name="sg.edu.rp.c346.a3pdwork.HomeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/About"
android:name="sg.edu.rp.c346.a3pdwork.AboutFragment"
android:label="fragment_about"
tools:layout="#layout/fragment_about" />
<fragment
android:id="#+id/Login"
android:name="sg.edu.rp.c346.a3pdwork.LoginFragment"
android:label="fragment_login"
tools:layout="#layout/fragment_login" />
</navigation>
This is my home.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:title="Home"
android:icon="#drawable/ic_action_home"
/>
<item
android:id="#+id/Login"
android:title="Login"
android:icon="#drawable/ic_action_login"
/>
<item
android:id="#+id/About"
android:title="About"
android:icon="#drawable/ic_action_about"
/>
</menu>
This is my fragment_home.xml file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
<ListView
android:id="#+id/listViewDetails"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
This is my HomeFragment.java file
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
This is my fragment_login.xml file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
This is my LoginFragment.java file
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class LoginFragment extends Fragment {
public LoginFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_login, container, false);
}
}
This is my fragment_about.xml file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AboutFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
This is my AboutFragment.java file
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class AboutFragment extends Fragment {
public AboutFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false);
}
}
This is my MainActivity.java file
package sg.edu.rp.c346.a3pdwork;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavController navController = Navigation.findNavController(this,
R.id.nav_host_fragment);
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
NavigationUI.setupWithNavController(bottomNav, navController);
}
}
Ruchin.
Wish you many good things to come with android developing.
In this situation I've got 2 things to tell you:
First: Your BottomNavigationView has no android:layout_width and android:layout_height attributes. I think you just forgot to put them in here (if so, it is not any problem, I just wanted to make sure you notice that).
Second: Your layout is shown correctly. Why you see nothing? Because you have two views TextView and ListView which have android:layout_height="match_parent". So at first TextView is rendered and then ListView is rendered on it.
How can you see some results? That's simple. Just change FrameLayout to LinearLayout, set orientation to vertical and set TextView's height to wrap_content. You can put these changes in your fragment_home.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello_blank_fragment" />
<ListView
android:id="#+id/listViewDetails"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I have a drawable pasek_postepu.xml file which I use as a background of View in activity_add.xml. the pasek_postepu.xml is a layer-list, containing 2 items: rectangles.
activity_add.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=".AddActivity"
android:background="#color/background">
<View
android:id="#+id/pasek_postepu_dodawanie"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_margin="5dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="#color/pasek_postepu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
pasek_postepu.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/pasek_postepu_1_element"
android:left="20dp"
android:right="200dp">
<shape android:shape="rectangle">
<solid android:color="#color/red"/>
</shape>
</item>
<item
android:id="#+id/pasek_postepu_2_element"
android:left="200dp"
android:right="20dp">
<shape android:shape="rectangle">
<solid android:color="#color/green"/>
</shape>
</item>
</layer-list>
I want to programmatically set the size of each of them, depending on screen width, so each one of them would take 1/2 of the screen, including margins and some space between them, let's say 10dp. I have already extracted screen width to int dpWidth, so that's not the problem. This is my java file:
AddActivity.java
package com.example.kamil.math;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class AddActivity extends AppCompatActivity {
private static int ilosc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
ChangeSize();
}
#SuppressLint("ResourceAsColor")
public void ChangeSize(int ktory_cykl){
LayerDrawable pasek = (LayerDrawable)getResources().getDrawable(R.drawable.pasek_postepu);
GradientDrawable pasek1 = (GradientDrawable) pasek.findDrawableByLayerId(R.id.pasek_postepu_1_element);
GradientDrawable pasek2 = (GradientDrawable) pasek.findDrawableByLayerId(R.id.pasek_postepu_2_element);
pasek1.setColor(Color.rgb(31,135,31)); //green
pasek2.setColor(Color.rgb(196,32,32)); //red
View pasek_id = findViewById(R.id.pasek_postepu_dodawanie);
pasek_id.setBackground(getApplicationContext().getResources().getDrawable(R.drawable.pasek_postepu));
}
}
I can "access" each item, and change their color, but I can't see the way to change size. I tried to modify Left and Right e.g. pasek1.setLeft(...); but I get a message "cannot resolve symbol setLeft". I can't use width because it works on Android 6+, and I need Android 5 compatibility.
Is there a way I can set size of each item form java file?
instead of constraintLayout use LinearLayout
in LinearLayout there is an attribute called android:weightSum
here is an example
<LinearLayout
android:background="#color/DarkGoldenrod"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="2" />
<TextView
android:background="#00FF00"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
<TextView
android:background="#color/red"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
by giving weightSum = 2
and giving each TextView = 1
they will each take same amount space no matter the device size
here is a link for more information
I am currently making an application where I want part of the logo of the application to appear at the center of the screen, translates to the top and then shows the entire logo. I currently have the application showing the entire logo and translating it as desired, but I am not sure how to hide most of the logo and make it show after. My logo consists of an icon on the left and text on the right. Once the logo reaches the top, I would like the name to appear smoothly while pushing the icon to the left as it goes. Is there a way to do this using the current code I have.
logo_animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="800"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
<translate
android:duration="800"
android:fillAfter="true"
android:fromYDelta="0%p"
android:startOffset="2000"
android:toYDelta="-30%p" />
</set>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingStart="15dp"
android:paddingRight="15dp"
android:paddingEnd="15dp"
android:background="#color/colorPrimary"
tools:context=".Main">
<ImageView
android:id="#+id/mainLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/logo"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>
Main.java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
public class Main extends AppCompatActivity implements AnimationListener {
ImageView mainLogo;
Animation mainLogoAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
searchLogo = (ImageView) findViewById(R.id.searchLogo);
mainLogoAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.logo_animation);
mainLogoAnimation.setAnimationListener(this);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
}
Just put the text in a separate TextView, and animate it on its own.
I have created a circular progress-bar, and have made it follow as closely as possible to the design in the image below.
But I'm having difficulty having it match the design in the picture below (color, angle, shape etc.):
I don't know how to match :
the color of the above?
or the shape?
Design I'm wanting to follow:
My current design looks like the below:
My code so far is below:
activity_main.xml
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="#+id/progressBar"
android:layout_width="240dp"
android:layout_height="240dp"
android:indeterminate="false"
android:max="100"
android:progress="75"
android:progressDrawable="#drawable/style_circular_fill"
android:secondaryProgress="10"
android:layout_alignParentBottom="false"
android:layout_alignParentStart="false"
android:focusableInTouchMode="false"
android:layout_alignParentTop="true"
android:layout_marginTop="70dp"
xmlns:android="http://schemas.android.com/apk/res/android" />
style_circular_fill.xml
<item android:id="#android:id/secondaryProgress">
<rotate
android:fromDegrees="270"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="270" >
<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thicknessRatio="20.0"
android:useLevel="true" >
<gradient
android:centerColor="#A9E2F3"
android:endColor="#A9E2F3"
android:startColor="#A9E2F3"
android:type="sweep" />
</shape>
</rotate>
</item>
<item android:id="#android:id/progress">
<rotate
android:fromDegrees="270"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="270" >
<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thicknessRatio="20.0"
android:useLevel="true" >
<gradient
android:centerColor="#26ce61"
android:endColor="#26ce61"
android:startColor="#26ce61"
android:type="sweep" />
</shape>
</rotate>
</item>
You could try the CircleProgress library. One of the examples they feature looks quite similar to that of your requirement.
I haven't used it, so cannot confirm the ease of its use.
For the sake of being balanced, check out this list of Progress Bar libraries. There may be a library that can achieve the desired effect.
i suggest that u export the image to the match resources folders (mdpi,hdpi, and so on...);
after that create an animation xml file like this (let's call this file "progress_bar_animation.xml"):
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/your_resource_file_name"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1"
android:fromDegrees="0"
android:toDegrees="1800" />
and then create a progress bar style (in style.xml file)
<style name="ProgressBar" parent="Base.Widget.AppCompat.ProgressBar">
<item name="android:indeterminateDrawable">#drawable/progress_bar_animation</item>
</style>
and finally add a progress bar to your xml and set the style you create before, like this:
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/ProgressBar" />
Use this progress dialogue
public class MyCustomProgressDialog extends ProgressDialog {
private Context c;
private ProgressWheel pd;
private ImageView imgLogo;
public MyCustomProgressDialog(Context context) {
super(context);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
c=context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_bar_loading);
}
#Override
public void show() {
super.show();
}
#Override
public void dismiss() {
super.dismiss();
}
}
progress_bar_loading.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transperent"
android:gravity="center"
>
<TextView
android:id="#+id/lbl_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:text=" "
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold"/>
<com.organigoal.customcontrol.ProgressWheel
android:id="#+id/progress_wheel"
xmlns:wheel="http://schemas.android.com/apk/res-auto"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
wheel:barColor="#color/grey"
wheel:progressIndeterminate="true"/>
I use following java code to run this,
public class MainActivity extends ActionBarActivity {
int transitionTime = 300;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TransitionDrawable transition = (TransitionDrawable) findViewById(R.id.relativeLayoutMain).getBackground();
transition.startTransition(transitionTime);
}
Please help me to sole this problem.
Hi I'm new to android and what I'm try to do is change layout background color continuously.
Above method trigger error to me, I not found solution to this but I use different approach to do this.
I use timer with TransitionDrawable and here is my working copy of code.
Here I add "drawable" resources,
transition_drawable.xml (this is main resource xml set this resource file to your layer backbround)
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
<item android:drawable="#drawable/color_selector_01" />
<item android:drawable="#drawable/color_selector_02" />
</transition>
color_selector_01.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#919bfa"
android:endColor="#4e5bda"
android:type="linear" />
</shape>
</item>
</selector>
color_selector_02.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#fe5528"
android:endColor="#3c1ed4"
android:type="linear" />
</shape>
</item>
</selector>
In my main layout I set layer background to "transition_drawable"
here is my "activity_main.xml"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#drawable/transition_drawable" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/linkTerms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:singleLine="true"
android:text="#string/linkTerms" />
<TextView
android:id="#+id/and"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="#string/and" />
<TextView
android:id="#+id/linkPrivacy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="#string/linkPrivacy" />
</LinearLayout>
</ScrollView>
Finlay here is my MainActivity.java file
#TargetApi(Build.VERSION_CODES.HONEYCOMB) #SuppressLint("NewApi") public class MainActivity extends ActionBarActivity {
int transitionTime = 6000;
private RelativeLayout Rlayout;
private TransitionDrawable trans;
final Handler handler_interact=new Handler();//not defined as final variable. may cause problem
View layout_interact;
#SuppressLint("NewApi") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout_interact =(View) findViewById(R.id.relativeLayoutMain);
Rlayout = (RelativeLayout)findViewById(R.id.relativeLayoutMain);
Resources res = this.getResources();
trans = (TransitionDrawable)res.getDrawable(R.drawable.transition_drawable);
Rlayout.setBackgroundDrawable(trans);
BackgroundColoerChangerCallTimer();
}
public void BackgroundColoerChangerCallTimer(){
//creating timer
Timer timer_interact=new Timer();
timer_interact.schedule(new TimerTask() {
#Override
public void run() {
UpdateGUI();
}
}, 3000);
}
private void UpdateGUI() {
handler_interact.post(runnable_interact);
}
//creating runnable
final Runnable runnable_interact = new Runnable() {
public void run() {
//ayout_interact.setBackgroundColor(Color.GREEN);
trans.reverseTransition(transitionTime);
BackgroundColoerChangerCallTimer();
}
};