How to create Android custom titlebar - android

I try to build an application on Android.
And I'm new in Android.
But I don't know how to build a Title Bar like this.
So we can give the application name like Seesmic and Komutta with the tab button.
Can anyone help me to give me the answer or just a link for that tutorial?
Thank you.
https://lh6.ggpht.com/Hf6XKfa9K0B-CvlV6tD6qj2Yt8wJcyJ7wa8vE9BVkBbUDm0Y2pqOxgxVf7auQgXrh0gR
https://lh4.ggpht.com/rwceS5ZK1IZkHHCVixbaXlsHXwstpmIO888aMC4U0uD2oa54NiGvphcp_penGK9Q9WE
I'm sorry I can't upload the image, so I just can give the link for that image.

This is called "Action Bar" you can get it nativly starting from Android 3.0 or grab code to do it on earlier versions of android here.

android site has a demo you can check CustomTitle, and how-to-create-custom-window-title-in-android

Create a new project and name your main activity "MyActivity"
Go to res - drawable and create a new xml file and call it "custom_title_background" and put the following code:
<item android:top="20dp">
<shape android:shape="rectangle">
<gradient android:angle="90" android:endcolor="#9eacbf" android:startcolor="#8296af">
</gradient></shape>
</item>
This drawable will be used to set the background from custom_title_bar (from step 3) and to set the windowTitleBackgroundStyle from custom_title_style (from step 4)
Go to res-layout and create a new xml and name it "custom_title_bar". Here you will create a layout with a text view like in the following code:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#android:color/white"
android:textStyle="bold"
android:id="#+id/custom_title_text"
android:layout_centerInParent="true"
android:shadowColor="#android:color/black"
android:shadowRadius="3"/>
Go to res - values and create a new xml file and call it custom_title_style. Here you will create a new theme by overriding the existing one. The name of the style "custom_title_theme" from below will be used into the manifest file to "activate" the new theme.
40dp
#drawable/custom_title_background
Now go to the AndroidManifest.xml file and put the new theme in the application tag.
?
1
And at this last step, you have to go to the MyActivity class and put the following code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this must be called BEFORE setContentView
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
//this must bew called AFTER setContentView
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
//set the title
TextView textView = (TextView)findViewById(R.id.custom_title_text);
textView.setText("Custom Title");
}
}

Related

Can not resolve R.id.toolbar

I'm dipping my toes into Android Development by following along examples from this book. I am unable to get the example below to work, though. Instructions are: 1) New project named Dialog 2) Empty Activity 3) Paste/edit to look like the code below.
The message is that Studio can't resolve: R.id.toolbar, R.id.fab, R.menu, and R.id.action_settings.
I'm running Android Studio 3.1.3 on macOS High Sierra. My best guess is that that either the instructions are missing steps or since the book is ~2 years old Android Studio has changed behavior causing this example to break. I don't know enough about this development process to even know how to start to diagnose this.
In AndroidManifest.xml add this line to the activity block:
android:theme="#style/Theme.AppCompat.Dialog"
And this is the only code file to change (DialogActivity.java) for the project:
package com.example.sample.dialog;
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;
import android.view.Menu;
import android.view.MenuItem;
public class DialogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with an action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_dialog, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_dialog.xml file:
<?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=".DialogActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The reason you are getting those errors is because Java is looking for references in XML that have not been created. For example, it is looking for a reference called "R.id.fab" which was never created.
To fix this, you are going to have to go into the res folder and create the necessary files. Inside of the res -> layout -> "activity_dialog.xml" file, you will have to create a FAB in order to get rid of that error. You can copy/paste this code.
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"/>
Here, I create the necessary View in XML, and give it an id called fab so you can reference it in the java code. You will also need to create a menu folder and file, so to do that right click on the res folder, and go to "new Android Resource File". Set the file name to "menu" and the resource type should also be menu. Then when you hit "OK", you will see a new folder called menu, and inside of that a file called "menu.xml".
Inside that "menu.xml" file, you're going to have to create your menu options with an id of "action_settings". You can do that by using the code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_settings" android:title="Settings"/>
</menu>
Lastly, you can create your toolbar by right clicking on the layout folder and selecting new layout resource file. You can name it 'toolbar', and set the root element to android.support.v7.widget.Toolbar. This will generate the appropriate code for you, and you can edit it however you'd like. After that go back into the "activity_dialog.xml" file and use this code:
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
This should get rid of all 4 errors
Double check the id's in the R.layout.activity_dialog file. Android studio will output that message when the id that you are looking for is not found in the inflated layout.
EDIT:
You do not have a Toolbar declared in your XML file. When you want to search for a layout element to use in a Fragment or Activity, you use the id parameter you set in the XML file. If you forget to set the id or use the wrong id, it will tell you that the symbol cannot be resolved. There are too many items to add to your code, but follow the links below and you'll pick it up quickly enough. Let me know if you need more information. Also, CodePath is an excellent resource that I heavily relied on when I started learning Android development.
Look at this for a tutorial for adding a toolbar to a layout file and this for more miscellaneous information.
You have not gotten a reference to the view from the xml.
Get the reference from the xml for example if have a button defined in xml with an id of myBtn i would get the reference as Button button = findViewById(R.id.myBtn).
On the main menu, choose File. Invalidate Caches/Restart. The Invalidate Caches message appears informing you that the caches will be invalidated and rebuilt on the next start. Use buttons in the dialog to invalidate caches.

Add an image to an android splash screen in intellij android studio? [duplicate]

This question already has answers here:
How do I make a splash screen? [closed]
(31 answers)
Closed 8 years ago.
Okay so what I'm wanting to do is add an image to my splash screen, so it displays the image before the app starts. I think(?) I found the right code to actually do the splash screen but I can't get the image in it. From what I've read it needs to be in a png file, which it is but how do I move it from a file on my computer to the code, and then where do I go from there?
Assuming that you have exactly the code given in How do I make a splash screen? then you simply need to save your picture as splash.png in the app/main/src/res/drawable folder. Be sure to clean and rebuild your project before running it. Note that you can give the PNG any name you want. Just change splash in android:src="#drawable/splash" to match the name you use. Also, I strongly encourage you to learn about the directory structure in an Android Studio project.
My post here answers this question.
To "move" the image from a file to your code, you need to place it into your drawable folder, then refrence it somewhere by using
#drawable/image
To better understand how to change the splash screen image please read below.
Add Splash Screen Image
First you need a splash screen image. Because Android devices come in
various resolutions, you may want to ship several splash screens as
described in Google's Best Practices for Supporting Multiple Screens.
For simplicity, we'll just ship one here that is 480x800. It should
support most phone sizes pretty well, and Android will scale it as
best it can.
Add the the image/gif you want into your Resources\Drawable
You need to define the splash screen in your layout.xml file
<?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">
<ImageView id="#+id/splashscreen" android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/splash"
android:layout_gravity="center"/>
<TextView android:layout_width="fill_parent" <!-- Not needed->-->
android:layout_height="wrap_content"
android:text="Hello World, splash"/> <!--Not Needed -->
</LinearLayout>
And your activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}

how to use android process button lib

I've download android process button lib and import it into my eclipse. :
android process button lib :
I created an android project then I added this lib into my project :
now, I want to use this library but I get this error :
ProgressGenerator cannot be resolved to a type
I am using eclipse.
#NIPHIN answer is correct. As you can notice library is using gradle folder structure.
Here are 2 options:
Move com.dd... folders to src folder.
Create new project library, and simply copy all res and classes to your new created folder.
CHeck the project structure, reorganize the folder "java" to reflect folder structure as same folder "src" in eclipse. Eclipse and Studio IDE have different folder structures.
Am not sure of how you integrated it, but the example clearly state to import
import com.dd.processbutton.iml.ActionProcessButton;
import com.dd.processbutton.iml.GenerateProcessButton;
import com.dd.processbutton.iml.SubmitProcessButton;
Even though the project has been added as a dependency library, you would still need to have these import statements. May be eclipse is having trouble automatically adding these imports? Jut add them i manually. If your folder structure and import are correct, it should work.
Thread is a bit old, but perhaps I can help you out.
I´ll show you an Example with the ActionProcessButton
first of all in your layout, u need the specific Button. For example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.dd.processbutton.iml.ActionProcessButton
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:textColor="#android:color/white"
android:textSize="18sp"
android:text="#string/login"
android:id="#+id/loginButton"
android:textAllCaps="true"
custom:pb_colorComplete="#color/green_complete"
custom:pb_colorNormal="#color/blue_normal"
custom:pb_colorPressed="#color/blue_pressed"
custom:pb_colorProgress="#color/purple_progress"
custom:pb_textComplete="#string/login_successfull"
custom:pb_textProgress="#string/login_auth" />
</RelativeLayout>
inside your activiy / Fragment / whatever:
public class LoginFragment extends Fragment {
private ActionProcessButton loginButton;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_fragment, container, false);
loginButton = (ActionProcessButton) view.findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loginButton.setProgress(50); // starts the Animation and sets the text defined in your .xml file for Progress
loginDone();
}
});
}
private void loginDone(){
//...
// do something time-consuming
loginButton.setProgress(100); // tolds the button, that your operation is done.
}
// ...
}
Of course, you can update the Progress state dynamically, but as I know, the critical values for setProgress are -1(for login failed), 0, 50 and 100.

Android - Andengine - How to use native UI SKD

I want use the native sdk interface layout, (How a normal app) to design my game menu, and link it to the BaseGameActivity, or GameScene, I know how to design the interface using sdk native, but I dont know how implement it on andengine :S
I cant find any solution, I will hope anybody can help me to find the best method, or the way to use these.
Sorry for my bad english.
More info: I know how to add a little framelayout on my baseactivity, but I can a set of menus (2/3) and that you can move on it, and enter on the game and exit of the game :)
Sorry my english again
Well, I do this works :)
Only create a normal activity, with the layout etc.. and use the intent.putExtra(); to send a particular info to the BaseGameActivy, Then, on onCreateResources() I set a serie of conditions to determine that I press before, and set the wished scene.
Sorry my english :)
EDIT: imported tutorials from original website
How to use UI Android SDK with AndEndine
NOTE : You may have filling errors if you change width and heights in these layouts so be carefull (this solution works with fullscreen usage)
XML layout
In your project's directory res/layout create an empty file named themainactivity.xml and put the following content inside.
Notes : Set the attribute tools:context to your application activity name, beginning with a dot (here: .MyMainActivity)
XML layout file: res/layout/themainactivity.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- code placed here will be above the AndEngine render -->
</RelativeLayout>
Java class
You just have to specify the IDs in your class.
MyMainActivity.java
package com.example;
import org.andengine.ui.activity.SimpleLayoutGameActivity;
public class MyMainActivity extends SimpleLayoutGameActivity
{
#Override
protected int getLayoutID()
{
return R.layout.themainactivity;
}
#Override
protected int getRenderSurfaceViewID()
{
return R.id.gameSurfaceView;
}
}
Create a custom Android SDK layout in AndEngine
XML layout
WARNING: the root node must be a merge node ! Inside this you can do what you want.
XML layout file: res/layout/my_view.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="Dat button" />
</LinearLayout>
</merge>
The controller class
For being able to use your interface you have to link it to the XML view using the inflater service.
NOTE: The UI Java code is compiled when you switch to the WYSIWIG editor so if you don't add the linking code below you won't see the contents of the layout in the activities that use it.
Custom layout: MyView.java
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
public class MyView extends LinearLayout
{
public MyView(Context context, AttributeSet attrs)
{
super(context, attrs);
// Link to the XML view
LayoutInflater.from(context).inflate(R.layout.my_view, this, true);
// Link to the XML view (alternative using service, you can delete if you don't need it)
//LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflater.inflate(R.layout.my_view, this);
}
}
Reuse in an other activity
Just add this code in the activity layout.
<com.example.MyView
android:id="#+id/myView1"
android:layout_width="100dp"
android:layout_height="wrap_content" />

Using Android Id's?

I am still trying to figure out frame animation, but I need to use IDs. I get these errors:
<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" android:id="#+id/carrotsmileanim">
<item android:drawable="#drawable/carrotsmile" android:duration="2000" />
<item android:drawable="#drawable/carrotblink" android:duration="2000" />
<android:id="#+id/carrotsmileanim></android:id>
</animation-list>
In the ID section the error is: "
android:id" must be followed by either attribute specifications, ">" or "/>"
My code:
package com.example.carrottest2;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.R;
public class Carrottest2 extends Activity {
/** Called when the activity is first created. */
AnimationDrawable mainanimation;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ImageView carrotsmile = (ImageView) findViewById(R.id.carrotblink);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
c = (AnimationDrawable) rocketImage.getBackground();
carrotsmileanim.start();
The tutorial frame animation android IDs are still there, I never changed them, but it does not recognize these, and it gives me a "cannot be resolved or is not a field." error.
Which is supposed to go where?
Is main.xml involved in this? I know there is supposed to be an imageview somewhere, but I'm not sure where.
Matt Huggins is right. What you've posted isn't valid XML because of the missing quotation mark. But also, I don't think 'id' is a tag that is used by android. It's an attribute (as you used it, correctly, on the root element) but AFAIK there is no <android:id> element. When you add the id attribute to something, it automatically creates that id, and makes it accessible via your generated R file.

Categories

Resources